/****************************************掃描數(shù)碼管,數(shù)據(jù)口接PA段碼接PC7 位碼接PC6穩(wěn)定顯示01234567****************************************/#include iom16v.h>#include macros.h>unsigned int i;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};/****************************************display****************************************/void display(unsigned int i){PORTA=0X00;PORTC|=BIT(7);PORTC=~BIT(7);PORTA=weima[i];PORTC|=BIT(6); PORTC=~BIT(6);PORTA=duanma[i];PORTC|=BIT(7);PORTC=~BIT(7);}/****************************************IO端口初始化****************************************/void init_port(void){PORTA=0XFF;DDRA=0XFF;PORTC=0XFF;DDRC=0XFF;}/****************************************定時器初始化****************************************/void init_timer(void){TCNT0=0X83; //1ms的定時器初值TCCR0=0X03; //定時器0的計數(shù)預分頻取64 TIMSK=0X01; //使能T/C0中斷SREG =0X80; //使能總中斷}/***************************************主函數(shù)***************************************/void main(void){init_port(); //調(diào)用IO端口初始化函數(shù)init_timer(); //定時器初始化while(1){//添加自己需要的程序代碼}}#pragma interrupt_handler timer0_ovf_isr:10 void timer0_ovf_isr(void){TCNT0=0X83; //重裝1ms定時器初值if(++i>7) i=0;display(i); }TCNT0=0X83; //1ms的定時器初值TCCR0=0X03; //定時器0的計數(shù)預分頻取64 8位定時器/計數(shù)器。 0x83=131。131-255 之間一共數(shù)了125.另外,外部晶振8M,經(jīng)過64分頻,周期成為1/(8000000/64)=8us 125個8us 就是1ms OK
評論