目录
前言:
一、数码管静态显示
1、电路图
2、PCB图
3,原理
注意!A~G与1~9为互反
数字 | 哪部分亮起 | 2进制 | 16进制 |
0 | 00FE DCBA | 0011 1111 | 3f |
1 | 0000 0CB0 | 0000 0110 | 06 |
2 | 0G0E D0BA | 0101 1011 | 5b |
3 | 0G00 DCBA | 0100 1111 | 4f |
4 | 0GF0 0CB0 | 0110 0110 | 66 |
5 | 0GF0 DC0A | 0110 1101 | 6d |
6 | 0GFE DC0A | 0111 1101 | 7d |
7 | 0000 0CBA | 0000 0111 | 07 |
8 | 0GFE DCBA | 0111 1111 | 7f |
9 | 0GF0 DCBA | 0110 1111 | 6f |
4,代码
显示(0~9数字)
#include <REGX52.H>
unsigned char shu1[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char shu2[10]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};
void Delay200ms() //@11.0592MHz
{
unsigned char data i, j, k;
i = 9;
j = 104;
k = 139;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
unsigned char k;
while (1)
{
// P0=~shu[0]
for(k=0;k>=0&&k<=9;k++)
{
P0=~shu1[k];
P2=~shu2[k];
Delay200ms();
}
}
}
示例
二、数码管动态显示
1、电路图
2、PCB图
此处的4位数码管是自己画的,以方便接线
3,原理
二进制 | 发光的管子 | 十六进制 |
0000 0001 | A | 0x01 |
0000 0010 | F | 0x02 |
0000 0100 | B | 0x04 |
0000 1000 | E | 0x08 |
0001 0000 | D | 0x10 |
0010 0000 | DP | 0x20 |
0100 0000 | C | 0x40 |
1000 0000 | G | 0x80 |
GCPD EBFA | |||
显示数字 | 二极管 | 二进制 | 十六进制 |
0 | 00FE DCBA | 0101 1111 | 0x5f |
1 | 0000 0CB0 | 0100 0100 | 0x44 |
2 | 0G0E D0BA | 1001 1101 | 0x9d |
3 | 0G00 DCBA | 1101 0101 | 0xd5 |
4 | 0GF0 0CB0 | 1100 0110 | 0xc6 |
5 | 0GF0 DC0A | 1101 0011 | 0xd3 |
6 | 0GFE DC0A | 1101 1011 | 0xd |
7 | 0000 0CBA | 0100 0101 | 0x45 |
8 | 0GFE DCBA | 1101 1111 | 0xdf |
9 | 0GF0 DCBA | 1101 0111 | 0xd7 |
4,代码
动态显示
#include <REGX52.H>
unsigned char shu[ ]={0x5f,0x44,0x9d,0xd5,0xc6,0xd3,0xdb,0x45,0xdf,0xd7};
void Delay() //@11.0592MHz 50ms
{
unsigned char data i, j, k;
i = 3;
j = 26;
k = 223;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
unsigned i;
while(1)
{
for(i=0;i<=10;i++)
{
P0=~shu[i];
P2=0x80>>i;
Delay();
}
}
}
产品计数器
(1)产品默认数量为0
(2)按K1模拟产品+1
(3)按k2模拟产品-1
(4)有效数前0不显示
数码管消影
#include <REGX52.H>
sbit K1=P1^0;
sbit K2=P1^1;
//数字的段位码
unsigned char NUM[10]={0x5f,0x44,0x9d,0xd5,0xc6,0xd3,0xdb,0x45,0xdf,0xd7};
unsigned int temp,counter=0;//总数
unsigned char digit[8];//位数
//@12.000MHz 单位毫秒的延时函数
void Delay(unsigned int xms)
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}
void key()
{
if(K1==0)
{
Delay(200);
counter++;
}
if(K2==0)
{
Delay(200);
counter--;
}
}
//把累加的数字才分成一个一个的数字
void chaizi()
{
unsigned int a,b;//数字的位数
temp=counter;
for(b=0;temp!=0;b++)
{
digit[b]=NUM[temp%10];
temp=temp/10;
for(a=0;a<9;a++)
{
P0=~digit[a];
P2=0x01<<a;
Delay(1);
}
}
}
void main()
{
while(1)
{
key();
if(counter==0)
{
P0=~NUM[0];
P2=0x01;
}
else chaizi();
}
}
课堂练习:用LED数码管显示小时、分钟、秒
要求如下:
显示格式为 hh-mm-ss
按键调节小时和分钟
K1:小时+,不能超过23小时
K2:小时-,不能小于0小时
K3:分钟+,不能超过59分钟
K4:分钟-,不能小于0分钟
不考虑时钟走不走,只要可调就好