我正在尝试使用CTC和外部中断来启动计时器,该计时器将计数到60秒,并使用一个函数在ATmega128
上显示LED的输出。该函数如下所示,在调试时将一直计入到十位的第一次迭代,但在下一次运行时,十位变量被重置为零,我无法理解它为什么重置。任何人都可以看到我可能在哪里犯了错误吗?
#include <avr/io.h>
#include <inttypes.h>
#include <avr/interrupt.h>
#include <util/delay_basic.h>
volatile uint8_t passval = 0;
void display(uint8_t value);
int main(void)
{
DDRC = 0xFF; //PORTC output for two seven segment displays(tenths and ones place)
DDRD = 0x0F; //PORTD output for one seven segment display(tens place)
EICRA = ((1 << ISC01) | (1 << ISC00) | (1 << ISC11) | (1 << ISC10)); //Interrupt set to trigger on rising edge for PINA1...0
TCCR1B |= (1 << WGM12); //Sets CTC Mode
OCR1A = 24999; //Prescaler as 64 with a count of 25000
TCNT1 = 0; //Clears clock
TIMSK |= (1 << OCIE1A); //Enables interrupt
sei(); //Enable Global Interrupt
while (1)
{
TCCR1B |= ((1 << CS11) | (1 << CS10));
display(passval);
}
return 0;
}
void display(uint8_t value)
{
static uint8_t tenths, ones, tens;
tenths = value;
if (tenths > 0x09) //Rolls Over tenths place at 10
{
passval = 0x00;
tenths = 0x00;
ones++; //Advances ones place
if (ones > 0x09) //Rolls over ones places at 10
{
ones = 0x00;
tens++; //Advances tens place
if (tens > 0x06) //Rolls over tens place at 6
{
tens = 0x00;
}
}
}
PORTC = ((ones << 4) + tenths); //Display Output
PORTD = tens;
}
ISR (TIMER1_COMPA_vect) //Timer ISR
{
passval++; //Advance tenths place
}