锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

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

时间:2024-05-03 14:37:09

本程序所用的原理图下载: 点这里 ,单片机芯片使用的stc89c51;找到部分的原理图即可.这是一整个单片机的电路图其他的忽略.

以下是3个程序的源码:
/**********利用定时器定时50毫秒动态扫描数码管***********/
/**
*功能:定时器T1实现数码管动态扫瞄123456(if)
*作者:徐冉
*日期:2013-06-12-22:10
*备注:不同的方法实现显示设备的动态扫描,节约能耗
**/
/****************AT89C52-RC *************/
/****************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毫秒
= 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(switch case)
*作者:徐冉
*日期: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();
}
}


-电子元器件采购网(www.ruidan.com)是本土元器件目录分销商,采用“小批量、现货、样品”销售模式,致力于满足客户多型号、高质量、快速交付的采购需求。 自建高效智能仓储,拥有自营库存超过50,000种,提供一站式正品现货采购、个性化解决方案、选型替代等多元化服务。
锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章