仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
#include //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include "intrins.h"
#define u8 unsigned char
#define u16 unsigned int
#define uchar unsigned char
#define uint unsigned int
uchar yushe_wendu=50; //温度预设值
uchar yushe_yanwu=100; //光照预设值
uchar yushe_tianranqi=100; //天然气预设值
uint wendu; //温度值全局变量
uchar yanwu; //用于读取ADC数据
uchar tianranqi; //用于读取ADC数据
uint times = 0; //变量times,计数,用于液晶切换显示
uchar shezhi_flag = 0; //变量标志位,用于切换显示设置天然气预设值
//运行模式
uchar Mode=0; //=1是设置温度阀值 =2是设置光照阀值 =0是正常监控模式
uint Buzzer=0;
uint count = 0;
//管脚声明
sbit Led_Yellow =P2^4; //黄灯
sbit speaker =P2^0; //蜂鸣器
sbit cg=P2^3; //总开关
sbit c1_k=P1^1; //窗帘1开关
sbit c2_k=P1^4; //窗帘2开关
/********************************************************************
* 名称 : delay_1ms()
* 功能 : 延时1ms函数
* 输入 : q
* 输出 : 无
***********************************************************************/
void delay_ms(uint q)
{
uint i,j;
for(i=0;i
for(j=0;j<110;j++);
}
/***********************************************************************************************************
LCD1602相关函数
***********************************************************************************************************/
//LCD管脚声明 (RW引脚实物直接接地,因为本设计只用到液晶的写操作,RW引脚一直是低电平)
sbit LCDRS = P2^7;
sbit LCDEN = P2^6;
sbit D0 = P0^0;
sbit D1 = P0^1;
sbit D2 = P0^2;
sbit D3 = P0^3;
sbit D4 = P0^4;
sbit D5 = P0^5;
sbit D6 = P0^6;
sbit D7 = P0^7;
//LCD延时
void LCDdelay(uint z) //该延时大约100us(不精确,液晶操作的延时不要求很精确)
{
uint x,y;
for(x=z;x>0;x--)
for(y=10;y>0;y--);
}
void LCD_WriteData(u8 dat)
{
if(dat&0x01)D0=1;else D0=0;
if(dat&0x02)D1=1;else D1=0;
if(dat&0x04)D2=1;else D2=0;
if(dat&0x08)D3=1;else D3=0;
if(dat&0x10)D4=1;else D4=0;
if(dat&0x20)D5=1;else D5=0;
if(dat&0x40)D6=1;else D6=0;
if(dat&0x80)D7=1;else D7=0;
}
//写命令
void write_com(uchar com)
{
LCDRS=0;
LCD_WriteData(com);
// DAT=com;
LCDdelay(5);
LCDEN=1;
LCDdelay(5);
LCDEN=0;
}
//写数据
void write_data(uchar date)
{
LCDRS=1;
LCD_WriteData(date);
// DAT=date;
LCDdelay(5);
LCDEN=1;
LCDdelay(5);
LCDEN=0;
}
/*------------------------------------------------
选择写入位置
------------------------------------------------*/
void SelectPosition(unsigned char x,unsigned char y)
{
if (x == 0)
{
write_com(0x80 + y); //表示第一行
}
else
{
write_com(0xC0 + y); //表示第二行
}
}
/*------------------------------------------------
写入字符串函数
------------------------------------------------*/
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{
SelectPosition(x,y) ;
while (*s)
{