單片機數碼管顯示數字為流水燈亮的個數
/**
*Function:數碼管顯示數字為流水燈亮的個數
*Author: 徐冉
*Time: 2013-07-28
**/
/*************基于AT89C52-RC MCU************/
/************基于HL-1開發(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 num, counter;
/*延時子程序*/
void delay(uint xms)
{
uint x, y;
for(x = xms; x > 0; x--)
for(y = 125; y > 0; y--);
}
/*定時器T0初始化程序*/
void Time0_init()
{
P1 = 0xff;//關閉led燈
TMOD = 0x01;//設置定時器T0工作方式0
TH0 = 0xB8;//設定T0定時20ms
TL0 = 0x00;
TR0 = 1;//打開定時器T0
EA = 1;//開總中斷
ET0 = 1;//開定時器T0中斷
}
/*數碼管顯示子程序*/
void Display(uint num)
{
uchar shi, ge;
shi = num / 10 % 10;
ge = num % 10;
dula = 1;
P0 = table[shi];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0x00;
delay(5);
dula = 1;
P0 = table[ge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
P0 = 0x00;
delay(5);
}
/*主程序main()*/
void main(void)
{
Time0_init();
while(1)
{
if(num == 0)
{
P1 = 0xff;//1111 1111
}
else if(num == 1)
{
P1 = 0xfe;//1111 1110
}
else if(num == 2)
{
P1 = 0xfc; //1111 1100
}
else if(num == 3)
{
P1 = 0xf8;//1111 1000
}
else if(num == 4)
{
P1 = 0xf0;//1111 0000
}
else if(num == 5)
{
P1 = 0xe0;//1110 0000
}
else if(num == 6)
{
P1 = 0xc0;//1100 0000
}
else if(num == 7)
{
P1 = 0x80;//1000 0000
}
else if(num == 8)
{
P1 = 0x00;//0000 0000
}
Display(num);
}
}
/*定時器T0中斷服務子程序*/
void Time0_int() interrupt 1
{
TH0 = 0xB8;
TL0 = 0x00;
counter++;
if(counter == 50)
{
counter = 0;
num++;
if(num > 8)
{
num = 0;
}
}
}
評論