7段数码管
而对于两位一体的数码管有共阴和共阳之分,共阳的内部电路结构如下图所示,这种就需要控制器对这两个数码管进行不同时间段的轮流显示,而轮流显示的频率要大于人眼能感觉到的闪烁频率。
下图是proteus的电路原理图
下面是C语言程序源代码
- //用AT89C52实现的两位共阳数码管显示程序
- #include <reg52.h>
- #include <type_def.h>
- #include <intrins.h>
- extern void Delay10ms(void);
- extern void display(UINT8 number);
- sbit SW = P1^7;
- sbit LED_GREEN = P2^3;
- sbit BUZZER = P2^4;
- sbit WEI_SHI = P2^5;
- sbit WEI_GE = P2^6;
- int tcount = 0;//记录经过的10ms区间段个数
- UINT8 add_cnt = 0;
- UINT8 num = 0;
- UINT8 code num_table[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- void T0_INT() interrupt 1 //timer0中断, 每10ms触发一次
- {
- TR0 = 0;//关闭timer0
- TH0 = 0xD8;
- TL0 = 0xF0;//65536 - 10000 = 55536
- if(tcount > 0)
- {
- tcount ++;
- }
- if(tcount > 11) //10ms * 10 = 100ms
- {
- tcount = 0;
- BUZZER = 1;
- LED_GREEN = 1;
- }
- display(num);
- TR0 = 1;//重新开启timer0
- }
- void display(UINT8 num)
- {
- if (!add_cnt)
- {
- P0 = 0xff; //关8个段码
- WEI_SHI = 1; //打开十位的数码管共阳极公共端
- WEI_GE = 0;
- P0 = num_table[num/10]; //显示的十位数字
- add_cnt = 1;
- }
- else
- {
- P0 = 0xff; //关8个段码
- WEI_SHI = 0; //关十位的数码管
- WEI_GE = 1; //打开个位的数码管共阳极公共端
- P0 = num_table[num%10]; //显示的个位数字
- add_cnt = 0;
- }
- }
- void Delay10ms(void) //@12.000MHz
- {
- unsigned char i, j;
- i = 20;
- j = 113;
- do
- {
- while (--j);
- } while (--i);
- }
- main()
- {
- BUZZER = 1;
- LED_GREEN = 1;
- TMOD = 0x01;
- TH0 = 0xD8;
- TL0 = 0xF0; //65536 - 10000 = 55536
- IE = 0x82; //EA + ET0 --> 1000 0010
- TR0 = 1; //开启timer0
- while(1){
- if(0 == SW){
- Delay10ms();
- if(0 == SW){
- while(0 == SW){}
- BUZZER = 0;
- LED_GREEN = 0;
- tcount = 1;
- num++;
- if (num > 99)
- {
- num = 0;
- }
- }
- }
- }
- return 1;
- }