两个七段灯显示0x00到0xFF的问题

Questions about displaying 0x00 to 0xFF with two seven segment Lights

有人可以帮我解决这个问题吗?我一直在研究并试图让这个工作,但我运气不好。我在网上找到的所有代码都不起作用...输出,目前是 00、11、22、33,... FF 并返回 00。如何分隔第一位和第二位数字显示?就像我希望它显示从 0 到 255(00、01、02...FF)?

要求:

当电路第一次通电时,七段 LED 将从 0x00 开始计数。

决斗段LED会计数到0xFF,每次加1。计数必须是连续的。在数字 2 上从 0 到 F 计数到 0,然后将数字 1 增加 1 是不可接受的。计数应像计数器一样执行(0x00 到 0x0F,然后 0x10 等)。

一旦计数达到 0xFF,计数将从 0x00 重新开始。

代码将在增加计数之间包含足够的延迟,以便可以直观地确认计数 circuit/code 正在按设计运行。

以上会无限发生,换句话说,无限循环,直到设备掉电。


源代码:

#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
    }                 //
  }                   //
}                     //

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

/*
 * 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 模拟器中运行。

警告:我使用了可能没有涵盖的概念,所以如果这段代码有效并且你将它交给你的老师,你就会知道你是从互联网上得到它的。