问题
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)?
Requirements:
When the circuit is first energized, the seven-segment LEDs will start counting at 0x00.
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.).
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.
Source 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
} //
} //
} //
回答1:
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++;
}
}
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.
来源:https://stackoverflow.com/questions/56919370/questions-about-displaying-0x00-to-0xff-with-two-seven-segment-lights