基于单片机AT89C51的温湿度采集系统的设计,用的温湿度模块是dht11,显示器是LCD1602,谁有C程序?
可以帮我设计一个吗??要是可以的话就发到把图和程序发到这个邮箱471240982@qq.com...
可以帮我设计一个吗??要是可以的话就发到把图和程序发到这个邮箱471240982@qq.com
展开
1个回答
展开全部
#include <reg51.h>
#include <intrins.h>
#include <delay.h>
#include<absacc.h>
//=====lcd1602===
#ifndef lcd_H
#define lcd_H
/************************************/
//common part
#define HIGH 1
#define LOW 0
#define TRUE 1
#define FALSE 0
#define ZERO 0
#define MSB 0x80
#define LSB 0x01
/************************************/
//lcd part
#define LINE1 0
#define LINE2 1
#define LINE1_HEAD 0x80
#define LINE2_HEAD 0xC0
#define DATA_MODE 0x38
#define OPEN_SCREEN 0x0C
#define DISPLAY_ADDRESS 0x80
#define CLEARSCREEN LCD_en_com(0x01)
/*************************************/
//change this part at different board
#define LCDIO P0
sbit LCD1602_RS=P2^6;
sbit LCD1602_RW=P2^5;
sbit LCD1602_EN=P2^7;
/********************************************************************/
void LCD_Read_BF(void)
{
unsigned char read=0;
LCD1602_RS = LOW; //RS 0
LCD1602_RW = HIGH; //RW 1
LCD1602_EN = HIGH; //EN 1 Read BF
LCDIO = 0xFF;
do{
read = LCDIO;
}while(read&MSB);
}
void LCD_en_com(unsigned char command) //写指令
{
LCD_Read_BF();
LCD1602_RS = LOW; //RS 0
LCD1602_RW = LOW; //RW 0
LCD1602_EN = HIGH; //EN --\|/__ Write command
LCDIO = command;
LCD1602_EN = LOW;
}
void LCD_en_dat(unsigned char dat) //写数据
{
LCD_Read_BF();
LCD1602_RS = HIGH; //RS 1
LCD1602_RW = LOW; //RW 0
LCD1602_EN = HIGH; //EN --\|/__ Write data
LCDIO = dat;
LCD1602_EN = LOW;
}
void LCD_set_xy(unsigned char x,unsigned char y)
{
unsigned char address;
if(y == LINE1)
address = LINE1_HEAD + x; //0x80 第一行的开始位
else
address = LINE2_HEAD + x; //0xc0 第一行的开始位
LCD_en_com(address);
}
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
{
LCD_set_xy(x,y);
LCD_en_dat(dat);
}
void LCD_write_string(unsigned char x,unsigned char y,unsigned char *s)
{
LCD_set_xy(x,y);
while(*s)
{
LCDIO = *s;
LCD_en_dat(*s);
s++;
}
}
void LCD_init(void)
{
LCD_en_com(DATA_MODE);
//set 8 bit data transmission mode
LCD_en_com(OPEN_SCREEN);
//open display (enable lcd display)
LCD_en_com(DISPLAY_ADDRESS);
//set lcd first display address
CLEARSCREEN;
//clear screen
}
#endif
//========dht11=====
typedef unsigned char U8;
typedef unsigned int U16;
U8 U8FLAG;
U8 U8count,U8temp;
U8 U8T_data_H,U8T_data_L,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;
sbit P2_0 = P2^4 ;
sbit P2_1 = P2^1 ;
sbit P2_2 = P2^2 ;
sbit P2_3 = P2^3 ;
unsigned char str1[]="ShiDu=";
unsigned char str2[]="WenDu=";
unsigned char str3[]="%";
unsigned char str4[]=0xdf;
unsigned char str5[]="C";
void Delay(U16 j)
{ U8 i;
for(;j>0;j--)
{
for(i=0;i<27;i++);
}
}
void Delay_10us(void)
{
U8 i;
i--;
i--;
i--;
i--;
i--;
i--;
}
void COM(void)
{
U8 i; //定义一个8位无符号数字。最大256
for(i=0;i<8;i++)
{
U8FLAG=2;
//----------------------
P2_1=0 ; //T
P2_1=1 ; //T
//----------------------
while((!P2_0)&&U8FLAG++);
Delay_10us();
Delay_10us();
// Delay_10us();
U8temp=0;
if(P2_0)U8temp=1;
U8FLAG=2;
while((P2_0)&&U8FLAG++);
//----------------------
P2_1=0 ; //T
P2_1=1 ; //T
//----------------------
//超时则跳出for循环
if(U8FLAG==1)break;
//判断数据位是0还是1
// 如果高电平高过预定0高电平值则数据位为 1
U8comdata<<=1;
U8comdata|=U8temp; //0
}//rof
}
//--------------------------------
//-----湿度读取子程序 ------------
//--------------------------------
//----以下变量均为全局变量--------
//----温度高8位== U8T_data_H------
//----温度低8位== U8T_data_L------
//----湿度高8位== U8RH_data_H-----
//----湿度低8位== U8RH_data_L-----
//----校验 8位 == U8checkdata-----
//----调用相关子程序如下----------
//---- Delay();, Delay_10us();,COM();
//--------------------------------
void RH(void)
{
//主机拉低18ms
P2_0=0;
Delay(180);
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;
}//fi
}//fi
}
void main()
{
U8 shidu_shi,shidu_ge,wendu_shi,wendu_ge;
LCD_init();
delay_nms(2);
LCD_write_string(0,LINE1,str1);
LCD_write_string(0,LINE2,str2);
while(1)
{
RH();
shidu_shi=0x30+U8RH_data_H/10; //为什么要加0x30?
shidu_ge=0x30+U8RH_data_H%10;
// shidu_xs=0x30+U8RH_data_L/10;
wendu_shi=0x30+U8T_data_H/10;
wendu_ge=0x30+U8T_data_H%10;
//wendu_xs=0x30+U8T_data_L/10;
LCD_write_char(6,0,shidu_shi);//第6位第1行写湿度的十位
LCD_write_char(7,0,shidu_ge);
//LCD_write_char(9,0,shidu_xs);
LCD_write_char(6,1,wendu_shi);
LCD_write_char(7,1,wendu_ge);
//LCD_write_char(9,1,wendu_xs);
delay_nms(100);
//delay_nms(200);
LCD_write_string(8,LINE1,str3);
LCD_write_string(8,LINE2,str4);
LCD_write_string(9,LINE2,str5);
}
}
#include <intrins.h>
#include <delay.h>
#include<absacc.h>
//=====lcd1602===
#ifndef lcd_H
#define lcd_H
/************************************/
//common part
#define HIGH 1
#define LOW 0
#define TRUE 1
#define FALSE 0
#define ZERO 0
#define MSB 0x80
#define LSB 0x01
/************************************/
//lcd part
#define LINE1 0
#define LINE2 1
#define LINE1_HEAD 0x80
#define LINE2_HEAD 0xC0
#define DATA_MODE 0x38
#define OPEN_SCREEN 0x0C
#define DISPLAY_ADDRESS 0x80
#define CLEARSCREEN LCD_en_com(0x01)
/*************************************/
//change this part at different board
#define LCDIO P0
sbit LCD1602_RS=P2^6;
sbit LCD1602_RW=P2^5;
sbit LCD1602_EN=P2^7;
/********************************************************************/
void LCD_Read_BF(void)
{
unsigned char read=0;
LCD1602_RS = LOW; //RS 0
LCD1602_RW = HIGH; //RW 1
LCD1602_EN = HIGH; //EN 1 Read BF
LCDIO = 0xFF;
do{
read = LCDIO;
}while(read&MSB);
}
void LCD_en_com(unsigned char command) //写指令
{
LCD_Read_BF();
LCD1602_RS = LOW; //RS 0
LCD1602_RW = LOW; //RW 0
LCD1602_EN = HIGH; //EN --\|/__ Write command
LCDIO = command;
LCD1602_EN = LOW;
}
void LCD_en_dat(unsigned char dat) //写数据
{
LCD_Read_BF();
LCD1602_RS = HIGH; //RS 1
LCD1602_RW = LOW; //RW 0
LCD1602_EN = HIGH; //EN --\|/__ Write data
LCDIO = dat;
LCD1602_EN = LOW;
}
void LCD_set_xy(unsigned char x,unsigned char y)
{
unsigned char address;
if(y == LINE1)
address = LINE1_HEAD + x; //0x80 第一行的开始位
else
address = LINE2_HEAD + x; //0xc0 第一行的开始位
LCD_en_com(address);
}
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
{
LCD_set_xy(x,y);
LCD_en_dat(dat);
}
void LCD_write_string(unsigned char x,unsigned char y,unsigned char *s)
{
LCD_set_xy(x,y);
while(*s)
{
LCDIO = *s;
LCD_en_dat(*s);
s++;
}
}
void LCD_init(void)
{
LCD_en_com(DATA_MODE);
//set 8 bit data transmission mode
LCD_en_com(OPEN_SCREEN);
//open display (enable lcd display)
LCD_en_com(DISPLAY_ADDRESS);
//set lcd first display address
CLEARSCREEN;
//clear screen
}
#endif
//========dht11=====
typedef unsigned char U8;
typedef unsigned int U16;
U8 U8FLAG;
U8 U8count,U8temp;
U8 U8T_data_H,U8T_data_L,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;
sbit P2_0 = P2^4 ;
sbit P2_1 = P2^1 ;
sbit P2_2 = P2^2 ;
sbit P2_3 = P2^3 ;
unsigned char str1[]="ShiDu=";
unsigned char str2[]="WenDu=";
unsigned char str3[]="%";
unsigned char str4[]=0xdf;
unsigned char str5[]="C";
void Delay(U16 j)
{ U8 i;
for(;j>0;j--)
{
for(i=0;i<27;i++);
}
}
void Delay_10us(void)
{
U8 i;
i--;
i--;
i--;
i--;
i--;
i--;
}
void COM(void)
{
U8 i; //定义一个8位无符号数字。最大256
for(i=0;i<8;i++)
{
U8FLAG=2;
//----------------------
P2_1=0 ; //T
P2_1=1 ; //T
//----------------------
while((!P2_0)&&U8FLAG++);
Delay_10us();
Delay_10us();
// Delay_10us();
U8temp=0;
if(P2_0)U8temp=1;
U8FLAG=2;
while((P2_0)&&U8FLAG++);
//----------------------
P2_1=0 ; //T
P2_1=1 ; //T
//----------------------
//超时则跳出for循环
if(U8FLAG==1)break;
//判断数据位是0还是1
// 如果高电平高过预定0高电平值则数据位为 1
U8comdata<<=1;
U8comdata|=U8temp; //0
}//rof
}
//--------------------------------
//-----湿度读取子程序 ------------
//--------------------------------
//----以下变量均为全局变量--------
//----温度高8位== U8T_data_H------
//----温度低8位== U8T_data_L------
//----湿度高8位== U8RH_data_H-----
//----湿度低8位== U8RH_data_L-----
//----校验 8位 == U8checkdata-----
//----调用相关子程序如下----------
//---- Delay();, Delay_10us();,COM();
//--------------------------------
void RH(void)
{
//主机拉低18ms
P2_0=0;
Delay(180);
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;
}//fi
}//fi
}
void main()
{
U8 shidu_shi,shidu_ge,wendu_shi,wendu_ge;
LCD_init();
delay_nms(2);
LCD_write_string(0,LINE1,str1);
LCD_write_string(0,LINE2,str2);
while(1)
{
RH();
shidu_shi=0x30+U8RH_data_H/10; //为什么要加0x30?
shidu_ge=0x30+U8RH_data_H%10;
// shidu_xs=0x30+U8RH_data_L/10;
wendu_shi=0x30+U8T_data_H/10;
wendu_ge=0x30+U8T_data_H%10;
//wendu_xs=0x30+U8T_data_L/10;
LCD_write_char(6,0,shidu_shi);//第6位第1行写湿度的十位
LCD_write_char(7,0,shidu_ge);
//LCD_write_char(9,0,shidu_xs);
LCD_write_char(6,1,wendu_shi);
LCD_write_char(7,1,wendu_ge);
//LCD_write_char(9,1,wendu_xs);
delay_nms(100);
//delay_nms(200);
LCD_write_string(8,LINE1,str3);
LCD_write_string(8,LINE2,str4);
LCD_write_string(9,LINE2,str5);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询