#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 LED5=P1^7; 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; int led_num = 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= 5) led_num = 0; }else{ if(userInfo.set == 0) userInfo.set = 1; else userInfo.set = 0; cut = 0; } LcdWriteCom(0x01); //清屏 } while((i<50)&&(key2==0)){ Delay10ms(); i++; } i=0; } if(IN==0) // 光电门 进 { Delay10ms(); if(IN==0){ // 去抖 userInfo.num++; if(userInfo.num > 99) userInfo.num = 99; } while((i<50)&&(IN==0)){ Delay10ms(); i++; } i=0; } if(OUT==0) // 光电门 出 { 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) // 按键3 { Delay10ms(); if(key3==0){ cut++; if(cut >= 4) cut = 0; // 切换修改的对象 } while((i<50)&&(key3==0)){ Delay10ms(); i++; } i=0; } // 按键四 if(key4==0) { Delay10ms(); if(key4==0){ // 根据cut的值,修改不同的对象 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; } } } // 获取时间是否达到 返回1为定时时间到 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(led_num){ if(led_num == 1) LED0 = 0, LED1 = 0, LED2 = 0 ,LED3 = 1; else if(led_num == 2) LED0 = 0, LED1 = 0, LED2 = 1 ,LED3 = 1; else if(led_num == 3) LED0 = 0, LED1 = 1, LED2 = 1 ,LED3 = 1; else if(led_num == 4) LED0 = 1, LED1 = 1, LED2 = 1 ,LED3 = 1; }else{ LED0 = 0, LED1 = 0, LED2 = 0 ,LED3 = 0; } } LED4 = userInfo.model; } // main void main() { int i = 0; uchar str[10]; Ds1302Init(); // 时钟初始化 Uart_Init(); LcdInit(); // lcd初始化 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(); } } // 显示 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--); }