第四节:数码管驱动
显示“12345678”
P1端口接8联共阴数码管SLED8的段极:P1.7接段h,…,P1.0接段a
P2端口接8联共阴数码管SLED8的段极:P2.7接左边的共阴极,…,P2.0接右边的共阴极
方案说明:晶振频率fosc=12MHz,数码管采用动态刷新方式显示,在1ms定时断服务程序中实现
- #include <at89x92.h>
- unsigned char DisBuf[8]; //全局显示缓冲区,DisBuf[0]对应右SLED,DisBuf[7]对应左SLED,
- void DisplayBrush( void )
- {
- code unsigned char cathode[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //阴极控制码
- code unsigned char Seg7Code[16]= //用十六进数作为数组下标,可直接取得对应的七段编码字节
- {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- static unsigned char i=0; // (0≤i≤7) 循环刷新显示,由于是静态变量,此赋值只做一次。
- P2 = 0xff; //显示消隐,以免下一段码值显示在前一支SLED
- P1 = Seg7Code[ DisBuf[i] ]; //从显示缓冲区取出原始数据,查表变为七段码后送出显示
- P2 = cathode[ i ]; //将对应阴极置低,显示
- if( ++i >= 8 ) i=0; //指向下一个数码管和相应数据
- }
- void Timer0IntRoute( void ) interrupt 1
- {
- TL0 = -1000; //由于TL0只有8bits,所以将(-1000)低8位赋给TL0
- TH0 = (-1000)>>8; //取(-1000)的高8位赋给TH0,重新定时1ms
- DisplayBrush();
- }
- void Timer0Init( void )
- {
- TMOD=(TMOD & 0xf0) | 0x01; //初始化,定时器T0,工作方式1
- TL0 = -1000; //定时1ms
- TH0 = (-1000)>>8;
- TR0 = 1; //允许T0开始计数
- ET0 = 1; //允许T0计数溢出时产生中断请求
- }
- void Display( unsigned char index, unsigned char dataValue )
- {
- DisBuf[ index ] = dataValue;
- }
- void main( void )
- {
- unsigned char i;
- for( i=0; i<8; i++ )
- {
- Display(i, 8-i); //DisBuf[0]为右,DisBuf[7]为左
- }
- Timer0Init();
- EA = 1; //允许CPU响应中断请求
- While(1);
- }