|
- //清理硬盤時(shí)發(fā)現(xiàn)自己以前寫的,貼上來(lái)吧,雖然沒(méi)什么水平,
- //5毫秒一個(gè)顯示周期,理論上顯示20位數(shù)碼管不會(huì)有閃爍感,
- 占用了一個(gè)定時(shí)器,簡(jiǎn)約的算法實(shí)現(xiàn)高位滅零處理
- #include <avr/io.h>
- #include <avr/interrupt.h>
- unsigned int flag=0;
- unsigned char count=0;
- unsigned int num=0;
- //==================================================
- const unsigned char seg[]={ 0xC0, // 0
- 0xF9, // 1
- 0xA4, // 2
- 0xB0, // 3
- 0x99, // 4
- 0x92, // 5
- 0x82, // 6
- 0xF8, // 7
- 0x80, // 8
- 0x90 // 9
- };
- //==================================================
- //IO端口初始化
- void PortInit(void)
- {
- DDRA=0XFF;
- PORTA=0X00;
- DDRB=0XFF;
- PORTB=0XFF;
- }
- //Timer0初始化
- void Timer0Init(void)
- {
- TCCR0 = 0x00; //stop
- TCNT0 = 0x06; //set count
- OCR0 = 0xFA; //set compare
- TCCR0 = 0x03; //start timer
- }
- //==================================================
- //定時(shí)器0溢出中斷服務(wù)程序
- ISR(TIMER0_OVF_vect)
- {
- TCNT0=0X06;
- flag++;
- count++;
- switch(count)
- {
- case 1:if(num/1000){PORTA=0X01;PORTB=seg[num/1000];}else PORTB=0XFF;break;
- case 2:if(((num/1000)+(num%1000/100))){PORTA=0X02;PORTB=seg[num%1000/100];}else PORTB=0XFF;break;
- case 3:if(((num/1000)+(num%1000/100)+(num%100/10))){PORTA=0X04;PORTB=seg[num%100/10];}else PORTB=0XFF;break;
- case 4:PORTA=0X08;PORTB=seg[num%10];break;
- case 5:count=0;break;
- default:break;
- }
- if (flag==100)
- {
- flag=0;
- num++;
- if(num==9999)
- {
- num=0;
- }
- }
- }
- //==================================================
- //主函數(shù)
- int main(void)
- {
- cli();
- TIMSK = 0x01; //timer interrupt sources
- PortInit();
- Timer0Init();
- sei();
- while(1)
- {
- ;
- }
- }
復(fù)制代碼
|
|