关于使用两个七段灯显示0x00到0xFF的问题 [英] Questions about displaying 0x00 to 0xFF with two seven segment Lights

查看:286
本文介绍了关于使用两个七段灯显示0x00到0xFF的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我吗?我一直在研究并尝试使其正常工作,但是我很不幸.我在网上找到的所有代码都无法正常工作...目前的输出为00、11、22、33,... FF,然后退回到00.如何分隔第一位和第二位显示?就像我希望它显示0到255(00、01、02 ... FF)一样?

Can someone please help me with this? I have been researching and trying to get this working, but I'm out of luck. All the codes I found online were not working... The output, for now, is 00, 11, 22, 33, ... FF and lop back to 00. How do I separate the first and second digit displays? Like I want it to display from 0 to 255 (00, 01, 02...FF)?

要求:

电路首次通电时,七段LED将从0x00开始计数.

When the circuit is first energized, the seven-segment LEDs will start counting at 0x00.

决斗段LED的计数最多为0xFF,每次增加1.计数必须是连续的.对数字2的0到F到0进行计数,然后将数字1递增1,这是不可接受的.计数的执行方式应类似于计数器(从0x00到0x0F然后是0x10等).

The duel segment LEDs will count up to 0xFF, increasing by 1 each time. The count MUST BE sequential. It is unacceptable to count 0 to F to 0 on digit 2, then increase digit 1 by 1. The count shall perform like a counter (0x00 to 0x0F then 0x10 etc.).

一旦计数达到0xFF,计数将从0x00开始.

Once the count reaches 0xFF, the count will start over at 0x00.

该代码将在增加计数之间包含足够的延迟,以便可以直观地确认该电路/代码是否按设计工作.

The code will contain sufficient delay between incrementing the count so that the count can be visually confirmed that the circuit/code is operating as designed.

以上内容将无限循环地反复发生,直到设备断电为止.

The above will occur infinitely, another word, in an endless loop until the device is powered down.

源代码:

#include

void PORTA_init(void)
{
  PORTA = 0; // All PORTA Pins are low
  CMCON0 = 7; // Turn off Comparators
  ANSEL = 0; // Turn off ADC
  //TRISA = 0b001111; // RA4 and 5 are outputs; RA0,1,2, and 3 are input
  return;
}
/******** END OF PORTA_init ****************************/
/********************************************************

* Notes:
*
* Delay was determined through trial and error
*
* Returns: None
* ********************************************************/

/** Function: main *************************************
*
* Notes:
*
* RA4 - Positive LED Connection for D0
* RA5 - Negative LED Connection for D0
*
* Returns: None -- This routine contains an infinite loop
*
*/
// CONFIG --- Configuration Word --- START
#pragma config FOSC = INTOSCIO
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config MCLRE = OFF
#pragma config CP = OFF
#pragma config CPD = OFF
#pragma config BOREN = OFF
#pragma config IESO = OFF
#pragma config FCMEN = OFF
// CONFIG --- Configuration Word --- END
int i, j;
int DisplayValue, DisplayLED;

//PLACE LEDDigit ARRAY HERE
const char LEDDigit[] = {
0b0000001,           //   "0"
0b1001111,           //   "1"
0b0010010,           //   "2"
0b0000110,           //   "3"
0b1001100,           //   "4"
0b0100100,           //   "5"
0b0100000,           //   "6"
0b0001111,           //   "7"
0b0000000,           //   "8"
0b0000100,           //   "9"
0b0001000,           //   "A"
0b1100000,           //   "b"
0b0110001,           //   "C"
0b1000010,           //   "d"
0b0110000,           //   "E"
0b0111000};           //   "F"


main()
{

  PORTA = 0;
  PORTC = 0;
  CMCON0 = 7;           //   Turn off Comparators
  ANSEL = 0;               //   Turn off ADC
  TRISA = 0b011100;
  TRISC = 0b000000;

  DisplayValue = 0;           //   Start Displaying at 0x00
  DisplayLED = 0;           //   Display the 1s first


  while(1 == 1)    //   Loop Forever
  {
    if (0 == DisplayLED)       //    True, then display right digit
    {
      RA5 = LEDDigit[DisplayValue & 0x0F] >> 6;
      //   Clears display bits 4 - 7 of DisplayValue,
      //   then selects bit 7 of LEDDigit

      PORTC = LEDDigit[DisplayValue & 0x0F] & 0x03F;
      //   clears display bits 4 - 7 of DisplayValue,
      //   then selects bits 0 - 6 of LEDDigit
    }
    else
    {
      RA5 = LEDDigit[(DisplayValue >> 4) & 0x0F] >> 6;
      PORTC = LEDDigit[(DisplayValue >> 4) & 0x0F] & 0x03F;
    } //
    TRISA = TRISA ^ 0b011111;   //   Swap Left/Right
    PORTA = PORTA & 0b111100;   //   Make Sure Bits are Low
    DisplayLED = DisplayLED ^ 1;   //   Other Digit Next

    NOP();               //   Used for 10 ms Timing
    for (i = 0; i < 660; i++);   //   10 ms Delay Loop
    NOP();               //   Used for 10 ms Timing

    j = j + 1;           //   Increment the Counter?
    if (25 == j)           //   1/4 Second Passed?
    {
      DisplayValue++;       //   Increment the Counter

      j = 0;          //   Reset for another 1/4 Second
    }                 //
  }                   //
}                     //

推荐答案

这可能是您的家庭作业的答案:

This is a possible answer to your homework assignment:

/*
 * File:   main.c
 * Author: dan1138
 * Target: PIC16F688
 * Compiler: XC8 v2.00
 *
 *                       PIC16F688
 *             +------------:_:------------+
 *    GND -> 1 : VDD                   VSS : 14 <- 5v0
 * SEG_An <> 2 : RA5/T1CKI     PGD/AN0/RA0 : 13 <> PGD DIGIT_1n
 *        <> 3 : RA4/AN3       PGC/AN1/RA1 : 12 <> PGC DIGIT_2n
 *    VPP -> 4 : RA3/VPP           AN2/RA2 : 11 <>
 * SEG_Bn <> 5 : RC5/RXD           AN4/RC0 : 10 <> SEG_Gn
 * SEG_Cn <> 6 : RC4/TXD           AN5/RC1 : 9  <> SEG_Fn
 * SEG_Dn <> 7 : RC3/AN7           AN6 RC2 : 8  <> SEG_En
 *             +---------------------------:
 *                        DIP-14
 *
 * Created on July 7, 2019, 6:56 PM
 */

#pragma config FOSC = INTOSCIO
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config MCLRE = OFF
#pragma config CP = OFF
#pragma config CPD = OFF
#pragma config BOREN = OFF
#pragma config IESO = OFF
#pragma config FCMEN = OFF

#include <xc.h>
#include <stdint.h>

#define _XTAL_FREQ (8000000ul)

const char LEDDigit[] = 
{ 
  /* abcdefg         _   */
  0b00000001,   /*  | |  */
                /*  |_|  */
                /*       */
  0b01001111,   /*    |  */
                /*    |  */
                /*   _   */
  0b00010010,   /*   _|  */
                /*  |_   */
                /*   _   */
  0b00000110,   /*   _|  */
                /*   _|  */
                /*       */
  0b01001100,   /*  |_|  */
                /*    |  */
                /*   _   */
  0b00100100,   /*  |_   */
                /*   _|  */
                /*   _   */
  0b00100000,   /*  |_   */
                /*  |_|  */
                /*   _   */
  0b00001111,   /*    |  */
                /*    |  */
                /*   _   */
  0b00000000,   /*  |_|  */
                /*  |_|  */
                /*   _   */
  0b00001100,   /*  |_|  */
                /*    |  */
                /*   _   */
  0b00001000,   /*  |_|  */
                /*  | |  */
                /*       */
  0b01100000,   /*  |_   */
                /*  |_|  */
                /*   _   */
  0b00110001,   /*  |    */
                /*  |_   */
                /*       */
  0b01000010,   /*   _|  */
                /*  |_|  */
                /*   _   */
  0b00110000,   /*  |_   */
                /*  |_   */
                /*   _   */
  0b00111000,   /*  |_   */
                /*  |    */
                /*       */
  0b01111111,   /* blank */
                /*       */
};
volatile uint8_t Digit1Segments;
volatile uint8_t Digit2Segments;

volatile uint8_t Timer0Ticks;

void __interrupt() ISR_handler(void)
{
    if (TMR0IE && TMR0IF) {  /* TIMER0 asserts and interrupt every 1.024 milliseconds */
        TMR0IF=0;
        Timer0Ticks++;
        if ((Timer0Ticks & 0x0F) == 0) { /* every 16.384 drive a new digit */
            if ((TRISA & 3) == 2) {
                TRISA |= 3;     /* Turn off all digit drivers */
                PORTA = 0;
                if ((Digit2Segments & (1<<6)) != 0 ) {
                    PORTA = (1<<5);
                }
                PORTC = Digit2Segments;
                /* Drive digit 2 segments */
                TRISA &= ~2;
            }
            else { 
                TRISA |= 3;
                PORTA = 0;
                if ((Digit1Segments & (1<<6)) != 0 ) {
                    PORTA = (1<<5);
                }
                PORTC = Digit1Segments;
                /* Drive digit 1 segments */
                TRISA &= ~1;
            }
        }
    }
}

void main(void) {
    uint8_t HexCount;
    /*
     * Initialize this PIC
     */
    INTCON = 0;
    OSCCON = 0x70;      /* Select 8MHz system oscillator */
    __delay_ms(500);    /* Give ICSP device programming tool a chance to get the PICs attention */

    Digit1Segments = 0b01111111;
    Digit2Segments = 0b01111111;
    TRISA = 0xDF;   /* PORTA bit 5 needs to be an output */
    TRISC = 0x00;
    ANSEL  = 0;
    OPTION_REG = 0b11000010; /* TIMER0 clock = FOSC/4, prescale 1:8 */
    PORTA = 0;
    PORTC = 0;
    CMCON0 = 7;
    TMR0 = 0;
    TMR0IF = 0;
    TMR0IE = 1;
    GIE = 1;
    /*
     * This is the application loop.
     * 
     * It counts up one count about every second.
     */
    HexCount = 0;
    while(1)
    {
        Digit1Segments = LEDDigit[HexCount & 0x0F];
        Digit2Segments = LEDDigit[(HexCount>>4) & 0x0F];
        __delay_ms(1000);
        HexCount++;
    }
}

仅使用MPLABX模拟器进行检查,因此它可能无法在实际硬件或Proteus模拟器中工作.

Only checked using the MPLABX simulator so it may not work in real hardware or in a Proteus simulator.

警告:我使用的概念可能没有涵盖,因此,如果此代码有效,并且您将其交给老师,就会知道您是从互联网上获得的.

Warning: I have used concepts that may not have been covered, so if this code works and you hand it in your instructor will know you got it from the internet.

这篇关于关于使用两个七段灯显示0x00到0xFF的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆