單片機(jī)左右流水燈與數(shù)碼管動(dòng)態(tài)顯示C程序
/*
*功能:用定時(shí)器0實(shí)現(xiàn)定時(shí)200毫秒流水燈,
* 同時(shí)用定時(shí)器1實(shí)現(xiàn)動(dòng)態(tài)顯示654321,
* 且蜂鳴器發(fā)聲;
*日期:2014-03-24
*作者:徐冉
*注意事項(xiàng):若打開兩個(gè)定時(shí)器時(shí),必須使用兩個(gè)定時(shí)器,
* 否則兩個(gè)定時(shí)器都不工作?。。?br />**/
/**********AT89C52-RC 51hei單片機(jī)實(shí)驗(yàn)板**************/
/*************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)//定時(shí)到4秒后,流水燈向右流動(dòng)
{
counter = 0;//counter清零,
//關(guān)閉定時(shí)器0
TR0 = 0;
//ET0 = 0;
flag = 1; //標(biāo)志變量置1后,定時(shí)器重新初始化
TH0 = 0xB8; //定時(shí)器0定時(shí)20毫秒
TL0 = 0x00;
TR0 = 1; //重新打開定時(shí)器0
//ET0 = 1;
j = 0;
}
}
}
評(píng)論