導航:首頁 > 編程語言 > 萬年歷的程序單片機

萬年歷的程序單片機

發布時間:2023-02-11 10:43:54

① 單片機和lcd1602編寫萬年歷C語言程序,求高手啊

下面是我的程序,還有模擬圖,不懂可以再問我,祝你成功

/*******************************************

ds1302計時+lcd1602顯示

*******************************************/

#include<reg51.h>

#include<intrins.h>

#defineucharunsignedchar

#defineuintunsignedint

sbitrs=P2^0;

sbitrw=P2^1;

sbite=P2^2;

sbitT_RST=P1^5;

sbitT_CLK=P1^6;

sbitT_IO=P1^7;

sbitACC0=ACC^0;

sbitACC7=ACC^7;

/*******************************************

向1302寫一個位元組

*******************************************/

voidinput_BYTE(uchardat)

{

uchari;

ACC=dat;

for(i=8;i>0;i--)

{

T_IO=ACC0;

T_CLK=1;

T_CLK=0;

ACC=(ACC>>1);

}

}

/*******************************************

1302讀出一個位元組

*******************************************/

ucharoutput_BYTE()

{

uchari;

for(i=8;i>0;i--)

{

ACC=(ACC>>1);

ACC7=T_IO;

T_CLK=1;

T_CLK=0;

}

return(ACC);

}

/*******************************************

寫數據

*******************************************/

voidwrite_1302(ucharadd,uchardat)

{

T_RST=0;

T_CLK=0;

T_RST=1;

input_BYTE(add);

input_BYTE(dat);

T_CLK=1;

T_RST=0;

}

/*******************************************

讀數據

*******************************************/

ucharread_1302(ucharadd)

{

ucharinf;

T_RST=0;

T_CLK=0;

T_RST=1;

input_BYTE(add);

inf=output_BYTE();

T_CLK=1;

T_RST=0;

return(inf);

}

voidinit_1302()

{

write_1302(0x8e,0x00);//關閉防寫;

/*write_1302(0x90,0xaa);//設置充電方式;

write_1302(0x80,0x00);//秒寄存器初始化;

write_1302(0x82,0x46);//分.......

write_1302(0x84,0x22);//時.......

write_1302(0x86,0x17);//日........

write_1302(0x88,0x03);//月.......

write_1302(0x8a,0x04);//星期...

write_1302(0x8c,0x11);//年......*/

write_1302(0x8e,0x80);//打開防寫;

}

ucharcom,inf;

ucharcodetable[]="DS1302";

ucharcodetable1[]="0123456789";

ucharcodetable2[]="WEEK-";

voiddelay1(uchart)

{

ucharx,y;

for(x=t;x>0;x--)

for(y=110;y>0;y--);

}

/*******************************************

給1602寫指令

*******************************************/

voidwrite_com(ucharcom)

{

rs=0;

P0=com;

delay1(5);

e=1;

delay1(5);

e=0;

}

/*******************************************

給1602寫數據

*******************************************/

voidwrite_date(uchardate)

{

rs=1;

P0=date;

delay1(5);

e=1;

delay1(5);

e=0;

}

/*******************************************

1602初始化

*******************************************/

voinit()

{

e=0;

rw=0;

write_com(0x38);

write_com(0x0c);

write_com(0x06);

write_com(0x01);

}

/*******************************************

主函數

*******************************************/

voidmain()

{

uchari;

uchart_sec,sec1,sec2;

uchart_min,min1,min2;

uchart_hour,hour1,hour2;

uchart_mon,mon1,mon2;

uchart_day,day1,day2;

uchart_year,year1,year2;

ucharweek,week1;

ucharnum;

unit();

init_1302();

while(1)

{

t_sec=read_1302(0x81);//miao;

sec1=t_sec&0x0f;

sec2=(t_sec>>4);

t_min=read_1302(0x83);//fen;

min1=t_min&0x0f;

min2=(t_min>>4);

t_hour=read_1302(0x85);//shi;

hour1=t_hour&0x0f;

hour2=(t_hour>>4);

t_day=read_1302(0x87);//ri;

day1=t_day&0x0f;

day2=(t_day>>4);

t_mon=read_1302(0x89);//yue;

mon1=t_mon&0x0f;

mon2=(t_mon>>4);

week=read_1302(0x8b);//xingqi;

week1=week&0x0f;

t_year=read_1302(0x8d);//nian;

year1=t_year&0x0f;

year2=(t_year>>4);

write_com(0x80);

for(i=0;i<6;i++)

{

write_date(table[i]);

}

write_com(0x80+0x40);

for(i=0;i<5;i++)

{

write_date(table2[i]);

}

write_com(0x80+0x07);

write_date(table1[year2]);

write_com(0x80+0x08);

write_date(table1[year1]);

write_com(0x80+0x09);

write_date('/');

write_com(0x80+0x0a);

write_date(table1[mon2]);

write_com(0x80+0x0b);

write_date(table1[mon1]);

write_com(0x80+0x0c);

write_date('/');

write_com(0x80+0x0d);

write_date(table1[day2]);

write_com(0x80+0x0e);

write_date(table1[day1]);

write_com(0x80+0x40+0x05);

write_date(table1[week1]);

write_com(0x80+0x40+0x07);

write_date(table1[hour2]);

write_com(0x80+0x40+0x08);

write_date(table1[hour1]);

write_com(0x80+0x40+0x09);

write_date(':');

write_com(0x80+0x40+0x0a);

write_date(table1[min2]);

write_com(0x80+0x40+0x0b);

write_date(table1[min1]);

write_com(0x80+0x40+0x0c);

write_date(':');

write_com(0x80+0x40+0x0d);

write_date(table1[sec2]);

write_com(0x80+0x40+0x0e);

write_date(table1[sec1]);

}

}

② 單片機萬年歷的c語言程序

沒分啊 那也給你發一個吧
DS1302+DS18B20 用1602顯示
我在PROTUES模擬上做的 你可以根據程序畫電路圖 很簡單
程序 採用模塊化方式
/*初始化*/
#define uint unsigned int
#define uchar unsigned char
uchar code tab[]=" 20 - - " ;
uchar code table[]=" : : ";
uchar code ttable[]={"Now temperature:"} ;
sbit RS=P3^0;
sbit E=P3^1;
sbit button1=P1^0;
sbit button2=P1^3;
sbit button3=P1^6;
sbit RST=P2^5;
sbit SCLK=P2^6;
sbit IO=P2^7;
sbit ACC7=ACC^7;
sbit DQ=P3^6;
char hour,minute,second,n,k,week,day,month,year1=20 ,year,nn,flag;
uchar key=0;
uint tem,a;
void delay(uint x)
{
while(x--);
}
void write_zl(uchar zl)
{E=0;
RS=0;
P0=zl;
E=1;
delay(500);
E=0;
}
void write_sj(uchar sj)
{E=0;
RS=1;
P0=sj;
E=1;
delay(500);
E=0;
}
void init_lcd()
{
write_zl(0x38);//開顯示
write_zl(0x01);//清屏
write_zl(0x06);//整屏不移動
write_zl(0x0C);//不開游標
}
/*18B20*/部分
void init_18b20()
{ DQ=1;
delay(8); //延時
DQ=0;
delay(80);
DQ=1;
delay(20);
}
void write_dat(uchar dat)
{ uchar i,k;
k=dat;
for(i=0;i<8;i++)
{ DQ=0;
DQ=k&0x01;//從低位開始
delay(5);
DQ=1;
k>>=1;
}
delay(4);
}

int read_sj()
{
uchar j,date;
for(j=0;j<8;j++)
{
DQ=1;
delay(1);
DQ=0;
date=date>>1;
DQ=1;
if(DQ==1) //
date|=0x80;
delay(4);}
return(date);
}
void temperaturechange()
{ uint tempH,tempL;
init_18b20();
write_dat(0xcc);//只對一個操作就跳過讀序列號操作
write_dat(0x44); //啟動溫度轉換
delay(60000);
delay(33750);//需要93.75ms
init_18b20();
write_dat(0xcc);
write_dat(0xbe);//讀溫度寄存器

tempL=read_sj();//讀取溫度低位LSB
tempH=read_sj();//讀取溫度高位MSB
if(tempH&0x80) //高5位是1為負數
{ a=1;
tem=tempH;
tem<<=8; //共16位
tem=tem|tempL; //合並高低位數
tem=~tem+1;
tem=tem*(0.0625);
}
else
{
a=0;
tem=((tempH*256)+tempL)*0.0625;//轉換實際溫度
}
}
void write_temperature(uint temp) //被調用的函數必須在前
{ uint ,shi,ge;
=temp/100;
shi=temp%100/10;
ge=temp%10;
if(a==1)
{ if(shi>0)
{write_zl(0x80+0x50+3);
write_sj('-');
write_zl(0x80+0x50+4);
write_sj(0x30+shi); }
else
{
write_zl(0x80+0x50+3);
write_sj(0x00);
write_zl(0x80+0x50+4);
write_sj('-');}
}

else
{
if(>0) //若顯示shi肯定顯示
{
write_zl(0x80+0x50+3);
write_sj(0x30+);
write_zl(0x80+0x50+4);
write_sj(0x30+shi);
}
else
{
if(shi>0) //否則判斷shi是否顯示
{
write_zl(0x80+0x50+4);
write_sj(0x30+shi);
}
else
{
write_zl(0x80+0x50+4);
write_sj(0x00);
}
write_zl(0x80+0x50+3);
write_sj(0x00);
}
}

write_zl(0x80+0x50+5); //個位公用總的顯示
write_sj(0x30+ge);
}

/*DS1302部分*/
void DS1302_write(uchar shju)
{ uchar s,j;
s=shju;
for(j=0;j<8;j++)
{ IO=s&0x01;
SCLK=0;
SCLK=1;

s=s>>1;
}}
uchar DS1302_readbyte()
{ uchar i,R=0;
SCLK=0;
delay(10);
for(i=8;i>0;i--)
{
ACC=ACC>>1;
ACC7=IO;
SCLK=1;
SCLK=0;
}
return(ACC);
/*{
R=R>>1;
if(IO==1)
R=R|0x80;
SCLK=1;
SCLK=0;
}
return(R); */}

void DS1302write(uchar dz,uchar shij)//初始化1302
{ RST=1;
SCLK=0;
DS1302_write(dz);
DS1302_write(shij);
SCLK=1;
RST=0;
}
void start1302()
{ uchar num;
for(num=0;num<13;num++)
{ write_sj(tab[num]);}
for(num=13;num<16;num++)
{ write_sj(tab[num]);}
write_zl(0x80+0x40);
for(num=0;num<12;num++)
{ write_sj(table[num]);
}}
void write_hms(uchar add,uchar sj) //被調用的函數必須在前
{ uchar shi,ge;
shi=sj/10;
ge=sj%10;
write_zl(0x80+0x40+add);
write_sj(0x30+shi);
write_sj(0x30+ge); }
void write_nyr(uchar add,uchar sj)
{ uchar shi,ge;
shi=(sj/10);
ge=(sj%10);
write_zl(0x80+add);
write_sj(0x30+shi);
write_sj(0x30+ge); }
void write_XQ(uchar wk)
{ write_zl(0x80+13);
switch(wk)
{ case 2: write_sj('M'); write_sj('O');write_sj('N'); break;
case 3: write_sj('T'); write_sj('U');write_sj('E'); break;
case 4: write_sj('W'); write_sj('E');write_sj('D'); break;
case 5: write_sj('T'); write_sj('H');write_sj('U'); break;
case 6: write_sj('F'); write_sj('R');write_sj('I'); break;
case 7: write_sj('S'); write_sj('A');write_sj('T'); break;
case 1: write_sj('S'); write_sj('U');write_sj('N'); break;
}
}
void write_day(uchar rue)
{
switch(rue)
{
case 1: nn=31;break;
case 2: if((year%4==0&&year%100!=0)||year%400==0)
nn=29;
else
nn=28;break;
case 3: nn=31;break;
case 4: nn=30;break;
case 5: nn=31;break;
case 6: nn=30;break;
case 7: nn=31;break;
case 8: nn=31;break;
case 9: nn=30;break;
case 10: nn=31;break;
case 11: nn=30;break;
case 12: nn=31;break;
}}
void keyboard()
{
if(button1==0)
{ delay(5);
if(button1==0)
{ while(button1==0);
key++; flag=1;
switch(key)
{ case 1: write_zl(0x80+0x40+10);
write_zl(0x0f); break;//shan shuo
case 2: write_zl(0x80+0x40+7); break;
case 3: write_zl(0x80+0x40+4); break;
case 4: write_zl(0x80+10); break;
case 5: write_zl(0x80+7); break;
case 6: write_zl(0x80+4); break;
case 7: write_zl(0x80+2); break;
case 8: key=0;
write_zl(0x0c);
flag=0;
DS1302write(0x80,second);
DS1302write(0x82,minute);
DS1302write(0x84,hour);
DS1302write(0x86,day);
DS1302write(0x88,month);
DS1302write(0x8A,week);
DS1302write(0x8C,year);break;
}
}}
if(button2==0)
{ delay(10);
if(button2==0)
{ while(button2==0);
switch(key)
{case 1: second++;if(second==60)second=0;
write_hms(10,second); write_zl(0x80+0x40+10); break;
case 2: minute++;if(minute==60)minute=0;
write_hms(7,minute);write_zl(0x80+0x40+7); break;
case 3: hour++;if(hour==24)hour=0;
write_hms(4,hour); write_zl(0x80+0x40+4); break;
case 4: day++;week++;
if(week==8)week=1;write_XQ(week);
write_day(month); if(day==nn+1)day=1;
write_nyr(10,day);write_zl(0x80+10); break;
case 5: month++;if(month==13)month=1;
write_nyr(7,month);write_zl(0x80+7); break;
case 6: year++ ;if(year==100)year=0;
write_nyr(4,year);write_zl(0x80+4); break;
case 7: year1++ ;if(year1==100)year1=0;
write_nyr(2,year1);write_zl(0x80+2); break;
}}}
if(button3==0)
{delay(10);
if(button3==0)
{ while(button3==0);
switch(key)
{case 1: second--;if(second<0)second=59;
write_hms(10,second); write_zl(0x80+0x40+10); break;
case 2: minute--;if(minute<0)minute=59;
write_hms(7,minute);write_zl(0x80+0x40+7); break;
case 3: hour--;if(hour<0)hour=23;
write_hms(4,hour);write_zl(0x80+0x40+4); break;
case 4: week--;if(week==0)week=7;write_XQ(week);
day--; write_day(month); if(day==0)day=nn;
write_nyr(10,day);write_zl(0x80+10); break;
case 5: month--;if(month==0)month=12;
write_nyr(7,month);write_zl(0x80+7); break;
case 6: year-- ;if(year<0)year=99;
write_nyr(4,year);write_zl(0x80+4); break;
case 7: year1-- ;if(year1<0)year1=99;
write_nyr(2,year1);write_zl(0x80+2); break;}
}}
}

void DS1302_sfm()
{ uchar a,b;
RST=1; //高電平期間啟動一次數據轉換
DS1302_write(0x81);
second=DS1302_readbyte();
RST=0;// 結束轉換
a=second/16;//BCD碼轉換
b=second%16;
second=a*10+b;
write_hms(10,second);
//分
RST=1;
DS1302_write(0x83);
minute= DS1302_readbyte();
a=minute/16;
b=minute%16;
minute=a*10+b;
write_hms(7,minute);
RST=0;
//時
RST=1;
DS1302_write(0x85);
hour= DS1302_readbyte();
RST=0;
a=hour/16;
b=hour%16;
hour=a*10+b;
write_hms(4,hour);
//日
RST=1;
DS1302_write(0x87);
day= DS1302_readbyte();
RST=0;
a=day/16;
b=day%16;
day=a*10+b;
write_nyr(10,day);
//月
RST=1;
DS1302_write(0x89);
month= DS1302_readbyte();
RST=0;
a=month/16;
b=month%16;
month=a*10+b;
write_nyr(7,month);
// 周
RST=1;
DS1302_write(0x8B);
week= DS1302_readbyte();
RST=0;
a=week/16;
b=week%16;
week=a*10+b;
write_XQ(week);
// 年
SCLK=0;
delay(5);
RST=1;
DS1302_write(0x8D);
IO=1;
year= DS1302_readbyte();
RST=0;
delay(5);
SCLK=1;
a=year/16;
b=year%16;
year=a*10+b;
write_nyr(4,year);
}
/* 總程序*/
#include<reg51.h>
#include<init.h>
#include<ds1302.h>
#include<18b20.h>
void main()
{
init_lcd();

start1302();
while(1)
{
RST=0;
keyboard();
if(flag==0) //標志位為0才能讀取1302,否則按鍵無效
DS1302_sfm();

write_zl(0x80+0x10);
for(n=0;n<16;n++)
write_sj(ttable[n]);
temperaturechange();
write_temperature(tem);
}
}

③ 怎樣製作萬年歷單片機

今天碰到了N個白痴問題。
萬年歷是能夠顯示年月日、二十四節氣、農歷之類的東西,單片機是一塊晶元。萬年歷可以用單片機來做,但是我真不知道「萬年歷單片機」怎麼做。我想你應該想問的是怎麼用單片機來做萬年歷,我提供思路。
1、單片機最小系統一塊。可以用STC89C52晶元+外圍電路。可以燒入用戶編寫的程序,實現萬年歷所需的邏輯。
2、實時時鍾電路一塊,可以用DS1302晶元。提供年月日、時分秒數據給單片機。
3、顯示器電路一塊。用來顯示這些數據。
4、一些必要的按鍵,用來調整數據。

④ C51單片機的萬年歷程序設計

我這有個數碼管顯示的程序
以前做的,。。
你可以在我這個程序上修改修改
包括鍵盤掃描,還有動態顯示



【。。。】
#include
//常量參數
#define TMODW 0x01;
#define SCONW 0x00;
#define xplay 0x04;//顯示分頻系數
//顯示位選
unsigned char data stb;
//鍵值緩存,0xFF無鍵命令
unsigned char data keynum;
//顯示字型變數
unsigned char data play[8];
//工作參數
unsigned char data l,m;
//字型碼
unsigned char code BCDPC[10]=
{0x3F,0x06,0x5B,0x4F,0x66,
0x6D,0x7D,0x07,0x7F,0x6F};
//字位碼
unsigned char code STBCODE[8]=
{0x01,0x02,0x04,0x08,
0x10,0x20,0x40,0x80 };
//400Hz xplay分頻計數
unsigned char data cttime;
//時鍾參數
unsigned char data hr,min,sec,sec100;
//調整時鍾參數(時鍾「走」)
void ct1()
{sec100++;
if (sec100==100)
{sec100=0;sec++;
if (sec==60)
{sec=0;min++;
if (min==60)
{min=0;hr++;
if (hr==24) hr=0;
}
}
}
}
//時鍾參數→LED 顯示緩存7段參數轉換函數;
void xcplay()
{play[0]=BCDPC[hr/10];
play[1]=BCDPC[hr%10];
play[2]=BCDPC[min/10];
play[3]=BCDPC[min%10];
play[4]=BCDPC[sec/10];
play[5]=BCDPC[sec%10];
play[6]=BCDPC[sec100/10];
play[7]=BCDPC[sec100%10];
}
//顯示掃描
void cplay()
{T0=1;//T0-高電平消隱
T1=0;//T1-低電平準備發脈沖前沿
TI=0;//?
P1=0;//?
SBUF=STBCODE[stb];
while (TI==0)
{
};
TI=0;
SBUF=play[stb];
while (TI==0)
{
};
T1=1;
T0=0;
stb=++stb&0x07;
}

extern void cthl0();
//定時器0中斷處理程序
void ct0(void) interrupt 1 using 1
{cthl0();
cttime--;
if (cttime==0)
{cttime=xplay;
ct1();//調用時鍾「走」函數
xcplay();//調用時鍾參數→Led顯示緩存轉換函數
};
cplay();
}
void w20ms()
{for (l=0;l<41;l++)
{for (m=0;m<81;m++)
{
}
}
}
void tkey()
{P1=0xF0;
keynum=0xFF;
if (P1!=0xF0)
{w20ms();
P1=0xF0;
if (P1!=0xF0)
{P1=0xFE;
switch (P1)
{case 0xEE:keynum=0;break;
case 0xDE:keynum=1;break;
case 0xBE:keynum=2;break;
case 0x7E:keynum=3;break;
}
P1=0xFD;
switch (P1)
{case 0xED:keynum=4;break;
case 0xDD:keynum=5;break;
case 0xBD:keynum=6;break;
case 0x7B:keynum=7;break;
}
P1=0xFB;
switch (P1)
{
case 0xEB:keynum=8;break;
}
};
};
P1=0x00;
}
void command()
{switch (keynum)
{
case 0:{hr=hr+1;
if (hr==24)
hr=0;
}
break;

case 1:{min=min+1;
if (min==60)
min=0;
}
break;

case 2:{sec=sec+1;
if (sec==60)
sec=0;
}
break;

case 3:{sec100=0;
}
break;

case 4:{
while(!(P1=0xED))
{
hr=0;
min=0;
sec=0;
}
}
break;

case 5:{hr=hr-1;
if (hr==00)
hr=24;
}
break;
case 6:{min=min-1;
if (min==00)
min=59;
}
break;

case 7:{sec=sec-1;
if (sec==00)
sec=0;
}
break;

case 0xFF:break;
}
keynum=0xFF;
}
main ()
{ hr=8;
min=5;
sec=8;
sec100=0;
TMOD=TMODW;
SCON=SCONW;
ET0=1;
TR0=1;
EA=1;
cttime=xplay;
while (1)
{w20ms();
tkey();
command();

};
}

⑤ 單片機萬年歷

今天碰到了N個白痴問題。萬年歷是能夠顯示年月日、二十四節氣、農歷之類的東西,單片機是一塊晶元。萬年歷可以用單片機來做,但是我真不知道「萬年歷單片機」怎麼做。我想你應該想問的是怎麼用單片機來做萬年歷,我提供思路。 1、單片機最小系統一塊。可以用STC89C52晶元+外圍電路。可以燒入用戶編寫的程序,實現萬年歷所需的邏輯。 2、實時時鍾電路一塊,可以用DS1302晶元。提供年月日、時分秒數據給單片機。 3、顯示器電路一塊。用來顯示這些數據。 4、一些必要的按鍵,用來調整數據。

⑥ 跪求 51單片機+12864液晶+1302時鍾製成的萬年歷c程序

頂層文件 萬年歷.C
#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 naoling(void)
{
uchar i=0,l=0,j;
init1602();
while(key_flag2&&i<12)
if(key_scan()){j=key_value();write_data(j);if(i%2==0)data_alarm[l]=(j-'0')*10;else {data_alarm[l]+=(j-'0');l++;}i++;delay(600);}
write_com(0x01);
}
uchar according(void)
{ uchar k;
if(data_alarm[0]==adjusted.Year&&data_alarm[1]==adjusted.Month&&data_alarm[2]==adjusted.Day&&data_alarm[3]==adjusted.Hour&&data_alarm[4]==adjusted.Minute&&data_alarm[5]==adjusted.Second)
k=1;
else k=0;
return k;
}
void speak(void)
{uint i=50;
while(i)
{speaker=0;
delay(1);
speaker=1;
delay(1);
i--;
}
}
void alarm(void)
{uint i=10;
while(i)
{
speak();
delay(10);
i--;
}
}
/**************************修改時間操作********************************/
void reset(void)
{
sec_add=0;
min_add=0;
hou_add=0;
day_add=0;
mon_add=0;
yea_add=0 ;
}
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;
case 'b':reset();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;

if(adjusted.Minute>59) adjusted.Minute=adjusted.Minute%60;

if(adjusted.Hour>23) adjusted.Hour=adjusted.Hour%24;

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;

}

/**************************中斷處理函數*********************************/
void changing(void) interrupt 0 using 0 //需要修改時間和日期,或者停止修改
{
if(key_flag1)key_flag1=0;
else key_flag1=1;
}
void alarming(void) interrupt 3 using 0 //需要設置鬧鈴或者停止設置
{
if(key_flag2)key_flag2=0;
else key_flag2=1;
}
/********************************主函數***********************************/
main()
{uint i;
uchar *l;
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();
if(key_flag2)naoling();
if(according())alarm();
DateToStr(&adjusted);
TimeToStr(&adjusted);
write_com(0x82);
write_string(adjusted.DateString,8);
write_com(0xc2);
write_string(adjusted.TimeString,8);
delay(10);
}
(二)頭文件1 顯示模塊 LCD1602.H
#ifndef LCD_CHAR_1602_2009_5_9
#define LCD_CHAR_1602_2009_5_9
#define uchar unsigned char
#define uint unsigned int
sbit lcdrs = P2^0;
sbit lcdrw = P2^1;
sbit lcden = P2^2;
void delay(uint z) // 延時
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com) // 寫入指令數據到 lcd
{
lcdrw=0;
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}

void write_data(uchar date) // 寫入字元顯示數據到 lcd
{
lcdrw=0;
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init1602() // 初始化設定
{
lcdrw=0;
lcden=0;
write_com(0x3C);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
void write_string(uchar *pp,uint n)
{
int i;
for(i=0;i<n;i++)
write_data(pp[i]);
}
#endif
(三)頭文件2 時鍾模塊 DS1302.H
#ifndef _REAL_TIMER_DS1302_2009_5_20_
#define _REAL_TIMER_DS1302_2003_5_20_

sbit DS1302_CLK = P2^6; //實時時鍾時鍾線引腳
sbit DS1302_IO = P2^7; //實時時鍾數據線引腳
sbit DS1302_RST = P2^5; //實時時鍾復位線引腳
sbit ACC0 = ACC^0;
sbit ACC7 = ACC^7;

typedef struct SYSTEM_TIME
{
unsigned char Second;
unsigned char Minute;
unsigned char Hour;
unsigned char Week;
unsigned char Day;
unsigned char Month;
unsigned char Year;
unsigned char DateString[9]; //用這兩個字元串來放置讀取的時間
unsigned char TimeString[9];
}SYSTEMTIME; //定義的時間類型

#define AM(X) X
#define PM(X) (X+12) // 轉成24小時制
#define DS1302_SECOND 0x80
#define DS1302_MINUTE 0x82
#define DS1302_HOUR 0x84
#define DS1302_WEEK 0x8A
#define DS1302_DAY 0x86
#define DS1302_MONTH 0x88
#define DS1302_YEAR 0x8C
#define DS1302_RAM(X) (0xC0+(X)*2) //用於計算 DS1302_RAM 地址的宏

/******內部指令**********/
void DS1302InputByte(unsigned char d) //實時時鍾寫入一位元組(內部函數)
{
unsigned char i;
ACC = d;
for(i=8; i>0; i--)
{
DS1302_IO = ACC0;
DS1302_CLK = 1;
DS1302_CLK = 0;
ACC = ACC >> 1; //因為在前面已經定義了ACC0 = ACC^0;以便再次利用DS1302_IO = ACC0;
}
}

unsigned char DS1302OutputByte(void) //實時時鍾讀取一位元組(內部函數)
{
unsigned char i;
for(i=8; i>0; i--)
{
ACC = ACC >>1;
ACC7 = DS1302_IO;
DS1302_CLK = 1;
DS1302_CLK = 0;
}
return(ACC);
}
/********************************/

void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要寫的數據
{
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr); // 地址,命令
DS1302InputByte(ucDa); // 寫1Byte數據
DS1302_CLK = 1;
DS1302_RST = 0;
}

unsigned char Read1302(unsigned char ucAddr) //讀取DS1302某地址的數據
{
unsigned char ucData;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr|0x01); // 地址,命令
ucData = DS1302OutputByte(); // 讀1Byte數據
DS1302_CLK = 1;
DS1302_RST = 0;
return(ucData);
}

void DS1302_SetProtect(bit flag) //是否防寫
{
if(flag)
Write1302(0x8E,0x10);
else
Write1302(0x8E,0x00);
}

void DS1302_SetTime(unsigned char Address, unsigned char Value) // 設置時間函數
{
DS1302_SetProtect(0);
Write1302(Address, ((Value/10)<<4 | (Value%10))); //將十進制數轉換為BCD碼
} //在DS1302中的與日歷、時鍾相關的寄存器存放的數據必須為BCD碼形式

void DS1302_GetTime(SYSTEMTIME *Time)
{
unsigned char ReadValue;
ReadValue = Read1302(DS1302_SECOND);
Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); //將BCD碼轉換為十進制數
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);
}
unsigned char *DataToBCD(SYSTEMTIME *Time)
{
unsigned char D[8];
D[0]=Time->Second/10<<4+Time->Second%10;
D[1]=Time->Minute/10<<4+Time->Minute%10;
D[2]=Time->Hour/10<<4+Time->Hour%10;
D[3]=Time->Day/10<<4+Time->Day%10;
D[4]=Time->Month/10<<4+Time->Month%10;
D[5]=Time->Week/10<<4+Time->Week%10;
D[6]=Time->Year/10<<4+Time->Year%10;
return D;
}
void DateToStr(SYSTEMTIME *Time)
{
//將十進制數轉換為液晶顯示的ASCII值
Time->DateString[0] = Time->Year/10 + '0';
Time->DateString[1] = Time->Year%10 + '0';
Time->DateString[2] = '-';
Time->DateString[3] = Time->Month/10 + '0';
Time->DateString[4] = Time->Month%10 + '0';
Time->DateString[5] = '-';
Time->DateString[6] = Time->Day/10 + '0';
Time->DateString[7] = Time->Day%10 + '0';
Time->DateString[8] = '\0';
}

void TimeToStr(SYSTEMTIME *Time)
{
//將十進制數轉換為液晶顯示的ASCII值
Time->TimeString[0] = Time->Hour/10 + '0';
Time->TimeString[1] = Time->Hour%10 + '0';
Time->TimeString[2] = ':';
Time->TimeString[3] = Time->Minute/10 + '0';
Time->TimeString[4] = Time->Minute%10 + '0';
Time->TimeString[5] = ':';
Time->TimeString[6] = Time->Second/10 + '0';
Time->TimeString[7] = Time->Second%10 + '0';
Time->DateString[8] = '\0';
}
void Initial_DS1302(void)
{
unsigned char Second;
Second=Read1302(DS1302_SECOND);
if(Second&0x80) //初始化時間
DS1302_SetTime(DS1302_SECOND,0);
}
void DS1302_TimeStop(bit flag) // 是否將時鍾停止
{
unsigned char Data;
Data=Read1302(DS1302_SECOND);
DS1302_SetProtect(0);
if(flag)
Write1302(DS1302_SECOND, Data|0x80);
else
Write1302(DS1302_SECOND, Data&0x7F);
}
#endif

閱讀全文

與萬年歷的程序單片機相關的資料

熱點內容
245倒角編程怎麼計算 瀏覽:599
可以買生活用品的app有哪些 瀏覽:175
cad在c盤產生的文件夾 瀏覽:541
聯想手機解鎖工具 瀏覽:696
瑞銀3887win10 瀏覽:833
學網路編程哪個好 瀏覽:805
手機vmos導入的文件在哪裡 瀏覽:115
蘋果手機可以把文件傳到華為嗎 瀏覽:63
海川化工下載的文件默認到哪裡 瀏覽:343
學唱粵語歌app 瀏覽:975
qq游戲生死狙擊玩不了 瀏覽:120
win10郵件不顯示圖片 瀏覽:922
口袋妖怪所有版本下載 瀏覽:504
我們身邊都有哪些大數據例子 瀏覽:25
震旦adc307掃描的文件在哪裡 瀏覽:999
圖片打開變成文件 瀏覽:194
松下微單電腦傳文件軟體 瀏覽:574
蘋果藍牙鍵盤surface 瀏覽:170
mindmaplinux 瀏覽:733
oppo手機怎麼連接電腦傳輸數據 瀏覽:624

友情鏈接