數(shù)碼管動態(tài)掃描三種實現(xiàn)方法
/**********利用定時器定時50毫秒動態(tài)掃描數(shù)碼管***********/ /**********利用延時函數(shù)定時50毫秒動態(tài)掃描數(shù)碼管***********/
/**
*功能:定時器T1實現(xiàn)數(shù)碼管動態(tài)掃瞄123456(switchcase)
*作者:徐冉
*日期:2013-06-12-21:50
*備注:不同的方法實現(xiàn)顯示設(shè)備的動態(tài)掃描,節(jié)約能耗
**/
/****************AT89C52-RC MCU*************/
/****************51hei開發(fā)板*************/
#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit wela = P2^7;
sbit dula = P2^6;
//段選數(shù)據(jù)編碼表
uchar code table[] = {
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F //"9"
};
//位選數(shù)據(jù)編碼表
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;//關(guān)閉數(shù)碼管顯示
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中斷服務(wù)程序
void timer1_int() interrupt 3
{
TH1 = 0xFC;
TL1 = 0x66;
counter++;
}
/**
*功能:延時函數(shù)實現(xiàn)數(shù)碼管動態(tài)掃瞄123456
*作者:徐冉
*日期:2013-06-12-20:50
*備注:不同的方法實現(xiàn)顯示設(shè)備的動態(tài)掃描,節(jié)約能耗
**/
/****************AT89C52-RC MCU*************/
/****************51hei開發(fā)板*************/
#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++);
}
//動態(tài)掃描程序
void display()
{
static uchar i = 0;//靜態(tài)變量i
dula = 1;
P0 = table[i+1];//段選數(shù)據(jù)
dula = 0;
P0 = 0xff;//消影
wela = 1;
P0 = table1[i]; //位選數(shù)據(jù)
wela = 0;
delay(50);//延時50毫秒
i++;
if(i >= 6)
{
i = 0;
}
}
//主程序
void main(void)
{
P0 = 0x00;//開始時關(guān)閉所有數(shù)碼管顯示
while(1)
{
display();
}
}
評論