/****************************************用定時器維持?jǐn)?shù)碼管顯示現(xiàn)在是數(shù)碼管示數(shù)不斷增加。****************************************/#include iom16v.h>unsigned int cnt;unsigned char shu[8];unsigned char aa;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};/***************************************數(shù)據(jù)轉(zhuǎn)換子函數(shù)***************************************/void change(unsigned int i){shu[0]=duanma[i/1000];shu[1]=duanma[i/100%10];shu[2]=duanma[i%100/10];shu[3]=duanma[i%10];}/****************************************延時函數(shù)****************************************/void delay(unsigned int t){unsigned int x,y;for(x=t;x>0;x--)for(y=100;y>0;y--);}/****************************************顯示函數(shù)****************************************/void display(unsigned char i){PORTB =0X00;PORTA|=0X01;PORTA=0XFE;PORTB =weima[i];PORTA|=0x02;PORTA=0XFD;PORTB=shu[i];PORTA|=0X01;PORTA=0XFE;}/***************************************端口初始化***************************************/void init_port(void){PORTA=0XFF;DDRA=0XFF;PORTB=0XFF;DDRB=0XFF;}/***************************************定時器0初始化***************************************/void timer0_init(void){TCNT0=0X83; //1MS的定時初值TCCR0=0X03; //定時器的計數(shù)預(yù)分頻取64}/***************************************定時器0服務(wù)函數(shù)***************************************/#pragma interrupt_handler timer0_ovf_isr:10void timer0_ovf_isr(void){TCNT0=0X83; //重裝1ms的定時初值if(++aa>3) aa=0;switch(aa){case 0: display(0);break;case 1: display(1);break;case 2: display(2);break;case 3: display(3);break;default:break;}}void main(void){init_port(); //IO端口初始化timer0_init(); //定時器1初始化 TIMSK=0X01; //使能T0中斷 SREG=0X80; //使能總中斷while(1){cnt++;delay(1000);change(cnt);}}
評論