数码管静态显示:
这个是一个共阳的数码管例子:
注意电阻排的阻值选用200欧姆
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
uchar code seg[] = {0x40,0x79,0x24,0x30,0x19,
0x12,0x02,0x78,0x00,0x10};
void delay(uint timer)
{
uchar j = 124;
while(timer--)
{
while(j--);
}
}
void main()
{
uchar i=0 ;
while(1)
{
P2 = seg[i];
delay(500);
i = (i+1)%10;
}
}
动态显示
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
uchar code seg[] = {0xC0,0xF9,0xA4,0xB0,0x99,
0x92,0x82,0xF8,0x80,0x90};
void delay(uint timer)
{
uchar j = 124;
while(timer--)
{
while(j--);
}
}
void main()
{
uchar i=0 ;
while(1)
{
for(i = 0;i<4;i++)
{
P1 = (1<<i);
P2 = seg[i];
delay(500);
}
}
}
共阴数码管的拓展
共阴和共阳数码管的程序移植,在P1.P2同时取非即可,不用更换段码表。
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
uchar code seg[] = {0xC0,0xF9,0xA4,0xB0,0x99,
0x92,0x82,0xF8,0x80,0x90};
void delay(uint timer)
{
uchar j = 124;
while(timer--)
{
while(j--);
}
}
void main()
{
uchar i=0 ;
while(1)
{
for(i = 0;i<4;i++)
{
P1 = ~(1<<i);
P2 = ~seg[i];
delay(500);
}
}
}
共阴数码管段码
uchar code segD[] = {
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,
0x00,0xf3};
使用74138节约IO口
注意:一定要先选同138的地址端,再送段码。
否则字码会混乱(也可以使用74ls48+74ls138)
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
uchar code seg[] = {
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,
0x00,0xf3};
void delay(uint timer)
{
uchar j = 124;
while(timer--)
{
while(j--);
}
}
void display(uint year, uchar month, uchar day)
{
uint tmp;
tmp=year/1000%10;
P3=0;
P2=seg[tmp];
delay(1);
tmp=year/100%10;
P3=1;
P2=seg[tmp];
delay(1);
tmp=year/10%10;
P3=0x02;
P2=seg[tmp];
delay(1);
tmp=year%10;
P3=0x03;
P2=seg[tmp];
delay(1);
tmp=month/10%10;
P3=0x04;
P2=seg[tmp];
delay(1);
tmp=month%10;
P3=0x05;
P2=seg[tmp];
delay(1);
tmp=day/10%10;
P3=0x06;
P2=seg[tmp];
delay(1);
tmp=day%10;
P3=0x07;
P2=seg[tmp];
delay(1);
}
void main()
{
while(1)
{
display(2017, 12, 29);
}
}
使用74ls48+74ls138
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
uchar code seg[] = {
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,
0x00,0xf3};
void delay(uint timer)
{
uchar j = 124;
while(timer--)
{
while(j--);
}
}
void display(uint year, uchar month, uchar day)
{
uint tmp;
tmp=year/1000%10;
P3=0;
P2=tmp;
delay(1);
tmp=year/100%10;
P3=1;
P2=tmp;
delay(1);
tmp=year/10%10;
P3=0x02;
P2=tmp;
delay(1);
tmp=year%10;
P3=0x03;
P2=tmp;
delay(1);
tmp=month/10%10;
P3=0x04;
P2=tmp;
delay(1);
tmp=month%10;
P3=0x05;
P2=tmp;
delay(1);
tmp=day/10%10;
P3=0x06;
P2=tmp;
delay(1);
tmp=day%10;
P3=0x07;
P2=tmp;
delay(1);
}
void main()
{
while(1)
{
display(2017, 12, 29);
}
}