ATmega16L-體育秒表設(shè)計(jì)
//有兩個(gè)按鍵,其中一個(gè)接INT0,另一個(gè)接PD7
//啟動(dòng)時(shí),數(shù)碼管顯示00.00 ,按下INT0 ,開(kāi)始計(jì)時(shí)
//再按下INT0,計(jì)時(shí)停止。
//無(wú)論何時(shí),按下PD7,計(jì)時(shí)清零。
//問(wèn)題在于:時(shí)間不準(zhǔn),比1秒慢。時(shí)間轉(zhuǎn)過(guò)一分鐘,計(jì)時(shí)只有36秒。此時(shí)熔絲位為
#includeiom16v.h>const unsigned char duanma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};const unsigned char weima[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};unsigned char shu[8];unsigned int aa;unsigned int cnt; /****************************************延時(shí)函數(shù)****************************************/void delay(unsigned char t){unsigned char x,y;for(x=t;x>0;x--)for(y=100;y>0;y--);}/****************************************顯示函數(shù)****************************************/void display(void){unsigned char i;for(i=0;i4;i++){PORTB =0X00;PORTA|=0X01;PORTA=0XFE;PORTB =weima[i];PORTA|=0x02;PORTA=0XFD;PORTB=shu[i];PORTA|=0X01;PORTA=0XFE;delay(3);} }/***************************************數(shù)據(jù)轉(zhuǎn)換子函數(shù)***************************************/void change(unsigned int i){shu[0]=duanma[i/1000];shu[1]=duanma[i/100%10]|0x80;shu[2]=duanma[i%100/10];shu[3]=duanma[i%10];}/***************************************端口初始化***************************************/void init_port(void){PORTA=0XFF;DDRA=0XFF;PORTB=0XFF;DDRB=0XFF;PORTD=0XFF;DDRD=0X7B; // 0111 1011}/***************************************按鍵檢測(cè)***************************************/void keyscan(void){if((PIND0X80)!=0X80){change(cnt);display(); //延時(shí)display();if((PIND0X80)!=0X80){cnt=0; //按鍵按下 計(jì)數(shù)清零}}}/***************************************定時(shí)器1初始化***************************************/void timer1_init(void){TCNT1H=0xd8; //裝10ms的定時(shí)器初值TCNT1L=0xf0;}/***************************************定時(shí)器1服務(wù)函數(shù)***************************************/#pragma interrupt_handler timer1_ovf_isr:9void timer1_ovf_isr(void){TCNT1H=0XD8; //重裝10ms的定時(shí)器初值TCNT1L=0XF0;if(++cnt>9999) cnt=0;}/***************************************INT0中斷服務(wù)子函數(shù)***************************************/#pragma interrupt_handler int0_isr:2void int0_isr(void){if(cnt10) TCCR1B=0x02; //啟動(dòng)定時(shí)器1else TCCR1B=0x00; //關(guān)閉定時(shí)器1 }/***************************************IO及中斷初始化***************************************/void init_interrupt(void){init_port(); //IO初始化timer1_init(); //定時(shí)器1初始化 MCUCR=0X02; //INT0下降沿觸發(fā)GICR=0X40; //使能INT0TIMSK=0X04; //使能T1中斷 SREG=0X80; //使能總中斷}/***************************************MAIN***************************************/ void main(void){init_interrupt();while(1){ change(cnt);display();keyscan(); }}
評(píng)論