目录
前言
我大二小学期主要是通过51单片机去做一个温度报警系统,而学习的资源来自于江协科技的视频。小学期一共分为了四周,其实这个作品总共只用了我一周的时间,51单片机相对来说很简单,后面我做的作品由于多了两个亮度和湿度检测,所以我把项目名称改成了智慧农业控制系统。😁
硬件资源介绍
图像连接
1.矩阵按键模块
2.LED模块
3.LED点阵模块
4.74HC595芯片:实现串行输入并行输出的功能来控制LED点阵模块,从而节约引脚资源。
5.ds18b20温度传感器
6.红外接收模块
7.LED1602液晶显示模块:可通过可调电阻来改变对比度。
8.两个四位数码管模块
9.74HC138芯片:通过3位二进制来控是数码管中的8个LED灯的亮灭情况。
10.74HC245芯片:通过74HC245芯片来扩展51单片机的IO口,以便驱动两个四位的数码管模块。
11.ULN2003芯片:该芯片能为单片机提供大电流的驱动能力,可以通过该芯片来驱动电机模块。
12.DS1302时钟芯片:可以提供秒、分、小时、日期、月、年等信息,并且还有软件自动调整的能力,可以通过配置AM/PM来决定采用24小时格式还是12小时格式。
13.晶振:晶振用于提供时钟信号,以便单片机能够按照特定的频率执行指令和操作。
14.电容:稳定电源电压,过滤噪声信号,保护芯片,提高系统的抗干扰能力
15.复位按键:当单片机出现程序故障时,重新启动单片机。
16.光敏电阻模块
17.热敏电阻模块
18.AT24C02存储芯片:AT24C02是一个2K位串行CMOS E2PROM, 内部含有256个8位字节。
19.独立按键模块
20.CH340C芯片:该芯片用于实现USB转串口或者USB转打印口。
21.AMS117是一种固定输出电压的线性稳压器芯片。具有电压稳定,电流调节,过热保护,过载保护,低功耗的特点。
程序实现
下面的许多程序我基本上都是使用的江科大视频上的代码,但在使用之间必须要看的懂代码的实现逻辑,我学习的过程中会多写注释,方便自己后面复习的时候使用,同时我还在代码的原本基础上进行了一定的修改,用于兼容各个模块相互之间的工作。
1.延时模块
Delay.h
#ifndef __DELAY_H__
#define __DELAY_H__
void Delay(unsigned int xms);//延时函数声明
#endif
Delay.c
void Delay(unsigned int xms)//延时函数体
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
2.外部中断模块
INT.h
#ifndef __INT_H__
#define __INT_H__
void Init_INT0(void);//中断0初始化函数
void Init_INT1(void);//中断1初始化函数
#endif
INT.c
#include <REGX52.H>
void Init_INT0(void)//中断0初始化函数
{
IT0=0;//设置外部中断电平触发方式为低电平触发
EA=1;//中断开关打开
EX0=1;//打开外部中断0
}
void Init_INT1(void)//中断1初始化函数
{
IT1=0;//设置外部中断电平触发方式为低电平触发
EA=1;//中断开关打开
EX1=1;//打开外部中断1
}
3.温度检测模块
DS18B20采用的是单总线通信协议
OneWire.h
#ifndef __ONEWIRE_H__
#define __ONEWIRE_H__
unsigned char OneWire_Init(void);//初始化
void OneWire_SendBit(unsigned char Bit);//发送一位
unsigned char OneWire_ReceiveBit(void);//接收一位
void OneWire_SendByte(unsigned char Byte);//发送一个字节
unsigned char OneWire_ReceiveByte(void);//接收一个字节
#endif
OneWire.c
#include <REGX52.H>
sbit OneWire_DQ=P3^7;//定义端口
sbit DS1302_CE=P3^5;//定义端口CE
sbit DS1302_SCLK=P3^6;//定义端口SCLK
unsigned char OneWire_Init(void)//初始化
{
unsigned char i;
unsigned char AckBit;
DS1302_CE=1;
DS1302_SCLK=1;
OneWire_DQ=1;//总线拉低先置1
OneWire_DQ=0;//再把总线拉低
i = 247;while (--i);//延时500us通过ISP软件生成
OneWire_DQ=1;//释放
i = 32;while (--i);//延时70us
AckBit=OneWire_DQ;//将I/O口的电平读出
i = 247;while (--i);//延时500us
return AckBit;
}
void OneWire_SendBit(unsigned char Bit)//发送一位
{
unsigned char i;
OneWire_DQ=0;//拉低
i = 4;while (--i);//延时10us,调用一个函数需要4us,所以需要总时间是14us
OneWire_DQ=Bit;
i = 24;while (--i);//延时50us
OneWire_DQ=1;//拉高
}
unsigned char OneWire_ReceiveBit(void)//接收一位
{
unsigned char i;
unsigned char Bit;
OneWire_DQ=0;
i = 2;while (--i);//延时5us
OneWire_DQ=1;
i = 2;while (--i);//延时5us
Bit=OneWire_DQ;
i = 24;while (--i);//延时50us
return Bit;
}
void OneWire_SendByte(unsigned char Byte)//发送一个字节
{
unsigned char i;
for(i=0;i<8;i++)
{
OneWire_SendBit(Byte&(0x01<<i));//循环8次
}
}
unsigned char OneWire_ReceiveByte(void)//接收一个字节
{
unsigned char i;
unsigned char Byte=0x00;
for(i=0;i<8;i++)
{
if(OneWire_ReceiveBit()){Byte|=(0x01<<i);}//循环8次
}
return Byte;
}
DS18B20.h
#ifndef __DS18B20_H__
#define __DS18B20_H__
void DS18B20_ConvertT(void);//温度变换
float DS18B20_ReadT(void);//温度读取
#endif
DS18B20.c
#include <REGX52.H>
#include "OneWire.h"
//指令
#define DS18B20_SKIP_ROM 0xCC
#define DS18B20_CONVERT_T 0x44
#define DS18B20_READ_SCRATCHPAD 0xBE
void DS18B20_ConvertT(void)//温度变换
{
OneWire_Init();//初始化
OneWire_SendByte(DS18B20_SKIP_ROM);//发送一个字节
OneWire_SendByte(DS18B20_CONVERT_T);//发送一个字节
}
float DS18B20_ReadT(void)//温度读取
{
unsigned char TLSB,TMSB;
int Temp;
float T;
OneWire_Init();//初始化
OneWire_SendByte(DS18B20_SKIP_ROM);//发送一个字节
OneWire_SendByte(DS18B20_READ_SCRATCHPAD);//发送一个字节
TLSB=OneWire_ReceiveByte();//接收一个字节
TMSB=OneWire_ReceiveByte();//接收一个字节
Temp=(TMSB<<8)|TLSB;//TMSB左移8位与TLSB进行或运算
T=Temp/16.0;//除以16是因为存储时数据向左移动了4位,在转化为实际值时要除以16.
return T;
}
4.LCD液晶显示模块
LCD1602.h
#ifndef __LCD1602_H__
#define __LCD1602_H__
void LCD_Init();//初始化LCD显示屏
void LCD_Init2();//关闭LCD显示屏
void LCD_Delay();//延时函数
void LCD_WriteCommand(unsigned char Command);//写入LCD控制命令
void LCD_WriteData(unsigned char Data);//向LCD写入数据
void LCD_SetCursor(unsigned char Line,unsigned char Column);//设置LCD屏幕上的光标位置
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char);//显示char类型的字符
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String);//显示字符串类型的数据
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);//第几行,第几列,显示无符号数字,数字长度
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length);//第几行,第几列,显示有符号数字,数字长度
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);//第几行,第几列,显示无符号十六进制数字,数字长度
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);//第几行,第几列,显示无符号二进制数字,数字长度
#endif
LCD1602.c
#include <REGX52.H>
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_EN=P2^7;
#define LCD_DataPort P0
void LCD_Delay()//延时函数
{
unsigned char i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
void LCD_WriteCommand(unsigned char Command)//写入LCD控制命令
{
LCD_RS=0;//写命令
LCD_RW=0;//写
LCD_DataPort=Command;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
void LCD_WriteData(unsigned char Data)//向LCD写入数据
{
LCD_RS=1;//写数据
LCD_RW=0;//写
LCD_DataPort=Data;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
void LCD_SetCursor(unsigned char Line,unsigned char Column)//设置LCD屏幕上的光标位置
{
if(Line==1)
{
LCD_WriteCommand(0x80|(Column-1));
}
else if(Line==2)
{
LCD_WriteCommand(0x80|(Column-1+0x40));
}
}
void LCD_Init()//初始化LCD显示屏
{
LCD_WriteCommand(0x38);//设置LCD显示模式为2行显示,每行5x7点阵字符
LCD_WriteCommand(0x0c);//打开显示屏且光标不可见
LCD_WriteCommand(0x06);//设置光标移动的方向为向右
LCD_WriteCommand(0x01);//清除显示屏上的内容
}
void LCD_Init2()//关闭LCD显示屏
{
LCD_WriteCommand(0x08);//关闭显示
}
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)//显示char类型的字符
{
LCD_SetCursor(Line,Column);//在第Line行,第Column列显示
LCD_WriteData(Char);//向LCD写入数据char
}
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)//显示字符串类型的数据
{
unsigned char i;
LCD_SetCursor(Line,Column);//在第Line行,第Column列显示
for(i=0;String[i]!='\0';i++)
{
LCD_WriteData(String[i]);
}
}
int LCD_Pow(int X,int Y)
{
unsigned char i;
int Result=1;
for(i=0;i<Y;i++)
{
Result*=X;
}
return Result;
}
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)//第几行,第几列,显示无符号数字,数字长度
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
}
}
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)//第几行,第几列,显示有符号数字,数字长度
{
unsigned char i;
unsigned int Number1;
LCD_SetCursor(Line,Column);
if(Number>=0)
{
LCD_WriteData('+');
Number1=Number;
}
else
{
LCD_WriteData('-');
Number1=-Number;
}
for(i=Length;i>0;i--)
{
LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');
}
}
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)//第几行,第几列,显示无符号十六进制数字,数字长度
{
unsigned char i,SingleNumber;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
SingleNumber=Number/LCD_Pow(16,i-1)%16;
if(SingleNumber<10)
{
LCD_WriteData(SingleNumber+'0');
}
else
{
LCD_WriteData(SingleNumber-10+'A');
}
}
}
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)//第几行,第几列,显示无符号二进制数字,数字长度
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');
}
}
5.时钟模块
DS1302.h
#ifndef __DS1302_H__//防止重复定义
#define __DS1302_H__
extern unsigned char DS1302_Time[];
void DS1302_Init(void);//初始化函数
void DS1302_WriteByte(unsigned char Command,Data);//单字节写
unsigned char DS1302_ReadByte(unsigned char Command);//单字节读
void DS1302_SetTime(void);//写入时间
void DS1302_ReadTime(void);//读取时间
#endif
DS1302.c
#include <REGX52.H>
sbit DS1302_SCLK=P3^6;//定义端口SCLK
sbit DS1302_IO=P3^4;//定义端口IO
sbit DS1302_CE=P3^5;//定义端口CE
#define DS1302_SECOND 0x80//秒
#define DS1302_MINUTE 0x82//分钟
#define DS1302_HOUR 0x84//小时
#define DS1302_DATE 0x86//日
#define DS1302_MONTH 0x88//月
#define DS1302_DAY 0x8A//星期
#define DS1302_YEAR 0x8C//年
#define DS1302_WP 0x8E//写保护
char DS1302_Time[]={19,11,16,12,59,55,6};//定义数组存时间:年,月,日,时,分,秒,星期
void DS1302_Init(void)//初始化函数
{
DS1302_CE=0;//CE置0
DS1302_SCLK=0;//SCLK置0
}
void DS1302_WriteByte(unsigned char Command,Data)//单字节写
{
unsigned char i;
DS1302_CE=1;//CE置1
for(i=0;i<8;i++)
{
DS1302_IO=Command&(0x01<<i);
DS1302_SCLK=1;
DS1302_SCLK=0;
}
for(i=0;i<8;i++)
{
DS1302_IO=Data&(0x01<<i);
DS1302_SCLK=1;
DS1302_SCLK=0;
}
DS1302_CE=0;
}
unsigned char DS1302_ReadByte(unsigned char Command)//单字节读
{
unsigned char i,Data=0x00;
Command|=0x01;
DS1302_CE=1;
for(i=0;i<8;i++)
{
DS1302_IO=Command&(0x01<<i);
DS1302_SCLK=0;
DS1302_SCLK=1;
}
for(i=0;i<8;i++)
{
DS1302_SCLK=1;
DS1302_SCLK=0;
if(DS1302_IO){Data|=(0x01<<i);}
}
DS1302_CE=0;
DS1302_IO=0;
return Data;
}
void DS1302_SetTime(void)//写入时间
{
DS1302_WriteByte(DS1302_WP,0x00);
if(DS1302_Time[0]>=0&&DS1302_Time[0]<=99)
{
DS1302_WriteByte(DS1302_YEAR,DS1302_Time[0]/10*16+DS1302_Time[0]%10);
}
else
{
DS1302_WriteByte(DS1302_YEAR,0);
}
if(DS1302_Time[1]>0&&DS1302_Time[1]<=12)
{
DS1302_WriteByte(DS1302_MONTH,DS1302_Time[1]/10*16+DS1302_Time[1]%10);
}
else
{
DS1302_WriteByte(DS1302_MONTH,1);
}
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]>31&&DS1302_Time[2]<1)
{
DS1302_WriteByte(DS1302_DATE,1);
}
else
{
DS1302_WriteByte(DS1302_DATE,DS1302_Time[2]/10*16+DS1302_Time[2]%10);
}
}
else
if(DS1302_Time[1]==4||DS1302_Time[1]==6||DS1302_Time[1]==9||DS1302_Time[1]==11)
{
if(DS1302_Time[2]>30&&DS1302_Time[2]<1)
{
DS1302_WriteByte(DS1302_DATE,1);
}
else
{
DS1302_WriteByte(DS1302_DATE,DS1302_Time[2]/10*16+DS1302_Time[2]%10);
}
}
else if(DS1302_Time[1]==2)
{
if(DS1302_Time[0]%4==0)
{
if(DS1302_Time[2]>29&&DS1302_Time[2]<1)
{
DS1302_WriteByte(DS1302_DATE,1);
}
else
{
DS1302_WriteByte(DS1302_DATE,DS1302_Time[2]/10*16+DS1302_Time[2]%10);
}
}
else
{
if(DS1302_Time[2]>28&&DS1302_Time[2]<1)
{
DS1302_WriteByte(DS1302_DATE,1);
}
else
{
DS1302_WriteByte(DS1302_DATE,DS1302_Time[2]/10*16+DS1302_Time[2]%10);
}
}
}
if(DS1302_Time[3]>23&&DS1302_Time[3]<0)
{
DS1302_WriteByte(DS1302_HOUR,0);
}
else
{
DS1302_WriteByte(DS1302_HOUR,DS1302_Time[3]/10*16+DS1302_Time[3]%10);
}
if(DS1302_Time[4]>59&&DS1302_Time[4]<0)
{
DS1302_WriteByte(DS1302_MINUTE,0);
}
else
{
DS1302_WriteByte(DS1302_MINUTE,DS1302_Time[4]/10*16+DS1302_Time[4]%10);
}
if(DS1302_Time[5]>59&&DS1302_Time[5]<0)
{
DS1302_WriteByte(DS1302_SECOND,0);
}
else
{
DS1302_WriteByte(DS1302_SECOND,DS1302_Time[5]/10*16+DS1302_Time[5]%10);
}
if(DS1302_Time[6]<1&&DS1302_Time[6]>7)
{
DS1302_WriteByte(DS1302_DAY,1);
}
else
{
DS1302_WriteByte(DS1302_DAY,DS1302_Time[6]/10*16+DS1302_Time[6]%10);
}
DS1302_WriteByte(DS1302_WP,0x80);
}
void DS1302_ReadTime(void)//读取时间
{
unsigned char Temp;
Temp=DS1302_ReadByte(DS1302_YEAR);
DS1302_Time[0]=Temp/16*10+Temp%16;
Temp=DS1302_ReadByte(DS1302_MONTH);
DS1302_Time[1]=Temp/16*10+Temp%16;
Temp=DS1302_ReadByte(DS1302_DATE);
DS1302_Time[2]=Temp/16*10+Temp%16;
Temp=DS1302_ReadByte(DS1302_HOUR);
DS1302_Time[3]=Temp/16*10+Temp%16;
Temp=DS1302_ReadByte(DS1302_MINUTE);
DS1302_Time[4]=Temp/16*10+Temp%16;
Temp=DS1302_ReadByte(DS1302_SECOND);
DS1302_Time[5]=Temp/16*10+Temp%16;
Temp=DS1302_ReadByte(DS1302_DAY);
DS1302_Time[6]=Temp/16*10+Temp%16;
}
6.存储模块
I2C.h
#ifndef __I2C_H__
#define __I2C_H__
void I2C_Start(void);//起始条件
void I2C_Stop(void);//终止条件
void I2C_SendByte(unsigned char Byte);//发送一个字节
unsigned char I2C_ReceiveByte(void);//接收一个字节
void I2C_SendAck(unsigned char AckBit);//发送应答
unsigned char I2C_ReceiveAck(void);//接收应答
#endif
I2C.c
#include <REGX52.H>
sbit I2C_SCL=P2^1;
sbit I2C_SDA=P2^0;
void I2C_Start(void)//起始条件
{
I2C_SDA=1;
I2C_SCL=1;
I2C_SDA=0;
I2C_SCL=0;
}
void I2C_Stop(void)//终止条件
{
I2C_SDA=0;
I2C_SCL=1;
I2C_SDA=1;
}
void I2C_SendByte(unsigned char Byte)//发送一个字节
{
unsigned char i;
for(i=0;i<8;i++)
{
I2C_SDA=Byte&(0x80>>i);
I2C_SCL=1;
I2C_SCL=0;
}
}
unsigned char I2C_ReceiveByte(void)//接收一个字节
{
unsigned char i,Byte=0x00;
I2C_SDA=1;
for(i=0;i<8;i++)
{
I2C_SCL=1;
if(I2C_SDA){Byte|=(0x80>>i);}
I2C_SCL=0;
}
return Byte;
}
void I2C_SendAck(unsigned char AckBit)//发送应答
{
I2C_SDA=AckBit;
I2C_SCL=1;
I2C_SCL=0;
}
unsigned char I2C_ReceiveAck(void)//接收应答
{
unsigned char AckBit;
I2C_SDA=1;
I2C_SCL=1;
AckBit=I2C_SDA;
I2C_SCL=0;
return AckBit;
}
AT24C02.h
#ifndef __AT24C02_H__
#define __AT24C02_H__
void AT24C02_WriteByte(unsigned char WordAddress,Data);//字节写
unsigned char AT24C02_ReadByte(unsigned char WordAddress);//随机读
#endif
AT24C02.c
#include <REGX52.H>
#include "I2C.h"
#define AT24C02_ADDRESS 0xA0
void AT24C02_WriteByte(unsigned char WordAddress,Data)//字节写
{
I2C_Start();
I2C_SendByte(AT24C02_ADDRESS);
I2C_ReceiveAck();
I2C_SendByte(WordAddress);
I2C_ReceiveAck();
I2C_SendByte(Data);
I2C_ReceiveAck();
I2C_Stop();
}
unsigned char AT24C02_ReadByte(unsigned char WordAddress)//随机读
{
unsigned char Data;
I2C_Start();
I2C_SendByte(AT24C02_ADDRESS);
I2C_ReceiveAck();
I2C_SendByte(WordAddress);
I2C_ReceiveAck();
I2C_Start();
I2C_SendByte(AT24C02_ADDRESS|0x01);
I2C_ReceiveAck();
Data=I2C_ReceiveByte();
I2C_SendAck(1);
I2C_Stop();
return Data;
}
7.矩阵按键模块
MatrixKey.h
#ifndef __MATRIXKEY_H__
#define __MATRIXKEY_H__
unsigned char MatrixKey();//矩阵按键识别
#endif
MatrixKey.c
#include <REGX52.H>
#include "Delay.h"
unsigned char MatrixKey()//矩阵按键识别
{
unsigned char KeyNumber=0;
P1=0xFF;
P1_3=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=1;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=5;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=9;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=13;}
P1=0xFF;
P1_2=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=2;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=6;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=10;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=14;}
P1=0xFF;
P1_1=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=3;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=7;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=11;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=15;}
P1=0xFF;
P1_0=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=4;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=8;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=12;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=16;}
return KeyNumber;
}
8.独立按键模块
Key.h
#ifndef __KEY_H__
#define __KEY_H__
unsigned char Key();//独立按键识别
#endif
Key.c
#include"reg52.h"
#include"Delay.h"
sbit KEY4=P3^3;
unsigned char Key()//独立按键识别
{
unsigned char KeyNumber=0;
if(KEY4==0){Delay(20);while(KEY4==0);Delay(20);KeyNumber=4;}
return KeyNumber;
}
9.蜂鸣器模块
BEEP.h
#ifndef __BEEP_H__
#define __BEEP_H__
void BEEP_sound();//蜂鸣器响
#endif
BEEP.c
#include"reg52.h"
sbit BEEP=P2^5;
typedef unsigned char u8;
typedef unsigned int u16;
void delay_10us(u16 ten_us)
{
while(ten_us--);
}
void BEEP_sound()//蜂鸣器响
{
int i;
for(i=0;i<1000;i++)
{
BEEP=!BEEP;
delay_10us(10);
BEEP=!BEEP;
delay_10us(10);
}
}
10.定时器中断1模块
Timer1.h
#ifndef __TIMER1_H__
#define __TIMER1_H__
void Timer1_Init(void);//定时器1初始化
void Timer1_Init2(void);//关闭定时器1
#endif
Timer1.c
#include <REGX52.H>
void Timer1_Init(void)
{
//定时器1将被配置为工作在模式1(16位自动重载模式)
TMOD&=0x0F;//将TMOD的低4位保留,将高4位清零
TMOD|=0x10;//将TMOD的高4位保留,将低4位置1
TL1=(65536-10000)%256;
TH1=(65536-10000)/256;
TF1=0;//定时器1溢出标志位置0
TR1=0;//启动定时器1
ET1=1;//允许定时器1的中断
EA=1;//总开关打开
PT1=0;//将其置为0,表示将定时器1中断设置为低优先级。
}
void Timer1_Init2(void)
{
TR1=0;//关闭定时器0
}
11.红外模块
Int0.h
#ifndef __INT0_H__
#define __INT0_H__
void Int0_Init(void);//外部中断0初始化
#endif
Int0.c
#include <REGX52.H>
void Int0_Init(void)//外部中断0初始化
{
IT0=1;//设置外部中断0的触发方式为边沿触发方式(上升沿或下降沿触发)。
IE0=0;//禁用外部中断0,即在初始化时不允许外部中断0触发。
EX0=1;//使能外部中断0,即允许外部中断0触发。
EA=1;//总中断使能,即允许所有中断请求。
PX0=1;//设置外部中断0的优先级为高优先级。
}
IR.h
#ifndef __IR_H__
#define __IR_H__
#define IR_POWER 0x45
#define IR_MODE 0x46
#define IR_MUTE 0x47
#define IR_START_STOP 0x44
#define IR_PREVIOUS 0x40
#define IR_NEXT 0x43
#define IR_EQ 0x07
#define IR_VOL_MINUS 0x15
#define IR_VOL_ADD 0x09
#define IR_0 0x16
#define IR_RPT 0x19
#define IR_USD 0x0D
#define IR_1 0x0C
#define IR_2 0x18
#define IR_3 0x5E
#define IR_4 0x08
#define IR_5 0x1C
#define IR_6 0x5A
#define IR_7 0x42
#define IR_8 0x52
#define IR_9 0x4A
void IR_Init(void);//红外初始化
unsigned char IR_GetDataFlag(void);//获取数据
unsigned char IR_GetRepeatFlag(void);//获取重复
unsigned char IR_GetAddress(void);//获取地址
unsigned char IR_GetCommand(void);//获取命令
#endif
IR.c
#include <REGX52.H>
#include "Timer0.h"
#include "Int0.h"
unsigned int IR_Time;
unsigned char IR_State;
unsigned char IR_Data[4];
unsigned char IR_pData;
unsigned char IR_DataFlag;
unsigned char IR_RepeatFlag;
unsigned char IR_Address;
unsigned char IR_Command;
void IR_Init(void)//红外初始化
{
Timer0_Init();//定时器0初始化
Int0_Init();//外部中断0初始化
}
unsigned char IR_GetDataFlag(void)//获取数据
{
if(IR_DataFlag)
{
IR_DataFlag=0;
return 1;
}
return 0;
}
unsigned char IR_GetRepeatFlag(void)//获取重复
{
if(IR_RepeatFlag)
{
IR_RepeatFlag=0;
return 1;
}
return 0;
}
unsigned char IR_GetAddress(void)//获取地址
{
return IR_Address;
}
unsigned char IR_GetCommand(void)//获取命令
{
return IR_Command;
}
void Int0_Routine(void) interrupt 0//外部中断0
{
if(IR_State==0)
{
Timer0_SetCounter(0);
Timer0_Run(1);
IR_State=1;
}
else if(IR_State==1)
{
IR_Time=Timer0_GetCounter();
Timer0_SetCounter(0);
if(IR_Time>12442-500&&IR_Time<12442+500)
{
IR_State=2;
}
else if(IR_Time>10368-500&&IR_Time<10368+500)
{
IR_RepeatFlag=1;
Timer0_Run(0);
IR_State=0;
}
else
{
IR_State=1;
}
}
else if(IR_State==2)
{
IR_Time=Timer0_GetCounter();
Timer0_SetCounter(0);
if(IR_Time>1032-500&&IR_Time<1032+500)
{
IR_Data[IR_pData/8]&=~(0x01<<(IR_pData%8));
IR_pData++;
}
else if(IR_Time>2074-500&&IR_Time<2074+500)
{
IR_Data[IR_pData/8]|=(0x01<<(IR_pData%8));
IR_pData++;
}
else
{
IR_pData=0;
IR_State=1;
}
if(IR_pData>=32)
{
IR_pData=0;
if((IR_Data[0]==~IR_Data[1])&&(IR_Data[2]==~IR_Data[3]))
{
IR_Address=IR_Data[0];
IR_Command=IR_Data[2];
IR_DataFlag=1;
}
Timer0_Run(0);
IR_State=0;
}
}
}
12.定时器中断0模块
Timer0.h
#ifndef __TIMER0_H__
#define __TIMER0_H__
void Timer0_Init(void);//定时器0初始化
void Timer0_SetCounter(unsigned int Value);//设置TH0和TL0的初值
unsigned int Timer0_GetCounter(void);//返回定时器中所记录的值
void Timer0_Run(unsigned char Flag);//设置定时器是否启动
#endif
Timer0.c
#include <REGX52.H>
void Timer0_Init(void)//定时器0初始化
{
TMOD &= 0xF0;//将TMOD寄存器的低4位清零,保留高4位不变。这是为了确保定时器0的工作模式只受到TMOD寄存器的高4位控制。
TMOD |= 0x01;//将TMOD寄存器的低4位设为01,表示定时器0工作在模式1下,即16位自动重装载定时器模式。
TL0 = 0;//将定时器0的低8位寄存器TL0清零。
TH0 = 0;//将定时器0的高8位寄存器TH0清零。
TF0 = 0;//将定时器0的溢出标志位TF0清零。
TR0 = 0;//将定时器0的运行控制位TR0置零,即停止定时器0的运行。
}
void Timer0_SetCounter(unsigned int Value)//设置TH0和TL0的初值
{
TH0=Value/256;
TL0=Value%256;
}
unsigned int Timer0_GetCounter(void)//返回定时器中所记录的值
{
return (TH0<<8)|TL0;
}
void Timer0_Run(unsigned char Flag)//设置定时器是否启动
{
TR0=Flag;
}
13.电机模块
SET.h
#ifndef __SET_H__
#define __SET_H__
void SET_UP(void);//打开电机
void SET_DOWN(void);//关闭电机
#endif
SET.c
#include<reg52.h>
sbit SET1=P2^2;
void SET_UP(void)
{
SET1=1;
}
void SET_DOWN(void)
{
SET1=0;
}
14.AD转换模块
XPT2046.h
#ifndef __XPT2046_H__
#define __XPT2046_H__
#define XPT2046_VBAT 0xAC
#define XPT2046_AUX 0xEC
#define XPT2046_XP 0x9C //0xBC
#define XPT2046_YP 0xDC
unsigned int XPT2046_ReadAD(unsigned char Command);//AD转化模块:SPI通信协议
#endif
XPT2046.c
#include <REGX52.H>
#include <INTRINS.H>
sbit XPY2046_DIN=P3^4;
sbit XPY2046_CS=P3^5;
sbit XPY2046_DCLK=P3^6;
sbit XPY2046_DOUT=P3^7;
unsigned int XPT2046_ReadAD(unsigned char Command)
{
unsigned char i;
unsigned int Data=0;
XPY2046_DCLK=0;
XPY2046_CS=0;
for(i=0;i<8;i++)
{
XPY2046_DIN=Command&(0x80>>i);
XPY2046_DCLK=1;
XPY2046_DCLK=0;
}
for(i=0;i<16;i++)
{
XPY2046_DCLK=1;
XPY2046_DCLK=0;
if(XPY2046_DOUT){Data|=(0x8000>>i);}
}
XPY2046_CS=1;
return Data>>8;
}
15.DHT11温湿度检测模块
DHT11.h
#ifndef __DHT11_H__
#define __DHT11_H__
#define uchar unsigned char
#define uint unsigned int
void DHT11_delay_ms(uint z);
void DHT11_receive(uchar rec_dat[]);//接收40位的数据
#endif
DHT11.c
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit Data=P2^3;//定义数据线
void DHT11_delay_us(uchar n)
{
while(--n);
}
void DHT11_delay_ms(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void DHT11_start()
{
Data=1;
DHT11_delay_us(2);
Data=0;
DHT11_delay_ms(20);//延时18ms以上
Data=1;
DHT11_delay_us(30);
}
uchar DHT11_rec_byte()//接收一个字节
{
uchar i,dat=0;
int AZ=0;
for(i=0;i<8;i++)//从高到低依次接收8位数据
{
while(!Data)//等待50us低电平过去
{
AZ++;
if(AZ==1000)
{
AZ=0;
break;
}
}
AZ=0;
DHT11_delay_us(8);//延时60us,如果还为高则数据为1,否则为0
dat<<=1;//移位使正确接收8位数据,数据为0时直接移位
if(Data==1)//数据为1时,使dat加1来接收数据1
dat+=1;
while(Data)//等待数据线拉低
{
AZ++;
if(AZ==1000)
{
AZ=0;
break;
}
}
}
return dat;
}
void DHT11_receive(uchar rec_dat[])//接收40位的数据
{
int AZ=0;
uchar R_H,R_L,T_H,T_L,RH,RL,TH,TL,revise;
DHT11_start();
if(Data==0)
{
while(Data==0)//等待拉高
{
AZ++;
if(AZ==1000)
{
AZ=0;
break;
}
}
AZ=0;
DHT11_delay_us(40);//拉高后延时80us
R_H=DHT11_rec_byte();//接收湿度高八位
R_L=DHT11_rec_byte();//接收湿度低八位
T_H=DHT11_rec_byte();//接收温度高八位
T_L=DHT11_rec_byte();//接收温度低八位
revise=DHT11_rec_byte();//接收校正位
DHT11_delay_us(25);//结束
if((R_H+R_L+T_H+T_L)==revise)//校正
{
RH=R_H;
RL=R_L;
TH=T_H;
TL=T_L;
}
/*数据处理,方便显示*/
rec_dat[0]='0'+(RH/10);
rec_dat[1]='0'+(RH%10);
rec_dat[2]='R';
rec_dat[3]='H';
rec_dat[4]=' ';
rec_dat[5]=' ';
rec_dat[6]='0'+(TH/10);
rec_dat[7]='0'+(TH%10);
rec_dat[8]='C';
}
}
16.蓝牙模块
uart.h
#ifndef __UART_H__
#define __UART_H__
void UartConfigurationInit(void);//初始化串口配置
#endif
uart.c
#include"reg52.h"
void UartConfigurationInit(){
SCON=0X50; //设置为工作方式1
TMOD=0x20; //设置定时器1工作方式为方式2
TH1=0xfd; //波特率9600
TL1=0xfd;
TR1=1; //启动定时器1
SM0=0;SM1=1; //串口方式1
REN=1; //允许接收
PCON=0x00; //关倍频
ES=1; //开串口中断
EA=1; //开总中断
TR1=1;
}
17.主函数代码
main.c
#include<intrins.h>
#include"reg52.h"
#include"INT.h"
#include"Delay.h"
#include"LCD1602.h"
#include"DS18B20.h"
#include"DS1302.h"
#include"AT24C02.h"
#include"MatrixKey.h"
#include"Key.h"
#include"BEEP.h"
#include"SET.h"
#include"Timer1.h"
#include"IR.h"
#include"XPT2046.h"
#include"DHT11.h"
#include"uart.h"
sbit K1=P3^2;
sbit K2=P3^3;
sbit SET1=P2^2;
float T,TShow,TH;
char TLow,THigh,TLow1,THigh1,TLow2,THigh2;
static char Sw,Sw1,St,Swt,Stt;//用于让蜂鸣器是否工作
unsigned char KeyNum;//用于温度设置或时间设置,用于调节温度高低,用于调节时间
unsigned char MODE,TimeSetSelect,TimeSetFlashFlag;
unsigned char Command;//键码
unsigned int ASD,str,stre,stre1,strw,strw1,strk,strk1,jstr;//计数
static unsigned int T0Count;
unsigned int ADValue;
unsigned int YU;
int YYY=0,WQ,WT,WI,J1,J2,J3;
unsigned char rec_dat[9];//用于显示的接收数据数组
void Show0(void);
void Show(void)//欢迎使用
{
LCD_ShowString(1,1,"< Welcome to >");
LCD_ShowString(2,1,"< this place >");
Delay(1000);
LCD_WriteCommand(0x01);//清除显示屏上的内容
}
void Temperature_Reading1(void)//第一次温度读取
{
DS18B20_ConvertT();//温度变换
TH=DS18B20_ReadT();//温度读取
}
void Power_off_save_S(void)//断电保护英文翻译
{
WQ=0;
while(WQ<50)
{
WQ++;
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
LCD_ShowString(1,4,"Power off");
LCD_ShowString(2,7,"save");
}
LCD_WriteCommand(0x01);
}
void Power_off_save(void)//断电保存模块
{
while(1)
{
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
LCD_ShowString(1,2,"T:");
LCD_ShowString(1,9,"B:");
LCD_ShowString(2,5,"H:");
WQ=AT24C02_ReadByte(6);
if(WQ==0)
{
LCD_ShowString(1,4,"high");
LCD_ShowString(1,11,"high");
}
else
if(WQ==1)
{
LCD_ShowString(1,4,"high");
LCD_ShowString(1,11,"right");
}
else
if(WQ==2)
{
LCD_ShowString(1,4,"high");
LCD_ShowString(1,11,"low");
}
else
if(WQ==3)
{
LCD_ShowString(1,4,"right");
LCD_ShowString(1,11,"high");
}
else
if(WQ==4)
{
LCD_ShowString(1,4,"right");
LCD_ShowString(1,11,"right");
}
else
if(WQ==5)
{
LCD_ShowString(1,4,"right");
LCD_ShowString(1,11,"low");
}
else
if(WQ==6)
{
LCD_ShowString(1,4,"low");
LCD_ShowString(1,11,"high");
}
else
if(WQ==7)
{
LCD_ShowString(1,4,"low");
LCD_ShowString(1,11,"right");
}
else
if(WQ==8)
{
LCD_ShowString(1,4,"low");
LCD_ShowString(1,11,"low");
}
WQ=AT24C02_ReadByte(7);
if(WQ==0)
{
LCD_ShowString(2,7,"high");
}
else
if(WQ==1)
{
LCD_ShowString(2,7,"right");
}
else
if(WQ==2)
{
LCD_ShowString(2,7,"low");
}
if(YU==0)
{
KeyNum=MatrixKey();
}
YU=0;
if(KeyNum==16)
{
break;
}
if(KeyNum==8)
{
LCD_WriteCommand(0x01);
Power_off_save_S();
}
if(IR_GetDataFlag())//是否接收数据或重复(连加功能)
{
Command=IR_GetCommand();//读取命令
if(Command==IR_EQ)
{
Command=0x00;
break;
}
if(Command==IR_RPT)
{
Command=0x00;
LCD_WriteCommand(0x01);
Power_off_save_S();
}
}
}
LCD_WriteCommand(0x01);
}
void work(void)//电机和蜂鸣器工作
{
DS18B20_ConvertT();//温度变换
T=DS18B20_ReadT();//温度读取
THigh=AT24C02_ReadByte(0);//读取寄存器地址0上的数据传给THigh
TLow=AT24C02_ReadByte(1);//读取寄存器地址1上的数据传给TLow
ADValue=XPT2046_ReadAD(XPT2046_VBAT);//光敏电阻
THigh1=AT24C02_ReadByte(2);//读取寄存器地址2上的数据传给THigh
TLow1=AT24C02_ReadByte(3);//读取寄存器地址3上的数据传给TLow
DHT11_receive(rec_dat);
ASD=(int)(rec_dat[0]-48)*10+(int)(rec_dat[1]-48);
THigh2=AT24C02_ReadByte(4);//读取寄存器地址4上的数据传给THigh
TLow2=AT24C02_ReadByte(5);//读取寄存器地址5上的数据传给TLow
if(T>TH+10||T<TH-10)//温度读取异常,温度范围控制
{
T=TH;
}
else
{
TH=T;
}
if(T>THigh||ADValue>THigh1||ASD>THigh2)
{
if(Sw==0)
{
BEEP_sound();
}
if(St==0&&T>THigh)
{
SET_UP();
}
else
{
SET_DOWN();
}
}
else if(T<TLow||ADValue<TLow1||(ASD<TLow2&&ASD!=0))
{
if(Sw==0)
{
BEEP_sound();
}
SET_DOWN();
}
else
{
SET_DOWN();
}
if(T>THigh&&ADValue>THigh1)
{
AT24C02_WriteByte(6,0);
Delay(5);
}
else
if(T>THigh&&ADValue<THigh1&&ADValue>TLow1)
{
AT24C02_WriteByte(6,1);
Delay(5);
}
else
if(T>THigh&&ADValue<TLow1)
{
AT24C02_WriteByte(6,2);
Delay(5);
}
else
if(T<THigh&&T>TLow&&ADValue>THigh1)
{
AT24C02_WriteByte(6,3);
Delay(5);
}
else
if(T<THigh&&T>TLow&&ADValue<THigh1&&ADValue>TLow1)
{
AT24C02_WriteByte(6,4);
Delay(5);
}
else
if(T<THigh&&T>TLow&&ADValue<TLow1)
{
AT24C02_WriteByte(6,5);
Delay(5);
}
else
if(T<TLow&&ADValue>THigh1)
{
AT24C02_WriteByte(6,6);
Delay(5);
}
else
if(T<TLow&&ADValue<THigh1&&ADValue>TLow1)
{
AT24C02_WriteByte(6,7);
Delay(5);
}
else
if(T<TLow&&ADValue<TLow1)
{
AT24C02_WriteByte(6,8);
Delay(5);
}
if(ASD>THigh2)
{
AT24C02_WriteByte(7,0);
Delay(5);
}
else
if(ASD<THigh2&&ASD>TLow2)
{
AT24C02_WriteByte(7,1);
Delay(5);
}
else
if(ASD<TLow2&&ASD!=0)
{
AT24C02_WriteByte(7,2);
Delay(5);
}
}
void Show0_S(void)
{
WQ=0;
while(WQ<10)
{
WQ++;
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
LCD_ShowString(1,7,"time");
LCD_ShowString(2,6,"display");
}
LCD_WriteCommand(0x01);
}
void Show0_R(void)
{
WQ=0;
J1=J2=J3=0;
while(1)
{
DS18B20_ConvertT();//温度变换
T=DS18B20_ReadT();//温度读取
if(T>TH+10||T<TH-10)//温度读取异常,温度范围控制
{
T=TH;
}
else
{
TH=T;
}
THigh=AT24C02_ReadByte(0);//读取寄存器地址0上的数据传给THigh
TLow=AT24C02_ReadByte(1);//读取寄存器地址1上的数据传给TLow
ADValue=XPT2046_ReadAD(XPT2046_VBAT);//光敏电阻
THigh1=AT24C02_ReadByte(2);//读取寄存器地址2上的数据传给THigh
TLow1=AT24C02_ReadByte(3);//读取寄存器地址3上的数据传给TLow
DHT11_receive(rec_dat);
ASD=(int)(rec_dat[0]-48)*10+(int)(rec_dat[1]-48);
THigh2=AT24C02_ReadByte(4);//读取寄存器地址4上的数据传给THigh
TLow2=AT24C02_ReadByte(5);//读取寄存器地址5上的数据传给TLow
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
LCD_ShowString(1,2,"T:");
LCD_ShowString(1,9,"B:");
LCD_ShowString(2,5,"H:");
if(T>THigh)
{
if(J1!=1)
LCD_ShowString(1,7," ");
LCD_ShowString(1,4,"high");
J1=1;
}
else if(T<TLow)
{
if(J1!=2)
LCD_ShowString(1,7," ");
LCD_ShowString(1,4,"low");
J1=2;
}
else
{
if(J1!=3)
LCD_ShowString(1,7," ");
LCD_ShowString(1,4,"right");
J1=3;
}
if(ADValue>THigh1)
{
if(J2!=1)
LCD_ShowString(1,14," ");
LCD_ShowString(1,11,"high");
J2=1;
}
else if(ADValue<TLow1)
{
if(J2!=2)
LCD_ShowString(1,14," ");
LCD_ShowString(1,11,"low");
J2=2;
}
else
{
if(J2!=3)
LCD_ShowString(1,14," ");
LCD_ShowString(1,11,"right");
J2=3;
}
if(ASD>THigh2)
{
if(J3!=1)
LCD_ShowString(2,10," ");
LCD_ShowString(2,7,"high");
J3=1;
}
else if(ASD<TLow2)
{
if(J3!=2)
LCD_ShowString(2,10," ");
LCD_ShowString(2,7,"low");
J3=2;
}
else
{
if(J3!=3)
LCD_ShowString(2,10," ");
LCD_ShowString(2,7,"right");
J3=3;
}
if(YU==0)
{
KeyNum=MatrixKey();
}
YU=0;
if(WQ==1000||KeyNum==16)
{
break;
}
if(IR_GetDataFlag())//是否接收数据或重复(连加功能)
{
Command=IR_GetCommand();//读取命令
if(Command==IR_EQ)
{
Command=0x00;
break;
}
}
WQ++;
}
LCD_WriteCommand(0x01);
}
void TimeSet(void)//时间调节
{
str++;
if(Command==IR_PREVIOUS)
{
if(TimeSetSelect==0)
{
TimeSetSelect=5;
}
else
{
TimeSetSelect--;
}
Command=0x00;
}
else
if(Command==IR_NEXT)
{
TimeSetSelect++;
TimeSetSelect%=6;
Command=0x00;
}
if(KeyNum==10)
{
if(TimeSetSelect==0)
{
TimeSetSelect=5;
}
else
{
TimeSetSelect--;
}
}
if(KeyNum==12)
{
TimeSetSelect++;
TimeSetSelect%=6;
}
if(Command==IR_VOL_ADD||KeyNum==7)
{
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]==5||DS1302_Time[1]==7||DS1302_Time[1]==8||DS1302_Time[1]==10||DS1302_Time[1]==12)
{
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]>30)
{
DS1302_Time[2]=1;
}
}
else if(DS1302_Time[1]==2)
{
if(DS1302_Time[0]%4==0)
{
if(DS1302_Time[2]>29)
{
DS1302_Time[2]=1;
}
}
else
{
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;
}
Command=0x00;
}
if(Command==IR_VOL_MINUS||KeyNum==15)
{
if(TimeSetSelect==0&&DS1302_Time[0]==0)
{
DS1302_Time[0]=99;
}
else
if(TimeSetSelect==1&&DS1302_Time[1]==1)
{
DS1302_Time[1]=12;
}
else
if(TimeSetSelect==2&&(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)&&DS1302_Time[2]==1)
{
DS1302_Time[2]=31;
}
else
if(TimeSetSelect==2&&(DS1302_Time[1]==4||DS1302_Time[1]==6||DS1302_Time[1]==9||DS1302_Time[1]==11)&&DS1302_Time[2]==1)
{
DS1302_Time[2]=30;
}
else
if(TimeSetSelect==2&&DS1302_Time[1]==2&&DS1302_Time[0]%4==0&&DS1302_Time[2]==1)
{
DS1302_Time[2]=29;
}
else
if(TimeSetSelect==2&&DS1302_Time[1]==2&&DS1302_Time[0]%4!=0&&DS1302_Time[2]==1)
{
DS1302_Time[2]=28;
}
else
if(TimeSetSelect==3&&DS1302_Time[3]==0)
{
DS1302_Time[3]=23;
}
else
if(TimeSetSelect==4&&DS1302_Time[4]==0)
{
DS1302_Time[4]=59;
}
else
if(TimeSetSelect==5&&DS1302_Time[5]==0)
{
DS1302_Time[5]=59;
}
else
{
DS1302_Time[TimeSetSelect]--;
}
Command=0x00;
}
if(str==4)
{
TimeSetFlashFlag=!TimeSetFlashFlag;
str=0;
}
if(TimeSetSelect==0 && TimeSetFlashFlag==1){LCD_ShowString(1,6," ");}
else {LCD_ShowNum(1,6,DS1302_Time[0],2);}
if(TimeSetSelect==1 && TimeSetFlashFlag==1){LCD_ShowString(1,9," ");}
else {LCD_ShowNum(1,9,DS1302_Time[1],2);}
if(TimeSetSelect==2 && TimeSetFlashFlag==1){LCD_ShowString(1,12," ");}
else {LCD_ShowNum(1,12,DS1302_Time[2],2);}
if(TimeSetSelect==3 && TimeSetFlashFlag==1){LCD_ShowString(2,5," ");}
else {LCD_ShowNum(2,5,DS1302_Time[3],2);}
if(TimeSetSelect==4 && TimeSetFlashFlag==1){LCD_ShowString(2,8," ");}
else {LCD_ShowNum(2,8,DS1302_Time[4],2);}
if(TimeSetSelect==5 && TimeSetFlashFlag==1){LCD_ShowString(2,11," ");}
else {LCD_ShowNum(2,11,DS1302_Time[5],2);}
}
void Show0_1(void)//时间修改
{
DS1302_Init();//初始化DS1302
DS1302_ReadTime();//时间读取
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
LCD_ShowNum(1,4,2,1);
LCD_ShowNum(1,5,0,1);
LCD_ShowString(1,8,"-");
LCD_ShowString(1,11,"-");
LCD_ShowNum(1,6,DS1302_Time[0],2);
LCD_ShowNum(1,9,DS1302_Time[1],2);
LCD_ShowNum(1,12,DS1302_Time[2],2);
LCD_ShowString(2,7,":");
LCD_ShowString(2,10,":");
LCD_ShowNum(2,5,DS1302_Time[3],2);
LCD_ShowNum(2,8,DS1302_Time[4],2);
LCD_ShowNum(2,11,DS1302_Time[5],2);
}
void Show0_2(void)
{
Show0_1();
if(MODE==0){MODE=1;TimeSetSelect=0;}
else if(MODE==1){MODE=0;DS1302_SetTime();}
while(1)
{
if(YU==0)
{
KeyNum=MatrixKey();
}
YU=0;
if(KeyNum==11)
{
TimeSetSelect=0;
MODE=0;
DS1302_SetTime();
break;
}
if(IR_GetDataFlag())//是否接收数据或重复(连加功能)
{
Command=IR_GetCommand();//读取命令
if(Command==IR_EQ)
{
TimeSetSelect=0;
MODE=0;
DS1302_SetTime();
Command=0x00;
break;
}
}
switch(MODE)
{
case 0:Show0_1();break;
case 1:TimeSet();break;
}
}
}
void Show0(void)//时间显示
{
Temperature_Reading1();
while(1)
{
str=0;
work();
Show0_1();
if(YU==0)
{
KeyNum=MatrixKey();
}
YU=0;
if(KeyNum==11)
{
Show0_2();
}
if(KeyNum==7||KeyNum==10)
{
if(YYY>0)
{
--YYY;
}
else
{
YYY=5;
}
LCD_WriteCommand(0x01);
break;
}
if(KeyNum==12||KeyNum==15)
{
if(YYY<5)
{
++YYY;
}
else
{
YYY=0;
}
LCD_WriteCommand(0x01);
break;
}
if(KeyNum==8)
{
LCD_WriteCommand(0x01);
Show0_S();
}
if(KeyNum==16)
{
LCD_WriteCommand(0x01);
Show0_R();
}
if(IR_GetDataFlag())//是否接收数据或重复(连加功能)
{
Command=IR_GetCommand();//读取命令
if(Command==IR_MODE)
{
Show0_2();
Command=0x00;
}
if(Command==IR_PREVIOUS)
{
if(YYY>0)
{
--YYY;
}
else
{
YYY=5;
}
LCD_WriteCommand(0x01);
Command=0x00;
break;
}
if(Command==IR_NEXT)
{
if(YYY<5)
{
++YYY;
}
else
{
YYY=0;
}
LCD_WriteCommand(0x01);
Command=0x00;
break;
}
if(Command==IR_RPT)
{
LCD_WriteCommand(0x01);
Show0_S();
Command=0x00;
}
if(Command==IR_MUTE)
{
LCD_WriteCommand(0x01);
Show0_R();
Command=0x00;
}
}
}
}
void Show1_S(void)
{
WQ=0;
while(WQ<10)
{
WQ++;
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
LCD_ShowString(1,3,"temperature");
LCD_ShowString(2,5,"display");
}
LCD_WriteCommand(0x01);
}
void Show1_R(void)
{
WQ=0;
while(WQ<10)
{
WQ++;
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
LCD_ShowString(1,6,"range");
LCD_ShowString(2,4,"[-55~+125]");
}
LCD_WriteCommand(0x01);
}
void TemperatureSet(void)
{
str++;
if(Command==IR_NEXT)
{
if(THigh<125){THigh++;}
Command=0x00;
}
else
if(Command==IR_PREVIOUS)
{
if(THigh>TLow+1){THigh--;}
Command=0x00;
}
if(Command==IR_VOL_ADD)
{
if(TLow<THigh-1){TLow++;}
Command=0x00;
}
else
if(Command==IR_VOL_MINUS)
{
if(TLow>-55){TLow--;}
Command=0x00;
}
if(KeyNum==7)
{
if(THigh<125){THigh++;}
}
else
if(KeyNum==15)
{
if(THigh>TLow+1){THigh--;}
}
if(KeyNum==12)
{
if(TLow<THigh-1){TLow++;}
}
else
if(KeyNum==10)
{
if(TLow>-55){TLow--;}
}
if(str==2)
{
TimeSetFlashFlag=!TimeSetFlashFlag;
str=0;
}
if(TimeSetFlashFlag==1)
{
LCD_ShowString(1,4," ");
LCD_ShowString(1,12," ");
}
else
{
LCD_ShowSignedNum(1,4,THigh,3);
LCD_ShowSignedNum(1,12,TLow,3);
}
AT24C02_WriteByte(0,THigh);
Delay(5);
AT24C02_WriteByte(1,TLow);
Delay(5);
LCD_ShowString(2,4,"T:");
DS18B20_ConvertT();//温度变换
T=DS18B20_ReadT();//温度读取
if(T>TH+10||T<TH-10)//温度读取异常,温度范围控制
{
T=TH;
}
else
{
TH=T;
}
if(T<0)
{
LCD_ShowChar(2,6,'-');
TShow=-T;
}
else
{
LCD_ShowChar(2,6,'+');
TShow=T;
}
LCD_ShowNum(2,7,TShow,3);
LCD_ShowChar(2,10,'.');
LCD_ShowNum(2,11,(unsigned long)(TShow*100)%100,2);
}
void Show1_1(void)//温度上下限修改
{
THigh=AT24C02_ReadByte(0);//读取寄存器地址0上的数据传给THigh
TLow=AT24C02_ReadByte(1);//读取寄存器地址1上的数据传给TLow
if(THigh>125||TLow<-55||THigh<=TLow)
{
THigh=20;
TLow=15;
}
LCD_ShowString(1,2,"H:");
LCD_ShowString(1,10,"L:");
LCD_ShowSignedNum(1,4,THigh,3);
LCD_ShowSignedNum(1,12,TLow,3);
LCD_ShowString(2,4,"T:");
DS18B20_ConvertT();//温度变换
T=DS18B20_ReadT();//温度读取
if(T>TH+10||T<TH-10)//温度读取异常,温度范围控制
{
T=TH;
}
else
{
TH=T;
}
if(T<0)
{
LCD_ShowChar(2,6,'-');
TShow=-T;
}
else
{
LCD_ShowChar(2,6,'+');
TShow=T;
}
if(T!=0)
{
LCD_ShowNum(2,7,TShow,3);
LCD_ShowChar(2,10,'.');
LCD_ShowNum(2,11,(unsigned long)(TShow*100)%100,2);
}
if(T>THigh||T<TLow)
{
WT++;
if(WT>=2&&WT<4)
{
LCD_ShowString(1,1," ");
LCD_ShowString(1,16," ");
LCD_ShowString(2,1," ");
LCD_ShowString(2,16," ");
}
else
if(WT>=0&&WT<2)
{
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
}
else
WT=0;
}
else
{
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
}
}
void Show1_2(void)
{
Show1_1();
if(MODE==0){MODE=1;}
else if(MODE==1){MODE=0;}
while(1)
{
if(YU==0)
{
KeyNum=MatrixKey();
}
YU=0;
if(KeyNum==11)
{
MODE=0;
break;
}
if(IR_GetDataFlag())//是否接收数据或重复(连加功能)
{
Command=IR_GetCommand();//读取命令
if(Command==IR_EQ)
{
MODE=0;
Command=0x00;
break;
}
}
switch(MODE)
{
case 0:Show1_1();break;
case 1:TemperatureSet();break;
}
}
}
void Show1(void)//温度显示
{
WQ=0;WT=0;
Temperature_Reading1();
while(1)
{
str=0;
Show1_1();
if(YU==0)
{
KeyNum=MatrixKey();
}
YU=0;
if(KeyNum==11)
{
Show1_2();
}
if(KeyNum==7||KeyNum==10)
{
if(YYY>0)
{
--YYY;
}
else
{
YYY=5;
}
LCD_WriteCommand(0x01);
break;
}
if(KeyNum==12||KeyNum==15)
{
if(YYY<5)
{
++YYY;
}
else
{
YYY=0;
}
LCD_WriteCommand(0x01);
break;
}
if(WQ==1000||KeyNum==6)
{
LCD_WriteCommand(0x01);
YYY=0;
break;
}
if(KeyNum==8)
{
LCD_WriteCommand(0x01);
Show1_S();
}
if(KeyNum==16)
{
LCD_WriteCommand(0x01);
Show1_R();
}
if(IR_GetDataFlag())//是否接收数据或重复(连加功能)
{
Command=IR_GetCommand();//读取命令
if(Command==IR_MODE)
{
Show1_2();
Command=0x00;
}
if(Command==IR_PREVIOUS)
{
if(YYY>0)
{
--YYY;
}
else
{
YYY=5;
}
LCD_WriteCommand(0x01);
Command=0x00;
break;
}
if(Command==IR_NEXT)
{
if(YYY<5)
{
++YYY;
}
else
{
YYY=0;
}
LCD_WriteCommand(0x01);
Command=0x00;
break;
}
if(Command==IR_POWER)
{
Command=0x00;
LCD_WriteCommand(0x01);
YYY=0;
break;
}
if(Command==IR_RPT)
{
LCD_WriteCommand(0x01);
Show1_S();
Command=0x00;
}
if(Command==IR_MUTE)
{
LCD_WriteCommand(0x01);
Show1_R();
Command=0x00;
}
}
WQ++;
}
}
void Show2_S(void)
{
WQ=0;
while(WQ<10)
{
WQ++;
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
LCD_ShowString(1,6,"bright");
LCD_ShowString(2,6,"display");
}
LCD_WriteCommand(0x01);
}
void Show2_R(void)
{
WQ=0;
while(WQ<10)
{
WQ++;
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
LCD_ShowString(1,6,"range");
LCD_ShowString(2,5,"[+0~+256]");
}
LCD_WriteCommand(0x01);
}
void BrightnessSet(void)
{
str++;
if(Command==IR_NEXT)
{
if(THigh<256){THigh++;}
Command=0x00;
}
else
if(Command==IR_PREVIOUS)
{
if(THigh>TLow+1){THigh--;}
Command=0x00;
}
if(Command==IR_VOL_ADD)
{
if(TLow<THigh-1){TLow++;}
Command=0x00;
}
else
if(Command==IR_VOL_MINUS)
{
if(TLow>0){TLow--;}
Command=0x00;
}
if(KeyNum==7)
{
if(THigh<256){THigh++;}
}
else
if(KeyNum==15)
{
if(THigh>TLow+1){THigh--;}
}
if(KeyNum==12)
{
if(TLow<THigh-1){TLow++;}
}
else
if(KeyNum==10)
{
if(TLow>0){TLow--;}
}
if(str==2)
{
TimeSetFlashFlag=!TimeSetFlashFlag;
str=0;
}
if(TimeSetFlashFlag==1)
{
LCD_ShowString(1,4," ");
LCD_ShowString(1,12," ");
}
else
{
LCD_ShowSignedNum(1,4,THigh,3);
LCD_ShowSignedNum(1,12,TLow,3);
}
AT24C02_WriteByte(2,THigh);
Delay(5);
AT24C02_WriteByte(3,TLow);
Delay(5);
LCD_ShowString(2,4,"B:");
ADValue=XPT2046_ReadAD(XPT2046_VBAT);//光敏电阻
LCD_ShowNum(2,6,ADValue,3);
}
void Show2_1(void)
{
THigh=AT24C02_ReadByte(2);//读取寄存器地址2上的数据传给THigh
TLow=AT24C02_ReadByte(3);//读取寄存器地址3上的数据传给TLow
if(THigh>256||TLow<0||THigh<=TLow)
{
THigh=30;
TLow=0;
}
LCD_ShowString(1,2,"H:");
LCD_ShowString(1,10,"L:");
LCD_ShowSignedNum(1,4,THigh,3);
LCD_ShowSignedNum(1,12,TLow,3);
LCD_ShowString(2,4,"B:");
ADValue=XPT2046_ReadAD(XPT2046_VBAT);//光敏电阻
LCD_ShowNum(2,6,ADValue,3);
if(ADValue>THigh||ADValue<TLow)
{
WT++;
if(WT>=2&&WT<4)
{
LCD_ShowString(1,1," ");
LCD_ShowString(1,16," ");
LCD_ShowString(2,1," ");
LCD_ShowString(2,16," ");
}
else
if(WT>=0&&WT<2)
{
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
}
else
WT=0;
}
else
{
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
}
}
void Show2_2(void)
{
Show2_1();
if(MODE==0){MODE=1;}
else if(MODE==1){MODE=0;}
while(1)
{
if(YU==0)
{
KeyNum=MatrixKey();
}
YU=0;
if(KeyNum==11)
{
MODE=0;
break;
}
if(IR_GetDataFlag())//是否接收数据或重复(连加功能)
{
Command=IR_GetCommand();//读取命令
if(Command==IR_EQ)
{
MODE=0;
Command=0x00;
break;
}
}
switch(MODE)
{
case 0:Show2_1();break;
case 1:BrightnessSet();break;
}
}
}
void Show2(void)//亮度指数显示
{
WQ=0;WT=0;
while(1)
{
str=0;
Show2_1();
if(YU==0)
{
KeyNum=MatrixKey();
}
YU=0;
if(KeyNum==11)
{
Show2_2();
}
if(KeyNum==7||KeyNum==10)
{
if(YYY>0)
{
--YYY;
}
else
{
YYY=5;
}
LCD_WriteCommand(0x01);
break;
}
if(KeyNum==12||KeyNum==15)
{
if(YYY<5)
{
++YYY;
}
else
{
YYY=0;
}
LCD_WriteCommand(0x01);
break;
}
if(WQ==1000||KeyNum==6)
{
LCD_WriteCommand(0x01);
YYY=0;
break;
}
if(KeyNum==8)
{
LCD_WriteCommand(0x01);
Show2_S();
}
if(KeyNum==16)
{
LCD_WriteCommand(0x01);
Show2_R();
}
if(IR_GetDataFlag())//是否接收数据或重复(连加功能)
{
Command=IR_GetCommand();//读取命令
if(Command==IR_MODE)
{
Show2_2();
Command=0x00;
}
if(Command==IR_PREVIOUS)
{
if(YYY>0)
{
--YYY;
}
else
{
YYY=5;
}
LCD_WriteCommand(0x01);
Command=0x00;
break;
}
if(Command==IR_NEXT)
{
if(YYY<5)
{
++YYY;
}
else
{
YYY=0;
}
LCD_WriteCommand(0x01);
Command=0x00;
break;
}
if(Command==IR_POWER)
{
Command=0x00;
LCD_WriteCommand(0x01);
YYY=0;
break;
}
if(Command==IR_RPT)
{
LCD_WriteCommand(0x01);
Show2_S();
Command=0x00;
}
if(Command==IR_MUTE)
{
LCD_WriteCommand(0x01);
Show2_R();
Command=0x00;
}
}
WQ++;
}
}
void Show3_S(void)
{
WQ=0;
while(WQ<10)
{
WQ++;
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
LCD_ShowString(1,5,"humidity");
LCD_ShowString(2,5,"display");
}
LCD_WriteCommand(0x01);
}
void Show3_R(void)
{
WQ=0;
while(WQ<10)
{
WQ++;
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
LCD_ShowString(1,6,"range");
LCD_ShowString(2,4,"[+0%~+100%]");
}
LCD_WriteCommand(0x01);
}
void HumiditySet(void)
{
str++;
if(Command==IR_NEXT)
{
if(THigh<100){THigh++;}
Command=0x00;
}
else
if(Command==IR_PREVIOUS)
{
if(THigh>TLow+1){THigh--;}
Command=0x00;
}
if(Command==IR_VOL_ADD)
{
if(TLow<THigh-1){TLow++;}
Command=0x00;
}
else
if(Command==IR_VOL_MINUS)
{
if(TLow>0){TLow--;}
Command=0x00;
}
if(KeyNum==7)
{
if(THigh<100){THigh++;}
}
else
if(KeyNum==15)
{
if(THigh>TLow+1){THigh--;}
}
if(KeyNum==12)
{
if(TLow<THigh-1){TLow++;}
}
else
if(KeyNum==10)
{
if(TLow>0){TLow--;}
}
if(str==2)
{
TimeSetFlashFlag=!TimeSetFlashFlag;
str=0;
}
if(TimeSetFlashFlag==1)
{
LCD_ShowString(1,4," ");
LCD_ShowString(1,12," ");
}
else
{
LCD_ShowSignedNum(1,4,THigh,3);
LCD_ShowSignedNum(1,12,TLow,3);
}
AT24C02_WriteByte(4,THigh);
Delay(5);
AT24C02_WriteByte(5,TLow);
Delay(5);
LCD_ShowString(2,4,"H:");
DHT11_receive(rec_dat);
LCD_ShowChar(2,6,rec_dat[0]);
LCD_ShowChar(2,7,rec_dat[1]);
LCD_ShowChar(2,8,'%');
}
void Show3_1(void)
{
THigh=AT24C02_ReadByte(4);//读取寄存器地址2上的数据传给THigh
TLow=AT24C02_ReadByte(5);//读取寄存器地址3上的数据传给TLow
if(THigh>100||TLow<0||THigh<=TLow)
{
THigh=75;
TLow=30;
}
LCD_ShowString(1,2,"H:");
LCD_ShowString(1,10,"L:");
LCD_ShowSignedNum(1,4,THigh,3);
LCD_ShowSignedNum(1,12,TLow,3);
LCD_ShowString(2,4,"H:");
DHT11_receive(rec_dat);
ASD=(int)(rec_dat[0]-48)*10+(int)(rec_dat[1]-48);
if(ASD!=0)
{
LCD_ShowChar(2,6,rec_dat[0]);
LCD_ShowChar(2,7,rec_dat[1]);
LCD_ShowChar(2,8,'%');
}
if(ASD>THigh||(ASD!=0&&ASD<TLow))
{
WT++;
if(WT>=2&&WT<4)
{
LCD_ShowString(1,1," ");
LCD_ShowString(1,16," ");
LCD_ShowString(2,1," ");
LCD_ShowString(2,16," ");
}
else
if(WT>=0&&WT<2)
{
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
}
else
WT=0;
}
else if(ASD==0||ASD<THigh||ASD>TLow)
{
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
}
}
void Show3_2(void)
{
Show3_1();
if(MODE==0){MODE=1;}
else if(MODE==1){MODE=0;}
while(1)
{
if(IR_GetDataFlag())//是否接收数据或重复(连加功能)
{
Command=IR_GetCommand();//读取命令
if(Command==IR_EQ)
{
MODE=0;
Command=0x00;
break;
}
}
if(YU==0)
{
KeyNum=MatrixKey();
}
YU=0;
if(KeyNum==11)
{
MODE=0;
break;
}
switch(MODE)
{
case 0:Show3_1();break;
case 1:HumiditySet();break;
}
}
}
void Show3(void)//湿度指数检测
{
WQ=0;WT=0;
while(1)
{
str=0;
Show3_1();
if(YU==0)
{
KeyNum=MatrixKey();
}
YU=0;
if(KeyNum==11)
{
Show3_2();
}
if(KeyNum==7||KeyNum==10)
{
if(YYY>0)
{
--YYY;
}
else
{
YYY=5;
}
LCD_WriteCommand(0x01);
break;
}
if(KeyNum==12||KeyNum==15)
{
if(YYY<5)
{
++YYY;
}
else
{
YYY=0;
}
LCD_WriteCommand(0x01);
break;
}
if(WQ==1000||KeyNum==6)
{
LCD_WriteCommand(0x01);
YYY=0;
break;
}
if(KeyNum==8)
{
LCD_WriteCommand(0x01);
Show3_S();
}
if(KeyNum==16)
{
LCD_WriteCommand(0x01);
Show3_R();
}
if(IR_GetDataFlag())//是否接收数据或重复(连加功能)
{
Command=IR_GetCommand();//读取命令
if(Command==IR_MODE)
{
Show3_2();
Command=0x00;
}
if(Command==IR_PREVIOUS)
{
if(YYY>0)
{
--YYY;
}
else
{
YYY=5;
}
LCD_WriteCommand(0x01);
Command=0x00;
break;
}
if(Command==IR_NEXT)
{
if(YYY<5)
{
++YYY;
}
else
{
YYY=0;
}
LCD_WriteCommand(0x01);
Command=0x00;
break;
}
if(Command==IR_POWER)
{
Command=0x00;
LCD_WriteCommand(0x01);
YYY=0;
break;
}
if(Command==IR_RPT)
{
LCD_WriteCommand(0x01);
Show3_S();
Command=0x00;
}
if(Command==IR_MUTE)
{
LCD_WriteCommand(0x01);
Show3_R();
Command=0x00;
}
}
WQ++;
}
}
void Show4(void)//蜂鸣器工作状态显示
{
WQ=0;
while(1)
{
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
LCD_ShowString(1,6,"Buzzer");
if(Sw==0)
{
LCD_ShowString(2,7,"YES");
}
else
{
LCD_ShowString(2,9," ");
LCD_ShowString(2,7,"NO");
}
if(YU==0)
{
KeyNum=MatrixKey();
}
YU=0;
if(KeyNum==11)
{
if(Sw==0)
{
Sw=1;
}
else
{
Sw=0;
}
}
if(KeyNum==7||KeyNum==10)
{
if(YYY>0)
{
--YYY;
}
else
{
YYY=5;
}
LCD_WriteCommand(0x01);
break;
}
if(KeyNum==12||KeyNum==15)
{
if(YYY<5)
{
++YYY;
}
else
{
YYY=0;
}
LCD_WriteCommand(0x01);
break;
}
if(WQ==1000||KeyNum==6)
{
LCD_WriteCommand(0x01);
YYY=0;
break;
}
if(IR_GetDataFlag())//是否接收数据或重复(连加功能)
{
Command=IR_GetCommand();//读取命令
if(Command==IR_START_STOP)
{
if(Sw==0)
{
Sw=1;
}
else
{
Sw=0;
}
Command=0x00;
}
if(Command==IR_PREVIOUS)
{
if(YYY>0)
{
--YYY;
}
else
{
YYY=5;
}
LCD_WriteCommand(0x01);
Command=0x00;
break;
}
if(Command==IR_NEXT)
{
if(YYY<5)
{
++YYY;
}
else
{
YYY=0;
}
LCD_WriteCommand(0x01);
Command=0x00;
break;
}
if(Command==IR_POWER)
{
Command=0x00;
LCD_WriteCommand(0x01);
YYY=0;
break;
}
}
WQ++;
}
}
void Show5(void)//电机工作状态显示
{
WQ=0;
while(1)
{
LCD_ShowString(1,1,"<");
LCD_ShowString(1,16,">");
LCD_ShowString(2,1,"<");
LCD_ShowString(2,16,">");
LCD_ShowString(1,6,"Motor");
if(St==0)
{
LCD_ShowString(2,7,"YES");
}
else
{
LCD_ShowString(2,9," ");
LCD_ShowString(2,7,"NO");
}
if(YU==0)
{
KeyNum=MatrixKey();
}
YU=0;
if(KeyNum==11)
{
if(St==1)
{
St=0;
}
else
{
St=1;
}
}
if(KeyNum==7||KeyNum==10)
{
if(YYY>0)
{
--YYY;
}
else
{
YYY=5;
}
LCD_WriteCommand(0x01);
break;
}
if(KeyNum==12||KeyNum==15)
{
if(YYY<5)
{
++YYY;
}
else
{
YYY=0;
}
LCD_WriteCommand(0x01);
break;
}
if(WQ==1000||KeyNum==6)
{
LCD_WriteCommand(0x01);
YYY=0;
break;
}
if(IR_GetDataFlag())//是否接收数据或重复(连加功能)
{
Command=IR_GetCommand();//读取命令
if(Command==IR_START_STOP)
{
if(St==1)
{
St=0;
}
else
{
St=1;
}
Command=0x00;
}
if(Command==IR_PREVIOUS)
{
if(YYY>0)
{
--YYY;
}
else
{
YYY=5;
}
LCD_WriteCommand(0x01);
Command=0x00;
break;
}
if(Command==IR_NEXT)
{
if(YYY<5)
{
++YYY;
}
else
{
YYY=0;
}
LCD_WriteCommand(0x01);
Command=0x00;
break;
}
if(Command==IR_POWER)
{
Command=0x00;
LCD_WriteCommand(0x01);
YYY=0;
break;
}
}
WQ++;
}
}
void main()
{
Init_INT1();//对外部中断1进行初始化
LCD_Init();//初始化LCD
IR_Init();//对IR进行初始化
UartConfigurationInit();//串口中断初始化
Sw=Sw1=St=0;
jstr=stre=strw=stre1=strw1=strk=strk1=0;
YU=0;
SET1=0;
Temperature_Reading1();
Show();
Power_off_save();//断电保存模块
while(1)
{
if(YYY==0)
{
Show0();
}
else
if(YYY==1)
{
Show1();
}
else
if(YYY==2)
{
Show2();
}
else
if(YYY==3)
{
Show3();
}
else
if(YYY==4)
{
Show4();
}
else
if(YYY==5)
{
Show5();
}
}
}
void Sending()
{
WQ=AT24C02_ReadByte(6);
if(WQ==0)
{
SBUF='T';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='H';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
SBUF='B';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='H';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
}
else
if(WQ==1)
{
SBUF='T';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='H';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
SBUF='B';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='R';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
}
else
if(WQ==2)
{
SBUF='T';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='H';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
SBUF='B';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='L';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
}
else
if(WQ==3)
{
SBUF='T';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='R';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
SBUF='B';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='H';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
}
else
if(WQ==4)
{
SBUF='T';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='R';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
SBUF='B';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='R';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
}
else
if(WQ==5)
{
SBUF='T';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='R';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
SBUF='B';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='L';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
}
else
if(WQ==6)
{
SBUF='T';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='L';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
SBUF='B';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='H';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
}
else
if(WQ==7)
{
SBUF='T';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='L';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
SBUF='B';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='R';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
}
else
if(WQ==8)
{
SBUF='T';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='L';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
SBUF='B';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='L';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
}
WQ=AT24C02_ReadByte(7);
if(WQ==0)
{
SBUF='H';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='H';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
}
else
if(WQ==1)
{
SBUF='H';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='R';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
}
else
if(WQ==2)
{
SBUF='H';while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF='L';while(!TI);TI=0;
SBUF=0x0D;while(!TI);TI=0;
}
}
void Receive(unsigned char m){
switch(m){
case 'A':
YU=1;
KeyNum=7;
break;
case 'B':
YU=1;
KeyNum=15;
break;
case 'C':
YU=1;
KeyNum=10;
break;
case 'D':
YU=1;
KeyNum=12;
break;
case 'E':
YU=1;
KeyNum=11;
break;
case 'F':
YU=1;
KeyNum=8;
break;
case 'G':
YU=1;
KeyNum=16;
break;
case 'H':
YU=1;
KeyNum=6;
break;
case 'I':
break;
default:
break;
}
}
void Timer1_Routine() interrupt 3//定时器中断1
{
TL1=(65536-10000)%256;
TH1=(65536-10000)/256;
T0Count++;
if(T0Count>=100)
{
T0Count=0;
stre++;
}
if(stre==60)
{
stre=0;
strw++;
}
if(strw==60)
{
strw=0;
strk++;
}
if(strk==60)
{
strk=0;
}
}
void Isr_INT1(void) interrupt 2//外部中断1
{
Delay(10);
if(K2==0)
{
if(Sw==0)
{
Sw=1;
}
else
{
Sw=0;
}
while(K2==0);
}
}
void Usart() interrupt 4
{
unsigned char ReceiveData;
ReceiveData=SBUF;
Receive(ReceiveData);
RI=0;//将接收中断标志位置0
SBUF=ReceiveData;
if(SBUF=='I')
{
SBUF=0x0D;while(!TI);TI=0;
Sending();
}
else
{
while(!TI);
TI=0;
}
}
功能实现
该代码能实现的功能包括,时间显示,时间修改,温度,亮度,湿度检测和所检测到的上下限范围的修改,温度,亮度,湿度异常时,蜂鸣器发生报警,温度过高电风扇转动。蜂鸣器和电风扇的使用和关闭。断电瞬间保存最后一刻所检测到的温度亮度湿度的状态信息,同时可以查看每个页面的英文说明和所要检测范围的查看,可以通过矩阵按键,独立按键遥控器和手机蓝牙三种方式进行控制。
引脚资源使用
延时模块:未使用引脚。
外部中断0模块:用于红外模块。
外部中断1模块:用于开关蜂鸣器。
温度模块:使用芯片DS18B20,采用onewire通信,使用引脚P3.5,P3.6,P3.7。
LCD1602显示模块:采用I2C通信,使用引脚P0,P2.5,P2.6,P2.7。
时钟模块:使用芯片DS1302,采用SPI串行总线进行通信,使用引脚P3.4,P3.5,P3.6。
存储模块:使用芯片AT24C02,采用I2C通信,使用引脚P2.0,P2.1。
矩阵按键模块:使用引脚P1。
独立按键模块:使用引脚P3.3。
蜂鸣器模块:使用引脚P2.5。
定时器中断1模块:用于定时器计数模式。
红外模块:无。
定时器中断0模块:用于红外模块。
电机模块:使用引脚P2.2。
AD转换模块:使用芯片XPT2046,采用SPI通信,使用引脚P3.4,P3.5,P3.6,P3.7。
DHT11温湿度检测模块:采用单总线通信,使用引脚P2.3。
蓝牙模块:使用单片机的串口资源。
到目前为止,51单片机除了两个引脚未使用,基本上51单片机的资源都已经被榨干了,51单片机CPU想说:我是真的带不动了。
总结
关于江科大视频上的代码只需要看懂实现的过程,然后在使用的时候直接调用就行了,当产生冲突时再在源代码上面进行修改就行了,同时我写的代码还有许多的地方可以进行修改。