- //清理硬盘时发现自己以前写的,贴上来吧,虽然没什么水平,
- //5毫秒一个显示周期,理论上显示20位数码管不会有闪烁感,
- 占用了一个定时器,简约的算法实现高位灭零处理
- #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
- }
- //==================================================
- //定时器0溢出中断服务程序
- 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;
- }
- }
- }
- //==================================================
- //主函数
- int main(void)
- {
- cli();
- TIMSK = 0x01; //timer interrupt sources
- PortInit();
- Timer0Init();
- sei();
- while(1)
- {
- ;
- }
- }
复制代码
|