微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 数码管动态扫描三种实现方法

数码管动态扫描三种实现方法

时间:12-02 来源:互联网 点击:
本程序所用的原理图下载:点这里,单片机芯片使用的stc89c51;找到数码管部分的原理图即可.这是一整个单片机开发板的电路图其他的忽略.

以下是3个程序的源码:
/**********利用定时器定时50毫秒动态扫描数码管***********/
/**
*功能:定时器T1实现数码管动态扫瞄123456(if)
*作者:徐冉
*日期:2013-06-12-22:10
*备注:不同的方法实现显示设备的动态扫描,节约能耗
**/
/****************AT89C52-RC MCU*************/
/****************51hei开发板*************/
#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit wela = P2^7;
sbit dula = P2^6;
//段选数据编码表
uchar code table[] = {
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F //"9"
};
//位选数据编码表
uchar code table1[] = {
0xfe,
0xfd,
0xfb,
0xf7,
0xef,
0xdf
};
uchar j = 1, counter = 0;
//延时程序
void display()
{
dula = 1;
P0 = table[j++];
dula = 0;
if(j >= 7)
{
j = 1;
}
}
//刷新程序
void refrash()
{
static uchar i = 0;
if(0 == i)
{
wela = 1;
P0 = table1[0];
wela = 0;
}
if(1 == i)
{
wela = 1;
P0 = table1[1];
wela = 0;
}
if(2 == i)
{
wela = 1;
P0 = table1[2];
wela = 0;
}
if(3 == i)
{
wela = 1;
P0 = table1[3];
wela = 0;
}
if(4 == i)
{
wela = 1;
P0 = table1[4];
wela = 0;
}
if(5 == i)
{
wela = 1;
P0 = table1[5];
wela = 0;
}
i++;
if(i >= 6)
{
i = 0;
}
}
//定时器T1初始化
void init()
{
P0 = 0x00;//关闭数码管显示
TMOD = 0x11;
TH1 = 0xFC; //T1定时1毫秒
TL1 = 0x66;
TR1 = 1;
EA = 1;
ET1 = 1;
}
//主程序
void main(void)
{
init();
while(1)
{
if(counter == 50)
{
counter = 0;
display();//显示
refrash();//刷新
}
}
}
//T1中断服务程序
void timer1_int() interrupt 3
{
TH1 = 0xFC;
TL1 = 0x66;
counter++;
}

/**********利用定时器定时50毫秒动态扫描数码管***********/
/**
*功能:定时器T1实现数码管动态扫瞄123456(switchcase)
*作者:徐冉
*日期:2013-06-12-21:50
*备注:不同的方法实现显示设备的动态扫描,节约能耗
**/
/****************AT89C52-RC MCU*************/
/****************51hei开发板*************/
#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit wela = P2^7;
sbit dula = P2^6;
//段选数据编码表
uchar code table[] = {
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F //"9"
};
//位选数据编码表
uchar code table1[] = {
0xfe,
0xfd,
0xfb,
0xf7,
0xef,
0xdf
};
uchar j = 1, counter = 0;
//延时程序
void display()
{
dula = 1;
P0 = table[j++];
dula = 0;
if(j >= 7)
{
j = 1;
}
}
//刷新程序
void refrash()
{
static uchar i = 0;
switch(i)
{
case 0: wela = 1; P0 = table1[i]; wela = 0; i++;
break;
case 1: wela = 1; P0 = table1[i]; wela = 0; i++;
break;
case 2: wela = 1; P0 = table1[i]; wela = 0; i++;
break;
case 3: wela = 1; P0 = table1[i]; wela = 0; i++;
break;
case 4: wela = 1; P0 = table1[i]; wela = 0; i++;
break;
case 5: wela = 1; P0 = table1[i]; wela = 0; i++;
break;
default: break;
}
if(i >= 6)
{
i = 0;
}
}
//定时器T1初始化
void init()
{
P0 = 0x00;//关闭数码管显示
TMOD = 0x11;
TH1 = 0xFC; //T1定时1毫秒
TL1 = 0x66;
TR1 = 1;
EA = 1;
ET1 = 1;
}
//主程序
void main(void)
{
init();
while(1)
{
if(counter == 50)
{
counter = 0;
display();//显示
refrash();//刷新
}
}
}
//T1中断服务程序
void timer1_int() interrupt 3
{
TH1 = 0xFC;
TL1 = 0x66;
counter++;
}

/**********利用延时函数定时50毫秒动态扫描数码管***********/
/**
*功能:延时函数实现数码管动态扫瞄123456
*作者:徐冉
*日期:2013-06-12-20:50
*备注:不同的方法实现显示设备的动态扫描,节约能耗
**/
/****************AT89C52-RC MCU*************/
/****************51hei开发板*************/
#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit wela = P2^7;
sbit dula = P2^6;
//段选编码表
uchar code table[] = {
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F //"9"
};
//位选编码表
uchar code table1[] = {
0xfe,
0xfd,
0xfb,
0xf7,
0xef,
0xdf
};
//毫秒延时程序
void delay(uint xms)
{
uint x, y;
for(x = 0; x < xms; x++)
for(y = 0; y < 110; y++);
}
//动态扫描程序
void display()
{
static uchar i = 0;//静态变量i
dula = 1;
P0 = table[i+1];//段选数据
dula = 0;
P0 = 0xff;//消影
wela = 1;
P0 = table1[i]; //位选数据
wela = 0;
delay(50);//延时50毫秒
i++;
if(i >= 6)
{
i = 0;
}
}
//主程序
void main(void)
{
P0 = 0x00;//开始时关闭所有数码管显示
while(1)
{
display();
}
}

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

网站地图

Top