ATmega16单片机定时器停止不了
- 提问者网友:人生佛魔见
- 2021-03-20 18:22
- 五星知识达人网友:舊物识亽
- 2021-03-20 19:07
- 1楼网友:空山清雨
- 2021-03-20 20:22
if (time <= 0)你的time 是全局变量,char型(-128~127)??那么time可以为负了,应该是if (time == 0)表示倒计时结束,为了可循环,可以做以下调整
if (time <= 0){
timsk &= ~bit(2);
time=99;//重新赋值
}
上一个我写的代码
#include<iom16v.h> #include"macroandconst.h"//自定义函数 #include<macros.h> int8 count; int8 ge; int8 shi; uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; void delay(uint ms) //误差 -0.651041666667us { int8 a,b,i; for(i=0;i<ms;i++) for(b=102;b>0;b--) for(a=3;a>0;a--); } void timer1_init(void) { tccr1b=0x04;//256分频 tcnt1h=0x85;//1s tcnt1l=0xee; timsk|=bit(2);//t/c1 interrupt sreg|=bit(7); } void port_init(void) { ddra=0xff; porta=0xff; ddrb=0xff; portb=0xff; ddrc=0xff; portc=0xff; ddrd=0xff; portd=0xff; } void display(int8 wela,int8 dula) { portb=table[dula];//send dula portc&=~bit(wela);//开启显示的位 delay(10); portc|=bit(wela);//开启显示的位 } void main() { port_init(); timer1_init(); while(1) { display(1,shi); display(2,ge); } } #pragma interrupt_handler timer1_isr:9 void timer1_isr(void) { count++; if(count==60)count=0; shi=count/10; ge =count%10; tcnt1h=0x85; tcnt1l=0xee; }
直奔→嵌入式团员为你解答,希望能帮助到你