#include <reg52.h>
#define LCD_DB P0
unsigned char s1[5];
unsigned char s2[5];
sbit LCD_RS=P2^4;
sbit LCD_RW=P2^5;
sbit LCD_E=P2^6;
sbit P2_0=P2^0;
sbit moshi=P2^1;
sbit INC=P2^2;
sbit DEC=P2^3;
sbit SPK=P2^7;
/******定义函数***********/
#define uchar unsigned char
#define uint unsigned int
#define U8 unsigned int
#define U16 unsigned int
uchar count1=70;
uchar count2=35;
uchar moshicount=0;
uchar TD=0;//定时次数
U8 U8FLAG;
U8 U8count, U8temp;
U8 U8T_data_H, U8T_data_L, UT_data_1, U8RH_data_H, U8RH_data_L, U8checkdata;
U8 U8T_data_H_temp, U8T_data_L_temp, U8RH_data_H_temp, U8RH_data_L_temp, U8checkdata_temp;
U8 U8comdata;
void LCD_init(void);//初始化函数
void LCD_write_command(uchar command);//指令函数
void LCD_write_data(uchar dat);
//写数据函数
void LCD_disp_char(uchar x,uchar y,uchar dat);
//在某个屏幕位置显示一个字符,X (0-16),y(1-2)
void delay_n40us(uint n);//延时函数
//************************
//*******液晶初始化函数**************
void LCD_init(void)
{
LCD_write_command(0x38);
//设置8位格式,2行,5x7
LCD_write_command(0x0c);
//整体显示,关光标,不闪烁
LCD_write_command(0x06);
//设置输入方式,增量不移位
LCD_write_command(0x03);
//清除屏幕显示
delay_n40us(100);
}
//*******************************
//******定时器初始化函数*********
void
timerinit()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
}
//******写指令函数*********
void LCD_write_command(uchar dat)
{
LCD_DB=dat;
LCD_RS=0;//指令
LCD_RW=0;//写入
LCD_E=1;//允许
delay_n40us(1);
LCD_E=0;
delay_n40us(1);
}
//*************************
//****写数据函数***********
void LCD_write_data(uchar dat)
{
LCD_DB=dat;
LCD_RS=1;//数据
LCD_RW=0;//写入
LCD_E=1;//允许
delay_n40us(1);
LCD_E=0;
delay_n40us(1);
}
//*************************
//*****显示一个字符函数****
void LCD_disp_char(uchar x,uchar y,uchar dat)
{
uchar address;
if(y == 1)
{
address = 0x80+x;
}
else
{
address = 0xc0+x;
}
LCD_write_command(address);
LCD_write_data(dat);
}
//********************
//***延时函数*********
void delay_n40us(uint n)
{
uint i;
uchar j;
for(i=n;i>0;i--)
{
for(j=0;j<2;j++);
}
}
void Delay_10us(void)
{
U8 i;
i--;
i--;
i--;
i--;
i--;
i--;
}
void Delay(U16 j)
{
U8 i;
for(;j>0;j--)
for(i=0;i<27;i++);
}
//*****一字节数据传送函数**********
void COM(void)
{
U8 i;
for(i=0;i<8;i++)
{
U8FLAG=2;
while((!P2_0)&&U8FLAG++);
Delay_10us();
Delay_10us();
Delay_10us();
U8temp=0;
if(P2_0)U8temp=1;
U8FLAG=2;
while((P2_0)&&U8FLAG++);
//超时则跳出for循环
if(U8FLAG==1)break;
//判断数据位是 0 还是 1
//如果高电平高过预定 0 高电平值则数据位位 1
U8comdata<<=1;
U8comdata|=U8temp;
}
}
/*******
单片机与传感器
通信函数***********/
void RH(void)
{
//主机拉低 18ms
P2_0=0;
Delay(200);
P2_0=1;
//总线由上拉电阻拉高 主机延时20us
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
//主机设为输入 判断从机响应信号
P2_0=1;
//判断从机是否有低电平响应信号 如不响应则跳出响应则向下运行
if(!P2_0) //T !
{
U8FLAG=2;
//判断从机是否发出 80us 的低电平响应信号是否结束
while((!P2_0)&&U8FLAG++);
U8FLAG=2;
//判断从机是否发出 80us的高电平,如发出则进入数据接收状态
while((P2_0)&&U8FLAG++);
//数据接收状态
COM();
U8RH_data_H_temp=U8comdata;
COM();
U8RH_data_L_temp=U8comdata;
COM();
U8T_data_H_temp=U8comdata;
COM();
U8T_data_L_temp=U8comdata;
COM();
U8checkdata_temp=U8comdata;
P2_0=1;
//数据校验
U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp +U8RH_data_L_temp);
if(U8temp==U8checkdata_temp)
{
U8RH_data_H=U8RH_data_H_temp;
U8RH_data_L=U8RH_data_L_temp;
U8T_data_H=U8T_data_H_temp;
U8T_data_L=U8T_data_L_temp;
U8checkdata=U8checkdata_temp;
}
//湿度整数部分
s1[0]=(char)(0X30+U8RH_data_H/10);
s1[1]=(char)(0X30+U8RH_data_H%10);
//湿度小数部分
s1[2]=(char)(0X30+U8RH_data_L/10);
//温度整数部分
s2[0]=(char)(0X30+U8T_data_H/10);
s2[1]=(char)(0X30+U8T_data_H%10);
//温度小数部分
s2[2]=(char)(0X30+U8T_data_L/10);
}
}
//************************
//液晶显示函数
void disp()
{
LCD_disp_char(0,1,'s');
LCD_disp_char(1,1,'h');
LCD_disp_char(2,1,'i');
LCD_disp_char(3,1,'d');
LCD_disp_char(4,1,'u');
LCD_disp_char(5,1,':');
LCD_disp_char(6,1,s1[0]+'0');
LCD_disp_char(7,1,s1[1]+'0');
LCD_disp_char(8,1,'.');
LCD_disp_char(9,1,s1[2]+'0');
LCD_disp_char(10,1,'%');
LCD_disp_char(11,1,'R');
LCD_disp_char(12,1,'H');
LCD_disp_char(0,2,'w');
LCD_disp_char(1,2,'e');
LCD_disp_char(2,2,'n');
LCD_disp_char(3,2,'d');
LCD_disp_char(4,2,'u');
LCD_disp_char(5,2,':');
LCD_disp_char(6,2,s2[0]+'0');
LCD_disp_char(7,2,s2[1]+'0');
LCD_disp_char(8,2,'.');
LCD_disp_char(9,2,s2[2]+'0');
LCD_disp_char(10,2,0xDF);
LCD_disp_char(11,2,'C');
}
//阀值设置函数
void shezhi()
{
//初值
s1[3]=(char)(0X30+count1/10);
s1[4]=(char)(0X30+count1%10);
//初值
s2[3]=(char)(0X30+count2/10);
s2[4]=(char)(0X30+count2%10);
moshi=1;
if(moshi==0)
{
Delay_10us();
while(moshi==0);
moshicount++;
}
switch(moshicount)
{
case 1:
{
INC=1;DEC=1;
if(INC==0)
{
Delay_10us();
while(INC==0);
count1++;
}
else if(DEC==0)
{
Delay_10us();
while(INC==0);
count1--;
}
LCD_disp_char(14,1,s1[3]);
LCD_disp_char(15,1,s1[4]);
LCD_disp_char(14,2,s2[3]);
LCD_disp_char(15,2,s2[4]);
}break;
case 2:
{
//moshicount=0;
INC=1;DEC=1;
if(INC==0)
{
Delay_10us();
while(INC==0);
count2++;
}
else if(DEC==0)
{
Delay_10us();
while(DEC==0);
count2--;
}
LCD_disp_char(14,1,s1[3]);
LCD_disp_char(15,1,s1[4]);
LCD_disp_char(14,2,s2[3]);
LCD_disp_char(15,2,s2[4]);
}break;
case 3:
{
moshicount=0;
LCD_disp_char(14,1,' ');
LCD_disp_char(15,1,' ');
LCD_disp_char(14,2,' ');
LCD_disp_char(15,2,' ');
}break;
default: break;
}
}
//蜂鸣器报警程序
void laba()
{
if((U8RH_data_H>=count1)||(U8T_data_H>=count2))
SPK=1;
else
SPK=0;
}
//定时器0 中断程序,每次定时 50ms
timer0() interrupt 1
{
TD++;
if(TD>=100)
{
TH0=-50000/256;
TL0=-50000%256;
TR0=0;
TD=0;
}
TH0=-50000/256;
TL0=-50000%256;
}
//*********主函数************
void main(void)
{
LCD_init();
timerinit();
Delay(4);
while(1)
{
RH();
disp();
laba();
TR0=1;
while((TD>=1)&&(TD<=100))
{
shezhi();
}
}
}
一周热门 更多>