單片機左右流水燈與數(shù)碼管動態(tài)顯示C程序
/*
*功能:用定時器0實現(xiàn)定時200毫秒流水燈,
* 同時用定時器1實現(xiàn)動態(tài)顯示654321,
* 且蜂鳴器發(fā)聲;
*日期:2014-03-24
*作者:徐冉
*注意事項:若打開兩個定時器時,必須使用兩個定時器,
* 否則兩個定時器都不工作!??!
**/
/**********AT89C52-RC 51hei單片機實驗板**************/
/*************51hei-開發(fā)板*********************/
#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit wela = P2^7; //數(shù)碼管位聲明
sbit dula = P2^6; //數(shù)碼管段選聲明
sbit FM = P2^3;//蜂鳴器聲明
uchar counter = 0, counter1 = 0, j = 0, flag = 0, flag1 = 0;
uchar num = 6, num1 = 0;
//數(shù)碼管段選編碼
uchar code table_du [] = {
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00, //熄滅
0x00 //自定義
};
//數(shù)碼管位選編碼
uchar code table_we[] = {
0xfe,
0xfd,
0xfb,
0xf7,
0xef,
0xdf
};
void init();
void display();
void main()
{
init();
while(1)
{
if(counter1 == 30)
{
counter1 = 0;
display();
num--;
num1++;
if(num == 0)
{
num = 6;
}
if(num1 == 6)
{
num1 = 0;
}
}
if(counter == 200)//定時到4秒后,流水燈向右流動
{
counter = 0;//counter清零,
//關(guān)閉定時器0
TR0 = 0;
//ET0 = 0;
flag = 1; //標志變量置1后,定時器重新初始化
TH0 = 0xB8; //定時器0定時20毫秒
TL0 = 0x00;
TR0 = 1; //重新打開定時器0
//ET0 = 1;
j = 0;
}
}
}
評論