定时器1基础实验:让PB口上接的LED循环闪亮
扫描二维码
随时随地手机看文章
#include #include void timer1_init(void) //定时器1初始化:0.5秒定时,预分频256 void main(void) MCUCR = 0x00;
unsigned char n=0;
void port_init(void) //端口初始化函数
{
DDRB = 0xFF; //B口定义为输出
}
{
TCCR1B = 0x00; //stop
TCNT1H = 0xF8; //setup
TCNT1L = 0x5F;
TCCR1A = 0x00;
TCCR1B = 0x04; //start Timer
}
{
port_init();//初始化输出端口
CLI(); //disable all interrupts
GICR = 0x00;
TIMSK = 0x05; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialised
timer1_init();//定时器1初始化
while(1)//循环
{
;
}
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void) //定时中断入口
{
TCNT1H = 0xF8; //reload counter high value
TCNT1L = 0x5F; //reload counter low value
if(n>=1)
{
n=0;
PORTB=55;
}
else
{
PORTB=0xAA;
n=n+1;
}
}