⑴ 單片機課程設計
剛給一個兄弟做的
KEYVAL EQU30H
KEYTM EQU31H
KEYSCAN EQU32H
DAT EQU33H
SCANLED EQU39H
CLK EQU77H
SEC EQU78H
MIN EQU79H
HOUR EQU7AH
PAUSE BIT00H
DOT BIT01H
ORG0000H
LJMPMAIN
ORG000BH
LJMPT0ISR ;50ms定時
ORG001BH
LJMPT1ISR ;掃描顯示
ORG0030H
MAIN:
MOVSP,#5FH
MOVTMOD,#11H
MOVTH0,#03CH
MOVTL0,#0B0H
MOVTH1,#0ECH
MOVTL1,#078H
MOVKEYVAL,#0
MOVSCANLED,#0
MOV33H,#10H
MOV34H,#10H
MOV35H,#10H
MOV36H,#10H
MOV37H,#10H
MOV38H,#10H
MOVSEC,#0
MOVMIN,#0
MOVHOUR,#0
MOVCLK,#0
CLRPAUSE
SETBEA
SETBET1
SETBTR1
LOOP:
LCALLKEYSEL
MOVA,KEYVAL
CJNEA,#0FFH,LOOP1
SJMPLOOP
LOOP1:
CJNEA,#10,LOOP2 ;「ON」啟動
SETBTR0
SETBET0
SETBPAUSE
SJMPLOOP
LOOP2:
CJNEA,#11,LOOP3 ;「=」清零
MOVSEC,#0
MOVMIN,#0
MOVHOUR,#0
LCALLDISCHG
SJMPLOOP
LOOP3:
CJNEA,#15,LOOP4 ;「+」暫停
CLRTR0
CLRET0
CLRPAUSE
SJMPLOOP
LOOP4:
CJNEA,#14,LOOP5 ;「-」清顯示暫停
MOV33H,#10H
MOV34H,#10H
MOV35H,#10H
MOV36H,#10H
MOV37H,#10H
MOV38H,#10H
CLRTR0
CLRET0
CLRPAUSE
SJMPLOOP
LOOP5:
CJNEA,#10,LOOP6 ;數字鍵
LOOP6:
JCLOOP7
LJMPLOOP
LOOP7:
JNBPAUSE,LOOP8 ;暫停狀態可以輸入數字鍵
LJMPLOOP
LOOP8:
MOV33H,34H
MOV34H,35H
MOV35H,36H
MOV36H,37H
MOV37H,38H
MOV38H,KEYVAL
MOVA,34H
SWAPA
ORLA,33H
LCALLBCDH
MOVHOUR,A
MOVA,36H
SWAPA
ORLA,35H
LCALLBCDH
MOVMIN,A
MOVA,38H
SWAPA
ORLA,37H
LCALLBCDH
MOVSEC,A
LJMPLOOP
;------------------
;BCD轉換為十六進制
BCDH:
MOVB,#10H
DIVAB
MOVR7,B
MOVB,#10
MULAB
ADDA,R7
RET
;------------------
;十六進制轉換為BCD
HBCD:
MOVB,#10
DIVAB
SWAPA
ORLA,B
RET
;------------------
KEYSEL:
MOV KEYVAL,#0
MOV KEYSCAN,#0EFH
LCALL GETKEY
MOV A,KEYTM
JZ KEYS1
MOV KEYVAL,A
SJMP KEYRTN
KEYS1:
MOV KEYSCAN,#0DFH
LCALL GETKEY
MOV A,KEYTM
JZ KEYS2
CLR C
ADD A,#4
MOV KEYVAL,A
SJMP KEYRTN
KEYS2:
MOV KEYSCAN,#0BFH
LCALL GETKEY
MOV A,KEYTM
JZ KEYS3
CLR C
ADD A,#8
MOV KEYVAL,A
SJMP KEYRTN
KEYS3:
MOV KEYSCAN,#7FH
LCALL GETKEY
MOV A,KEYTM
JZ KEYRTN
CLR C
ADD A,#12
MOV KEYVAL,A
KEYRTN:
LCALLCHGKEY
RET
;--------------------
GETKEY:
MOVKEYTM,#0
MOV A,KEYSCAN
MOV P3,A
NOP
MOV A,P3
ANL A,#0FH
XRL A,#0FH
JZ NOKEY
MOVR2,#10
LCALLDELAY
MOV A,P3
ANL A,#0FH
XRL A,#0FH
JZ NOKEY
MOV A,P3
ANL A,#0FH
MOV R7,A
SF:
MOV A,P3
ANL A,#0FH
XRL A,#0FH
JNZ SF
MOV A,R7
CJNE A,#0EH,NK1
MOV KEYTM,#1
SJMP NOKEY
NK1:
CJNE A,#0DH,NK2
MOV KEYTM,#2
SJMP NOKEY
NK2:
CJNE A,#0BH,NK3
MOV KEYTM,#3
SJMP NOKEY
NK3:
CJNE A,#07H,NOKEY
MOV KEYTM,#4
NOKEY: RET
;--------------------
DELAY:
MOVR3,#50
DELAY1:
MOVR4,#100
DJNZR4,$
DJNZR3,DELAY1
DJNZR2,DELAY
RET
;--------------------
T0ISR:
PUSHACC
CLRTR0
MOVTH0,#3CH
MOVTL0,#0B0H
SETBTR0
INCCLK
MOVA,CLK
CJNEA,#20,T0ISRE
MOVCLK,#0
INCSEC
MOVA,SEC
CJNEA,#60,T0ISRE
MOVSEC,#0
INCMIN
MOVA,MIN
CJNEA,#60,T0ISRE
MOVMIN,#0
INCHOUR
MOVA,HOUR
CJNEA,#24,T0ISRE
MOVSEC,#0
MOVMIN,#0
MOVHOUR,#0
T0ISRE:
LCALLDISCHG
POPACC
RETI
;--------------------
DISCHG:
MOVA,HOUR
LCALLHBCD
PUSHACC
ANLA,#0FH
MOV34H,A
POPACC
ANLA,#0F0H
SWAPA
MOV33H,A
MOVA,MIN
LCALLHBCD
PUSHACC
ANLA,#0FH
MOV36H,A
POPACC
ANLA,#0F0H
SWAPA
MOV35H,A
MOVA,SEC
LCALLHBCD
PUSHACC
ANLA,#0FH
MOV38H,A
POPACC
ANLA,#0F0H
SWAPA
MOV37H,A
RET
;--------------------
T1ISR:
PUSHACC
CLRTR1
MOVTH1,#0ECH
MOVTL1,#78H
SETBTR1
MOVDPTR,#LEDTAB
T100:
MOVR0,#DAT
MOVA,SCANLED
ADDA,R0
MOVR0,A
MOVA,SCANLED
JNZT101
MOVP2,#01H
CLRDOT
SJMPT1DIS
T101:
DECA
JNZT102
MOVP2,#02H
SETBDOT
SJMPT1DIS
T102:
DECA
JNZT103
MOVP2,#04H
CLRDOT
SJMPT1DIS
T103:
DECA
JNZT104
MOVP2,#08H
SETBDOT
SJMPT1DIS
T104:
DECA
JNZT105
MOVP2,#10H
CLRDOT
SJMPT1DIS
T105:
MOVP2,#20H
CLRDOT
T1DIS:
MOVA,@R0
MOVCA,@A+DPTR
JNBDOT,T1DIS1
ORLA,#01H
T1DIS1:
CPLA
MOVP0,A
INCSCANLED
MOVA,SCANLED
CJNEA,#6,T1END
MOVSCANLED,#0
T1END:
POPACC
RETI
;--------------------
CHGKEY:
MOVA,KEYVAL
JZKV16
DECA
JNZKV01
MOVKEYVAL,#7
RET
KV01:
DECA
JNZKV02
MOVKEYVAL,#4
RET
KV02:
DECA
JNZKV03
MOVKEYVAL,#1
RET
KV03:
DECA
JNZKV04
MOVKEYVAL,#10
RET
KV04:
DECA
JNZKV05
MOVKEYVAL,#8
RET
KV05:
DECA
JNZKV06
MOVKEYVAL,#5
RET
KV06:
DECA
JNZKV07
MOVKEYVAL,#2
RET
KV07:
DECA
JNZKV08
MOVKEYVAL,#0
RET
KV08:
DECA
JNZKV09
MOVKEYVAL,#9
RET
KV09:
DECA
JNZKV10
MOVKEYVAL,#6
RET
KV10:
DECA
JNZKV11
MOVKEYVAL,#3
RET
KV11:
DECA
JNZKV12
MOVKEYVAL,#11
RET
KV12:
DECA
JNZKV13
MOVKEYVAL,#12
RET
KV13:
DECA
JNZKV14
MOVKEYVAL,#13
RET
KV14:
DECA
JNZKV15
MOVKEYVAL,#14
RET
KV15:
DECA
JNZKV16
MOVKEYVAL,#15
RET
KV16:
MOVKEYVAL,#0FFH
RET
;--------------------
LEDTAB: DB0FCH ;"0" 00H
DB60H ;"1" 01H
DB0DAH ;"2" 02H
DB0F2H ;"3" 03H
DB66H ;"4" 04H
DB0B6H ;"5" 05H
DB0BEH ;"6" 06H
DB0E0H ;"7" 07H
DB0FEH ;"8" 08H
DB0F6H ;"9" 09H
DB0EEH ;"A" 0AH
DB3EH ;"B" 0BH
DB9CH ;"C" 0CH
DB7AH ;"D" 0DH
DB9EH ;"E" 0EH
DB8EH ;"F" 0FH
DB00H ;"" 10H
;--------------------
⑵ 求單片機簡易數字鍾的課程設計 :要求自製一個單片機最小系統,包括串口下載、復位電路,採用內部定時器計
下面的程序配合這個電路運行就是對的
這個是完整的程序,電路圖如下
#include<reg52.h>
//定義共陽極字型碼0123456789-
unsignedcharcodedispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsignedchartime[]={0,0,0,0};//用來儲存分秒
unsignedchardate[]={0,0,0,0};//用來儲存日時
unsignedcharyear[]={0,0,0,0};//用來儲存年月
typedefstruct__SYSTEMTIME__
{
unsignedcharSecond;
unsignedcharMinute;
unsignedcharHour;
unsignedcharWeek;
unsignedcharDay;
unsignedcharMonth;
unsignedcharYear;
unsignedcharDateString[9];
unsignedcharTimeString[9];
}SYSTEMTIME; //定義的時間類型
SYSTEMTIMEtime1;
sbitDS1302_CLK=P1^6;//實時時鍾時鍾線引腳
sbitDS1302_IO=P1^7;//實時時鍾數據線引腳
sbitDS1302_RST=P1^5;//實時時鍾復位線引腳
sbitACC0=ACC^0;
sbitACC7=ACC^7;
sbitP10=P1^0;
sbitP11=P1^1;
sbitP12=P1^2;
sbitP13=P1^3;
sbitP14=P1^4;
//#defineAM(X) X
//#definePM(X) (X+12) //轉成24小時制
#defineDS1302_SECOND 0x80//秒寄存器
#defineDS1302_MINUTE 0x82//分寄存器
#defineDS1302_HOUR 0x84
#defineDS1302_WEEK 0x8A
#defineDS1302_DAY 0x86
#defineDS1302_MONTH 0x88
#defineDS1302_YEAR 0x8C
#defineDS1302_RAM(X) (0xC0+(X)*2) //用於計算DS1302_RAM地址的宏
voidDS1302InputByte(unsignedchard) //實時時鍾寫入一位元組(內部函數)
{
unsignedchari;
ACC=d;
for(i=8;i>0;i--)
{
DS1302_IO=ACC0; //相當於匯編中的RRC
DS1302_CLK=1;
DS1302_CLK=0;//發一個高跳變到低的脈沖
ACC=ACC>>1;
}
}
unsignedcharDS1302OutputByte(void) //實時時鍾讀取一位元組(內部函數)
{
unsignedchari;
for(i=8;i>0;i--)
{
ACC=ACC>>1; //相當於匯編中的RRC
ACC7=DS1302_IO;
DS1302_CLK=1;
DS1302_CLK=0;//發一個高跳變到低的脈沖
}
return(ACC);
}
voidWrite1302(unsignedcharucAddr,unsignedcharucDa) //ucAddr:DS1302地址,ucData:要寫的數據
{
DS1302_RST=0;
DS1302_CLK=0;
DS1302_RST=1;
DS1302InputByte(ucAddr); //地址,命令
DS1302InputByte(ucDa); //寫1Byte數據
DS1302_CLK=1;
DS1302_RST=0;//RST0->1->0,CLK0->1
}
unsignedcharRead1302(unsignedcharucAddr) //讀取DS1302某地址的數據
{
unsignedcharucData;
DS1302_RST=0;
DS1302_CLK=0;
DS1302_RST=1;//enable
DS1302InputByte(ucAddr|0x01);//地址,命令
ucData=DS1302OutputByte();//讀1Byte數據
DS1302_CLK=1;//RST0->1->0,CLK0->1
DS1302_RST=0;
return(ucData);
}
voidDS1302_SetProtect(bitflag)//是否防寫
{
if(flag)
Write1302(0x8E,0x10);//WP=1,不能寫入
else
Write1302(0x8E,0x00);//WP=0,可以寫入
}
voidDS1302_SetTime(unsignedcharAddress,unsignedcharValue)//設置時間函數
{
DS1302_SetProtect(0);
Write1302(Address,((Value/10)<<4|(Value%10)));//高4位為十位,低4位為個位
}
voidDS1302_GetTime(SYSTEMTIME*Time)
{
unsignedcharReadValue;
ReadValue=Read1302(DS1302_SECOND);
Time->Second=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);//轉換成10進制的秒
ReadValue=Read1302(DS1302_MINUTE);
Time->Minute=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
ReadValue=Read1302(DS1302_HOUR);
Time->Hour=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
ReadValue=Read1302(DS1302_DAY);
Time->Day=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
ReadValue=Read1302(DS1302_WEEK);
Time->Week=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
ReadValue=Read1302(DS1302_MONTH);
Time->Month=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
ReadValue=Read1302(DS1302_YEAR);
Time->Year=((ReadValue&0xf0)>>4)*10+(ReadValue&0x0F);
}
voidInitial_DS1302(void)
{
unsignedcharSecond=Read1302(DS1302_SECOND);
if(Second&0x80)//如果第七為1(表明沒有啟動),則啟動時鍾
DS1302_SetTime(DS1302_SECOND,0);
}
voiddelay(unsignedchari)//延時子程序
{
unsignedcharj;
while((i--)!=0)
{
for(j=625;j>0;j--);
}
}
/*unsignedcharbutton_time(n,x,y)//時鍾調整子程序
unsignedcharn,x,y;
{
if(P1^7==0)
{
delay(50);
if(P1^7==0)
{
n++;
if(n==x)
n=0;
while(P1^7==0);
}
}
if(P1^1==0)
{
delay(50);
if(P1^1==0)
{
if(n==0)
n=y;
else
n--;
while(P1^1==0);
}
}
returnn;
}
*/
/*unsignedcharbutton_date(n,x,y)//日期調整子程序
unsignedcharn,x,y;
{
if(P1^7==0)
{
delay(50);
if(P1^7==0)
{
n++;
if(n==x)
n=1;
while(P1^7==0);
}
}
if(P1^1==0)
{
delay(50);
if(P1^1==0)
{
if(n==1)
n=y;
else
n--;
while(P1^1==0);
}
}
returnn;
}*/
voiddisplay1(minute10,minute1,second10,second1) //顯示第一頁分秒子程序
//unsignedcharsecond10,second1,minute10,minute1;
{
P2=0x08;
P0=dispcode[second1];//顯示秒的個位
delay(1);
P2=0x04;
P0=dispcode[second10]; //顯示秒的十位
delay(1);
P2=0x02;
P0=dispcode[minute1]; //顯示分的個位
delay(1);
P2=0x01;
P0=dispcode[minute10];//顯示分的十位
delay(1);
}
voiddisplay2(data10,data1,hour10,hour1) //顯示第二頁天時子程序
//unsignedchardata10,data1,hour10,hour1;
{
P2=0xf8;
P0=dispcode[data1];//顯示天的個位
delay(1);
P2=0xf4;
P0=dispcode[data10]; //顯示天的十位
delay(1);
P2=0xf2;
P0=dispcode[hour1]; //顯示時的個位
delay(1);
P2=0xf1;
P0=dispcode[hour10];//顯示時的十位
delay(1);
}
voiddisplay3(year10,year1,month10,month1) //顯示第三頁年月子程序
//unsignedcharyear10,year1,month10,month1;
{
P2=0xf2;
P0=dispcode[month1];//顯示月的個位
delay(1);
P2=0xf1;
P0=dispcode[month10]; //顯示月的十位
delay(1);
P2=0xf8;
P0=dispcode[year1]; //顯示月的個位
delay(1);
P2=0xf4;
P0=dispcode[year10];//顯示月的十位
delay(1);
}
voidmain()
{
unsignedcharflag=0;
Initial_DS1302(); //初始化DS1302這個時鍾晶元,
P10=0;//點亮測試燈
while(1)
{
DS1302_GetTime(&time1); //讀取時間參數
time[3]=(time1.Second)%10; //把秒的個位數據存入time[3]
time[2]=(time1.Second)/10; //把秒的十位數據存入time[2]
time[1]=(time1.Minute)%10; //把分的個位數據存入time[1]
time[0]=(time1.Minute)/10; //把分的十位數據存入time[0]
date[3]=(time1.Day)%10;
date[2]=(time1.Day)/10;
date[1]=(time1.Hour)%10;
date[0]=(time1.Hour)/10;
year[3]=(time1.Year)%10;
year[2]=(time1.Year)/10;
year[1]=(time1.Month)%10;
year[0]=(time1.Month)/10;
//display1(time[0],time[1],time[2],time[3]);
if(P11==0)
{
delay(50);
if(P11==0)
{
flag++;
if(flag>2)//flag:1顯示第二頁日時;2顯示第三頁年月0:顯示第一頁分秒
{
flag=0;
}
}
while(P11==0);
}
/*if(P1^6==0) //如果按下TimeSet鍵一下,開始顯示日期,再按一下進入日期跟時鍾的調節模式
{
delay(50);
if(P1^6==0)
{
flag++;
if(flag>6)
{
flag=0;
}
}
while(P1^6==0);
}*/
switch(flag)
{
case0:display1(time[0],time[1],time[2],time[3]); //調用子函數display,把存入數組time的數據給顯示出來
break;
case1:display2(date[0],date[1],date[2],date[3]); //調用子函數display,把存入數組date的數據給顯示出來
break;
case2:display3(year[0],year[1],year[2],year[3]);
break;
/* case3:time1.Month=button_date(time1.Month,13,12); //調整月
DS1302_SetTime(0x88,time1.Month);
display(10,10,date[2],date[3]);
break;
case4:time1.Day=button_date(time1.Day,32,31); //調整日
DS1302_SetTime(0x86,time1.Day);
display(10,10,date[4],date[5]);
break;
case5:time1.Minute=button_time(time1.Minute,60,59); //調整分
DS1302_SetTime(0x82,time1.Minute);
display(time[2],time[3],10,10);
break;
case6:time1.Second=button_time(time1.Second,60,59); //調整秒
DS1302_SetTime(0x80,time1.Second);
display(10,10,time[4],time[5]);
break;*/
}
}
}
⑶ 單片機課程設計(數字時鍾) 麻煩大家不要發鏈接 直接幫我把程序寫過來。
哈哈有個1602顯示的不過程序太長貼不上給你個數碼管的吧不行再聯系
1302.c
#include<DS1302.h>
#include<key.h>
ucharbit_ser[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
ucharseven_seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
/***********************時間顯示*****************/
voidtimer0_init(void) //T0初始化函數,用於時間的動態顯示
{
TMOD=0x21;
TL0=(65536-5000)%256;
TH0=(65536-5000)/256;
EA=1;
ET0=1;
TR0=1;
}
voidtimer0_isr(void)interrupt1 //T0中斷處理函數
{
charflag; //flag用於表示調整時閃爍的亮或滅
TR0=0;
TL0=(65536-5000)%256;
TH0=(65536-5000)/256;
TR0=1;
flag=x/100*0xff; //設置閃爍標志,如果x大於100則flag為0xff,小於100則為0x00
x++;
if(x>200)
x=0;
switch(i)
{
case0:
P2=bit_ser[0];
if(setflag==3) //根據setflag的值判斷當前位是否需要閃爍
P0=flag|seven_seg[dis_buffer[0]];
else
P0=seven_seg[dis_buffer[0]];
break;
case1:
P2=bit_ser[1];
if(setflag==3)
P0=flag|seven_seg[dis_buffer[1]];
else
P0=seven_seg[dis_buffer[1]];
break;
case2:
P2=bit_ser[2];
if(setflag==2)
P0=flag|seven_seg[dis_buffer[2]];
else
P0=seven_seg[dis_buffer[2]];
break;
case3:
P2=bit_ser[3];
if(setflag==2)
P0=flag|seven_seg[dis_buffer[3]];
else
P0=seven_seg[dis_buffer[3]];
break;
case4:
P2=bit_ser[4];
if(setflag==1)
P0=flag|seven_seg[dis_buffer[4]];
else
P0=seven_seg[dis_buffer[4]];
break;
case5:
P2=bit_ser[5];
if(setflag==1)
P0=flag|seven_seg[dis_buffer[5]];
else
P0=seven_seg[dis_buffer[5]];
break;
}
i++;
if(i>=6)
{
i=0;
if(j==10)
{
j=0;
if(setflag==0)
DS1302_GetTime(&Time); //如果setflag是0,就從1302中讀出時間,因為setflag不是0時,說明處於調整狀態,不需要讀時間
dis_buffer[5]=Time.Second%10; //把當前時間放入顯示緩沖區
dis_buffer[4]=Time.Second/10;
dis_buffer[3]=Time.Minute%10;
dis_buffer[2]=Time.Minute/10;
dis_buffer[1]=Time.Hour%10;
dis_buffer[0]=Time.Hour/10;
}
j++;
}
}
voidmain()
{
Initial_DS1302(Time);
timer0_init();
while(1)
{
set_down();
timer_down();
up_down();
down_down();
beepflag_down();
if(setflag==0&&Time.Hour==romhour&&Time.Minute==romminute&&Beepflag==1) //判斷蜂鳴器是否要響
Beep=!Beep;
}
}
//key.c
#include<reg51.h>
#defineucharunsignedchar
#defineuintunsignedint
uchari=0,j=0,x=0,setflag,flag_set,flag_timer;//setflag用來表示調整的位置,flag_set和flag_timer分別表示當前處於調整狀態還是定時狀態
SYSTEMTIMETime={0,20,15,3,30,6,10}; //系統時間的初始值2010年6月30日星期三,15時20分0秒
chardis_buffer[6]; //存放顯示數據的緩沖區
sbitBeep_flag=P3^2; //蜂鳴器的介面
sbitkey_timer=P3^4; //定時按鈕
sbitkey_set=P3^5; //調整按鈕
sbitkey_up=P3^6; //增加按鈕
sbitkey_down=P3^7; //減小按鈕
charromhour,romminute,romsec; //分別存放定時的時,分,秒
bitBeepflag; //標記鬧鍾是否開啟
//延時函數
voiddelays(ucharx)
{
while(x)x--;
}
//設置鍵的處理函數
voidset()
{
setflag++;
flag_set=1;
if(setflag>=4)
{
setflag=0;
flag_set=0;
Initial_DS1302(Time);
}
}
//定時間的處理函數
voidtimer()
{
setflag++;
flag_timer=1;
if(setflag==1)
{
Time.Hour=romhour;
Time.Minute=romminute;
Time.Second=romsec;
}
elseif(setflag>=4)
{
setflag=0;
flag_timer=0;
romhour=Time.Hour;
romminute=Time.Minute;
romsec=Time.Second;
}
}
//增加鍵的處理函數
voip()
{
switch(setflag)
{
case0:
break;
case1:
Time.Second++;
if(Time.Second>=60)
Time.Second=0;
break;
case2:
Time.Minute++;
if(Time.Minute>=60)
Time.Minute=0;
break;
case3:
Time.Hour++;
if(Time.Hour>=24)
Time.Hour=0;
break;
}
}
//減小鍵的處理函數
voiddown()
{
switch(setflag)
{
case0:
break;
case1:
Time.Second--;
if(Time.Second<0)
Time.Second=59;
break;
case2:
Time.Minute--;
if(Time.Minute<0)
Time.Minute=59;
break;
case3:
Time.Hour--;
if(Time.Hour<0)
Time.Hour=23;
break;
}
}
//設置鍵的掃描函數
voidset_down()
{
if(key_set==0&&flag_timer==0)
{
delays(100);
if(key_set==0)
{
set();
}
while(!key_set);
}
}
//定時鍵的掃描函數
voidtimer_down()
{
if(key_timer==0&&flag_set==0)
{
delays(100);
if(key_timer==0)
{
timer();
}
while(!key_timer);
}
}
//增加鍵的掃描函數
voip_down()
{
if(key_up==0&&setflag!=0)
{
delays(100);
if(key_up==0)
{
up();
while(!key_up);
}
}
}
//減少鍵的處理函數
voiddown_down()
{
if(key_down==0&&setflag!=0)
{
delays(100);
if(key_down==0)
{
down();
while(!key_down);
}
}
}
//定時開關的掃描處理函數
voidbeepflag_down()
{
if(Beep_flag==0)
{
delays(100);
{
Beepflag=!Beepflag;
while(!Beep_flag);
}
}
}
//ds1302.h
#ifndef_REAL_TIMER_DS1302
#define_REAL_TIMER_DS1302
#include<REG51.h>
sbitDS1302_CLK=P1^1;//實時時鍾時鍾線引腳
sbitDS1302_IO=P1^2;//實時時鍾數據線引腳
sbitDS1302_RST=P1^3;//實時時鍾復位線引腳
sbitACC0=ACC^0;
sbitACC7=ACC^7;
sbitBeep=P2^7;
typedefstruct__SYSTEMTIME__
{ charSecond;
charMinute;
charHour;
charWeek;
charDay;
charMonth;
charYear;
}SYSTEMTIME; //定義的時間類型
#defineAM(X) X
#definePM(X) (X+12) //轉成24小時制
#defineDS1302_SECOND 0x80//秒寄存器
#defineDS1302_MINUTE 0x82//分寄存器
#defineDS1302_HOUR 0x84
#defineDS1302_WEEK 0x8A
#defineDS1302_DAY 0x86
#defineDS1302_MONTH 0x88
#defineDS1302_YEAR 0x8C
#defineDS1302_RAM(X) (0xC0+(X)*2) //用於計算DS1302_RAM地址的宏
voidDS1302InputByte(unsignedchard) //實時時鍾寫入一位元組(內部函數)
{unsignedchari;
ACC=d;
for(i=8;i>0;i--)
{ DS1302_IO=ACC0; //相當於匯編中的RRC
DS1302_CLK=1;
DS1302_CLK=0;//發一個高跳變到低的脈沖
ACC=ACC>>1;
}
}
unsignedcharDS1302OutputByte(void) //實時時鍾讀取一位元組(內部函數)
{ unsignedchari;
for(i=8;i>0;i--)
{ ACC=ACC>>1; //相當於匯編中的RRC
ACC7=DS1302_IO;
DS1302_CLK=1;
DS1302_CLK=0;//發一個高跳變到低的脈沖
}
return(ACC);
}
voidWrite1302(unsignedcharucAddr,unsignedcharucDa)//ucAddr:DS1302地址,ucData:要寫的數據
{ DS1302_RST=0;
DS1302_CLK=0;
DS1302_RST=1;
DS1302InputByte(ucAddr); //地址,命令
DS1302InputByte(ucDa); //寫1Byte數據
DS1302_CLK=1;
DS1302_RST=0;//RST0->1->0,CLK0->1
}
unsignedcharRead1302(unsignedcharucAddr) //讀取DS1302某地址的數據
{ unsignedcharucData;
DS1302_RST=0;
DS1302_CLK=0;
DS1302_RST=1;//enable
DS1302InputByte(ucAddr|0x01);//地址,命令
ucData=DS1302OutputByte();//讀1Byte數據
DS1302_CLK=1;//RST0->1->0,CLK0->1
DS1302_RST=0;
return(ucData);
}
voidDS1302_SetProtect(bitflag)//是否防寫
{ if(flag)
Write1302(0x8E,0x80);//WP=1,不能寫入
else
Write1302(0x8E,0x00);//WP=0,可以寫入
}
voidDS1302_SetTime(unsignedcharAddress,unsignedcharValue)//設置時間函數
{ DS1302_SetProtect(0);
Write1302(Address,((Value/10)<<4|(Value%10)));//高4位為十位,低4位為個位
DS1302_SetProtect(1);
}
//獲取時間函數,從DS1302內讀取時間然後存入Time內
voidDS1302_GetTime(SYSTEMTIME*Time)
{ unsignedcharReadValue;
ReadValue=Read1302(DS1302_SECOND);
Time->Second=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);//轉換成10進制的秒
ReadValue=Read1302(DS1302_MINUTE);
Time->Minute=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
ReadValue=Read1302(DS1302_HOUR);
Time->Hour=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
ReadValue=Read1302(DS1302_DAY);
Time->Day=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
ReadValue=Read1302(DS1302_WEEK);
Time->Week=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
ReadValue=Read1302(DS1302_MONTH);
Time->Month=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
ReadValue=Read1302(DS1302_YEAR);
Time->Year=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
}
//利用STime初始化DS1302
voidInitial_DS1302(SYSTEMTIMESTime)
{ unsignedcharSecond=Read1302(DS1302_SECOND);
if(Second&0x80) DS1302_SetTime(DS1302_SECOND,0);//如果第七為1(表明沒有啟動),則啟動時鍾
DS1302_SetTime(DS1302_SECOND,STime.Second); //設定起始時間
DS1302_SetTime(DS1302_MINUTE,STime.Minute);
DS1302_SetTime(DS1302_HOUR,STime.Hour);
DS1302_SetTime(DS1302_DAY,STime.Day);
DS1302_SetTime(DS1302_MONTH,STime.Month);
DS1302_SetTime(DS1302_YEAR,STime.Year);
DS1302_SetTime(DS1302_WEEK,STime.Week);
}
#endif