1. 用51單片機做日歷時鍾 【軟體模擬】
你好:
這時我用51單片機做的萬年歷,能顯示日期和時間,LCD1602顯示,並可通過按鍵調整時間。
里邊有protues的dsn模擬圖和萬年歷的程序。
有不懂的再問我吧。
希望我的回答能幫助到你。
2. 關於單片機日歷時鍾C編程
我寫的程序,自己移植一下吧,應該看得懂
閏年計算:
uint8 isleapyear(uint16 year)
{
if(((year%400)==0)||(((year%4)==0)&&(year%100)==0))
{
return 1;
}
else
return 0;
}
下面是按月計算日的最大值,每次mon變化計算一次就行了。
switch(mon)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
daymax = 31;
}break;
case 4:
case 6:
case 9:
case 11:
{
daymax = 31;
}break;
case 2:
{
daymax = 28+isleapyear(year);
}break;
3. 單片機萬年歷設計
#include<reg52.h>#define uint unsigned int#define uchar unsigned charsbit DS1302_CLK = P3^1;sbit DS1302_IO = P3^2;sbit DS1302_RST = P3^0;uchar a,b,c,d,e,f,g,h;uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};uchar wr_data[8]={0x11,0x12,0x06,0x02,0x10,0x53,0x10};uchar readtimeR[8];void delays(uint n){while(n--);}/*****************************************************************************函數功能:寫DS1302一個位元組子程序入口參數:出口參數:*****************************************************************************/void sendbyte(uchar a){uchar i,temp;temp=a;for(i=8;i>0;i--) { if(temp&0x01) //先發低位DS1302_IO=1;elseDS1302_IO=0;temp=temp>>1;DS1302_CLK=1;delays(5);DS1302_CLK=0;delays(5); }}/*****************************************************************************函數功能:讀DS1302一個位元組子程序入口參數:出口參數:*****************************************************************************/uchar readbyte(void){uchar i,dat=0;for(i=8;i>0;i--) {DS1302_IO=1;dat>>=1; if(DS1302_IO) dat|=0x80; DS1302_CLK=1;delays(5);DS1302_CLK=0;}return dat;} /*****************************************************************************函數功能:寫DS1302地址子程序入口參數:add入口數據:dat*****************************************************************************/void write_DS1302(uchar address,uchar dat){DS1302_RST=0;delays(10);DS1302_CLK=0;delays(10);DS1302_RST=1;delays(5);sendbyte(address);delays(5);sendbyte(dat);DS1302_RST=0;DS1302_CLK=0;}/*****************************************************************************函數功能:讀DS1302地址子程序入口參數:add出口參數:readtimeR[]*****************************************************************************/uchar read_DS1302(uchar address){ uchar timer;DS1302_RST=0;delays(2);DS1302_CLK=0;delays(2);DS1302_RST=1;sendbyte(address); timer=readbyte();DS1302_RST=0;DS1302_CLK=0;return timer;}/*****************************************************************************函數功能:初始化DS1302子程序入口參數:wr_data[](全局變數)出口參數:*****************************************************************************/void DS1302_init(){write_DS1302(0xbe,0x00);write_DS1302(0x8e,0x00); //關閉保護,若寫0x80,則開啟保護write_DS1302(0x8c,wr_data[0]); //年write_DS1302(0x88,wr_data[1]); //月write_DS1302(0x86,wr_data[2]); //日 write_DS1302(0x8A,wr_data[3]); //星期write_DS1302(0x84,wr_data[4]); //時write_DS1302(0x82,wr_data[5]); //分write_DS1302(0x80,wr_data[6]); //秒 write_DS1302(0x8e,0x80);}/*****************************************************************************函數功能:讀DS1302時間子程序入口參數:出口參數:全局變數(year,mouth,data,day,hour,minute,second)*****************************************************************************/void read_time(void){readtimeR[0]=read_DS1302(0x8d); //年readtimeR[1]=read_DS1302(0x89); //月readtimeR[2]=read_DS1302(0x87); //日readtimeR[3]=read_DS1302(0x8b); //周readtimeR[4]=read_DS1302(0x85); //時readtimeR[5]=read_DS1302(0x83); //分readtimeR[6]=read_DS1302(0x81); //秒}void ms(){ e=readtimeR[5]>>4;f=readtimeR[5]&0x0f;g=readtimeR[6]>>4;h=readtimeR[6]&0x0f;P2=0xfc;P0=table[e];delays(500);P2=0xfd;P0=table[f];delays(500);P2=0xfB;P0=table[g];delays(500);P2=0xf7;P0=table[h];delays(500);}void hm(){ c=readtimeR[4]>>4;d=readtimeR[4]&0x0f; e=readtimeR[5]>>4;f=readtimeR[5]&0x0f; P2=0xfc;P0=table[c];delays(500);P2=0xfd;P0=table[d];delays(500); P2=0xfb;P0=table[e];delays(500);P2=0xf7;P0=table[f];delays(500);}void main(){DS1302_init();while(1){read_time();// a=readtimeR[3]>>4;// b=readtimeR[3]&0x0f;/* P2=0xf8;P0=table[a];delays(500);P2=0xf9;P0=table[b];delays(500);*/// ms();hm();}}
4. 怎麼利用單片機做日歷
http://www.ourdev.cn/bbs/bbs_content.jsp?bbs_sn=992373&bbs_page_no=2&bbs_id=1006
這里有很詳細的資料
5. 單片機日歷時鍾系統
//給你一個DS1302的庫函數
//液晶那個庫要的話也能給你哈
//#include "intrins.h"
sbit DS1302_clk=0xa0;
sbit DS1302_data=0xa1;
sbit DS1302_cs=0xa2;
unsigned char year,month,date,hour,minute,second,week;
//unsigned char display_buffer[6];
void short_ds1302_delay();//短延時
void long_ds1302_delay();//長延時
void DS1302_write(unsigned char DS1302_temp); //寫入一個位元組
unsigned char DS1302_read(void); //讀一位元組
void DS1302_init();//ds1302初始化
void DS1302_write_time(int command,int dat);//寫入second or minute or hour...... or 寫入一個命令
unsigned char DS1302_read_time(int command);//讀取second or minute or hour......
void short_ds1302_delay()
{
unsigned char i;
for(i=0;i<5;i++);
}
void long_ds1302_delay()
{
unsigned char i;
for(i=0;i<25;i++);
}
void DS1302_write(unsigned char DS1302_temp) //寫入一個位元組
{
unsigned char i,temp;
long_ds1302_delay();
temp=DS1302_temp;
for(i=0;i<8;i++)
{
DS1302_data=temp&0x01;
short_ds1302_delay();
DS1302_clk=1;
short_ds1302_delay();
DS1302_clk=0;
temp=temp>>1;
}
}
unsigned char DS1302_read(void) //讀一位元組
{
unsigned char i,temp;
long_ds1302_delay();
temp=0;
for(i=0;i<8;i++)
{
temp=temp>>1;
if(DS1302_data==1)
temp=temp+0x80;
DS1302_clk=1;
short_ds1302_delay();
DS1302_clk=0;
short_ds1302_delay();
}
return temp;
}
/*
/////////寫操作設置,當a=0x80時禁止寫操作,a=0x00時允許////////////////
void DS1302_write_able(unsigned char a)
{
DS1302_cs=0;
DS1302_clk=0;
short_ds1302_delay();
DS1302_cs=1;
DS1302_write(0x8e);
DS1302_data=1;
DS1302_write(a);
DS1302_cs=0;
short_ds1302_delay();
DS1302_clk=1;
}
/////////讀操作設置,當a=0x80時禁止讀操作,a=0x00時允許////////////////
void DS1302_read_able(unsigned char a)
{
DS1302_cs=0;
DS1302_clk=0;
short_ds1302_delay();
DS1302_cs=1;
DS1302_write(0x8f);
DS1302_data=1;
DS1302_write(a);
DS1302_cs=0;
short_ds1302_delay();
DS1302_clk=1;
}
*/
////////////////////ds1302初始化////////////////////////
void DS1302_init()
{
unsigned char i;
DS1302_write_time(0x8e,0x00); //允許寫操作
DS1302_cs=0;
DS1302_clk=0;
short_ds1302_delay();
DS1302_cs=1;
DS1302_write(0x81);
DS1302_data=1;
i=DS1302_read();
DS1302_cs=0;
short_ds1302_delay();
DS1302_clk=1;
second=((i>>4)&0x07)*10+(i&0x0f);
DS1302_clk=0;
short_ds1302_delay();
DS1302_cs=1;
DS1302_write(0x80);
DS1302_data=1;
DS1302_write(0x7f&i);
DS1302_cs=0;
short_ds1302_delay();
DS1302_clk=1;
DS1302_clk=0; //充電設置
short_ds1302_delay();
DS1302_cs=1;
DS1302_write(0x90);
DS1302_data=1;
// DS1302_write(0xa6); //允許充電,一個二極體 4k電阻
DS1302_write(0x00); //禁止充電
DS1302_cs=0;
short_ds1302_delay();
DS1302_clk=1;
DS1302_write_time(0x8e,0x80); //禁止寫操作
}
unsigned char DS1302_read_time(int command)//讀取second or minute or hour......
{
unsigned char i;
DS1302_clk=0;
short_ds1302_delay();
DS1302_cs=1;
DS1302_write(command);
DS1302_data=1;
i=DS1302_read();
DS1302_cs=0;
short_ds1302_delay();
DS1302_clk=1;
return i;
}
void DS1302_readdate()
{
unsigned char i;
DS1302_cs=0;
//讀取second
i=DS1302_read_time(0x81);
second=((i>>4)&0x07)*10+(i&0x0f);
//讀取分minute
i=DS1302_read_time(0x83);
minute=((i>>4)&0x07)*10+(i&0x0f);
//讀取時hour
i=DS1302_read_time(0x85);
hour=((i>>4)&0x03)*10+(i&0x0f);
//讀取日date
i=DS1302_read_time(0x87);
date=((i>>4)&0x03)*10+(i&0x0f);
//讀取月份month
i=DS1302_read_time(0x89);
month=((i>>4)&0x01)*10+(i&0x0f);
//讀取周week
i=DS1302_read_time(0x8b);
week=i&0x07;
//讀取年份year
i=DS1302_read_time(0x8d);
year=((i>>4)&0x0f)*10+(i&0x0f);
}
void DS1302_write_time(int command,int dat)//寫入second or minute or hour...... or 寫入一個命令
{
DS1302_clk=0;
short_ds1302_delay();
DS1302_cs=1;
DS1302_write(command);
DS1302_data=1;
DS1302_write(dat);
DS1302_cs=0;
short_ds1302_delay();
DS1302_clk=1;
}
void DS1302_writedate()
{
DS1302_write_time(0x8e,0x00); //允許寫操作
DS1302_cs=0;
//寫second
DS1302_write_time(0x80,0x7f&(((second/10)<<4)+(second%10)));
//minute
DS1302_write_time(0x82,0x7f&(((minute/10)<<4)+(minute%10)));
//hour
DS1302_write_time(0x84,0x3f&(((hour/10)<<4)+(hour%10)));
//date
DS1302_write_time(0x86,0x3f&(((date/10)<<4)+(date%10)));
//month
DS1302_write_time(0x88,0x1f&(((month/10)<<4)+(month%10)));
//week
DS1302_write_time(0x8a,0x07&week);
//year
DS1302_write_time(0x8c,((year/10)<<4)+(year%10));
DS1302_write_time(0x8e,0x80); //禁止寫操作
}
6. 單片機日歷時間系統
http://www.jldz08.cn/04cp/04_991.htm
7. 51單片機製做日歷
看看這個程序嘛,,雖然沒有你要得年月日,但是有時間得啊。。。可以藉此來點靈感。。我也在做這個東西。。。我還在收集資料啊。。。共同學習。。
;採用8位LED軟體解碼動態顯示程序
;使用AT89C51單片機,12MHZ晶振,P0輸出欄位碼,P2口輸出位選碼,用共陽
;LED數碼管,P1.0為調時位選擇按鍵,P1.1為加1鍵,P1.2為減1鍵。
;片內RAM的70H到77H單元為LED數碼管的顯示緩沖區
;78H,79H,7AH分別為秒、分、小時計數單元
;7BH為50ms計數器,7CH為調時按鍵計數器
ORG 0000H
LJMP START
ORG 000BH ;定時器/計數器T0中斷程序入口
LJMP INTT0
;主程序
START:MOV R0,#70H
MOV R7,#0CH
INIT: MOV @R0,#00H
INC R0
DJNZ R7,INIT
MOV 72H,#10
MOV 75H,#10
MOV TMOD,#01H
MOV TL0,#0B0H
MOV TH0,#03CH
SETB EA
SETB ET0
SETB TR0
START1: LCALL SCAN
LCALL KEYSCAN
SJMP START1
;延時1MS子程序
DL1MS: MOV R6,#14H
DL1: MOV R7,#19H
DL2: DJNZ R7,DL2
DJNZ R6,DL1
RET
;延時20MS子程序
DL20MS: ACALL SCAN
ACALL SCAN
ACALL SCAN
RET
;數碼管顯示程序
SCAN: MOV A,78H ;時間存入顯示緩沖區相應位置
MOV B,#0AH
DIV AB
MOV 71H,A
MOV 70H,B
MOV A,79H
MOV B,#0AH
DIV AB
MOV 74H,A
MOV 73H,B
MOV A,7AH
MOV B,#0AH
DIV AB
MOV 77H,A
MOV 76H,B
MOV R1,#70H ;循環掃描顯示
MOV R5,#80H
MOV R3,#08H
SCAN1: MOV A,R5
MOV P2,A
MOV A,@R1
MOV DPTR,#TAB
MOVC A,@A+DPTR
MOV P0,A
MOV A,R5
LCALL DL1MS
INC R1
MOV A,R5
RR A
MOV R5,A
DJNZ R3,SCAN1
MOV P2,#00H
MOV P0,#0FFH
RET
TAB:DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,0BFH
;「0~9」,「-」的共陽極欄位碼
;定時器/計數器T0中斷服務程序
INTT0: PUSH ACC
PUSH PSW
CLR ET0
CLR TR0
MOV TL0,#0B0H
MOV TH0,#03CH
SETB TR0
INC 7BH
MOV A,7BH
CJNE A,#14H,OUTT0
MOV 7BH,#00
INC 78H
MOV A,78H
CJNE A,#3CH,OUTT0
MOV 78H,#00
INC 79H
MOV A,79H
CJNE A,#3CH,OUTT0
MOV 79H,#00
INC 7AH
MOV A,7AH
CJNE A,#18H,OUTT0
MOV 7AH,#00
OUTT0:
SETB ET0
pop psw
pop acc
RETI
;按鍵處理程序
KEYSCAN: CLR EA
JNB P1.0,KEYSCAN0
JNB P1.1,KEYSCAN1
JNB P1.2,KEYSCAN2
KEYOUT: SETB EA
RET
KEYSCAN0: LCALL DL20MS
JB P1.0,KEYOUT
WAIT0: JNB P1.0,WAIT0
INC 7CH
MOV A,7CH
CLR TR0
CLR ET0
CJNE A,#03H,KEYOUT
MOV 7CH,#00
SETB TR0
SETB ET0
SJMP KEYOUT
KEYSCAN1: LCALL DL20MS
JB P1.1,KEYOUT
WAIT1: JNB P1.1,WAIT1
MOV A,7CH
CJNE A,#02H,KSCAN11
INC 79H
MOV A,79H
CJNE A,#3CH,KEYOUT
MOV 79H,#00
SJMP KEYOUT
KSCAN11: INC 7AH
MOV A,7AH
CJNE A,#18H,KEYOUT
MOV 7AH,#00
SJMP KEYOUT
KEYSCAN2: LCALL DL20MS
JB P1.2,KEYOUT
WAIT2: JNB P1.2,WAIT2
MOV A,7CH
CJNE A,#02H,KSCAN21
DEC 79H
MOV A,79H
CJNE A,#0FFH,KEYOUT
MOV 79H,#3BH
SJMP KEYOUT
KSCAN21: DEC 7AH
MOV A,7AH
CJNE A,#0FFH,KEYOUT
MOV 7AH,#17H
SJMP KEYOUT
END
8. 求單片機日歷時鍾C語言源程序
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code DSY_CODE[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0xff};
uchar DSY_BUFFER[]={0,0,0xBF,0,0,0xBF,0,0};
uchar Scan_BIT;
uchar DSY_IDX;
uchar Key_State;
uchar h,m,s,s100;
void DelayMS(uchar x)
{
uchar i;
while(x--) for(i=0;i<120;i++);
}
void Increase_Hour()
{
if(++h>23)h=0;
DSY_BUFFER[0]=DSY_CODE[h/10];
DSY_BUFFER[1]=DSY_CODE[h%10];
}
void Increase_Minute()
{
if(++m>59)
{
m=0;Increase_Hour();
}
DSY_BUFFER[3]=DSY_CODE[m/10];
DSY_BUFFER[4]=DSY_CODE[m%10];
}
void Increase_Second()
{
if(++s>59)
{
s=0;Increase_Minute();
}
DSY_BUFFER[6]=DSY_CODE[s/10];
DSY_BUFFER[7]=DSY_CODE[s%10];
}
void T0_INT()interrupt 1
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
P3=Scan_BIT;
P0=~DSY_BUFFER[DSY_IDX];
Scan_BIT=_crol_(Scan_BIT,1);
DSY_IDX=(DSY_IDX+1)%8;
}
void T1_INT()interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
if(++s100==20)
{
s100=0;Increase_Second();
}
}
void main()
{
P0=P3=0xFF;
TMOD=0x11;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
TH1=0xDC;
TL1=0;
TCON=0x01;
EA=1;
ET0=1;
ET1=1;
h=12;m=s=s100=0;
DSY_BUFFER[0]=DSY_CODE[h/10];
DSY_BUFFER[1]=DSY_CODE[h%10];
DSY_BUFFER[3]=DSY_CODE[m/10];
DSY_BUFFER[4]=DSY_CODE[m%10];
DSY_BUFFER[6]=DSY_CODE[s/10];
DSY_BUFFER[7]=DSY_CODE[s%10];
Scan_BIT=0xFE;
DSY_IDX=0;
TR0=TR1=1;
Key_State=0xFF;
while(1)
{
if(P1^Key_State)
{
DelayMS(10);
if(P1^Key_State)
{
Key_State=P1;EA=0;
if((Key_State&0x01)==0)Increase_Hour();
else if((Key_State&0x02)==0)
{
m=(m+1)%60;
DSY_BUFFER[3]=DSY_CODE[m/10];
DSY_BUFFER[4]=DSY_CODE[m%10];
}
EA=1;
}
}
}
}
分給我吧,我不知道您要的是液晶時鍾還是數碼管的,但是我給你的是時鍾,日歷很好加,自己根據理解家上吧。希望對你有幫助。
9. 單片機日歷時鍾顯示程序
建議你看看DS12C887或者DS1302的資料。
10. 電子日歷單片機製作
實現日歷的顯示、鬧鍾設置、日期和時間的設置功能