有人可以帮我吗?我一直在研究并试图让这个工作,但我不走运。我在网上找到的所有代码都不起作用...现在的输出是 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 重新开始。
代码将在增加计数之间包含足够的延迟,以便可以直观地确认计数是否按设计运行。
换句话说,上述情况将无限循环,直到设备断电。
源代码:
#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
} //
} //
} //