Timer 设置标志在我的计时器中断中不起作用(当中断工作时)
我曾经用ICCAVR编写代码,在那里我没有问题,但不是出于某种原因,我应该迁移到AtmelStudio。 在下面的代码中,LED在中断中闪烁,但当我仅在中断中设置一个标志,并希望在轮询中使用该标志闪烁LED时,它将不起作用:Timer 设置标志在我的计时器中断中不起作用(当中断工作时),timer,interrupt,avr,avr-gcc,atmelstudio,Timer,Interrupt,Avr,Avr Gcc,Atmelstudio,我曾经用ICCAVR编写代码,在那里我没有问题,但不是出于某种原因,我应该迁移到AtmelStudio。 在下面的代码中,LED在中断中闪烁,但当我仅在中断中设置一个标志,并希望在轮询中使用该标志闪烁LED时,它将不起作用: #include<avr/io.h> #include<avr/interrupt.h> #define LED PA1 ISR (TIMER1_OVF_vect) // Timer1 ISR { //PORTA ^= (1 &l
#include<avr/io.h>
#include<avr/interrupt.h>
#define LED PA1
ISR (TIMER1_OVF_vect) // Timer1 ISR
{
//PORTA ^= (1 << LED);
TCNT1 = 63974; // for 1 sec at 16 MHz
PORTA ^= (1 << LED);
}
int main()
{
DDRA = (0x01 << LED); //Configure the PORTD4 as output
TCNT1 = 63974; // for 1 sec at 16 MHz
TCCR1A = 0x00;
TCCR1B = (1<<CS10) | (1<<CS12);; // Timer mode with 1024 prescler
TIMSK = (1 << TOIE1) ; // Enable timer1 overflow interrupt(TOIE1)
sei(); // Enable global interrupts by setting global interrupt enable bit in SREG
while(1)
{
}
}
虽然此更改将使其不闪烁:
#include<avr/io.h>
#include<avr/interrupt.h>
#define LED PA1
unsigned int counter=0;
unsigned char flag=0;
ISR (TIMER1_OVF_vect) // Timer1 ISR
{
//PORTA ^= (1 << LED);
TCNT1 = 63974; // for 1 sec at 16 MHz
counter++;
if(counter>=10)
{
flag=1;
counter=0;
}
}
int main()
{
DDRA = (0x01 << LED); //Configure the PORTD4 as output
TCNT1 = 63974; // for 1 sec at 16 MHz
TCCR1A = 0x00;
TCCR1B = (1<<CS10) | (1<<CS12);; // Timer mode with 1024 prescler
TIMSK = (1 << TOIE1) ; // Enable timer1 overflow interrupt(TOIE1)
sei(); // Enable global interrupts by setting global interrupt enable bit in SREG
while(1)
{
if(flag)
{
flag=0;
PORTA ^= (1 << LED);
}
}
}
有谁能帮我一下吗?编译器在程序开始时看到该标志设置为0,但不知道该变量可以由中断处理程序更改,因为代码从未在程序中直接调用过。
所以它优化了out标志签入while循环
对从不同代码流、主代码和中断处理程序、多线程环境中的不同线程访问的变量使用volatile限定符
volatile unsigned char flag = 0;