- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit clk=P3^3; // 時鐘控制引腳
- sbit io=P3^4; // 數據輸入輸出引腳
- sbit rst=P3^5;
- sbit k1=P2^3;//狀態鍵
- sbit k2=P2^2;//加
- sbit k3=P2^1;//減
- sbit k4=P2^0;//查看鍵
- sbit beep=P3^2;//蜂鳴器
- sbit scl = P3^6;
- sbit sda = P3^7;
- sbit m1 = P2^4;
- sbit m2 = P2^5;//閃爍燈
- sbit m3 = P2^7;//標志燈
- bit anflg = 1;//查看標志
- bit write = 0;//寫標志
- static uchar set = 0;
- static uint js = 0;
- static uint num = 0;
- void initTimer();
- void key();
- void ShowTwinkle();
- uchar code wu[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
- uchar code du[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xc7,0x7f};
- uchar time_buf1[]={20,15,9,5,11,26,50,5}; //初值
- uchar buf[]={0,0,0,0,0,0};
- uchar clockh=8,clockm=0,clocks=0;
- void clock();
- void delay(int z)//延時
- {
- int x;
- for(;z>0;z--)
- for(x=110;x>0;x--);
- }
- void write_1302dat(uchar add,uchar dat)//寫地址和數據
- {
- uchar i;
- clk=0;
- rst=1;
- for(i=0;i<8;i++) //寫地址
- {
- clk=0;
- io=add&0x01;
- clk=1;
- add>>=1;
- }
- for(i=0;i<8;i++) //寫數據
- {
- clk=0;
- io=dat&0x01;
- clk=1;
- dat>>=1;
- }
- clk=0;
- rst=0;
- }
- uchar read_1302dat(uchar add)//寫 讀數據
- {
- uchar i,dat;
- clk=0;
- rst=1;
- for(i=0;i<8;i++) // 寫地址
- {
- clk=0;
- io=add&0x01;
- clk=1;
- add>>=1;
- }
-
- for(i=0;i<8;i++) //讀數據
- {
- clk=0;
- dat>>=1;
- if(io==1)
- dat|=0x80;
- clk=1;
- }
- rst=0;
- clk=0;
- return dat;
- }
- void Ds1302_Write_Time(void)//將數據寫入相對應的地址
- {
-
- unsigned char i,tmp;
- for(i=0;i<8;i++)
- { //BCD處理
- tmp=time_buf1[i]/10;
- time_buf1[i]=time_buf1[i]%10;
- time_buf1[i]=time_buf1[i]+tmp*16;
- }
- rst=0;
- clk=0;
- rst=1; //啟動
- write_1302dat(0x8e,0x00); //關閉寫保護
- write_1302dat(0x80,0x80); //暫停
- write_1302dat(0x90,0xa9); //涓流充電
- write_1302dat(0x8c,time_buf1[1]); //年
- write_1302dat(0x88,time_buf1[2]); //月
- write_1302dat(0x86,time_buf1[3]); //日
- // write_1302dat(ds1302_day_add,time_buf[7]); //周
- write_1302dat(0x84,time_buf1[4]); //時
- write_1302dat(0x82,time_buf1[5]); //分
- write_1302dat(0x80,time_buf1[6]); //秒
- write_1302dat(0x8a,time_buf1[7]); //周
- write_1302dat(0x8e,0x80); //打開寫保護
- }
- void Ds1302_Read_Time(void) //轉換
- {
- unsigned char i,tmp;
- io=0;
- time_buf1[1]=read_1302dat(0x8d); io=0; //年
- time_buf1[2]=read_1302dat(0x89); io=0; //月
- time_buf1[3]=read_1302dat(0x87); io=0; //日
- time_buf1[4]=read_1302dat(0x85); io=0; //時
- time_buf1[5]=read_1302dat(0x83); io=0; //分
- time_buf1[6]=read_1302dat(0x81); io=0; //秒
- time_buf1[7]=read_1302dat(0x8b); io=0; //周
-
- for(i=0;i<8;i++)
- { //BCD處理
- tmp=time_buf1[i]/16;
- time_buf1[i]=time_buf1[i]%16;
- time_buf1[i]=time_buf1[i]+tmp*10;
- }
- }
- void clock()//鬧鐘函數,在一分鐘之內只叫兩次,一次15秒
- {
- if(time_buf1[4]==clockh&&time_buf1[5]==clockm&&(time_buf1[6]>30&&time_buf1[6]<45||time_buf1[6]<15))
- {
- num++;
-
- if(num<40||num>80&&num<120||num>160&&num<200||num>240&&num<280)
- {
- beep=0;
- m3 =~ m3;
- }
- else
- beep=1;
- if(num>300)
- num=0;
- return;
- }
- else if(time_buf1[6]<=1&&time_buf1[5]==0&&set==0)
- {
- m3 = 0;
- beep=0;
- }
- else
- {
- m3 = 1;
- beep=1;
- }
- }
- void start() //啟始信號
- {
- sda=1;
- scl=1;
- sda=0;
- }
- void stop() //結束信號
- {
- sda=0;
- scl=1;
- sda=1;
- }
- void ack() //檢測應答
- {
- uchar i;
- sda=1;
- scl=1;
- while((sda==1)&(i<100)) i++;
- scl=0;
- }
- void write_byte(uchar dat) //寫一個字節
- {
- uchar i;
- for(i=0;i<8;i++) //循環移入八位數據,高位在前
- {
- dat<<=1;
- scl=0;
- sda=CY;
- scl=1;
- }
- scl=0;
- }
- uchar read_byte() //讀一個字節
- {
- uchar i,dat;
- for(i=0;i<8;i++) //循環讀入八位數據,高位在前
- {
- dat<<=1;
- scl=1;
- if(sda==1)
- dat|=0x01;
- scl=0;
- }
- return dat;
- }
- void write_dat2402(uchar add,uchar dat) //向24C02寫一個字節進去
- {
- start();
- write_byte(0xa0); // 器件地址以及寫入操作
- ack();
- write_byte(add); // 寫入數據的地址
- ack();
- write_byte(dat); // 寫入的數據
- ack();
- stop();
- }
- uchar read_dat2402(uchar add) //從24C02讀一個字節進去
- {
- uchar dat;
- start();
- write_byte(0xa0); // 器件地址以及寫入操作
- ack();
- write_byte(add); // 讀出數據的地址
- ack();
- start();
- write_byte(0xa1); // 器件地址以及讀取操作
- ack();
- dat=read_byte();
- stop();
- return dat;
- }
- void init24c02()//初始化24c02
- {
- sda=1;
- scl=1;
- }
- void cunchu()//將數據存入24c02,加判斷防止數據出錯
- {
- time_buf1[4] = read_dat2402(4); //時
- if(time_buf1[4] > 23)
- time_buf1[4] = 0;
- delay(5);
- time_buf1[5] = read_dat2402(5); //分
- if(time_buf1[5] > 59)
- time_buf1[5] = 0;
- delay(5);
- time_buf1[6] = read_dat2402(6); //秒
- if(time_buf1[6] > 59)
- time_buf1[6] = 0;
- delay(5);
- }
- void xie()//將數據寫入24c02
- {
- if(write == 1)
- {
- write = 0;
- write_dat2402(4,time_buf1[4]); //時
- write_dat2402(5,time_buf1[5]); //分
- write_dat2402(6,time_buf1[6]); //秒
- }
- }
- void main()//主函數
- {
- rst=0;
- clk=0;
- write_1302dat(0x80,0x00);
- initTimer();
- init24c02();
- cunchu();
-
- beep = 0;
- m3 = 0;
- delay(1000);
- m3 = 1;
- beep = 1;//開機響一次
-
- while(1)
- {
- key(); //按鍵
- clock();
- Ds1302_Read_Time();
- ShowTwinkle(); //顯示函數
- xie();
- }
- }
- void time0() interrupt 1
- {
- static uchar cnt = 0;
- TH0=(65536-1000)/256;
- TL0=(65536-1000)%256;
- js++;
- if(js>1000)
- {
- write = 1; //每一秒向24c02寫一次
- js=0;
- }
- if(++cnt>5) cnt=0;
- P0=0xff;
- P1=wu[cnt];
- P0=du[buf[cnt]];
- }
- void initTimer()//定時器初始化函數
- {
- TMOD=0x01;
- TH0=(65536-1000)/256;
- TL0=(65536-1000)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void key()//按鍵函數
- {
- static uchar flg=0;
- static uchar j = 0;
- if(k1==0)
- {
- if(flg==0)
- {
- flg=1;
- set++;
- if(set>9) set=0;
- }
- }
-
- else if(k2==0)
- {
- if(flg==0)
- {
- flg=1;
- switch(set)
- {
- case 9 : clockh++;//clockh
- if(clockh>23)
- clockh=0;
- break;
-
- case 8 : clockm++;//clockm
- if(clockm>59)
- clockm=0;
- break;
-
- case 7 : time_buf1[1]++;//年
- if(time_buf1[1]>99)
- time_buf1[1]=0;
- Ds1302_Write_Time(); break;
-
- case 6 : time_buf1[2]++; //月
- if(time_buf1[2]>12)
- time_buf1[2]=1;
- Ds1302_Write_Time(); break;
-
- case 5 : time_buf1[3]++; //日
- if(time_buf1[3]>31)
- time_buf1[3]=1;
- Ds1302_Write_Time(); break;
- case 4 : time_buf1[7]++; //星期
- if(time_buf1[7]>7)
- time_buf1[7]=1;
- Ds1302_Write_Time(); break;
-
- case 3 : time_buf1[4]++; //時
- if(time_buf1[4]>23)
- time_buf1[4]=0;
- Ds1302_Write_Time(); break;
-
- case 2 : time_buf1[5]++; //分
- if(time_buf1[5]>59)
- time_buf1[5]=0;
- Ds1302_Write_Time(); break;
-
- case 1 : time_buf1[6]++; //秒
- if(time_buf1[6]>59)
- time_buf1[6]=0;
- Ds1302_Write_Time(); break;
- }
- }
- }
-
- else if(k3==0)
- {
- if(flg==0)
- {
- flg=1;
- switch(set)
- {
- case 9 : clockh--; //clockh
- if(clockh>100)
- clockh=23;
- if(clockh<0)
- clockh=23;
- break;
- case 8 : clockm--; //clockm
- if(clockm>100)
- clockm=59;
- if(clockm<0)
- clockm=59;
- break;
-
- case 7 : time_buf1[1]--; //年
- if(time_buf1[1]>100)
- time_buf1[1]=99;
- if(time_buf1[1]<0)
- time_buf1[1]=99;
- Ds1302_Write_Time(); break;
-
- case 6 : time_buf1[2]--; //月
- if(time_buf1[2]>100)
- time_buf1[2]=12;
- if(time_buf1[2] < 1)
- time_buf1[2] = 12;
- Ds1302_Write_Time(); break;
- case 5 : time_buf1[3]--; //日
- if(time_buf1[3]>100)
- time_buf1[3]=31;
- if(time_buf1[3]<1)
- time_buf1[3]=31;
- Ds1302_Write_Time(); break;
- case 4 : time_buf1[7]--; //周
- if(time_buf1[7]>100)
- time_buf1[7]=7;
- if(time_buf1[7]<1)
- time_buf1[7]=1;
- Ds1302_Write_Time(); break;
-
- case 3 : time_buf1[4]--; //時
- if(time_buf1[4]>100)
- time_buf1[4]=23;
- if(time_buf1[4]<0)
- time_buf1[4]=23;
- Ds1302_Write_Time(); break;
-
- case 2 : time_buf1[5]--; //分
- if(time_buf1[5]>100)
- time_buf1[5]=59;
- if(time_buf1[5]<0)
- time_buf1[5]=59;
- Ds1302_Write_Time(); break;
-
- case 1 : time_buf1[6]--; //秒
- if(time_buf1[6]>100)
- time_buf1[6]=59;
- if(time_buf1[6]<0)
- time_buf1[6]=59;
- Ds1302_Write_Time(); break;
- }
- }
- }
- else if(k4 == 0)
- {
- if(flg == 0)
- {
- flg =1;
- anflg = anflg*0;
- j++;
- if(j == 1)
- set = 6;
- else if(j == 2)
- set = 7;
- else if(j == 3)
- set = 9;
- else
- set = 0;
-
- if(j > 4)
- j = 0;
- }
- }
- else
- flg=0;
- }
- void ShowTwinkle()//狀態顯示函數
- {
- if(set==0)
- {
-
- buf[0]=time_buf1[4]/10;
- buf[1]=time_buf1[4]%10;
-
-
-
- buf[2]=time_buf1[5]/10;
- buf[3]=time_buf1[5]%10;
-
-
-
- buf[4]=time_buf1[6]/10;
- buf[5]=time_buf1[6]%10;
-
- if(js<500)
- {
- m1 = 0;
- m2 = 0;
- }
- if(js>500)
- {
- m1 = 1;
- m2 = 1;
- }
- }
-
- if(set==1)
- {
- buf[0]=time_buf1[4]/10;
- buf[1]=time_buf1[4]%10;
-
- buf[2]=time_buf1[5]/10;
- buf[3]=time_buf1[5]%10;
-
- if(anflg == 1)
- {
- if(js<500)
- {
- buf[4]=time_buf1[6]/10;
- buf[5]=time_buf1[6]%10;
-
- m1 = 0;m2 = 0;
- }
- if(js>500)
- {
- buf[4]=10;buf[5]=10;
- m1 = 1;m2 = 1;
- }
- }
- else
- {
- buf[4]=time_buf1[6]/10;
- buf[5]=time_buf1[6]%10;
-
- m1 = 0;m2 = 0;
- }
- }
- if(set==2)
- {
- buf[0]=time_buf1[4]/10;
- buf[1]=time_buf1[4]%10;
- buf[4]=time_buf1[6]/10;
- buf[5]=time_buf1[6]%10;
-
- if(anflg == 1)
- {
- if(js<500)
- {
- buf[2]=time_buf1[5]/10;
- buf[3]=time_buf1[5]%10;
-
- m1 = 0;m2 = 0;
- }
- if(js>500)
- {
- buf[2]=10;buf[3]=10;
- m1 = 1;m2 = 1;
- }
- }
- else
- {
- buf[2]=time_buf1[5]/10;
- buf[3]=time_buf1[5]%10;
-
- m1 = 0;m2 = 0;
- }
- }
-
- if(set==3)
- {
- buf[2]=time_buf1[5]/10;
- buf[3]=time_buf1[5]%10;
-
- buf[4]=time_buf1[6]/10;
- buf[5]=time_buf1[6]%10;
-
- if(anflg == 1)
- {
- if(js<500)
- {
- buf[0]=time_buf1[4]/10;
- buf[1]=time_buf1[4]%10;
-
- m1 = 0;m2 = 0;
- }
- if(js>500)
- {
- buf[0]=10;buf[1]=10;
-
- m1 = 1;m2 = 1;
- }
- }
- else
- {
- buf[0]=time_buf1[4]/10;
- buf[1]=time_buf1[4]%10;
-
- m1 = 0;m2 = 0;
- }
- }
- if(set==4)
- {
- buf[0]=time_buf1[2]/10;
- buf[1]=time_buf1[2]%10;
-
- buf[2]=time_buf1[3]/10;
- buf[3]=time_buf1[3]%10;
-
- buf[4]=10;
-
- if(anflg == 1)
- {
- if(js<500)
- {
- buf[5]=time_buf1[7];
- m1 = 0;m2 = 0;
- }
- if(js>500)
- {
- buf[5]=10;
- m1 = 1;m2 = 1;
- }
- }
- else
- {
- buf[5]=time_buf1[7];
- m1 = 0;m2 = 0;
- }
- }
- if(set==5)
- {
- buf[0]=time_buf1[2]/10;
- buf[1]=time_buf1[2]%10;
- buf[4]=10;
- buf[5]=time_buf1[7];
-
- if(anflg == 1)
- {
- if(js<500)
- {
-
- buf[2]=time_buf1[3]/10;
- buf[3]=time_buf1[3]%10;
-
- m1 = 0;m2 = 0;
- }
- if(js>500)
- {
- buf[2]=10;buf[3]=10;
- m1 = 1;m2 = 1;
- }
- }
- else
- {
- buf[2]=time_buf1[3]/10;
- buf[3]=time_buf1[3]%10;
-
- m1 = 0;m2 = 0;
- }
- }
-
- if(set==6)
- {
- buf[2]=time_buf1[3]/10;
- buf[3]=time_buf1[3]%10;
-
- buf[4]=10;
- buf[5]=time_buf1[7];
-
- if(anflg == 1)
- {
- if(js<500)
- {
- buf[0]=time_buf1[2]/10;
- buf[1]=time_buf1[2]%10;
-
- m1 = 0;m2 = 0;
- }
- if(js>500)
- {
- buf[0]=10;
- buf[1]=10;
-
- m1 = 1;m2 = 1;
- }
- }
- else
- {
- buf[0]=time_buf1[2]/10;
- buf[1]=time_buf1[2]%10;
-
- m1 = 0;m2 = 0;
- }
- }
-
- if(set==7)
- {
- buf[0]=2;
- buf[1]=0;
- buf[4]=10;buf[5]=10;
-
- if(anflg == 1)
- {
- if(js<500)
- {
- buf[2]=time_buf1[1]/10;
- buf[3]=time_buf1[1]%10;
-
- m1 = 0;m2 = 0;
- }
-
- if(js>500)
- {
- buf[2]=10;buf[3]=10;
- m1 = 1;m2 = 1;
- }
- }
- else
- {
- buf[2]=time_buf1[1]/10;
- buf[3]=time_buf1[1]%10;
-
- m1 = 0;m2 = 0;
- }
- }
-
- if(set==8)
- {
- buf[0]=clockh/10;
- buf[1]=clockh%10;
- buf[4]=clocks/10;
- buf[5]=clocks%10;
-
- if(anflg == 1)
- {
- if(js<500)
- {
- buf[2]=clockm/10;
- buf[3]=clockm%10;
- m1 = 0;m2 = 0;
- }
- if(js>500)
- {
- buf[2]=10;buf[3]=10;
-
- m1 = 1;m2 = 1;
- }
- }
- else
- {
- buf[2]=clockm/10;
- buf[3]=clockm%10;
-
- m1 = 0;m2 = 0;
- }
- }
-
- if(set==9)
- {
- buf[2]=clockm/10;
- buf[3]=clockm%10;
-
- buf[4]=clocks/10;
- buf[5]=clocks%10;
-
- if(anflg == 1)
- {
- if(js<500)
- {
- buf[0]=clockh/10;
- buf[1]=clockh%10;
-
- m1 = 0;m2 = 0;
- }
- if(js>500)
- {
- buf[0]=10;buf[1]=10;
-
- m1 = 1;m2 = 1;
- }
- }
- else
- {
- buf[0]=clockh/10;
- buf[1]=clockh%10;
-
- m1 = 0;m2 = 0;
- }
- }
- }
復制代碼 高端版時鐘DS1302+AT24C02 6位數碼管顯示,功能齊全,可進此網站鏈接下載仿真加源代碼 http://www.zg4o1577.cn/bbs/dpj-49727-1.html
|