目前只实现了1、2、3、4和1、2、4
DS1302、内部eeprom、DHT11、1.3寸OLED
单片机源程序如下:
- #include <STC15F2K60S2.H>
- #include "oled.h"
- #include "Delay.h"
- #include <DHT.h>
- #include "MatrixKey.h"
- #include "Buzzer.h"
- #include "UART.h"
- #include "DS1302.H"
- #include "Timer0.h"
- #include "esp8266.h"
- #include "eeprom.h"
- sbit LED = P3^4;
- #define IAP_ADDRESS 0X000
- unsigned char tempH,MiniSec,tempL,humiH,humiL,KeyNum,MODE=0,Run=0,i,j,temp,TimeSetselect;
- unsigned char Sec=0,Min=0,Hour=0;
- unsigned char ReadTime[14];
- unsigned char receive[4];
- unsigned char code table[]="AT+CIPMUX=1\r\n";
- unsigned char code table1[]="AT+CIPSERVER=1,8080\r\n";
- char xdata clock1_hour=0,clock1_min=0;
- //unsigned char xdata clock1[7]="00:00";
- static unsigned int T0Count3;
- void TimeShow(void);
- void Stopwatch();
- void TimeSet(void);
- void Clock(void);
- void ESP8266(void);
- void Buzzer(void);
- void esp_init();
- void int_init();
- void eeprom(void);
- void EEPROM_Init(void);
- void Judge_Clock(void);
- void main(void)
- {
- DHT11_Rst();
- OLED_Init(); //初始化OLED
- OLED_Clear();
- Buzzer();
- UART_Init();
- DS1302_Init();
- Timer0Init();
- EEPROM_Init();
- // DS1302_SetTime();
- // DS1302_WriteByte(0x80,0x07);//关闭1302写保护
- // EEPROM_Erase(0);
- while(1)
- {
- // LED=~LED;
- // OLED_ShowString(0,0,"hello");
- KeyNum=MatrixKey();
-
- P42=0;
- if(KeyNum==13)
- {
- P42=1;
- Delay(100);
- P42=0;
- if(MODE==0){MODE=1;TimeSetselect=0;}
- else if(MODE==1){MODE=0;OLED_Clear();DS1302_SetTime();}
- }
- if(KeyNum==14)
- {
- P42=1;
- Delay(100);
- P42=0;
- if(MODE==0){MODE=2; OLED_Clear();}
- else if(MODE==2){MODE=0; OLED_Clear(); }
- }
- if(KeyNum==15)
- {
- P42=1;
- Delay(100);
- P42=0;
- if(MODE==0){MODE=3; OLED_Clear();}
- else if(MODE==3){MODE=0; OLED_Clear(); Judge_Clock(); }
- }
- if(KeyNum==9)
- {
- P42=1;
- Delay(100);
- P42=0;
- Run=~Run;
- // Run=1;
- }
- if(KeyNum==10)
- {
- P42=1;
- Delay(100);
- P42=0;
- Sec=0;
- Min=0;
- MiniSec=0;
- }
- if(KeyNum==7)
- {
- P42=1;
- Delay(100);
- P42=0;
- if(MODE==0){MODE=4; OLED_Clear();}
- else if(MODE==4){MODE=0; OLED_Clear(); }
- }
- if(KeyNum==8)
- {
- P42=1;
- Delay(100);
- P42=0;
- if(MODE==0){MODE=5; OLED_Clear();}
- else if(MODE==5){MODE=0; OLED_Clear(); }
- }
- switch(MODE)
- {
- case 0:TimeShow();break;
- case 1:TimeSet();break;
- case 2:Stopwatch();break;
- case 3:Clock();break;
- case 4:ESP8266();break;
- case 5:eeprom();break;
- }
- }
- }
- void ESP8266(void)
- {
- // int_init(); //中断初始化,定时器初始化
- // esp_init(); //模块初始化,发送那两条指令
- // if(receive[1]=='1'){ //收到1点亮LED
- // P34 =0 ;
- // }
- // else P34 =1;
- DS1302_ReadTime2();
- ReadTime[0]=(DS1302_Time[0]>>4); //分离出年十位
- ReadTime[1]=(DS1302_Time[0]&0x0F); //分离出年个位
-
- ReadTime[2]=(DS1302_Time[1]>>4); //分离出月十位
- ReadTime[3]=(DS1302_Time[1]&0x0F); //分离出月个位
- ReadTime[4]=(DS1302_Time[2]>>4); //分离出日十位
- ReadTime[5]=(DS1302_Time[2]&0x0F); //分离出日个位
- ReadTime[6]=(DS1302_Time[3]>>4); //分离出小时十位
- ReadTime[7]=(DS1302_Time[3]&0x0F); //分离出小时个位
- ReadTime[8]=(DS1302_Time[4]>>4); //分离出分钟十位
- ReadTime[9]=(DS1302_Time[4]&0x0F); //分离出分钟个位
- ReadTime[10]=(DS1302_Time[5]>>4); //分离出秒钟十位
- ReadTime[11]=(DS1302_Time[5]&0x0F); //分离出秒钟个位
- Delay(10);
- EA=0;
- UART_SendByte(ReadTime[0]+48);//年
- UART_SendByte(ReadTime[1]+48);
-
- UART_SendByte('-');
-
- UART_SendByte(ReadTime[2]+48);//月
- UART_SendByte(ReadTime[3]+48);
- UART_SendByte('-');
- UART_SendByte(ReadTime[4]+48);//日
- UART_SendByte(ReadTime[5]+48);
- UART_SendByte(' ');
- UART_SendByte(ReadTime[6]+48);//时
- UART_SendByte(ReadTime[7]+48);
- UART_SendByte(':');
- UART_SendByte(ReadTime[8]+48);//分
- UART_SendByte(ReadTime[9]+48);
- UART_SendByte(':');
- UART_SendByte(ReadTime[10]+48);//秒
- UART_SendByte(ReadTime[11]+48);
- UART_SendByte('\n');
- SBUF='\n';
- while(!TI); //等待发送完毕
- TI=0; //清中断
- EA=1;
- Delay(1000);
-
- }
- void eeprom(void)
- {
- unsigned char i;
- // EEPROM_Erase(0x000);
- for(i=0;i<1;i++)
- {
- UART_SendByte(EERPOM_ReadByte(0x000));
- }
-
- }
- void Clock(void)
- {
- // OLED_Clear();
- // clock1_hour=EERPOM_ReadByte(0x000);
- OLED_ShowString(0,0," Clock");
- OLED_ShowString(47,2,":");
- OLED_ShowNum(30,2,clock1_hour,2,16);
- OLED_ShowNum(54,2,clock1_min,2,16);
-
- if(KeyNum==1) {clock1_hour++;if(clock1_hour>=24) clock1_hour=0;}
- if(KeyNum==2) {clock1_hour--;if(clock1_hour<0) clock1_hour=23;}
- if(KeyNum==3) {clock1_min++;if(clock1_min>=60) clock1_min=0;}
- if(KeyNum==4) {clock1_min--;if(clock1_min<0) clock1_min=59;}
- // EEPROM_WriteByte(0x000,clock1_hour);
-
- }
- void TimeShow(void)
- {
- // Delay(1200);
- DS1302_ReadTime();
- OLED_ShowNum(0,0,DS1302_Time[0],2,16);
- OLED_ShowString(17,0,"-");
- OLED_ShowNum(24,0,DS1302_Time[1],2,16);
- OLED_ShowString(41,0,"-");
- OLED_ShowNum(48,0,DS1302_Time[2],2,16);
- OLED_ShowNum(30,2,DS1302_Time[3],2,16);
- OLED_ShowString(47,2,":");
- OLED_ShowNum(54,2,DS1302_Time[4],2,16);
- OLED_ShowString(71,2,":");
- OLED_ShowNum(78,2,DS1302_Time[5],2,16);
- OLED_ShowString(87,0,"DAY:");
- OLED_ShowNum(120,0,DS1302_Time[6],1,16);
-
- DHT11_Read_Data(&tempH,&tempL,&humiH,&humiL);
- OLED_ShowString(75,6,"T:");
- OLED_ShowNum(95,6,tempH,2,16);
- // OLED_ShowNum(119,6,tempL,2,16);
- OLED_ShowString(0,6,"H:");
- OLED_ShowNum(20,6,humiH,2,16);
- // OLED_ShowNum(44,6,humiL,2,16);
- // OLED_ShowString(37,6,".");
- OLED_ShowString(50,6,"%");
-
- }
- void TimeSet(void)
- {
-
- if(KeyNum==16)
- {
- P42=1;
- Delay(100);
- P42=0;
- TimeSetselect++;
- TimeSetselect%=6;
- }
- OLED_ShowNum(102,2,TimeSetselect,2,16);
- if(KeyNum==5)
- {
-
- DS1302_Time[TimeSetselect]++;
- if(DS1302_Time[0]>99){DS1302_Time[0]=0;}
- if(DS1302_Time[1]>12){DS1302_Time[1]=1;}
- if(DS1302_Time[1]==1||DS1302_Time[1]==3||DS1302_Time[1]==3||DS1302_Time[1]==5||DS1302_Time[1]==7||DS1302_Time[1]==8||DS1302_Time[1]==10||DS1302_Time[1]==12){if(DS1302_Time[2]>31)
- {
- if(DS1302_Time[2]<1){DS1302_Time[2]=31;}//大月
- if(DS1302_Time[2]>31){DS1302_Time[2]=1;}}
- }
- else if(DS1302_Time[1]==4||DS1302_Time[1]==6||DS1302_Time[1]==9||DS1302_Time[1]==11)
- {
- if(DS1302_Time[2]<1){DS1302_Time[2]=30;}//小月
- if(DS1302_Time[2]>30){DS1302_Time[2]=1;}
- }
- else if(DS1302_Time[1]==2)
- {
- if(DS1302_Time[0]%4==0)
- {
- if(DS1302_Time[2]<1){DS1302_Time[2]=29;}//闰年2月
- if(DS1302_Time[2]>29){DS1302_Time[2]=1;}
- }
- else
- {
- if(DS1302_Time[2]<1){DS1302_Time[2]=28;}//平年2月
- if(DS1302_Time[2]>28){DS1302_Time[2]=1;}
- }
- }
- if(DS1302_Time[3]>23){DS1302_Time[3]=0;}
- if(DS1302_Time[4]>59){DS1302_Time[4]=0;}
- if(DS1302_Time[5]>59){DS1302_Time[5]=0;}
- }
- if(KeyNum==6)//按键3按下
- {
- DS1302_Time[TimeSetselect]--;//时间设置位数值减1
- if(DS1302_Time[0]<0){DS1302_Time[0]=99;}//年越界判断
- if(DS1302_Time[1]<1){DS1302_Time[1]=12;}//月越界判断
- if( DS1302_Time[1]==1 || DS1302_Time[1]==3 || DS1302_Time[1]==5 || DS1302_Time[1]==7 ||
- DS1302_Time[1]==8 || DS1302_Time[1]==10 || DS1302_Time[1]==12)//日越界判断
- {
- if(DS1302_Time[2]<1){DS1302_Time[2]=31;}//大月
- if(DS1302_Time[2]>31){DS1302_Time[2]=1;}
- }
- else if(DS1302_Time[1]==4 || DS1302_Time[1]==6 || DS1302_Time[1]==9 || DS1302_Time[1]==11)
- {
- if(DS1302_Time[2]<1){DS1302_Time[2]=30;}//小月
- if(DS1302_Time[2]>30){DS1302_Time[2]=1;}
- }
- else if(DS1302_Time[1]==2)
- {
- if(DS1302_Time[0]%4==0)
- {
- if(DS1302_Time[2]<1){DS1302_Time[2]=29;}//闰年2月
- if(DS1302_Time[2]>29){DS1302_Time[2]=1;}
- }
- else
- {
- if(DS1302_Time[2]<1){DS1302_Time[2]=28;}//平年2月
- if(DS1302_Time[2]>28){DS1302_Time[2]=1;}
- }
- }
- if(DS1302_Time[3]<0){DS1302_Time[3]=23;}//时越界判断
- if(DS1302_Time[4]<0){DS1302_Time[4]=59;}//分越界判断
- if(DS1302_Time[5]<0){DS1302_Time[5]=59;}//秒越界判断
- }
- OLED_ShowNum(0,0,DS1302_Time[0],2,16);
- OLED_ShowString(17,0,"-");
- OLED_ShowNum(24,0,DS1302_Time[1],2,16);
- OLED_ShowString(41,0,"-");
- OLED_ShowNum(48,0,DS1302_Time[2],2,16);
- OLED_ShowNum(30,2,DS1302_Time[3],2,16);
- OLED_ShowString(47,2,":");
- OLED_ShowNum(54,2,DS1302_Time[4],2,16);
- OLED_ShowString(71,2,":");
- OLED_ShowNum(78,2,DS1302_Time[5],2,16);
- // OLED_Clear();
- }
- void Stopwatch()
- {
- // LED=~LED;
- // KeyNum=MatrixKey();
- // OLED_Clear();
- OLED_ShowString(4,0," StopWatch");
- OLED_ShowNum(30,2,MiniSec,2,16);
- OLED_ShowString(47,2,":");
- OLED_ShowNum(54,2,Sec,2,16);
- OLED_ShowString(71,2,":");
- OLED_ShowNum(78,2,Min,2,16);
- // if(KeyNum==9)
- // {
- // Buzzer_Time(100);
- // Run=1;
- // Delay(100);
- // LED=~LED;
- // }
-
- }
- void Sec_Loop(void)
- {
- // if(Run)
- // {
- MiniSec++;
- if(MiniSec>=10)
- {
- MiniSec=0;
- Sec++;
- if(Sec>=60)
- {
- Sec=0;
- Min++;
- if(Min>=60)
- {
- Min=0;
- }
- }
- }
- // }
- }
- void Buzzer(void)
- {
- P4M1=0X00;
- P4M0=0x04;
- // P42=1;
- // Delay(100);
- // P42=0;
- }
- void EEPROM_Init(void)
- {
- if(EERPOM_ReadByte(0)==0xff)//可用一个校验位来判断是否进行擦写
- {
- P34=~P34;
- }
- else
- {
- clock1_hour=EERPOM_ReadByte(0);
- clock1_min =EERPOM_ReadByte(0x001);
- }
- }
- void Judge_Clock(void)
- {
- EEPROM_Erase(0);
-
- EEPROM_WriteByte(0x000,clock1_hour);
-
- // EEPROM_Erase(0x400);
-
- EEPROM_WriteByte(0x001,clock1_min);
- }
- void shortdelay(unsigned int n){ //短延时
- unsigned int i,j;
- for(i=n;i>0;i--){
- for(j=110;j>0;j--){;}
- }
- }
- void esp_init()
- {
- unsigned char a=0,b=0;
- while(table[a]!='\0')
- {
- SBUF=table[a]; //写入要发送的字符
- while(!TI); //等待发送完成
- TI=0; //发送
- a++;
- }
- shortdelay(50); //延时函数
- while(table1[b]!='\0')
- {
- SBUF=table1[b];
- while(!TI); //等待发送完成
- TI=0;
- b++;
- }
- }
- void int_init(){ //开启定时器1,产生9600波特率
- TMOD = 0x20; //定时器1工作在方式2八位自动重装模式
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
Keil代码下载:
代码.zip
(155.92 KB, 下载次数: 57)
|