数码管动态扫描三种实现方法
以下是3个程序的源码: /**********利用定时器定时50毫秒动态扫描数码管***********/ /**********利用延时函数定时50毫秒动态扫描数码管***********/
/**********利用定时器定时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++;
}
/**
*功能:定时器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++;
}
/**
*功能:延时函数实现数码管动态扫瞄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();
}
}
数码管动态扫描stc89c5 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)