微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 数码管: 基于STC89C52的数码管设计详解

数码管: 基于STC89C52的数码管设计详解

时间:11-22 来源:互联网 点击:
一. 硬件设计详解


说明:
选通一个数码管分两步,
第一步是位选,即选择哪个数码管亮, 这里位选是通过锁存器U2的WE1~WE6来选择第一个数码管到第六个数码管,由于数码管为共阴极(相对于8位数码管),那么这里WE端为0代表位选选通,
第二步是段选,即选择数码管的哪位亮, 这里段选是通过锁存器U1的a~h来选择数码管的8位管子,这里DUAN端为1代表段选选通.

二. 程序设计详解
1. 让第一个数码管显示8
#include

sbit duan = P2 ^ 6;
sbit wela = P2 ^ 7;

void main()
{
wela = 1;
P0 = 0xfe;
wela = 0;

duan = 1;
P0 = 0x7f;
duan = 0;

while (1);
}

2. 让6个数码管显示反复显示0~F
#include
#define uchar unsigned char
#define uint unsigned int

sbit duan = P2 ^ 6;
sbit wela = P2 ^ 7;

uchar code table[] = {
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};

void delayms(uint xms);

void main()
{
uchar num;

wela = 1;
P0 = 0xc0;
wela = 0;

while (1)
{
for (num = 0; num < 16; num++)
{
duan = 1;
P0 = table[num];
duan = 0;

delayms(500);
}
}

}

void delayms(uint xms)
{
uint i, j;

for (i = xms; i > 0; i--)
for (j = 110; j > 0; j--)
;
}

3.
第一个数码管显示1,时间为0.5s,然后关闭它,
第二个数码管显示2,时间为0.5s,然后关闭它,

第六个数码管显示6,时间为0.5s,然后关闭它,
依次循环.
#include
#define uchar unsigned char
#define uint unsigned int

sbit duan = P2 ^ 6;
sbit wela = P2 ^ 7;

uchar code we_table[] = {
0xfe,0xfd,0xfb,0xf7,0xef,0xdf
};

uchar code du_table[] = {
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};

void delayms(uint xms);

void main()
{
uchar num;

while (1)
{
for (num = 0; num < 6; num++)
{
wela = 1;
P0 = we_table[num];
wela = 0;

P0 = 0xff;

duan = 1;
P0 = du_table[num+1];
duan = 0;

delayms(1);
}
}
}

void delayms(uint xms)
{
uint i, j;

for (i = xms; i > 0; i--)
for (j = 110; j > 0; j--)
;
}

说明:
只要将延时时间调整为1s,我们可以看到数码管一起显示123456.
P0 = 0xff; 是用来消影, 即消除段选与位选之间的影响.



4.
定时器0的方式1实现第一个发光二极管以200ms间隔闪烁,
定时器1的方式1实现数码管前两位59s循环计时
#include
#define uchar unsigned char
#define uint unsigned int

sbit dula = P2 ^ 6;
sbit wela = P2 ^ 7;
sbit led1 = P1 ^ 0;

uchar code table[] = {
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};

void delayms(uint);
void display(uint);

uchar num, num1, num2;

void main()
{
TMOD = 0x11;
TH0 = (65536 - 45872) / 256;
TL0 = (65536 - 45872) % 256;
TH1 = (65536 - 45872) / 256;
TL1 = (65536 - 45872) % 256;
EA = 1;
ET0 = 1;
ET1 = 1;
TR0 = 1;
TR1 = 1;
while (1)
{
display(num);
}
}

void display(uint num)
{
dula = 1;
P0 = table[num / 10];
dula = 0;

P0 = 0xff;

wela = 1;
P0 = 0xfe;
wela = 0;

delayms(5);

dula = 1;
P0 = table[num % 10];
dula = 0;

P0 = 0xff;

wela = 1;
P0 = 0xfd;
wela = 0;

delayms(5);
}

void delayms(uint xms)
{
uint i, j;

for (i = xms; i > 0; i--)
for (j = 110; j > 0; j--)
;
}

void T0_time() interrupt 1
{

TH0 = (65536 - 45872) / 256;
TL0 = (65536 - 45872) % 256;
num1++;
if (num1 == 4)
{
num1 = 0;
led1 = ~led1;
}
}

void T1_time() interrupt 3
{

TH1 = (65536 - 45872) / 256;
TL1 = (65536 - 45872) % 256;
num2++;
if (num2 == 20)
{
num2 = 0;
num++;
if (num == 60)
num = 0;
}
}

5. 用定时器以间隔500MS在6位数码管上依次显示0、1、2、3….C、D、E、F,重复
#include
#include

#define uint unsigned int
#define uchar unsigned char

sbit dula = P2 ^ 6;
sbit wela = P2 ^ 7;

uchar num, tt;
uchar code table[] = {
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};

void main()
{
num = 0;
tt = 0;

TMOD = 0x01;
TH0 = (65536 - 45872) / 256;
TL0 = (65536 - 45872) % 256;
EA = 1;
ET0 = 1;
TR0 = 1;

dula = 1;
P0 = 0x3f;
dula = 0;

wela = 1;
P0 = 0xc0;
wela = 0;

while (1)
{
if (tt == 10)
{
tt = 0;
num++;
if (num == 16)
num = 0;

dula = 1;
P0 = table[num];
dula = 0;
}
}
}

void T0_timer() interrupt 1
{
TH0 = (65536 - 45872) / 256;
TL0 = (65536 - 45872) % 256;
tt++;
}

6. 利用动态扫描方法在六位数码管上显示出稳定的654321
#include
#include

#define uint unsigned int
#define uchar unsigned char

sbit dula = P2 ^ 6;
sbit wela = P2 ^ 7;

uchar code du_table[] = {
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};

uchar code we_table[] = {
0xfe,0xfd,0xfb,0xf7,0xef,0xdf
};

void delayms(uint);

void main()
{
uchar num;

for (num = 6; num > 0; num--)
{
dula = 1;
P0 = du_table[num];
dula = 0;

P0 = 0xff;

wela = 1;
P0 = we_table[6 - num];
wela = 0;

delayms(1);
}
}

void delayms(uint xms)
{
uint i, j;

for (i = xms; i > 0; i--)
for (j = 110; j > 0; j--)
;
}



7. 用动态扫描方法和定时器1在数码管的前三位显示出秒表,精确到1%秒,即后两位显示1%秒,一直循环下去
#include
#include

#define uint unsigned int
#define uchar unsigned char

sbit dula = P2 ^ 6;
sbit wela = P2 ^ 7;

uint tt;
uchar code du_table[] = {
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};

void display(uint);
void delayms(uint);

void main()
{
TMOD = 0x10;
TH1 = (65536 - 10000) / 256;
TL1 = (65536 - 10000) % 256;
EA = 1;
ET1 = 1;
TR1 = 1;
while (1)
{
display(tt);
}
}

void T1_timer() interrupt 3
{
TH1 = (65536 - 10000) / 256;
TL1 = (65536 - 10000) % 256;
tt++;
if (tt == 1000)
tt = 0;
}

void display(uint num)
{
dula = 1;
P0 = du_table[num / 100];
dula = 0;

P0 = 0xff;

wela = 1;
P0 = 0xfe;
wela = 0;

delayms(1);

dula = 1;
P0 = du_table[num % 100 / 10];
dula = 0;

P0 = 0xff;

wela = 1;
P0 = 0xfd;
wela = 0;

delayms(1);

dula = 1;
P0 = du_table[num % 100 % 10];
dula = 0;

P0 = 0xff;

wela = 1;
P0 = 0xfb;
wela = 0;

delayms(1);
}

void delayms(uint xms)
{
uint i, j;

for (i = xms; i > 0; i--)
for (j = 110; j > 0; j--)
;
}

8. 利用动态扫描和定时器1在数码管上显示出从765432开始以1/10秒的速度往下递减直至765398并保持显示此数,与此同时利用定时器0以500MS速度进行

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top