编译器是atmel studio 7
#include <avr/io.h>
#include <avr/interrupt.h>
unsigned int i=0;
int main(void)
{
DDRG|=_BV(PG0);
TCCR0|=_BV(CS02)|_BV(CS00); //1024分频
TCNT0=256-F_CPU/1024*0.001; //定时1ms
tiMSK|=0x01; //开定时器中断
sei();
while (1)
{
}
}
ISR(TIMER0_OVF_vect){
TCNT0=256-F_CPU/1024*0.001; //重装定时1ms
i++;
if(i>300){
PORTG^=_BV(PE0);
i=0;
}
}
#include <avr/io.h>
#include <avr/interrupt.h>
unsigned int i=0;
unsigned char Timer0Flag=0;
int main(void)
{
DDRG|=_BV(PG0);
TCCR0|=_BV(CS02)|_BV(CS00); //1024分频
TCNT0=256-F_CPU/1024*0.001; //定时1ms
TIMSK|=0x01; //开定时器中断
sei();
while (1)
{
if(Timer0Flag){
PORTG^=_BV(PE0);
Timer0Flag=0;
}
}
}
ISR(TIMER0_OVF_vect){
TCNT0=256-F_CPU/1024*0.001; //重装定时1ms
i++;
if(i>300){
//PORTG^=_BV(PE0);
Timer0Flag=1;
i=0;
}
}
第二个也能点灯,将Timer0Flag定义成全局变量,建议用第一个吧,毕竟中断处理的事情不多,效率还是比while要高。
一周热门 更多>