DHT90与LCD1602的温湿度显示KeilC程序Word格式文档下载.docx
《DHT90与LCD1602的温湿度显示KeilC程序Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《DHT90与LCD1602的温湿度显示KeilC程序Word格式文档下载.docx(13页珍藏版)》请在冰点文库上搜索。
(LCD1602)
P2.0------RS
(LCD1602)
P2.1------RW
P2.2------E
P2.6------SCK
(DHT90)
P2.7------DATA
*****************************************/
#include<
AT89x51.h>
intrins.h>
math.h>
//Keillibrary
stdio.h>
//Keillibrary
//*********************第一部分LCD1602设置START****************************************
#defineLCD_DB
P0
sbit
LCD_RS=P2^0;
//P2^0是p2.0的意思;
LCD_RS与P2.0等效起来,对LCD_RS读写,就是对P2.0读写好处在于LCD_RS含义直接明了,写程序多了就会知道有必要de
LCD_RW=P2^1;
//P2^1是p2.1的意思
LCD_E=P2^2;
//P2^2是p2.2的意思
/******定义函数****************/
#defineucharunsignedchar
#defineuintunsignedint
voidLCD_init(void);
//初始化函数
voidLCD_write_command(ucharcommand);
//写指令函数
voidLCD_write_data(uchardat);
//写数据函数
voidLCD_disp_char(ucharx,uchary,uchardat);
//在某个屏幕位置上显示一个字符,X(0-15),y(1-2)
voidLCD_disp_str(ucharx,uchary,uchar*str);
//LCD1602显示字符串函数
voiddelay_n10us(uintn);
//延时函数
/*--------------------------------------
模块名称:
LCD_init();
功
能:
初始化LCD1602
占用资源:
--
参数说明:
2008.08.15
FV1.0(函数版本FunctionVersion)
-------------------------------------*/
voidLCD_init(void)
{
delay_n10us(10);
LCD_write_command(0x38);
//设置8位格式,2行,5x7
LCD_write_command(0x0c);
//整体显示,关光标,不闪烁
LCD_write_command(0x06);
//设定输入方式,增量不移位
LCD_write_command(0x01);
//清除屏幕显示
delay_n10us(100);
//延时清屏,延时函数,延时约n个10us
}
LCD_write_command();
LCD1602写指令函数
P2.0--RS(LCD_RS),P2.1--RW(LCD_RW),P2.2--E(LCD_E).
dat为写命令参数
voidLCD_write_command(uchardat)
LCD_RS=0;
//指令
LCD_RW=0;
//写入
LCD_E=1;
//允许
LCD_DB=dat;
//实践证明,我的LCD1602上,用for循环1次就能完成普通写指令。
LCD_E=0;
LCD_write_data();
LCD1602写数据函数
dat为写数据参数
-------------------------------------*/
voidLCD_write_data(uchardat)
LCD_RS=1;
//数据
LCD_disp_char();
LCD1602显示一个字符函数,在某个屏幕位置上显示一个字符,X(0-15),y(1-2)。
X为1602的列值(取值范围是0-15),y为1602的行值(取值范围是1-2),dat为所要显示字符对应的地址参数。
voidLCD_disp_char(ucharx,uchary,uchardat)
ucharaddress;
if(y==1)
address=0x80+x;
else
address=0xc0+x;
LCD_write_command(address);
LCD_write_data(dat);
LCD_disp_str();
LCD1602显示字符串函数,在某个屏幕起始位置{X(0-15),y(1-2)}上显示一个字符串。
X为1602的列值(取值范围是0-15),y为1602的行值(取值范围是1-2),str为所要显示字符串对应的指针参数。
2008.08.16
--
voidLCD_disp_str(ucharx,uchary,uchar*str)
while(*str!
='
\0'
)
{
LCD_write_data(*str);
str++;
}
delay_n10us();
延时函数,延时约n个10us
FV1.1(函数版本FunctionVersion)
修改为较精确的延时函数,"
_nop_()"
延时1us@12M晶振
voiddelay_n10us(uintn)
//延时n个10us@12M晶振
{
uinti;
for(i=n;
i>
0;
i--)
{
_nop_();
_nop_();
}
//*********************第一部分LCD1602设置END****************************************
//*********************第二部分DHT90设置
START****************************************
sbitSCK
=P2^6;
//定义通讯时钟端口
sbitDATA=P2^7;
//定义通讯数据端口
typedefunion
{unsignedinti;
//定义了两个共用体
floatf;
}value;
enum{TEMP,HUMI};
//TEMP=0,HUMI=1
#definenoACK0
//用于判断是否结束通讯
#defineACK
1
//结束数据传输
//adr
command
r/w
#defineSTATUS_REG_W0x06
//000
0011
0
#defineSTATUS_REG_R0x07
1
#defineMEASURE_TEMP0x03
0001
#defineMEASURE_HUMI0x05
0010
#defineRESET
0x1e
1111
/****************定义函数****************/
voids_transstart(void);
//启动传输函数
voids_connectionreset(void);
//连接复位函数
chars_write_byte(unsignedcharvalue);
//DHT90写函数
chars_read_byte(unsignedcharack);
//DHT90读函数
chars_measure(unsignedchar*p_value,unsignedchar*p_checksum,unsignedcharmode);
//测量温湿度函数
voidcalc_dht90(float*p_humidity,float*p_temperature);
//温湿度补偿
s_transstart();
启动传输函数
-------------------------------------*/
voids_transstart(void)
//generatesatransmissionstart
//
_____
________
//DATA:
|_______|
___
___
//SCK:
___|
|___|
|______
DATA=1;
SCK=0;
//Initialstate
SCK=1;
DATA=0;
}
s_connectionreset();
连接复位函数
voids_connectionreset(void)
//communicationreset:
DATA-line=1andatleast9SCKcyclesfollowedbytransstart
_____________________________________________________
_
__||__||__||__||__||__||__||__||__||______|
unsignedchari;
for(i=0;
i<
9;
i++)
//9SCKcycles
}
s_transstart();
//transmissionstart
s_write_byte();
DHT90写函数
chars_write_byte(unsignedcharvalue)
//----------------------------------------------------------------------------------
//writesabyteontheSensibusandcheckstheacknowledge
unsignedchari,error=0;
for(i=0x80;
i/=2)
//shiftbitformasking
{
if(i&
value)DATA=1;
//maskingvaluewithi,writetoSENSI-BUS
elseDATA=0;
//clkforSENSI-BUS
//pulswithapprox.3us
//releaseDATA-line
//clk#9forack
error=DATA;
//checkack(DATAwillbepulleddownbyDHT90),DATA在第9个上升沿将被DHT90自动下拉为低电平。
returnerror;
//error=1incaseofnoacknowledge//返回:
0成功,1失败
s_read_byte();
DHT90读函数
chars_read_byte(unsignedcharack)
//readsabyteformtheSensibusandgivesanacknowledgeincaseof"
ack=1"
unsignedchari,val=0;
{SCK=1;
if(DATA)val=(val|i);
//readbit
//pulswithapprox.3us
if(ack==1)DATA=0;
//incaseof"
ack==1"
pulldownDATA-Line
elseDATA=1;
//如果是校验(ack==0),读取完后结束通讯
//pulswithapprox.3us
//clk#9forack
returnval;
s_measure();
测量温湿度函数
FV1.0(函数版本F