You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

570 lines
14 KiB

3 years ago
#include<reg51.h>
#include"lcd.h"
#include"ds1302.h"
#include"i2c.h"
#include "DS18B20.h"
3 years ago
#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;
3 years ago
sbit key1 = P1^2;
sbit key2 = P1^3;
sbit key3 = P2^2;
sbit key4 = P2^7;
3 years ago
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;
};
3 years ago
struct UserInfo{
int num;
int model;
int set;
3 years ago
int start;
float Light;
int temp;
struct Time time[2];
3 years ago
};
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)
{
SDA1 = ack; //дӦ´ðÐźÅ
SCL1 = 1; //À­¸ßʱÖÓÏß
Delay5us(); //ÑÓʱ
SCL1 = 0; //À­µÍʱÖÓÏß
Delay5us(); //ÑÓʱ
}
/**************************************
½ÓÊÕÓ¦´ðÐźÅ
**************************************/
bit BH1750_RecvACK()
{
SCL1 = 1; //À­¸ßʱÖÓÏß
Delay5us(); //ÑÓʱ
CY = SDA1; //¶ÁÓ¦´ðÐźÅ
SCL1 = 0; //À­µÍʱÖÓÏß
Delay5us(); //ÑÓʱ
return CY;
}
/**************************************
ÏòIIC×ÜÏß·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ý
**************************************/
void BH1750_SendByte(BYTE dat)
{
BYTE i;
for (i=0; i<8; i++) //8λ¼ÆÊýÆ÷
{
dat <<= 1; //ÒƳöÊý¾ÝµÄ×î¸ßλ
SDA1 = CY; //ËÍÊý¾Ý¿Ú
SCL1 = 1; //À­¸ßʱÖÓÏß
Delay5us(); //ÑÓʱ
SCL1 = 0; //À­µÍʱÖÓÏß
Delay5us(); //ÑÓʱ
}
BH1750_RecvACK();
}
/**************************************
´ÓIIC×ÜÏß½ÓÊÕÒ»¸ö×Ö½ÚÊý¾Ý
**************************************/
BYTE BH1750_RecvByte()
{
BYTE i;
BYTE dat = 0;
SDA1 = 1; //ʹÄÜÄÚ²¿ÉÏÀ­,×¼±¸¶ÁÈ¡Êý¾Ý,
for (i=0; i<8; i++) //8λ¼ÆÊýÆ÷
{
dat <<= 1;
SCL1 = 1; //À­¸ßʱÖÓÏß
Delay5us(); //ÑÓʱ
dat |= SDA1; //¶ÁÊý¾Ý
SCL1 = 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(); //·¢ËÍÍ£Ö¹ÐźÅ
}
//********µ¥×Ö½Ú¶ÁÈ¡*****************************************
/*
uchar Single_Read_BH1750(uchar REG_Address)
{ uchar REG_data;
BH1750_Start(); //ÆðʼÐźÅ
BH1750_SendByte(SlaveAddress); //·¢ËÍÉ豸µØÖ·+дÐźÅ
BH1750_SendByte(REG_Address); //·¢ËÍ´æ´¢µ¥ÔªµØÖ·£¬´Ó0¿ªÊ¼
BH1750_Start(); //ÆðʼÐźÅ
BH1750_SendByte(SlaveAddress+1); //·¢ËÍÉ豸µØÖ·+¶ÁÐźÅ
REG_data=BH1750_RecvByte(); //¶Á³ö¼Ä´æÆ÷Êý¾Ý
BH1750_SendACK(1);
BH1750_Stop(); //Í£Ö¹ÐźÅ
return REG_data;
}
*/
//*********************************************************
//
//Á¬Ðø¶Á³ö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£¬¸ù¾ÝÐèÒªÇë²Î¿¼pdf½øÐÐÐÞ¸Ä****
void Init_BH1750()
{
Single_Write_BH1750(0x01);
}
3 years ago
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()
{
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)
3 years ago
{
Delay10ms();
3 years ago
if(key1==0){
if(userInfo.set == 0){
if(userInfo.model == 0) userInfo.model = 1;
else userInfo.model = 0;
}
3 years ago
}
while((i<50)&&(key1==0)){
Delay10ms();
i++;
}
i=0;
}
if(key2==0)
3 years ago
{
Delay10ms();
3 years ago
if(key2==0){
if(userInfo.model){
if(userInfo.start == 0) userInfo.start = 1;
else userInfo.start = 0;
}else{
if(userInfo.set == 0) userInfo.set = 1;
else userInfo.set = 0;
cut = 0;
}
LcdWriteCom(0x01); //清屏
3 years ago
}
while((i<50)&&(key2==0)){
Delay10ms();
i++;
}
i=0;
}
if(IN==0)
3 years ago
{
Delay10ms();
3 years ago
if(IN==0){
userInfo.num++;
if(userInfo.num > 99) userInfo.num = 99;
}
while((i<50)&&(IN==0)){
Delay10ms();
i++;
}
i=0;
}
if(OUT==0) //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>K1<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>
{
Delay10ms(); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(OUT==0){
userInfo.num--;
if(userInfo.num < 0) userInfo.num = 0;
}
while((i<50)&&(OUT==0)){
Delay10ms();
i++;
}
i=0;
}
if(key3==0) //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>K1<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>
{
Delay10ms(); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(key3==0){
cut++;
if(cut >= 4) cut = 0;
}
while((i<50)&&(key3==0)){
Delay10ms();
i++;
}
i=0;
}
if(key4==0) //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>K1<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>
{
Delay10ms(); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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;
}
}
3 years ago
}
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;
}
3 years ago
void Dispose()
{
if(userInfo.model == 0){
if(getTimeResult() && userInfo.Light < 100){
3 years ago
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;
3 years ago
}
}
LED4 = userInfo.model;
}
void main()
{
int i = 0;
uchar str[10];
3 years ago
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;
3 years ago
while(1)
{
Ds1302ReadTime();
LcdDisplay();
Detection();
Dispose();
sprintf(str, "%d\r\n", userInfo.temp);
Bluetooth_Set(str);
3 years ago
}
}
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);
}
3 years ago
}
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--);
}