AVR 定时记数器0 普通模式的简单应用 作者: 时间:2016-11-22 来源:网络 加入技术交流群 扫码加入和技术大咖面对面交流海量资料库查询 收藏 #include <avr/io.h>#include #include interrupt.h>#define ling 0x80 //这里是设制从最高位一个一个的点亮volatile unsigned char count,i = 0,j = 0; //设制两个变量void Timer0_Init(void) //定时器0的初始化{ TCNT0 = 0x3d; //给计数器赋初值 TCCR0 = 0x05; //CLK/1024分频 TIMSK = 0x01; //始能定时器0的溢出中断 sei(); //置位总中断}int main(void){ DDRB = 0xff; //PB口设制为输出 PORTB = 0xff; //PB口初始化为FF Timer0_Init(); //调用定时器0的初始化 while(1); }ISR(TIMER0_OVF_vect) //定时器溢出中断函数{ //unsigned char i; TCNT0 = 0x3d; //重装载计数器的值a i++; //标志位加1 if(i == 20) { //PORTB = 0xff; i = 0; count++; if(count > 7) //点亮8个LED的循环 { count = 0; j =!j; } } if(j == 0) PORTB &= ~(ling >> count); else PORTB |= (1 << count);}
评论