|
這是我編寫走60秒的帶小數(shù)點(diǎn)閃爍的時(shí)鐘,如果加上按鍵程序,可以做成時(shí)鐘。寫得不太好,你可以參考一下。
#include<reg52.h>
unsigned char code LedChar[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x80,0x00,
};
unsigned char LedBuff[8]={
0x3f, 0x3f, 0x40, 0x3f, 0x5b, 0x80, 0x5b, 0x5b ,
};
unsigned int cnt = 0;
void main()
{
unsigned char miao = 0;
unsigned char feng = 0;
unsigned char shi = 0;
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x67;
TR0 = 1;
EA = 1;
ET0 = 1;
while(1)
{
if(cnt >= 1000)
{
cnt = 0;
miao++;
if (miao%2==0) {LedBuff[2]=LedChar[10];}
else {LedBuff[2]=LedChar[11];}
if ( miao>=60) { miao=0;feng++; }
if ( feng>=60) {feng=0; shi++ ; }
if ( shi>=12) {shi=0; }
LedBuff[0] = LedChar[miao%10];
LedBuff[1] = LedChar[miao/10%10];
LedBuff[3] = LedChar[feng%10];
LedBuff[4] = LedChar[feng/10%10];
LedBuff[6] = LedChar[shi%10];
LedBuff[7] = LedChar[shi/10];
}
}
}
unsigned char i = 0; //i 這個(gè)變量寫到中斷函數(shù)里面,只能顯示個(gè)位的數(shù)碼管,不知為什么的
void InterruptTimer0() interrupt 1
{
TH0 = 0xFC;
TL0 = 0x67;//定時(shí)器一毫秒中斷一次
cnt++;
P0 = 0x00;
if(i == 0)
{P2=7; i++; P0=LedBuff[0];}
else if(i == 1)
{P2=6; i++; P0=LedBuff[1];}
else if(i == 2)
{P2=5; i++; P0=LedBuff[2];}
else if(i == 3)
{P2=4; i++; P0=LedBuff[3];}
else if(i == 4)
{P2=3; i++; P0=LedBuff[4];}
else if(i == 5)
{P2=2; i++; P0=LedBuff[5];}
else if(i == 6)
{P2=1; i++; P0=LedBuff[6];}
else if(i == 7)
{P2=0; i=0; P0=LedBuff[7];}
} |
評(píng)分
-
查看全部評(píng)分
|