單片機(jī)C語言電子時鐘加日歷顯示編程
設(shè)計制作一個電子表并且能夠顯示日歷,用LED數(shù)碼管直接顯示,并且能夠通過按鍵調(diào)整時間、年月日,功能為按第一個鍵被調(diào)整的相應(yīng)調(diào)整區(qū)域開始閃爍,第二個鍵加一,第三個減一,第四個確認(rèn),最后一個在時分秒和年月日中間相互切換。
二、方案論證
我們在這里選用了C8051F單片機(jī),它是完全集成的混合信號系統(tǒng)級芯片(SoC),具有與8051兼容的高速CIP-51內(nèi)核,與MCS-51指令集完全兼容,片內(nèi)集成了數(shù)據(jù)采集和控制系統(tǒng)中常用的模擬、數(shù)字外設(shè)及其他功能部件;內(nèi)置FLASH程序存儲器、內(nèi)部RAM,大部分器件內(nèi)部還有位于外部數(shù)據(jù)存儲器空間的RAM,即XRAM。C8051F單片機(jī)具有片內(nèi)調(diào)試電路,通過4腳的JTAG接口可以進(jìn)行非侵入式、全速的在系統(tǒng)調(diào)試。
三、 系統(tǒng)硬件電路設(shè)計
該系統(tǒng)分為兩個部分,主控電路和掃描電路,其中鍵盤掃描和LED掃描為今后節(jié)省I/O口線,我將鍵盤的掃描和LED的掃描共用,在掃描LED的同時也掃描鍵盤,這樣不但方便,而且為今后的擴(kuò)展留下了相當(dāng)大的余地。
3.1主控制器
單片機(jī)使用c8051f310,并且使用它的開發(fā)板易于燒錄,改變程序和外圍電路。
3.2顯示電路
顯示板因為常用,是我自己畫出來交給工廠做的,采用4個兩位共陽數(shù)碼管,從P0口輸出,8位段碼分別和P0相接,通過定時器實現(xiàn)秒鐘的加一,又因為310有推挽輸出,所以不必再加外部電路對數(shù)碼管進(jìn)行驅(qū)動,直接將它們與P0口相連就可以
3.3鍵盤電路
鍵盤是是與數(shù)碼管的位選相連,通過二極管使它們具有單向性,同時將開頭最終通過三極管接單片機(jī)的P3.4,只要有按鍵按下,P3.4就一定會發(fā)生變化,通過這個變化在去掃描位就可以得出是哪個按鍵了。
附電路圖:
四、系統(tǒng)程序設(shè)計
系統(tǒng)和程序主要包括:顯示程序,鍵盤掃描程序,自動加秒,分,時,日,月,年,
附程序:
#include "c8051f310.h"
#define uchar unsigned char
#define unchar unsigned char
uchar code duan[10] ={0xa0,0xbd,0x26,0x2c,0x39,0x68,0x60,0xad,0x20,0x28};
//段碼表 0 1 2 3 4 5 6 7 8 9
uchar code wei[8]={0x02,0x01,0x08,0x04,0x20,0x10,0x80,0x40};//位碼表
uchar code week1[7]={0xbd,0x26,0x2c,0x39,0x68,0x60,0xad} ;//星期一到星期日
uchar dataa,sec,min,hour,ymd,year,year1=9,year2=20,month=8,day=22,week=6;//初始值是7-00:00:00,日期是2009-08-22;
uchar n,cnt,sw1=0,sw2,sw3,cnt1,sw5;
uchar key1,key2,key3,key4=0,key5=0;
sbit P3_4=P3^4;
void scan_key(void);
void delay(ms);
void init(void)
{
IE =0x8A;
TCON =0x50;
TMOD =0x11;
IP =0x0A;
TH0 =(65536-1270)/256;
TL0 =(65536-1270)%256;
TH1 =(65536-500)/256;
TL1 =(65536-500)%256;
}
void time0(void) interrupt 1
{
TH0 =(65536-1270)/256;
TL0 =(65536-1270)%256;
dataa++;
if(sw1!=0&&key4==0) //第一個按鍵按下,且第四個沒有按下
{
if(++cnt1==40)
{
cnt=0;
if(sw5==0&&sw1==1)
{
for(n=0;n<20;n++)
{
P1 =0x00;
P1 =P1&0xf3; //閃爍程序
P0 =duan[sec%10];
P1 =wei[0];
delay(8);
P1 =0x00;
P0 =duan[sec/10];
P1 =wei[1];
delay(8);
P1 =0x00;
P0 =duan[hour%10];
P1 =wei[4];
delay(8);
P1 =0x00;
P0 =duan[hour/10];
P1 =wei[5];
delay(8);
P1=0x00;
P0 =0x7f;
P1 =wei[6];
delay(8);
P1=0x00;
P0 =week1[week];
P1 =wei[7];
delay(8);
}
scan_key();
}
if(sw5==0&&sw1==2)
{
for(n=0;n<20;n++)
{
P1 =0x00;
P1 =P1&0xcf;
P0 =duan[sec%10];
P1 =wei[0];
delay(8);
P1 =0x00;
P0 =duan[sec/10];
P1 =wei[1];
delay(8);
P1 =0x00;
P0 =duan[min%10];
P1 =wei[2];
delay(8);
P1 =0x00;
P0 =duan[min/10];
P1 =wei[3];
delay(8);
P1 =0x00;
P0 =0x7f;
P1 =wei[6];
delay(8);
P1 =0x00;
P0 =week1[week];
P1 =wei[7];
delay(8);
}
scan_key();
}
if(sw5==0&&sw1==3)
{
for(n=0;n<20;n++)
{
P1 =0x00;
P1 =P1&0x7f;
P0 =duan[sec%10];
P1 =wei[0];
delay(8);
P1 =0x00;
P0 =duan[sec/10];
P1 =wei[1];
delay(8);
P1 =0x00;
P0 =duan[min%10];
P1 =wei[2];
delay(8);
P1 =0x00;
P0 =duan[min/10];
P1 =wei[3];
delay(8);
P1=0x00;
P0=duan[hour%10];
P1=wei[4];
delay(8);
P1 =0x00;
P0 =duan[hour/10];
P1 =wei[5];
delay(8);
P1 =0x00;
P0 =0x7f;
P1 =wei[6];
delay(8);
}
scan_key();
}
if(sw5==1&&sw1==9)
{
for(n=0;n<20;n++)
{
P1 =0x00;
P1 =P1&0x3f;
P0 =duan[month%10];
P1 =wei[2];
delay(8);
P1 =0x00;
P0 =duan[month/10];
P1 =wei[3];
delay(8);
P1 =0x00;
P0 =duan[year1%10];
P1 =wei[4];
delay(8);
P1 =0x00;
P0 =duan[year1/10];
P1 =wei[5];
delay(8);
P1 =0x00;
P0 =duan[year2%10];
P1 =wei[6];
delay(8);
P1 =0x00;
P0 =duan[year2/10];
P1 =wei[7];
delay(8);
}
scan_key();
}
if(sw5==1&&sw1==8)
{
for(n=0;n<20;n++)
{
P1 =0x00;
P1 =P1&0xf3;
P0 =duan[day%10];
P1 =wei[0];
delay(8);
P1 =0x00;
P0 =duan[day/10];
P1 =wei[1];
delay(8);
P1 =0x00;
P0 =duan[year1%10];
P1 =wei[4];
delay(8);
P1 =0x00;
P0 =duan[year1/10];
P1 =wei[5];
delay(8);
P1 =0x00;
P0 =duan[year2%10];
P1 =wei[6];
delay(8);
P1 =0x00;
P0 =duan[year2/10];
P1 =wei[7];
delay(8);
}
scan_key();
}
if(sw5==1&&sw1==7)
{
for(n=0;n<20;n++)
{
P1 =0x00;
P1 =P1&0xc3;
P0 =duan[day%10];
P1 =wei[0];
delay(8);
P1 =0x00;
P0 =duan[day/10];
P1 =wei[1];
delay(8);
P1 =0x00;
P0 =duan[month%10];
P1 =wei[2];
delay(8);
P1 =0x00;
P0 =duan[month/10];
P1 =wei[3];
delay(8);
P1 =0x00;
P0 =duan[year2%10];
P1 =wei[6];
delay(8);
P1 =0x00;
P0 =duan[year2/10];
P1 =wei[7];
delay(8);
}
scan_key();
}
if(sw5==1&&sw1==6)
{
for(n=0;n<20;n++)
{
P1 =0x00;
P1 =P1&0xc3;
P0 =duan[day%10];
P1 =wei[0];
delay(8);
P1 =0x00;
P0 =duan[day/10];
P1 =wei[1];
delay(8);
P1 =0x00;
P0 =duan[month%10];
P1 =wei[2];
delay(8);
P1 =0x00;
P0 =duan[month/10];
P1 =wei[3];
delay(8);
P1 =0x00;
P0 =duan[year1%10];
P1 =wei[4];
delay(8);
P1 =0x00;
P0 =duan[year1/10];
P1 =wei[5];
delay(8);
}
scan_key();
}
}
}
}
void time1(void) interrupt 3
{
TH1 =(65536-500)/256;
TL1 =(65536-500)%256;
if(++cnt>8) cnt=0;
if(sw5==0) //顯示時分秒
{switch(cnt)
{
case 0: P1 =0x00;
P0 =duan[sec%10];
P1 =wei[cnt]; break;
case 1: P1 =0x00;
P0 =duan[sec/10];
P1 =wei[cnt];break;
case 2: P1 =0x00;
P0 =duan[min%10];
P1 =wei[cnt]; break;
case 3: P1 =0x00;
P0 =duan[min/10];
P1 =wei[cnt];
scan_key(); break;
case 4: P1 =0x00;
P0 =duan[hour%10];
P1 =wei[cnt];
scan_key();break;
case 5: P1 =0x00;
P0 =duan[hour/10];
P1 =wei[cnt];
scan_key();break;
case 6: P1 =0x00;
P0 =0x7f;
P1 =wei[cnt];
scan_key(); break;
case 7: P1 =0x00;
P0 =week1[week];
P1 =wei[cnt];
scan_key();break;
default: break;
}
}
if(sw5==1) //顯示年月日
{switch(cnt)
{
case 0: P1 =0x00;
P0 =duan[day%10];
P1 =wei[cnt]; break;
case 1: P1 =0x00;
P0 =duan[day/10];
P1 =wei[cnt];break;
case 2: P1 =0x00;
P0 =duan[month%10];
P1 =wei[cnt]; break;
case 3: P1 =0x00;
P0 =duan[month/10];
P1 =wei[cnt];
scan_key(); break;
case 4: P1 =0x00;
P0 =duan[year1%10];
P1 =wei[cnt];
scan_key();break;
case 5: P1 =0x00;
P0 =duan[year1/10];
P1 =wei[cnt];
scan_key();break;
case 6: P1 =0x00;
P0 =duan[year2%10];
P1 =wei[cnt];
scan_key(); break;
case 7: P1 =0x00;
P0 =duan[year2/10];
P1 =wei[cnt];
scan_key();break;
default: break;
}
}
}
void conv(void)
{
if(dataa>=201)
{dataa=0;sec++;}
if(sec>=60)
{sec=0;min++;}
if(sw1==0&&min>=60)
{min=0;hour++;}
if(sw1==0&&hour>=24)
{
if(year%4==0)
{
if(month==2)
{
if(day==29)
{
day =1;
month++;
}
else day++;
}
else
{
if(month==4||month==6||month==9||month==11)
{
if(day==30)
{
day =1;
month++;
}
else day++;
}
else
{
if(day==31)
{
day =1;
month++;
if(month==13)
{
month =1;
year++;
}
}
else day++;
}
}
}
評論