RTC實時時鐘DS1302液晶顯示 ds18b20
0.png (78.13 KB, 下載次數: 89)
下載附件
2017-5-12 16:28 上傳
單片機源程序如下:
- /*-----------------------------------------------
- 名稱:LCD1602顯示實時時鐘
- 修改:無
- 內容:音樂鬧鐘 普通鬧鐘已被注釋 音樂鬧鐘無法停止 自動播放后停止 (普通鬧鐘功能正常)
- error:無
- warning:四個 函數未被調用
- -----------------------------------------------*/
- #include<reg52.h>
- #include <String.h>
- #include"ds1302.h"
- #include"LCD1602.h"
- #include"key.h"
- #include "18B20.h"
- #include "240c02.h"
- sbit buzeer = P2^3; //普通鬧鐘
- sbit led1 =P2^4; //指示燈
- sbit led2 =P2^5;
- sbit led3 =P2^6;
-
- sbit fm=P2^3; //音樂鬧鐘 接口相同
- unsigned char timeh,timel,music;// 鬧鐘變量
- unsigned char code zbjbxf[]={//
- 0x64,0xA3,0xC1,0xA2,0x62,0x84,0x61,0x81,0x61,0xA4,0xA1,
- 0x91,0xA1,0x81,0x64,0xA3,0xC1,0xD2,0xD2,0xD2,0xA2,0xC4,
- 0xA1,0xC1,0xA1,0xC1,0xD2,0xD2,0xD2,0xA2,0xC4,0xC2,0x62,
- 0xC2,0x62,0xA2,0xA2,0x84,0x94,0x94,0x92,0x81,0x91,
- 0xA2,0xC2,0xD4,0xE4,0xA4,0xE4,0xA2,0xE2,0xA2,0xE2,0xA2,
- 0xA2,0x84,0x94,0x94,0x92,0x81,0x91,0xA2,0xC2,0xD8};
- uchar code chuzhi[]={
- 0xff,0xff,//
- 0xFC,0x8E,//L1-7
- 0xFC,0xED,
- 0xFD,0x43,
- 0xFD,0x6A,
- 0xFD,0xB3,
- 0xFD,0xF3,
- 0xFE,0x2D,
- 0xFE,0x47,//H1-7
- 0xFE,0x76,
- 0xFE,0xA1,
- 0xFE,0xC7,
- 0xFE,0xD9,
- 0xFE,0xF9,
- 0xFF,0x16
- };
- void timer0() interrupt 1 //音樂鬧鐘中斷
- {
- TH0=timeh;
- TL0=timel;
- fm=~fm;
- }
- void delay(uint z) //??165MS,??????
- { uint y;
- for(z;z>0;z--)
- for(y=19000;y>0;y--);//????
- }
- void delay1(uint z) //??1MS
- { uint y;
- for(z;z>0;z--)
- for(y=112;y>0;y--);//????
- }
- //sbit bell = P2^3;
- char key;
- char keyflg;
- //時鐘變量
- unsigned char times[7]={1};
- unsigned char time[3];
- unsigned char PPP;
- unsigned char clock_buf[7];
- //鬧鐘變量
- unsigned char counter;
- unsigned char baifenmiao;
- unsigned char shifenmiao;
- unsigned char miao;
- unsigned char shimiao;
- unsigned char baimiao;
- void key_timeset(void);
- void dateTrans(void);
- void alarm_aclok(void);
- void arm(void);
- void timer_run(void);
- void timer_runback(void);
- void clock_display(void);
- void clock_delay(void);
- void start(void);
- void delay_ms(unsigned int z);
- void ds18b20(void);
- void ds18b20_xiugai(void);
- void xieshuju(void);
- void dushuju(void);
-
- void buzeer_music() //音樂鬧鐘
- {
- uint temp; //??????????????????
- uchar i=0;
- uchar jp; //jp????temp???8???8?
- TMOD=0x01;//?????T0?????1
- IE=0x82; //??T0??
- while(music)
- {
- /* ds1302_read_time();//讀取時鐘信息
-
- dateTrans(); //數據復制
- disp();*/
- key = Key4Scan();
- if(key==4){cflg=1; keyflg=0;music=0;}
- if(key==6){ keyflg=0;}
- clock_delay();
-
-
- temp=zbjbxf[i];
- if(temp==0xff)
- break;
- jp=temp/16; //????4?,????
- if(jp!=0)
- {
- timeh=chuzhi[jp*2];//????????8?
- timel=chuzhi[jp*2+1]; //????????8?
- TR0=1; //????
- }
- else
- {
- TR0=0; //????
- fm=1; //????
- }
- delay(temp%16); //????4?,??(?????)
- TR0=0; //??????5MS
- fm=1;
- delay1(5);
- //TR0=1;
- i++;
- }
- /*TR0=0; //????
- fm=1; //????*/ //JJX
- }
- void Time1_Int(){ //用于定時 倒計時
- TMOD=0x10;
- TH1=0xfc; //10毫秒
- TL1=0x66;
- EA=1;
- ET1=1;
- TR1=1;
- }
- void main()
- {
- //buzeer=0; //鬧鐘停
- buzeer=1; // 接非門后鬧鐘停
- // dushuju(); //e2prom 讀數據 有問題
- ds1302_init(); /*DS302初始化函數*/
-
- ds1302_write_time();
- ds1302_read_time();
- // tmpchange();
- //Time0_Int();
- InitLcd(); //初始化LCD
- DelayMs(15); //延時用于穩定功能
- Time1_Int();
- start(); //開機顯示
-
- while(1)
- {
- key = Key4Scan();
- if(!cflag){
-
- if(key==4) { buzeer= 1;/*buzeer= 0;*/} //按鍵停止鬧鐘
- // while(1); //調試
- alarm_aclok(); //鬧鐘
-
- ds1302_read_time();//讀取時鐘信息
-
- dateTrans(); //數據復制
- disp(); //液晶顯示時間信息
- timer_run(); //秒表
- timer_runback(); //倒計時
- clock_display(); //時鐘顯示
- if(key==7) keyflg=7;
- ds18b20(); //按鍵顯示
- // ds18b20_xiugai(); // 顯示位置修改
- if(key==4) keyflg=0;
- if(key==5) keyflg=5;
- if(key==6) keyflg=6;
-
-
- clock_delay(); //瞌睡
- xieshuju(); // ——>到e2prom
- // WriteCommand(0x80+0x40+15); //按鍵調試信息
- // WriteData(shuzi[key]);
- }
- key_timeset(); //案件時間設定
- }
-
- }
- void key_timeset(void){
- char i;
- switch(key) //Key4Scan()
- {
- case 0x01:
- switch(PP++) //??????
- {
- case 0: WriteCommand(0x80+0x40+7);break; //秒
- case 1: WriteCommand(0x80+3);break; //年
- case 2: WriteCommand(0x80+6);break; //月
- case 3: WriteCommand(0x80+9);break; //日
- case 4: WriteCommand(0x80+15);break; //周
- case 5: WriteCommand(0x80+0x40+1);break; //時
- case 6: WriteCommand(0x80+0x40+4);break; //分
- default:break;
- }
- WriteCommand(0x0f);//光標閃爍
- if(PP == 8) PP = 0; //7還是 8好像和編譯器有關系
-
- cflag = 1; //????
-
- break;
- case 0x02:
- if(cflag)
- {
- switch(PP-1)
- {
- case 1: // 年
- times[6]++;
- if((times[6]&0x0f) == 0x0a) // 0000 1111 1010 轉變為16進制
- {
- times[6] += 0x06;
- }
- if(times[6] > 0x99)
- {
- times[6] = 0x00;
- }
- WriteCommand(0x80+2);
- WriteData(shuzi[times[6]>>4]);
- WriteData(shuzi[times[6]&0x0f]);
- WriteCommand(0x80+3);
- break;
- case 2: //月
- times[4]++;
- if((times[4]&0x0f) == 0x0a)
- {
- times[4] += 0x06;
- }
- if(times[4] > 0x12)
- {
- times[4] = 0x01;
- }
- WriteCommand(0x80+5);
- WriteData(shuzi[times[4]>>4]);
- WriteData(shuzi[times[4]&0x0f]);
- WriteCommand(0x80+6);
- break;
- case 3: //
- times[3]++;
- if((times[3]&0x0f) == 0x0a)
- {
- times[3] += 0x06;
- }
- if(times[3] > 0x31)
- {
- times[3] = 0x01;
- }
- times[5]++;
- if((times[5]&0x0f) == 0x08)
- {
- times[5] = 0x01;
- }
- WriteCommand(0x80+8);
- WriteData(shuzi[times[3]>>4]);
- WriteData(shuzi[times[3]&0x0f]);
- WriteCommand(0x80+9);
-
- break;
- case 4: //?
- times[5]++;
- if((times[5]&0x0f) == 0x08)
- {
- times[5] = 0x01;
- }
- WriteCommand(0x80+15);
- WriteData(shuzi[times[5]]);
- WriteCommand(0x80+15);
-
- break;
- case 5: //?
- times[2]++;
- if((times[2]&0x0f) == 0x0a)
- {
- times[2] += 0x06;
- }
- if(times[2] > 0x23)
- {
- times[2] = 0x00;
- }
- WriteCommand(0x80+0x40+0);
- WriteData(shuzi[times[2]>>4]);
- WriteData(shuzi[times[2]&0x0f]);
- WriteCommand(0x80+0x40+1);
-
- break;
- case 6: //?
- times[1]++;
- if((times[1]&0x0f) == 0x0a)
- {
- times[1] += 0x06;
- }
- if(times[1] > 0x59)
- {
- times[1] = 0x00;
- }
- WriteCommand(0x80+0x40+3);
- WriteData(shuzi[times[1]>>4]);
- WriteData(shuzi[times[1]&0x0f]);
- WriteCommand(0x80+0x40+4);
-
- break;
- case 0: //?
- times[0]++;
- if((times[0]&0x0f) == 0x0a)
- {
- times[0] += 0x06;
- }
- if(times[0] > 0x59)
- {
- times[0] = 0x00;
- }
- WriteCommand(0x80+0x40+6);
- WriteData(shuzi[times[0]>>4]);
- WriteData(shuzi[times[0]&0x0f]);
- WriteCommand(0x80+0x40+7);
-
- break;
- default:
- break;
-
- }
- }
- break;
- case 0x03:
- if(cflag)
- {
- cflag = 0;
- PP = 0;
- WriteCommand(0X0C); //取消 并關光標
- }
- break;
- case 0x04: //保存數據到 DS1302
- if(cflag)
- {
- cflag = 0;
- PP = 0;
- WriteCommand(0X0C); //關光標
- ds1302_write_byte(ds1302_control_add,0x00); //關閉寫保護
- ds1302_write_byte(ds1302_sec_add,0x80); //暫停
- ds1302_write_byte(ds1302_charger_add,0xa9); //涓流充電
-
- ds1302_write_byte(ds1302_year_add,times[6]); //年
- ds1302_write_byte(ds1302_month_add,times[4]); //月
- ds1302_write_byte(ds1302_day_add,times[5]); //周
- ds1302_write_byte(ds1302_date_add,times[3]); //日
-
- ds1302_write_byte(ds1302_hr_add,times[2]); //時
- ds1302_write_byte(ds1302_min_add,times[1]); //分
- ds1302_write_byte(ds1302_sec_add,times[0]); //秒
- ds1302_write_byte(ds1302_control_add,0x80); //打開寫保護
-
- // Set_DS1302(times); //DS1302 寫入時間
- }
- break;
- case 0x05: //保存數據到鬧鐘
- if(cflag)
- {
- cflag = 0;
- PP = 0;
- WriteCommand(0X0C); //關光標
- for(i=0;i<7;i++){
- clock_buf[i]=times[i];
- }
- // strcpy(clock_buf,times);
- }
- break;
- default:
- break;
- }
- }
- void dateTrans(void){
-
- times[6]=time_buf[1];
- times[5]=time_buf[7];
-
- times[4]=time_buf[2];
- times[3]=time_buf[3];
- times[2]=time_buf[4];
- times[1]=time_buf[5];
- times[0]=time_buf[6];
- }
- void alarm_aclok(void)
- {
-
- if((times[2]==clock_buf[2])&&(times[1]==clock_buf[1])&&(times[0]==0)) {/*buzeer= 0;*/ music=1;buzeer_music();/*可替換為普通鬧鐘*/} //1111 0111 P2 & 0xf7
- //else { buzeer= 1;}
- }
-
- void T1_time() interrupt 3 {
- TH1=0xfc;
- TL1=0x66;
- counter++;
- if(counter == 10) { counter = 0; baifenmiao++;}
- if(baifenmiao == 10) {baifenmiao = 0; shifenmiao++;}
- if(shifenmiao == 10) {shifenmiao = 0;led1=~led1; miao++;}
- if(miao == 10) { miao = 0; shimiao++;}
- if(shimiao == 10) { shimiao = 0; baimiao++;}
- if(baimiao == 10) baimiao =0;
-
- }
- void timer_run(void)
- {
- if(key==2) keyflg=2; //計時功能
- while(key==2)
- {
- WriteCommand(0x01); //顯示清屏
- while(keyflg==2){
- key = Key4Scan();
- if(key==4) keyflg=0;
- WriteCommand(0x80+5); //計時功能
- WriteData(shuzi[baimiao]);
- WriteData(shuzi[shimiao]);
- WriteData(shuzi[miao]);
- WriteData('.');
- WriteData(shuzi[shifenmiao]);
- WriteData(shuzi[baifenmiao]);
- if(key==2) TR1=~TR1;
- if(key==3) {shifenmiao=0;baifenmiao=0;baimiao=0;shimiao=0;miao=0;TR0=0;}
- }
- WriteCommand(0x01); //顯示清屏
- }
- }
- void timer_runback(void){
- if(key==3) keyflg=3; //倒計時功能
- while(key==3)
- {
- WriteCommand(0x01); //顯示清屏
- WriteCommand(0x80+5); //顯示清屏 k1 功能選擇 k2 定是數據選擇 k3數據保存
- WriteData('0');
- WriteData('0');
- WriteData('0');
- /* WriteData(':');
- WriteData('0');
- WriteData('0');*/
- while(keyflg==3)
- {
- key = Key4Scan();
- if(key==4) keyflg=0;
-
- switch(key) //Key4Scan()
- {
- case 0x01:
- switch(PPP++) //??????
- {
- case 0: WriteCommand(0x80+5);break; //分鐘
- case 1: WriteCommand(0x80+6);break; //
- case 2: WriteCommand(0x80+7);break; //
- /*case 3: WriteCommand(0x80+9);break; // 秒
- case 4: WriteCommand(0x80+10);break; //*/
- default:break;
- }
- WriteCommand(0x0f);//光標閃爍
- if(PPP == 4){ PPP = 0; time[0]=time[1]=time[2] =0; } //7還是 8好像和編譯器有關系
-
- cflg = 1; //標志位
-
- break;
- case 0x02:
- if(cflg)
- {
- switch(PPP-1)
- {
- case 0: // 百秒
- time[0]++;
-
- if(time[0] > 9)
- {
- time[0] = 0;
- }
- WriteCommand(0x80+5);
- WriteData(shuzi[time[0]]);
- WriteCommand(0x80+5);
- break;
- case 1: //十秒
- time[1]++;
-
- if(time[1] > 9)
- {
- time[1] = 0;
- }
- WriteCommand(0x80+6);
- WriteData(shuzi[time[1]]);
- WriteCommand(0x80+6);
- break;
- case 2: //秒
- time[2]++;
- if(time[2] > 9)
- {
- time[2] = 0;
- }
- WriteCommand(0x80+7);
- WriteData(shuzi[time[2]]);;
- WriteCommand(0x80+7);
-
- break;
-
-
- default:
- break;
-
- }
- }
- break;
- case 0x03:
- if(cflg)
- {
- cflg = 0;
- PPP = 0;
- WriteCommand(0X0C); //取消 并關光標
- miao=9-time[2];
- shimiao=9-time[1];
- baimiao=9-time[0];
-
- }
- break;
-
- /* case 0x05: //保存數據到鬧鐘
- if(cflag)
- {
- cflag = 0;
- PP = 0;
- WriteCommand(0X0C); //關光標
- }
- break; */
- default:
- break;
- }
- while(!cflg)
- {
-
- WriteCommand(0X80+5); //取消 并關光標
- WriteData(shuzi[9-baimiao]);
- WriteData(shuzi[9-shimiao]);
- WriteData(shuzi[9-miao]);
- key = Key4Scan();
- if(key==4){cflg=1; keyflg=0; }
- while((9-baimiao)==0&&(9-shimiao==0)&&(9-miao)==0)
- {
- while(keyflg){
- led3=~led3;
- DelayMs(3500);
- key = Key4Scan();
- WriteCommand(0X80+7);
- WriteData('0');
- if(key==4){cflg=1; keyflg=0; }
- }
- }
- }
- }
- WriteCommand(0X01);
- }
- }
- void clock_display(void)
- {
- if(key==5)
- {
- WriteCommand(0x01);
- WriteCommand(0x80+5);
- WriteData(shuzi[clock_buf[2]/16]);
- WriteData(shuzi[clock_buf[2]%16]);
- WriteData(':');
- WriteData(shuzi[clock_buf[1]/16]);
- WriteData(shuzi[clock_buf[1]%16]);
- delay_ms(2000);
-
-
-
- }
- }
- void clock_delay(void)
- {
-
- if(key==6)
- {
- miao=0;
- shimiao=0;
- TR0=0;
- buzeer=1;
- }
- if(keyflg==6)
- {
- if(miao==9)
- {
-
- //buzeer=0;
- TR0=1;
- buzeer_music();
- }
- }
- }
- void start(void)
- {
-
-
- WriteCommand(0x80+3);
-
- WriteData('3');
- WriteData('-');
- WriteData('0');
- WriteData('5');
- WriteData('-');
- WriteData('0');
- WriteData('6');
-
- led2=1;
- delay_ms(1000);
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
RTC實時時鐘DS1302液晶顯示.rar
(96.15 KB, 下載次數: 24)
2017-5-12 13:59 上傳
點擊文件名下載附件
RTC實時時鐘DS1302液晶顯示 下載積分: 黑幣 -5
|