電子時鐘.png (143.35 KB, 下載次數(shù): 29)
下載附件
2022-5-3 16:43 上傳
51hei.png (30.02 KB, 下載次數(shù): 30)
下載附件
2022-5-4 02:06 上傳
單片機源程序如下:
#include<reg51.h>
#include "LCD1602.h"
#include "DS1302.h"
#define uchar unsigned char
#define uint unsigned int
sbit speaker=P2^4;
bit key_flag1=0,key_flag2=0;
SYSTEMTIME adjusted;//此處為結構體定義
uchar sec_add=0,min_add=0,hou_add=0,day_add=0,mon_add=0,yea_add=0;
uchar data_alarm[7]={0};
/*********************************鍵盤控制****************************************************/
int key_scan() //掃描是否有鍵按下
{
int i=0;
uint temp;
P1=0xf0;
temp=P1;
if(temp!=0xf0)
{
i=1;
}
else
{
i=0;
}
return i;
}
uchar key_value() //確定按鍵的值
{
uint m=0,n=0,temp;
uchar value;
uchar v[4][3]={'2','1','0','5','4','3','8','7','6','b','a','9'} ;
P1=0xfe;temp=P1; if(temp!=0xfe)m=0;//采用分行、分列掃描的形式獲取按鍵信息
P1=0xfd;temp=P1; if(temp!=0xfd)m=1;
P1=0xfb;temp=P1; if(temp!=0xfb)m=2;
P1=0xf7;temp=P1; if(temp!=0xf7)m=3;
P1=0xef;temp=P1; if(temp!=0xef)n=0;
P1=0xdf;temp=P1; if(temp!=0xdf)n=1;
P1=0xbf;temp=P1; if(temp!=0xbf)n=2;
value=v[m][n];
return value;
}
/************************************修改時間操作****************************************/
void adjust(void)
{
if(key_scan()&&key_flag1)
switch(key_value())
{
case '0':sec_add++;break;
case '1':min_add++;break;
case '2':hou_add++;break;
case '3':day_add++;break;
case '4':mon_add++;break;
case '5':yea_add++;break;
default: break;
}
adjusted.Second+=sec_add;
adjusted.Minute+=min_add;
adjusted.Hour+=hou_add;
adjusted.Day+=day_add;
adjusted.Month+=mon_add;
adjusted.Year+=yea_add;
if(adjusted.Second>59)
{
adjusted.Second=adjusted.Second%60;
adjusted.Minute++;
}
if(adjusted.Minute>59)
{
adjusted.Minute=adjusted.Minute%60;
adjusted.Hour++;
}
if(adjusted.Hour>23)
{
adjusted.Hour=adjusted.Hour%24;
adjusted.Day++;
}
if(adjusted.Day>31)
adjusted.Day=adjusted.Day%31;
if(adjusted.Month>12)
adjusted.Month=adjusted.Month%12;
if(adjusted.Year>100)
adjusted.Year=adjusted.Year%100;
}
/************************************************中斷處理函數(shù)*********************************/
void changing(void) interrupt 0 using 0 //需要修改時間和日期,或者停止修改
{
if(key_flag1)
key_flag1=0;
else
key_flag1=1;
}/*********************************主函數(shù)*************************************************/
main()
{
uint i;
uchar p1[]="D:",p2[]="T:";
SYSTEMTIME T;
EA=1;
EX0=1;
IT0=1;
EA=1;
EX1=1;
IT1=1;
init1602();
Initial_DS1302() ;
while(1)
{
write_com(0x80);
write_string(p1,2);
write_com(0xc0);
write_string(p2,2);
DS1302_GetTime(&T) ;
adjusted.Second=T.Second;
adjusted.Minute=T.Minute;
adjusted.Hour=T.Hour;
adjusted.Week=T.Week;
adjusted.Day=T.Day;
adjusted.Month=T.Month;
adjusted.Year=T.Year;
for(i=0;i<9;i++)
{
adjusted.DateString[ i]=T.DateString[ i];
adjusted.TimeString[ i]=T.TimeString[ i];
}
adjust();
DateToStr(&adjusted);
TimeToStr(&adjusted);
write_com(0x82);
write_string(adjusted.DateString,8);
write_com(0xc2);
write_string(adjusted.TimeString,8);
delay(10);
}
}
Keil代碼與Proteus8.8仿真下載:
仿真程序.7z
(49.57 KB, 下載次數(shù): 34)
2022-5-4 02:07 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|