|
|
#include<reg51.h> |
|
|
#include"lcd.h" |
|
|
#include"ds1302.h" |
|
|
#include"i2c.h" |
|
|
#include "DS18B20.h" |
|
|
|
|
|
#include <stdio.h> |
|
|
#include <string.h> |
|
|
|
|
|
|
|
|
#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<k;i++) |
|
|
{ |
|
|
for(j=0;j<121;j++) |
|
|
{;}} |
|
|
} |
|
|
|
|
|
void Delay5us() |
|
|
{ |
|
|
_nop_();_nop_();_nop_();_nop_(); |
|
|
_nop_();_nop_();_nop_();_nop_(); |
|
|
_nop_();_nop_();_nop_();_nop_(); |
|
|
_nop_();_nop_();_nop_();_nop_(); |
|
|
} |
|
|
|
|
|
void Delay5ms() |
|
|
{ |
|
|
WORD n = 560; |
|
|
|
|
|
while (n--); |
|
|
} |
|
|
|
|
|
/************************************** |
|
|
起始信号 |
|
|
**************************************/ |
|
|
void BH1750_Start() |
|
|
{ |
|
|
SDA1 = 1; //拉高数据线 |
|
|
SCL1 = 1; //拉高时钟线 |
|
|
Delay5us(); //延时 |
|
|
SDA1 = 0; //产生下降沿 |
|
|
Delay5us(); //延时 |
|
|
SCL1 = 0; //拉低时钟线 |
|
|
} |
|
|
|
|
|
/************************************** |
|
|
停止信号 |
|
|
**************************************/ |
|
|
void BH1750_Stop() |
|
|
{ |
|
|
SDA1 = 0; //拉低数据线 |
|
|
SCL1 = 1; //拉高时钟线 |
|
|
Delay5us(); //延时 |
|
|
SDA1 = 1; //产生上升沿 |
|
|
Delay5us(); //延时 |
|
|
} |
|
|
|
|
|
/************************************** |
|
|
发送应答信号 |
|
|
入口参数:ack (0:ACK 1:NAK) |
|
|
**************************************/ |
|
|
void BH1750_SendACK(bit ack) |
|
|
{ |
|
|
SDA = ack; //写应答信号 |
|
|
SCL = 1; //拉高时钟线 |
|
|
Delay5us(); //延时 |
|
|
SCL = 0; //拉低时钟线 |
|
|
Delay5us(); //延时 |
|
|
} |
|
|
|
|
|
/************************************** |
|
|
接收应答信号 |
|
|
**************************************/ |
|
|
bit BH1750_RecvACK() |
|
|
{ |
|
|
SCL = 1; //拉高时钟线 |
|
|
Delay5us(); //延时 |
|
|
CY = SDA; //读应答信号 |
|
|
SCL = 0; //拉低时钟线 |
|
|
Delay5us(); //延时 |
|
|
|
|
|
return CY; |
|
|
} |
|
|
|
|
|
/************************************** |
|
|
向IIC总线发送一个字节数据 |
|
|
**************************************/ |
|
|
void BH1750_SendByte(BYTE dat) |
|
|
{ |
|
|
BYTE i; |
|
|
|
|
|
for (i=0; i<8; i++) //8位计数器 |
|
|
{ |
|
|
dat <<= 1; //移出数据的最高位 |
|
|
SDA = CY; //送数据口 |
|
|
SCL = 1; //拉高时钟线 |
|
|
Delay5us(); //延时 |
|
|
SCL = 0; //拉低时钟线 |
|
|
Delay5us(); //延时 |
|
|
} |
|
|
BH1750_RecvACK(); |
|
|
} |
|
|
|
|
|
/************************************** |
|
|
从IIC总线接收一个字节数据 |
|
|
**************************************/ |
|
|
BYTE BH1750_RecvByte() |
|
|
{ |
|
|
BYTE i; |
|
|
BYTE dat = 0; |
|
|
|
|
|
SDA = 1; //使能内部上拉,准备读取数据, |
|
|
for (i=0; i<8; i++) //8位计数器 |
|
|
{ |
|
|
dat <<= 1; |
|
|
SCL = 1; //拉高时钟线 |
|
|
Delay5us(); //延时 |
|
|
dat |= SDA; //读数据 |
|
|
SCL = 0; //拉低时钟线 |
|
|
Delay5us(); //延时 |
|
|
} |
|
|
return dat; |
|
|
} |
|
|
|
|
|
//********************************* |
|
|
|
|
|
void Single_Write_BH1750(uchar REG_Address) |
|
|
{ |
|
|
BH1750_Start(); //起始信号 |
|
|
BH1750_SendByte(SlaveAddress); //发送设备地址+写信号 |
|
|
BH1750_SendByte(REG_Address); //内部寄存器地址, |
|
|
// BH1750_SendByte(REG_data); //内部寄存器数据, |
|
|
BH1750_Stop(); //发送停止信号 |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//********************************************************* |
|
|
// |
|
|
//连续读出BH1750内部数据 |
|
|
// |
|
|
//********************************************************* |
|
|
void Multiple_read_BH1750(void) |
|
|
|
|
|
{ uchar i; |
|
|
BH1750_Start(); //起始信号 |
|
|
BH1750_SendByte(SlaveAddress+1); //发送设备地址+读信号 |
|
|
|
|
|
for (i=0; i<3; i++) //连续读取2个地址数据,存储中BUF |
|
|
{ |
|
|
BUF[i] = BH1750_RecvByte(); //BUF[0]存储0x32地址中的数据 |
|
|
if (i == 3) |
|
|
{ |
|
|
|
|
|
BH1750_SendACK(1); //最后一个数据需要回NOACK |
|
|
} |
|
|
else |
|
|
{ |
|
|
BH1750_SendACK(0); //回应ACK |
|
|
} |
|
|
} |
|
|
|
|
|
BH1750_Stop(); //停止信号 |
|
|
Delay5ms(); |
|
|
} |
|
|
|
|
|
|
|
|
//初始化BH1750 |
|
|
void Init_BH1750() |
|
|
{ |
|
|
Single_Write_BH1750(0x01); |
|
|
|
|
|
} |
|
|
|
|
|
// 串口初始化 |
|
|
void Uart_Init() |
|
|
{ |
|
|
TMOD = 0x20 | 0x01; |
|
|
SCON = 0x50; |
|
|
TH1 = 0xFD; |
|
|
TL1 = TH1; |
|
|
PCON = 0x00; |
|
|
EA = 1; |
|
|
ES = 1; |
|
|
TR1 = 1; |
|
|
} |
|
|
|
|
|
void us_delay(uchar t) |
|
|
{ |
|
|
while(t--); |
|
|
} |
|
|
|
|
|
void Send_Uart(uchar value) |
|
|
{ |
|
|
ES=0; |
|
|
TI=0; |
|
|
SBUF=value; |
|
|
while(TI==0); |
|
|
TI=0; |
|
|
ES=1; |
|
|
} |
|
|
|
|
|
void Bluetooth_Set(uchar *puf) |
|
|
{ |
|
|
while(*puf!='\0') |
|
|
{ |
|
|
Send_Uart(*puf); |
|
|
us_delay(5); |
|
|
puf++; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 获取数据 |
|
|
void Detection() |
|
|
{ |
|
|
static int num = 0; |
|
|
int i = 0; |
|
|
Single_Write_BH1750(0x01); |
|
|
Single_Write_BH1750(0x10); |
|
|
delay_nms(180); |
|
|
Multiple_Read_BH1750(); |
|
|
dis_data=BUF[0]; |
|
|
dis_data=(dis_data<<8)+BUF[1]; |
|
|
userInfo.Light = (float)dis_data/1.2; // 获取光照值 |
|
|
|
|
|
userInfo.temp = Temper_change(); |
|
|
userInfo.temp /= 100; // 获取温度,去除小数部分 |
|
|
|
|
|
// 按键一 |
|
|
if(key1==0) |
|
|
{ |
|
|
Delay10ms(); |
|
|
if(key1==0){ |
|
|
if(userInfo.set == 0){ |
|
|
if(userInfo.model == 0) userInfo.model = 1; |
|
|
else userInfo.model = 0; |
|
|
if(userInfo.model == 1) LED4 = 1, LED5 = 0; |
|
|
else LED4 = 0, LED5 = 1; |
|
|
} |
|
|
} |
|
|
while((i<50)&&(key1==0)){ |
|
|
Delay10ms(); |
|
|
i++; |
|
|
} |
|
|
i=0; |
|
|
} |
|
|
|
|
|
// 按键二 |
|
|
if(key2==0) |
|
|
{ |
|
|
Delay10ms(); |
|
|
if(key2==0){ |
|
|
if(userInfo.model){ |
|
|
led_num++; |
|
|
if(led_num >= 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) //<EFBFBD><EFBFBD><EFBFBD> 0us |
|
|
{ |
|
|
unsigned char a,b,c; |
|
|
for(c=1;c>0;c--) |
|
|
for(b=38;b>0;b--) |
|
|
for(a=130;a>0;a--); |
|
|
} |