#include #include"lcd.h" #include"ds1302.h" #include"i2c.h" #include "DS18B20.h" #include #include #define uchar unsigned char #define uint unsigned int sbit LED0=P2^5; sbit LED1=P2^6; sbit LED2=P2^1; sbit LED3=P2^4; sbit LED4=P2^3; sbit key1 = P1^2; sbit key2 = P1^3; sbit key3 = P2^2; sbit key4 = P2^7; sbit IN = P3^7; sbit OUT = P3^6; sbit SCL1=P1^1; sbit SDA1=P1^0; int cut = 0; struct Time{ int hour; int min; int sec; }; struct UserInfo{ int num; int model; int set; int start; float Light; int temp; struct Time time[2]; }; struct UserInfo userInfo; void LcdDisplay(); void LcdDisplay1(); void Delay10ms(void); #define SlaveAddress 0x46 //¶¨ÒåÆ÷¼þÔÚIIC×ÜÏßÖеĴӵØÖ·,¸ù¾ÝALT ADDRESSµØÖ·Òý½Å²»Í¬ÐÞ¸Ä //ALT ADDRESSÒý½Å½ÓµØʱµØַΪ0x46£¬½ÓµçԴʱµØַΪ0xB8 typedef unsigned char BYTE; typedef unsigned short WORD; BYTE BUF[8]; //½ÓÊÕÊý¾Ý»º´æÇø int dis_data; //±äÁ¿ void delay_nms(unsigned int k); void InitLcd(); void Init_BH1750(void); void WriteDataLCM(uchar dataW); void WriteCommandLCM(uchar CMD,uchar Attribc); void DisplayOneChar(uchar X,uchar Y,uchar DData); void conversion(uint temp_data); void Single_Write_BH1750(uchar REG_Address); //µ¥¸öдÈëÊý¾Ý uchar Single_Read_BH1750(uchar REG_Address); //µ¥¸ö¶ÁÈ¡ÄÚ²¿¼Ä´æÆ÷Êý¾Ý void Multiple_Read_BH1750(); //Á¬ÐøµÄ¶ÁÈ¡ÄÚ²¿¼Ä´æÆ÷Êý¾Ý //------------------------------------ void Delay5us(); void Delay5ms(); void BH1750_Start(); //ÆðʼÐźŠvoid BH1750_Stop(); //Í£Ö¹ÐźŠvoid BH1750_SendACK(bit ack); //Ó¦´ðACK bit BH1750_RecvACK(); //¶Áack void BH1750_SendByte(BYTE dat); //IICµ¥¸ö×Ö½Úд BYTE BH1750_RecvByte(); //IICµ¥¸ö×Ö½Ú¶Á //ºÁÃëÑÓʱ************************** void delay_nms(unsigned int k) { unsigned int i,j; for(i=0;i 99) userInfo.num = 99; } while((i<50)&&(IN==0)){ Delay10ms(); i++; } i=0; } if(OUT==0) //��ⰴ��K1�Ƿ��� { Delay10ms(); //�������� if(OUT==0){ userInfo.num--; if(userInfo.num < 0) userInfo.num = 0; } while((i<50)&&(OUT==0)){ Delay10ms(); i++; } i=0; } if(key3==0) //��ⰴ��K1�Ƿ��� { Delay10ms(); //�������� if(key3==0){ cut++; if(cut >= 4) cut = 0; } while((i<50)&&(key3==0)){ Delay10ms(); i++; } i=0; } if(key4==0) //��ⰴ��K1�Ƿ��� { Delay10ms(); //�������� if(key4==0){ if(cut == 0){ userInfo.time[0].hour++; if(userInfo.time[0].hour >= 24) userInfo.time[0].hour = 0; }else if(cut == 1){ userInfo.time[0].min++; if(userInfo.time[0].min >= 59) userInfo.time[0].min = 0; }else if(cut == 2){ userInfo.time[1].hour++; if(userInfo.time[1].hour >= 24) userInfo.time[1].hour = 0; }else if(cut == 3){ userInfo.time[1].min++; if(userInfo.time[1].min >= 59) userInfo.time[1].min = 0; } while((i<50)&&(key4==0)){ Delay10ms(); i++; } i=0; } } } int getTimeResult() { int result = 1; struct Time Nowtime; Nowtime.hour = TIME[2]/16 * 10 + (TIME[2]&0x0f); Nowtime.min = TIME[1]/16 * 10 + (TIME[1]&0x0f); if(Nowtime.hour < userInfo.time[0].hour || Nowtime.hour > userInfo.time[1].hour) return 0; if(Nowtime.hour == userInfo.time[0].hour && Nowtime.min < userInfo.time[0].min) return 0; if(Nowtime.hour == userInfo.time[1].hour && Nowtime.min > userInfo.time[1].min) return 0; return result; } void Dispose() { if(userInfo.model == 0){ if(getTimeResult() && userInfo.Light < 100){ if(userInfo.num < 10 && userInfo.num > 0) LED0 = 0, LED1 = 0, LED2 = 0 ,LED3 = 1; else if(userInfo.num >= 10 && userInfo.num < 20) LED0 = 0, LED1 = 0, LED2 = 1 ,LED3 = 1; else if(userInfo.num >= 20 && userInfo.num < 30) LED0 = 0, LED1 = 1, LED2 = 1 ,LED3 = 1; else if(userInfo.num >= 30) LED0 = 1, LED1 = 1, LED2 = 1 ,LED3 = 1; else LED0 = 0, LED1 = 0, LED2 = 0 ,LED3 = 0; }else{ LED0 = 0, LED1 = 0, LED2 = 0 ,LED3 = 0; } }else{ if(userInfo.start){ LED0 = 1, LED1 = 1, LED2 = 1 ,LED3 = 1; }else{ LED0 = 0, LED1 = 0, LED2 = 0 ,LED3 = 0; } } LED4 = userInfo.model; } void main() { int i = 0; uchar str[10]; Ds1302Init(); Uart_Init(); LcdInit(); userInfo.time[0].hour = 18; userInfo.time[0].min = 0; userInfo.time[0].sec = 0; userInfo.time[1].hour = 23; userInfo.time[1].min = 30; userInfo.time[1].sec = 0; while(1) { Ds1302ReadTime(); LcdDisplay(); Detection(); Dispose(); sprintf(str, "%d\r\n", userInfo.temp); Bluetooth_Set(str); } } void LcdDisplay() { uint temp_data; if(userInfo.set == 0){ // TODO 显示时间 LcdWriteCom(0x80); LcdWriteData('0'+TIME[4]/16); //月 LcdWriteData('0'+(TIME[4]&0x0f)); LcdWriteData('-'); LcdWriteData('0'+TIME[3]/16); //日 LcdWriteData('0'+(TIME[3]&0x0f)); LcdWriteData(' '); LcdWriteData('0'+TIME[2]/16); //时 LcdWriteData('0'+(TIME[2]&0x0f)); LcdWriteData(':'); LcdWriteData('0'+TIME[1]/16); //分 LcdWriteData('0'+(TIME[1]&0x0f)); LcdWriteData(':'); LcdWriteData('0'+TIME[0]/16); //秒 LcdWriteData('0'+(TIME[0]&0x0f)); LcdWriteData(' '); LcdWriteData('0'+(TIME[5]&0x0f)); LcdWriteCom(0x80 + 0x40); temp_data = userInfo.Light; temp_data=temp_data%10000; LcdWriteData(temp_data/1000+0x30); temp_data=temp_data%1000; LcdWriteData(temp_data/100+0x30); temp_data=temp_data%100; LcdWriteData(temp_data/10+0x30); temp_data=temp_data%10; LcdWriteData(temp_data+0x30); LcdWriteData('L'); LcdWriteData('x'); LcdWriteData(' '); LcdWriteData(userInfo.temp / 10 + 0x30); LcdWriteData(userInfo.temp % 10 + 0x30); LcdWriteData('C'); LcdWriteData(' '); LcdWriteData(userInfo.num / 10 % 10 + 0x30); LcdWriteData(userInfo.num % 10 + 0x30); }else{ LcdWriteCom(0x80); LcdWriteData('0'+ userInfo.time[0].hour/10); //时 LcdWriteData('0'+ userInfo.time[0].hour% 10); LcdWriteData(':'); LcdWriteData('0'+userInfo.time[0].min/10); //分 LcdWriteData('0'+ userInfo.time[0].min % 10); LcdWriteData(':'); LcdWriteData('0'+userInfo.time[0].sec / 10); //秒 LcdWriteData('0'+userInfo.time[0].sec % 10); LcdWriteData(' '); LcdWriteData('0'+cut); LcdWriteCom(0x80 + 0x40); LcdWriteData('0'+ userInfo.time[1].hour/10); //时 LcdWriteData('0'+ userInfo.time[1].hour% 10); LcdWriteData(':'); LcdWriteData('0'+userInfo.time[1].min/10); //分 LcdWriteData('0'+ userInfo.time[1].min % 10); LcdWriteData(':'); LcdWriteData('0'+userInfo.time[1].sec / 10); //秒 LcdWriteData('0'+userInfo.time[1].sec % 10); } } void Delay10ms(void) //��� 0us { unsigned char a,b,c; for(c=1;c>0;c--) for(b=38;b>0;b--) for(a=130;a>0;a--); }