本帖最后由 xiashan 于 2015-4-5 16:14 编辑
1.bell.c- #include "main.h"
- // 原始频率表,低音。
- uint code FreTab[12]={262,277,293,311,329,349,370,392,415,440,466,494};
- // 1-7.
- uchar code XY_Tab[7]={0,2,4,5,7,9,11};
- uint New_FreTab[12];
- void Delay_10MS()
- {
- uchar a,b;
- for(a = 50;a>0;a--)
- {
- for(b = 100;b>0;b--);
- }
- }
- uchar YC_Tab[]={1,2,4,8,16,32,64};
- void Beep(uchar num)
- {
- uint i,j;
- uchar D=60,E=20;
- switch(num)
- {
- case 1: //开机响铃 ·
- j=0;while(j<300){BEE=!BEE;for(i=0;i<D;i++){;}j++;};
- break;
- case 2: //简单闹铃 ·
- j=0;while(j<400){BEE=!BEE;for(i=0;i<E;i++){;}j++;}for(i=0;i<18;i++)Delay_10MS();
- j=0;while(j<400){BEE=!BEE;for(i=0;i<E;i++){;}j++;}for(i=0;i<18;i++)Delay_10MS();
- //j=0;while(j<400){BEE=!BEE;for(i=0;i<E;i++){;}j++;}for(i=0;i<12;i++)Delay_10MS();
- j=0;while(j<500){BEE=!BEE;for(i=0;i<E;i++){;}j++;}
- break;
- case 3: //操作按键响铃 ·
- j=0;while(j<200){BEE=!BEE;for(i=0;i<30;i++){;}j++;};
- break;
- case 4: //整点报时响铃 —
- j=0;while(j<2800){BEE=!BEE;for(i=0;i<D;i++){;}j++;}
- break;
- }
- BEE=1;
- }
- // QSD 0-11
- // SYD 1-3 1降8度 2不变,3升8度
- void JX_New_Few_FerTab(uchar QSD)
- {
- uchar i,j;
- for(i = 0 ;i <12;i++) // 12次循环
- {
- j = i +QSD; //根据起始调判断起始音符位置
- if(j >11) //如果起始调不是最低
- {
- j = j - 12; // 0-11
- New_FreTab[i] = FreTab[j]*2;// 频率*2
- }
- else //频率不变
- {
- New_FreTab[i] = FreTab[j];
- }
- /*if(SYD == 1) //判断升降调。1降调,2不变,3升调。
- {
- New_FreTab[i] >>= 1;//频率减一半
- }
- else
- {
- if(SYD == 3)
- {
- New_FreTab[i] <<= 1;
- }
- }*/
- }
- }
- void Play_Music(uchar *Sound,uchar QSD)
- {
- uint GQ_CD = 0; //歌曲长度
- uint GQ_WZ = 0;
- uchar GQ_YG = 0;
- uchar GQ_YC = 0;
- uint YP_PL = 0;
- uchar YG_Bai = 0;
- uchar YG_Shi = 0;
- uchar YG_Ge = 0;
- uchar YC_Bai = 0;
- uchar YC_Shi = 0;
- uchar YC_Ge = 0;
- uint DSD_PL = 0;
- uint YF0 = 0; // 1分音符变量
- uint YF1 = 0; // 发音长度时间
- uint YF2 = 0; //不发音长度时间
- uint YF4 = 0; // 4分音符时间
- uint FY_JG = 0;
- uint YZ_TC = 0;
- uint JF_YF = 0;
- uchar i =0;
-
- JX_New_Few_FerTab(QSD);
-
- while(Sound[GQ_CD] != 0) //判断歌曲长度
- {
- GQ_CD += 2; //指向下一位音符数据
- }
- while(GQ_WZ<GQ_CD && Al_Flag==1)
- {
- GQ_YG = Sound[GQ_WZ];
- YG_Bai = GQ_YG/100; // 1升半音,0不升
- YG_Shi = GQ_YG/10%10; // 1低音,2为中音,3为高音
- YG_Ge = GQ_YG%10; // 1-7 音符位;
- YP_PL = New_FreTab[XY_Tab[YG_Ge -1] + YG_Bai];// yg_ge -1 1-7 0-6
- if(YG_Ge != 0)
- {
- switch(YG_Shi)
- {
- case 2: YP_PL <<=1;break;
- case 3: YP_PL <<=2;break;
- }
- DSD_PL = 65536-(50000/YP_PL)*10;
- Time_H = DSD_PL >>8;
- Time_L = DSD_PL & 0xFF;
- TH0 = Time_H;
- TL0 = Time_L;
- }
- GQ_YC = Sound[GQ_WZ +1];
- YC_Bai = GQ_YC/100; // 1有符点,0无符点
- YC_Shi = GQ_YC/10%10; // 演奏效果0普通,1连音,2顿音
- YC_Ge = GQ_YC%10; // 几分音符。
- YF0 = YF_YF;
- YF4 = YF0 /4;
- FY_JG =YF4- YF4 *SFYF_JG;
- JF_YF = YC_Tab[YC_Ge];// 几分音符;
- YZ_TC = YF0 /JF_YF; // 算出对应延时长度多少个10MS
-
- if(YC_Bai == 1) //判断是否有符点
- {
- YZ_TC = YZ_TC+YZ_TC/2;//有符点
- }
- if(YC_Shi != 1) //不是连音
- {
- if(YC_Shi == 0) //是否为普通音
- {
- if(JF_YF <= 4) // 是否小于16分音
- {
- YF1 = YZ_TC -FY_JG; // 发音有间隔
- }
- else // 不小于16分音
- {
- YF1 = YZ_TC * SFYF_JG;//发音无音隔
- }
- }
- else //是顿音,演奏时间为原来的一半
- {
- YF1 = YZ_TC /2;
- }
- }
- else //是连音,发音无音隔
- {
- YF1 = YZ_TC;
- }
- if(YG_Ge == 0) //没有音符数据
- {
- YF1 = 0; //演奏时间 = 0
- }
- else //有音符数据
- {
- YF2 = YZ_TC - YF1;
- TR0 =1;
- for(i = YF1; i >0;i --)
- {
- Delay_10MS();
- }
- BEE = 1;
- TR0 = 0;
- for(i = YF2; i >0;i --)
- {
- Delay_10MS();
- }
- }
- GQ_WZ += 2; //指向下一个音符
-
- }
- }
复制代码
2.bell.h
- #ifndef __BELL_H__
- #define __BELL_H__
- #define uchar unsigned char
- #define uint unsigned int
- #define YF_YF 160
- #define SFYF_JG 4/5
- sbit BEE = P3 ^ 4;
- void Beep(uchar num);
- void Delay_10MS(void);
- void JX_New_Few_FerTab(uchar QSD);
- void Play_Music(uchar *Sound,uchar QSD);
- #endif
复制代码
3.DS18B20.C
4.DS18B20.H
- #ifndef __DS18B20_H__
- #define __DS18B20_H__
- //DS18B20管脚定义
- sbit DQ = P1^0;
- extern uchar temp_I;
- extern bit fg;
- extern bit Init_DS18B20();
- void Write_18B20();
- extern bit ReadTemp();
- #endif#ifndef __DS18B20_H__
- #define __DS18B20_H__
- //DS18B20管脚定义
- sbit DQ = P1^0;
- extern uchar temp_I;
- extern bit fg;
- extern bit Init_DS18B20();
- void Write_18B20();
- extern bit ReadTemp();
- #endif
复制代码
5.DS3231.C
- #include "DS3231.H"
- /************************* Global Variables ***************************/
- xdata uchar sec, min, hour, day, date, month, year;
- xdata uchar Dtemp;
- /**************************** functions ******************************/
- void delay()
- {
- ;;
- }
- void start() /* --------- Initiate start condition ---------- */
- {
- sda = 1; delay();
- scl = 1; delay();
- sda = 0; delay();
- scl = 0;
- }
- void stop() /* ---------- Initiate stop condition ----------- */
- {
- sda = 0; delay();
- scl = 1; delay();
- sda = 1; delay();
- scl = 0;
- }
- void i2cack()
- {
- uchar i=0;
- scl=1;delay();
- while(sda==1 && i<255) i++;
- scl=0;delay();
- }
- void i2cwrite(uchar d) /* ----------------------------- */
- {
- uchar i;
- scl = 0;
- for (i = 0;i < 8; i++)
- {
- sda = d & 0x80; /* Send the msbits first */
- scl = 0;delay();
- scl = 1;delay();
- d = d << 1; /* do shift here to increase scl high time */
- scl = 0;
- }
- sda = 1;delay(); /* Release the sda line */
- scl = 0;delay();
- }
- uchar i2cread(uchar b) /* ----------------------------------- */
- {
- uchar i, d;
- d = 0;
- sda = 1;delay(); /* Let go of sda line */
- scl = 0;delay();
- for (i = 0; i < 8; i++) /* read the msb first */
- {
- scl = 1;delay();
- d = d << 1;
- d = d | (uchar)sda;
- scl = 0;delay();
- }
- sda = b;delay(); /* low for ack, high for nack */
- scl = 1;delay();
- scl = 0;delay();
- sda = 1;delay(); /* Release the sda line */
- return d;
- }
- void i2cwrite_add(uchar address,uchar dat)//指定地址写一个字节数据
- {
- start();
- i2cwrite(ADDRTC);
- i2cack();
- i2cwrite(address);
- i2cack();
- i2cwrite(dat);
- i2cack();
- stop();
- }
- uchar i2cread_add(uchar address)
- //指定地址读一个字节数据
- {
- uchar dd;
- start();
- i2cwrite(ADDRTC);
- i2cack();
- i2cwrite(address);
- i2cack();
- start();
- i2cwrite(ADDRTC);
- i2cack();
- dd=i2cread(0);
- stop();
- return dd;
- }
- void InitDS3231() /* ----- set time & date; user data entry ------ */
- {
- scl=1;delay();
- sda=1;delay();
- start();
- i2cwrite(ADDRTC); /* write slave address, write 1339 */
- i2cwrite(0x00); /* write register address, 1st clock register */
- i2cwrite(0x00); //秒
- i2cwrite(0x01); //分
- i2cwrite(0x01); //时
- i2cwrite(0x03); //星期
- i2cwrite(0x12); //日
- i2cwrite(0x01); //月
- i2cwrite(0x11); //年
- //i2cwrite(0x10); /* enable sqw, 1hz output */
- stop();
- }
- void GetTime() /* --- get date/time from DS3231 --- */
- {
- //while(int0); /* loop until int pin goes low */
- //start();
- //i2cwrite(ADDRTC);
- //i2cwrite(0x0f);
- //i2cwrite(0); /* clear alarm flags */
- //stop();
- start();
- i2cwrite(ADDRTC);
- i2cwrite(0);
- start();
- i2cwrite(ADDRTC | 1);
- sec = i2cread(ACK);
- min = i2cread(ACK);
- hour= i2cread(ACK);
- day = i2cread(ACK);
- date= i2cread(ACK);
- month= i2cread(ACK);
- year= i2cread(NACK);
- stop();
- }
- void read_temp() /* -------- read temperature -------- */
- {
- int itemp;
- float ftemp;
- do
- {
- start();
- i2cwrite(ADDRTC);
- i2cwrite(0x0e); /* address of control register */
- start();
- i2cwrite(ADDRTC + 1); /* send the device address for read */
- itemp = i2cread(NACK); /* get the control register value */
- stop();
- } while(itemp & 0x20); /* wait until CNVT bit goes inactive */
- start();
- i2cwrite(ADDRTC);
- i2cwrite(0x11); /* address of temperature MSB */
- start();
- i2cwrite(ADDRTC + 1); /* send the device address for read */
- itemp = ( (int) i2cread(ACK) << 5 );
- itemp += ( i2cread(NACK) >> 3);
- stop();
- if(itemp & 0x1000) itemp += 0xe000; /* if sign bit set, make 16 bit 2's comp */
- ftemp = 0.03125 * (float) itemp; /* convert to degrees C */
- /* ftemp = ftemp * 9 / 5 + 32; /* skip this if you don't want degrees F */
- Dtemp = (uchar) ftemp;
- }
- /*void frq_out_tog() // --- toggle en32khz bit to enable/disable sqw ---
- {
- uchar val;
- start();
- i2cwrite(ADDRTC);
- i2cwrite(0x0f); // control/status reg address
- start();
- i2cwrite(ADDRTC + 1); //send the device address for read
- val = i2cread(NACK);
- stop();
- val ^= 0x08; //toggle en32khz bit
- start();
- i2cwrite(ADDRTC);
- i2cwrite(0x0f); //control/status reg address
- i2cwrite(val);
- stop();
- }*/
- void init_alrm() /* --- enable alarm 1 for once-per-second --- */
- {
- start();
- i2cwrite(ADDRTC);
- i2cwrite(7); /* 1st alarm 1 reg address */
- i2cwrite(0x80); /* mask alarm register */
- i2cwrite(0x80);
- i2cwrite(0x80);
- i2cwrite(0x80);
- stop();
- start();
- i2cwrite(ADDRTC);
- i2cwrite(0x0e); /* control/status reg address */
- i2cwrite(0x05); /* enable interrupts, alarm 1 output */
- }
- void comm_init() /* ------ reset DS3231 comm interface ------ */
- {
- do /* because the DS3231 I2C interface is active for both supplies */
- { /* after a micro reset, we must get the comm into a known state */
- sda = 1; /* make sure master has released SDA */
- scl = 1;
- if(sda) /* if sda is high, generate a start */
- {
- sda = 0; /* The DS3231 will recognize a valid start */
- sda = 1; /* condition anywhere in a I2C data transfer */
- }
- scl = 0;
- } while(sda == 0); /* if the DS3231 is holding sda low, try again */
- }
复制代码
6.DS3231.H
- #ifndef __DS3231_H__
- #define __DS3231_H__
- #include <reg52.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit scl = P1^7; /* I2C pin definitions */
- sbit sda = P1^6;
- sbit int0 = P3^2;
- /**************************** defines *******************************/
- #define ADDRTC 0xd0 /* DS3231 slave address (write) */
- #define ACK 0
- #define NACK 1
- extern xdata uchar sec, min, hour, day, date, month, year,Dtemp;
- /*********************** Function Prototypes **************************/
- void start();
- void stop();
- void i2cwrite(uchar d);
- uchar i2cread(uchar b);
- void writebyte();
- void Init_DS3231();
- //void frq_out_tog();
- void init_alrm();
- void comm_init();
- void GetTime(void);
- void SetTime(uchar yea,uchar mon,uchar da,uchar hou,uchar min,uchar sec);
- void i2cwrite_add(uchar address,uchar dat);
- uchar i2cread_add(uchar address);
- void InitDS3231();
- void read_temp();
- #endif
复制代码
7.KEY.C
- #include "MAIN.H"
- xdata uchar keyvalue,SetFlag=0;
- void Scan_Key()
- {
- uchar a=80;
- if(SET==0)
- {
- Delay5Ms();
- if(SET==0)
- {
- keyvalue=1;
- Beep(3);
- Disp_b=0; //开始返回菜单计时
- }
- while((SET==0)&&a)
- {
- --a;
- Delay5Ms();
- }
- }
- if(UP==0)
- {
- Delay5Ms();
- if(UP==0)
- {
- keyvalue=2;
- Beep(3);
- Disp_b=0;
- }
- while((UP==0)&&a)
- {
- --a;
- Delay5Ms();
- }
- }
- if(DOWN==0)
- {
- Delay5Ms();
- if(DOWN==0)
- {
- keyvalue=3;
- Beep(3);
- Disp_b=0;
- }
- while((DOWN==0)&&a)
- {
- --a;
- Delay5Ms();
- }
- }
- }
- void KeyProcess()//时钟设置程序
- {
- if(keyvalue==1)
- {
- //DisplayStr(0,0," Time Set ");
- // ----------------
- SetFlag++;
- if(SetFlag==14) {SetFlag=0;Disp_Time();}
- keyvalue=0;
- }
- switch(SetFlag)
- {
- case 1:
- {
- Readtime();
- DisplayStr(0,0," Adj Year ");
- // ----------------
- DisplayStr(0,1,"20");
- DisplayChar(2,1,Flash?(year/10+0x30):' ');
- DisplayChar(3,1,Flash?(year%10+0x30):' ');
- DisplayChar(4,1,0x00);
- DisplayChar(5,1,month/10+0x30);
- DisplayChar(6,1,month%10+0x30);
- DisplayChar(7,1,0x01);
- DisplayChar(8,1,date/10+0x30);
- DisplayChar(9,1,date%10+0x30);
- DisplayChar(10,1,0x02);
- DisplayChar(11,1,' ');
- switch(day)
- {
- case 1:DisplayStr(12,1,"Mon");break;
- case 2:DisplayStr(12,1,"Tue");break;
- case 3:DisplayStr(12,1,"Wed");break;
- case 4:DisplayStr(12,1,"Thu");break;
- case 5:DisplayStr(12,1,"Fri");break;
- case 6:DisplayStr(12,1,"Sat");break;
- case 7:DisplayStr(12,1,"Sun");break;
- default :DisplayStr(12,1,"ERR");break;
- }
- DisplayChar(15,1,' ');
- if(keyvalue==2&&SetFlag==1) //年+
- {
- Set(6,0);
- keyvalue=0;
- }
- if(keyvalue==3&&SetFlag==1) //年-
- {
- Set(6,1);
- keyvalue=0;
- }
- }
- break;
- case 2:
- {
- Readtime();
- DisplayStr(0,0," Adj Month ");
- // ----------------
- DisplayStr(0,1,"20");
- DisplayChar(2,1,year/10+0x30);
- DisplayChar(3,1,year%10+0x30);
- DisplayChar(4,1,0x00);
- DisplayChar(5,1,Flash?(month/10+0x30):' ');
- DisplayChar(6,1,Flash?(month%10+0x30):' ');
- DisplayChar(7,1,0x01);
- DisplayChar(8,1,date/10+0x30);
- DisplayChar(9,1,date%10+0x30);
- DisplayChar(10,1,0x02);
- DisplayChar(11,1,' ');
- switch(day)
- {
- case 1:DisplayStr(12,1,"Mon");break;
- case 2:DisplayStr(12,1,"Tue");break;
- case 3:DisplayStr(12,1,"Wed");break;
- case 4:DisplayStr(12,1,"Thu");break;
- case 5:DisplayStr(12,1,"Fri");break;
- case 6:DisplayStr(12,1,"Sat");break;
- case 7:DisplayStr(12,1,"Sun");break;
- default :DisplayStr(12,1,"ERR");break;
- }
- DisplayChar(15,1,' ');
- if(keyvalue==2&&SetFlag==2) //月+
- {
- Set(5,0);
- keyvalue=0;
- }
- if(keyvalue==3&&SetFlag==2) //月-
- {
- Set(5,1);
- keyvalue=0;
- }
- }
- break;
- case 3:
- {
- Readtime();
- DisplayStr(0,0," Adj Date ");
- // ----------------
- DisplayStr(0,1,"20");
- DisplayChar(2,1,year/10+0x30);
- DisplayChar(3,1,year%10+0x30);
- DisplayChar(4,1,0x00);
- DisplayChar(5,1,month/10+0x30);
- DisplayChar(6,1,month%10+0x30);
- DisplayChar(7,1,0x01);
- DisplayChar(8,1,Flash?(date/10+0x30):' ');
- DisplayChar(9,1,Flash?(date%10+0x30):' ');
- DisplayChar(10,1,0x02);
- DisplayChar(11,1,' ');
- switch(day)
- {
- case 1:DisplayStr(12,1,"Mon");break;
- case 2:DisplayStr(12,1,"Tue");break;
- case 3:DisplayStr(12,1,"Wed");break;
- case 4:DisplayStr(12,1,"Thu");break;
- case 5:DisplayStr(12,1,"Fri");break;
- case 6:DisplayStr(12,1,"Sat");break;
- case 7:DisplayStr(12,1,"Sun");break;
- default :DisplayStr(12,1,"ERR");break;
- }
- DisplayChar(15,1,' ');
- if(keyvalue==2&&SetFlag==3) //日+
- {
- Set(4,0);
- keyvalue=0;
- }
- if(keyvalue==3&&SetFlag==3) //日-
- {
- Set(4,1);
- keyvalue=0;
- }
- }
- break;
- case 4:
- {
- Readtime();
- DisplayStr(0,0," Adj Hour ");
- // ----------------
- DisplayChar(0,1,month/10+0x30);
- DisplayChar(1,1,month%10+0x30);
- DisplayChar(2,1,0x01);
- DisplayChar(3,1,date/10+0x30);
- DisplayChar(4,1,date%10+0x30);
- DisplayChar(5,1,0x02);
- DisplayChar(6,1,' ');
- DisplayChar(7,1,' ');
- DisplayChar(8,1,Flash?(hour/10+0x30):' ');
- DisplayChar(9,1,Flash?(hour%10+0x30):' ');
- DisplayChar(10,1,':');
- DisplayChar(11,1,min/10+0x30);
- DisplayChar(12,1,min%10+0x30);
- DisplayChar(13,1,':');
- DisplayChar(14,1,sec/10+0x30);
- DisplayChar(15,1,sec%10+0x30);
- if(keyvalue==2&&SetFlag==4) //时+
- {
- Set(2,0);
- keyvalue=0;
- }
- if(keyvalue==3&&SetFlag==4) //时-
- {
- Set(2,1);
- keyvalue=0;
- }
- }
- break;
- case 5:
- {
- Readtime();
- DisplayStr(0,0," Adj Minute ");
- // ----------------
- DisplayChar(0,1,month/10+0x30);
- DisplayChar(1,1,month%10+0x30);
- DisplayChar(2,1,0x01);
- DisplayChar(3,1,date/10+0x30);
- DisplayChar(4,1,date%10+0x30);
- DisplayChar(5,1,0x02);
- DisplayChar(6,1,' ');
- DisplayChar(7,1,' ');
- DisplayChar(8,1,hour/10+0x30);
- DisplayChar(9,1,hour%10+0x30);
- DisplayChar(10,1,':');
- DisplayChar(11,1,Flash?(min/10+0x30):' ');
- DisplayChar(12,1,Flash?(min%10+0x30):' ');
- DisplayChar(13,1,':');
- DisplayChar(14,1,sec/10+0x30);
- DisplayChar(15,1,sec%10+0x30);
- if(keyvalue==2&&SetFlag==5) //分+
- {
- Set(1,0);
- keyvalue=0;
- }
- if(keyvalue==3&&SetFlag==5) //分-
- {
- Set(1,1);
- keyvalue=0;
- }
- }
- break;
- case 6:
- {
- Readtime();
- DisplayStr(0,0," Adj Second ");
- // ----------------
- DisplayChar(0,1,month/10+0x30);
- DisplayChar(1,1,month%10+0x30);
- DisplayChar(2,1,0x01);
- DisplayChar(3,1,date/10+0x30);
- DisplayChar(4,1,date%10+0x30);
- DisplayChar(5,1,0x02);
- DisplayChar(6,1,' ');
- DisplayChar(7,1,' ');
- DisplayChar(8,1,hour/10+0x30);
- DisplayChar(9,1,hour%10+0x30);
- DisplayChar(10,1,':');
- DisplayChar(11,1,min/10+0x30);
- DisplayChar(12,1,min%10+0x30);
- DisplayChar(13,1,':');
- DisplayChar(14,1,Flash?(sec/10+0x30):' ');
- DisplayChar(15,1,Flash?(sec%10+0x30):' ');
- if(keyvalue==2&&SetFlag==6) //秒+
- {
- Set(0,0);
- keyvalue=0;
- }
- if(keyvalue==3&&SetFlag==6) //秒-
- {
- Set(0,1);
- keyvalue=0;
- }
- }
- break;
- case 7:
- {
- ReadAlarm();
- DisplayStr(0,0," Alarm1 Set ");
- // ----------------
- // ALARM1 OFF **:**
- DisplayStr(0,1,"ALARM1");
- DisplayChar(6,1,' ');
- if((I2CReadAdd(DS3231_CONTROL) & 0x01)==0) DisplayStr(7,1,Flash?(" "):("OFF"));
- else DisplayStr(7,1,Flash?(" "):(" ON"));
- DisplayChar(10,1,' ');
- DisplayChar(11,1,al1_hour/10+0x30);
- DisplayChar(12,1,al1_hour%10+0x30);
- DisplayChar(13,1,':');
- DisplayChar(14,1,al1_min/10+0x30);
- DisplayChar(15,1,al1_min%10+0x30);
- if((keyvalue==2||keyvalue==3)&&SetFlag==7) //闹铃1开关
- {
- Set(50,1);
- keyvalue=0;
- }
- }
- break;
- case 8:
- {
- ReadAlarm();
- DisplayStr(0,0," AL1 Adj Hour ");
- // ----------------
- DisplayStr(0,1,"ALARM1");
- DisplayChar(6,1,' ');
- DisplayStr(7,1,(I2CReadAdd(DS3231_CONTROL) & 0x01)?(" ON"):("OFF"));
- DisplayChar(10,1,' ');
- DisplayChar(11,1,Flash?(al1_hour/10+0x30):' ');
- DisplayChar(12,1,Flash?(al1_hour%10+0x30):' ');
- DisplayChar(13,1,':');
- DisplayChar(14,1,al1_min/10+0x30);
- DisplayChar(15,1,al1_min%10+0x30);
- if(keyvalue==2&&SetFlag==8) //闹铃1时+
- {
- Set(9,0);
- keyvalue=0;
- }
- if(keyvalue==3&&SetFlag==8) //闹铃1时-
- {
- Set(9,1);
- keyvalue=0;
- }
- }
- break;
- case 9:
- {
- ReadAlarm();
- DisplayStr(0,0," AL1 Adj Minute ");
- // ----------------
- DisplayStr(0,1,"ALARM1");
- DisplayChar(6,1,' ');
- DisplayStr(7,1,(I2CReadAdd(DS3231_CONTROL) & 0x01)?(" ON"):("OFF"));
- DisplayChar(10,1,' ');
- DisplayChar(11,1,al1_hour/10+0x30);
- DisplayChar(12,1,al1_hour%10+0x30);
- DisplayChar(13,1,':');
- DisplayChar(14,1,Flash?(al1_min/10+0x30):' ');
- DisplayChar(15,1,Flash?(al1_min%10+0x30):' ');
- if(keyvalue==2&&SetFlag==9) //闹铃1分+
- {
- Set(8,0);
- keyvalue=0;
- }
- if(keyvalue==3&&SetFlag==9) //闹铃1分-
- {
- Set(8,1);
- keyvalue=0;
- }
- }
- break;
- case 10:
- {
- ReadAlarm();
- DisplayStr(0,0," Alarm2 Set ");
- // ----------------
- // ALARM2 OFF **:**
- DisplayStr(0,1,"ALARM2");
- DisplayChar(6,1,' ');
- if((I2CReadAdd(DS3231_CONTROL) & 0x02)==0) DisplayStr(7,1,Flash?(" "):("OFF"));
- else DisplayStr(7,1,Flash?(" "):(" ON"));
- DisplayChar(10,1,' ');
- DisplayChar(11,1,al2_hour/10+0x30);
- DisplayChar(12,1,al2_hour%10+0x30);
- DisplayChar(13,1,':');
- DisplayChar(14,1,al2_min/10+0x30);
- DisplayChar(15,1,al2_min%10+0x30);
- if((keyvalue==2||keyvalue==3)&&SetFlag==10) //闹铃2开关
- {
- Set(60,1);
- keyvalue=0;
- }
- }
- break;
- case 11:
- {
- ReadAlarm();
- DisplayStr(0,0," AL2 Adj Hour ");
- // ----------------
- DisplayStr(0,1,"ALARM2");
- DisplayChar(6,1,' ');
- DisplayStr(7,1,(I2CReadAdd(DS3231_CONTROL) & 0x02)?(" ON"):("OFF"));
- DisplayChar(10,1,' ');
- DisplayChar(11,1,Flash?(al2_hour/10+0x30):' ');
- DisplayChar(12,1,Flash?(al2_hour%10+0x30):' ');
- DisplayChar(13,1,':');
- DisplayChar(14,1,al2_min/10+0x30);
- DisplayChar(15,1,al2_min%10+0x30);
- if(keyvalue==2&&SetFlag==11) //闹铃2时+
- {
- Set(12,0);
- keyvalue=0;
- }
- if(keyvalue==3&&SetFlag==11) //闹铃2时-
- {
- Set(12,1);
- keyvalue=0;
- }
- }
- break;
- case 12:
- {
- ReadAlarm();
- DisplayStr(0,0," AL2 Adj Minute ");
- // ----------------
- DisplayStr(0,1,"ALARM2");
- DisplayChar(6,1,' ');
- DisplayStr(7,1,(I2CReadAdd(DS3231_CONTROL) & 0x02)?(" ON"):("OFF"));
- DisplayChar(10,1,' ');
- DisplayChar(11,1,al2_hour/10+0x30);
- DisplayChar(12,1,al2_hour%10+0x30);
- DisplayChar(13,1,':');
- DisplayChar(14,1,Flash?(al2_min/10+0x30):' ');
- DisplayChar(15,1,Flash?(al2_min%10+0x30):' ');
- if(keyvalue==2&&SetFlag==12) //闹铃2分+
- {
- Set(11,0);
- keyvalue=0;
- }
- if(keyvalue==3&&SetFlag==12) //闹铃2分-
- {
- Set(11,1);
- keyvalue=0;
- }
- }
- break;
- case 13:
- {
- DisplayStr(0,0," Sound Set ");
- // ----------------
- DisplayStr(0,1,"Sound:");
- DisplayChar(6,1,Flash?' ':((uchar)aa+0x30));
- DisplayChar(7,1,' ');
- DisplayStr(8,1,aa?"Music ":"Beep ");
- if((keyvalue==2||keyvalue==3)&&SetFlag==13) //闹铃铃声选择
- {
- aa=!aa;
- keyvalue=0;
- }
- }
- break;
- }
- if(SetFlag==0&&keyvalue==2)
- {
- SetFlag=7;
- keyvalue=0;
- }
- }
- void Set(uchar sel,uchar selby)
- {
- char item;
- uchar max,mini;
- if(sel==0) {max=59;mini=0;} //秒
- if(sel==1) {max=59;mini=0;} //分钟
- if(sel==2) {max=23;mini=0;} //小时
- if(month==2)
- if(runnian())
- {
- if(sel==4) {max=29;mini=1;}
- }
- else
- {
- if(sel==4) {max=28;mini=1;}
- }
- else
- {
- if(month==1|month==3|month==5|month==7|month==8|month==10|month==12)
- {
- if(sel==4) {max=31;mini=1;}
- }
- if(month==4|month==6|month==9|month==11)
- {
- if(sel==4) {max=30;mini=1;}
- }
- }
- if(sel==5) {max=12;mini=1;} //月
- if(sel==6) {max=99;mini=0;} //年
- if(sel==8) {max=59;mini=0;} //闹铃1小时
- if(sel==9) {max=23;mini=0;} //闹铃1分钟
- if(sel==11) {max=59;mini=0;} //闹铃2小时
- if(sel==12) {max=23;mini=0;} //闹铃2分钟
- if(sel==50) { //闹铃1开关
- item=I2CReadAdd(DS3231_CONTROL)^0x01;
- I2CWrite(DS3231_CONTROL,item);
- }
- if(sel==60) { //闹铃2开关
- item=I2CReadAdd(DS3231_CONTROL)^0x02;
- I2CWrite(DS3231_CONTROL,item);
- }
- else{
- item=(I2CReadAdd(sel)&0x7F)/16*10+(I2CReadAdd(sel)&0x7F)%16;
- if (selby==0) item++; else item--;
- if(item>max) item=mini;
- if(item<mini) item=max;
- I2CWrite(sel,B_BCD(item));
- autoweek();
- }
- }
复制代码
8.KEY.H
- #ifndef __KEY_H__
- #define __KEY_H__
- #define uchar unsigned char
- #define uint unsigned int
- sbit SET = P3^7;
- sbit UP = P3^6;
- sbit DOWN = P3^5;
- extern xdata uchar keyvalue,SetFlag;
- void Scan_Key();
- void KeyProcess();
- void Set(uchar sel,uchar selby);
- #endif
复制代码
9.LCD1602.C
- #include "main.h"
- uchar lastsec=0xFF;
- uchar code mychar[8][8]={
- 0x08,0x0F,0x12,0x0F,0x0A,0x1F,0x02,0x02, //年 0x00
- 0x0F,0x09,0x0F,0x09,0x0F,0x09,0x13,0x00, //月 0x01
- 0x1F,0x11,0x11,0x1F,0x11,0x11,0x1F,0x00, //日 0x02
- 0x07,0x05,0x07,0x00,0x00,0x00,0x00,0x00, //° 0x03
- //0x02,0x05,0x05,0x02,0x00,0x00,0x00,0x00, //° 0x03
- 0x01,0x03,0x1D,0x11,0x1D,0x03,0x01,0x00, //小喇叭标记 0x04
- 0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00, //闹铃1标记 0x05
- 0x00,0x00,0x00,0x00,0x0C,0x0C,0x00,0x00, //闹铃2标记 0x06
- 0x00,0x0C,0x00,0x00,0x0C,0x0C,0x00,0x00 //闹铃1+2标记 0x07
- };
-
- void Delay5Ms()
- {
- uchar a,b;
- for(b=19;b>0;b--)
- for(a=130;a>0;a--);
- }
- void WriteDataLCM(uchar WDLCM)
- {
- ReadStatusLCM(); //检测忙
- LCM_Data = WDLCM;
- LCM_RS = 1;
- LCM_RW = 0;
- LCM_E = 0;
- LCM_E = 0; //延时
- LCM_E = 1;
- }
- void WriteCommandLCM(uchar WCLCM,BuysC) //BuysC为0时忽略忙检测
- {
- if(BuysC) ReadStatusLCM(); //根据需要检测忙
- LCM_Data = WCLCM;
- LCM_RS = 0;
- LCM_RW = 0;
- LCM_E = 0;
- LCM_E = 0;
- LCM_E = 1;
- }
- uchar ReadStatusLCM()
- {
- LCM_Data = 0xFF;
- LCM_RS = 0;
- LCM_RW = 1;
- LCM_E = 0;
- LCM_E = 0;
- LCM_E = 1;
- while (LCM_Data & 0x80); //检测忙信号
- return(LCM_Data);
- }
- void LCMInit()
- {
- LCM_Data = 0;
- WriteCommandLCM(0x38,0); //三次显示模式设置,不检测忙信号
- Delay5Ms();
- WriteCommandLCM(0x38,0);
- Delay5Ms();
- WriteCommandLCM(0x38,0);
- Delay5Ms();
- WriteCGRAM();
- WriteCommandLCM(0x38,1); //显示模式设置,开始要求每次检测忙信号
- WriteCommandLCM(0x08,1); //关闭显示
- WriteCommandLCM(0x01,1); //显示清屏
- WriteCommandLCM(0x06,1); // 显示光标移动设置
- WriteCommandLCM(0x0C,1); // 显示开及光标设置
- }
- void DisplayChar(uchar X, uchar Y, uchar DData)
- {
- Y &= 0x01;
- X &= 0x0F; //限制X不能大于15,Y不能大于1
- if(Y) X |= 0x40; //当要显示第二行时地址码+0x40;
- X |= 0x80; //算出指令码
- WriteCommandLCM(X, 0); //这里不检测忙信号,发送地址码
- WriteDataLCM(DData);
- }
- void DisplayStr(uchar X, uchar Y, uchar code *DData)
- {
- uchar ListLength,j;
- ListLength = strlen(DData);
- Y &= 0x01;
- X &= 0x0F; //限制X不能大于15,Y不能大于1
- if (X <= 0x0F) //X坐标应小于0xF
- {
- for(j=0;j<ListLength;j++)
- {
- DisplayChar(X, Y, DData[j]); //显示单个字符
- X++;
- }
- }
- }
- void WriteCGRAM() //CGRAM区写入自定义的字符
- {
- uchar x,y;
- WriteCommandLCM(0x40,0);
- for(y=0;y<8;y++)
- {
- for(x=0;x<8;x++)
- {
- WriteDataLCM(mychar[y][x]);
- }
- }
-
- }
- void Disp_Time()
- {
- Readtime();
- if(F_500ms){
- DisplayChar(2,1,' ');
- DisplayChar(5,1,' ');
- }
- else{
- DisplayChar(2,1,':');
- DisplayChar(5,1,':');
- }
- if(sec!=lastsec){
- DisplayChar(2,1,':');
- DisplayChar(5,1,':');
- F_500ms_b=0;F_500ms=0;
- DisplayStr(0,0,"20");
- DisplayChar(2,0,year/10+0x30);
- DisplayChar(3,0,year%10+0x30);
- DisplayChar(4,0,0x00);
- DisplayChar(5,0,month/10+0x30);
- DisplayChar(6,0,month%10+0x30);
- DisplayChar(7,0,0x01);
- DisplayChar(8,0,date/10+0x30);
- DisplayChar(9,0,date%10+0x30);
- DisplayChar(10,0,0x02);
- DisplayChar(11,0,' ');
- DisplayChar(12,0,' ');
- switch(day)
- {
- case 1:DisplayStr(13,0,"Mon");break;
- case 2:DisplayStr(13,0,"Tue");break;
- case 3:DisplayStr(13,0,"Wed");break;
- case 4:DisplayStr(13,0,"Thu");break;
- case 5:DisplayStr(13,0,"Fri");break;
- case 6:DisplayStr(13,0,"Sat");break;
- case 7:DisplayStr(13,0,"Sun");break;
- default :DisplayStr(13,0,"ERR");break;
- }
-
- DisplayChar(0,1,(hour<10)?' ':(hour/10+0x30));
- DisplayChar(1,1,hour%10+0x30);
- DisplayChar(3,1,min/10+0x30);
- DisplayChar(4,1,min%10+0x30);
- DisplayChar(6,1,sec/10+0x30);
- DisplayChar(7,1,sec%10+0x30);
- switch(I2CReadAdd(DS3231_CONTROL) & 0x03)
- {
- case 1: DisplayChar(8,1,0x04);DisplayChar(9,1,0x05);break;
- case 2: DisplayChar(8,1,0x04);DisplayChar(9,1,0x06);break;
- case 3: DisplayChar(8,1,0x04);DisplayChar(9,1,0x07);break;
- default: DisplayChar(8,1,' ');DisplayChar(9,1,' ');break;
- }
- if((I2CReadAdd(DS3231_CONTROL) & 0x02)==1 && (I2CReadAdd(DS3231_CONTROL) & 0x01)==0) DisplayChar(9,1,0x06);
-
- if(ReadTemp()==1){
- DisplayChar(10,1,' ');
- DisplayChar(11,1,' ');
- DisplayChar(12,1,(Dtemp<10)?' ':(Dtemp/10+0x30));
- DisplayChar(13,1,Dtemp%10+0x30);
- DisplayChar(14,1,0x03);
- DisplayChar(15,1,'C');
- }
- lastsec=sec;
- }
- }
复制代码
10.LCD1602.H
- #ifndef __LCD1602_H__
- #define __LCD1602_H__
- #define LCM_Data P0
- #define uchar unsigned char
- #define uint unsigned int
- sbit LCM_RS = P2 ^ 1;
- sbit LCM_RW = P2 ^ 2;
- sbit LCM_E = P2 ^ 5;
- void Delay5Ms();
- void Delay400ms();
- void WriteDataLCM(uchar WDLCM);
- void WriteCommandLCM(uchar WCLCM,BuysC);
- uchar ReadStatusLCM(void);
- void LCMInit(void);
- void DisplayChar(uchar X, uchar Y, uchar DData);
- void DisplayStr(uchar X, uchar Y, uchar code *DData);
- void WriteCGRAM(void);
- void Disp_Time(void);
- void Disp_Temp(void);
- #endif
复制代码
11.main.C
12.main.h
- #ifndef __MAIN_H__
- #define __MAIN_H__
- #include <reg52.h>
- #include <string.h>
- #include "LCD1602.H"
- #include "DS3231_2.H"
- #include "bell.h"
- #include "key.h"
- #include "ds18b20.h"
- #define uchar unsigned char
- #define uint unsigned int
- extern uchar F_500ms,F_500ms_b,Disp_b,Time_H,Time_L;
- extern bit aa,Flash,Al_Flag;
- #endif
复制代码
|