/****************************************用定时器维持数码管显示现在是数码管示数不断增加。****************************************/#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};/***************************************数据转换子函数***************************************/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];}/****************************************延时函数****************************************/void delay(unsigned int t){unsigned int x,y;for(x=t;x>0;x--)for(y=100;y>0;y--);}/****************************************显示函数****************************************/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; //定时器的计数预分频取64}/***************************************定时器0服务函数***************************************/#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);}}
评论