您的当前位置:首页DHT11温度湿度传感器pic18驱动程序

DHT11温度湿度传感器pic18驱动程序

2023-09-20 来源:乌哈旅游
DHT11温度湿度传感器pic18驱动程序

#include

#define uchar unsigned char #define uint unsigned int

#define LCD_EN_PORT LATG //#define LCD_RW_PORT PORTF #define LCD_RS_PORT LATG #define LCD_DATA_PORT LATF //#define LCD_DATA_DDR DDRA //#define LCD_DATA_PIN PINA

//LCD的 r/w 脚直接接 GND #define LCD_EN 0x08 //portd7 out #define LCD_RS 0x10 //portc6 out

//#define LCD_DATA 0x0F //porta 4/5/6/7 out

//DHT 11

#define PD4 1

#define SET_DDR_DHT11 TRISE |= (1<>PD4)&0x01)

unsigned

char

DHT11T_Data_H,

DHT11T_Data_L,

DHT11RH_Data_H, DHT11RH_Data_L;

char szStr[14]; char szStr1[11];

/*--------------------------------------------------------------------------------------------------

Public function prototypes

--------------------------------------------------------------------------------------------------*/

void LCD_init (void); void LCD_en_write (void);

void LCD_write_char (unsigned char command,unsigned char data);

void LCD_set_xy (unsigned char x, unsigned char y); void LCD_write_string (unsigned char X,unsigned char Y,unsigned char *s);

void delay_nus (unsigned int n); void delay_nms (unsigned int n);

void delay_1us(void) //1us延时函数 { Nop(); }

void delay_nus(unsigned int n) //N us延时函数 {

while(n--) { Nop();

} }

void delay_40us() {

unsigned char n; n=9; while(n--) { Nop(); } }

void delay_1ms(void) //1ms延时函数 {

unsigned int i; for (i=0;i<130;i++); }

void delay_nms(unsigned int n) //N ms延时函数 {

unsigned int i=0; for (i=0;i delay_1ms(); }

void LCD_init(void) //液晶初始化 {

PORTF = 0xff; LATF = 0xff; ADCON1 = 0x0f; TRISF = 0x00;

PORTG = 0xff; LATG = 0xff; TRISG = 0X00;

delay_nms(200);

LCD_write_char(0x30,0); //4位显示 delay_nms(20);

LCD_write_char(0x30,0); //4位显示 delay_nms(20);

LCD_write_char(0x30,0); //4位显示 delay_nms(20);

LCD_write_char(0x02,0); //4位显示 delay_nms(20);

LCD_write_char(0x28,0); //4位显示 delay_nms(20);

LCD_write_char(0x08,0); //4位显示 delay_nms(20);

LCD_write_char(0x01,0); //4位显示 delay_nms(20);

LCD_write_char(0x06,0); //显示开 delay_nms(20);

LCD_write_char(0x0c,0); //显示开 delay_nms(20); }

void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)

{

LCD_set_xy( X, Y ); //写地址 delay_nms(10);

while (*s) {

LCD_write_char( 0, *s ); s++; } }

void LCD_set_xy( unsigned char x, unsigned char y ) //写地址函数

{

unsigned char address;

if (y == 0) a ddress = 0x80 + x; else

address = 0xc0 + x;

LCD_write_char(address, 0 ); }

void LCD_en_write(void) //液晶使能 {

LCD_EN_PORT |= LCD_EN; delay_nus(1); //delay_1us();

LCD_EN_PORT &= ~LCD_EN; }

void LCD_write_char(unsigned char command,unsigned char data) // 写数据

{

unsigned char command_temp,data_temp;

command_temp = command; data_temp = data; delay_nus(16);

if(command == 0) {

LCD_RS_PORT |= LCD_RS; //RS=1

LCD_DATA_PORT &= 0x0f;

LCD_DATA_PORT |= data_temp & 0xf0; //写高四位 LCD_en_write();

data_temp = data_temp <<4;

LCD_DATA_PORT &= 0X0f;

LCD_DATA_PORT |= data_temp & 0xf0; //写低四位 LCD_en_write(); } else {

LCD_RS_PORT &= ~LCD_RS; //RS=0

LCD_DATA_PORT &= 0X0f;

LCD_DATA_PORT |= command_temp & 0xF0; //写高四位 LCD_en_write();

command_temp = command_temp <<4; LCD_DATA_PORT &= 0x0f;

LCD_DATA_PORT |= command_temp & 0xf0; //写低四位 LCD_en_write(); } }

///////////////////////////////////// uchar DHT11_ReadChar(void) {

uchar dat=0;

uint count; //计数防止死等 uchar i;

for(i=0;i<8;i++) { count=2;

while((!PORTEbits.RE1)&&count++) //等待50us低电平结束

;

delay_40us();

dat <<= 1; //50us低电平+28us高电平表示'0' if(PORTEbits.RE1) //50us低电平+70us高电平表示'1' dat |= 1;

count=2;

while((PORTEbits.RE1)&&count++) ;

if(count==1) //超时则跳出for循环 break; }

return dat; }

uchar DHT11_GetRH(void) { uchar

TData_H_temp,TData_L_temp,RHData_H_temp,RHData_L_temp,CheckData_temp;

uchar checktemp; uint count;

TRISEbits.TRISE1=0; //SET_DDR_DHT11; 输出 http://www..com/doc/db13565091.html,TE1 0;//CLR_OP_DHT11;

delay_nms(18); //主机拉低18ms

http://www..com/doc/db13565091.html,TE1

= =

1;//SET_OP_DHT11;

TRISEbits.TRISE1=1;//CLR_DDR_DHT11;

delay_40us(); //总线由上拉电阻拉高 主机延时20us //主机设为输入 判断从机响应信号

//判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行

if(!PORTEbits.RE1) { count=2;

//判断从机是否发出 80us 的低电平响应信号是否结束 while((!PORTEbits.RE1)&&count++) ;

count=2;

//判断从机是否发出 80us 的高电平,如发出则进入数据接收状态 while((PORTEbits.RE1)&&count++) ;

RHData_H_temp = DHT11_ReadChar(); RHData_L_temp = DHT11_ReadChar(); TData_H_temp = DHT11_ReadChar(); TData_L_temp = DHT11_ReadChar(); CheckData_temp = DHT11_ReadChar();

TRISEbits.TRISE1=0; //SET_DDR_DHT11; http://www..com/doc/db13565091.html,TE1 1;//SET_OP_DHT11;

checktemp = (RHData_H_temp + RHData_L_temp + TData_H_temp + TData_L_temp);

=

if (checktemp == CheckData_temp) {

DHT11RH_Data_H = RHD ata_H_temp;

DHT11RH_Data_L = RHData_L_temp; DHT11T_Data_H = TData_H_temp; DHT11T_Data_L = TData_L_temp; return 1; } } return 0; }

//////////////////////////////////////

//char szStr[14]; //char szStr1[11]; void GetWS(void) { uint tt; szStr[0]='R'; szStr[1]='H'; szStr[2]=':'; szStr[3]='\\0';

szStr1[0]='T'; szStr1[1]='E'; szStr1[2]='M'; szStr1[3]='P'; szStr1[4]=':';

szStr1[5]='\\0'; if (DHT11_GetRH()) {

tt = DHT11RH_Data_H; tt <<= 8;

tt += DHT11RH_Data_L; tt >>= 1;

//szStr[3] = tt%10000/1000 + '0'; //szStr[4] = tt%1000/100 + '0'; szStr[3] = DHT11RH_Data_H / 10 + '0'; szStr[4] = DHT11RH_Data_H % 10 + '0'; szStr[5] = '%'; szStr[6] = 0;

tt = DHT11T_Data_H; tt <<= 8;

tt += DHT11T_Data_L; tt >>= 1;

//szStr1[5] = tt%10000/1000 + '0'; //szStr1[6] = tt%1000/100 + '0'; szStr1[5] = DHT11T_Data_H/10 + '0'; szStr1[6] = DHT11T_Data_H%10 + '0';

szStr1[7] = 0; }

//LCD_write_string(0,0,szStr); //LCD_write_string(0,1,szStr1); }

void main(void) {

delay_40us(); delay_40us(); delay_40us(); delay_40us();

//char szStr[]={\"hello 1602\LATE = 0xff; TRISE =0 ;

delay_nms(500);

LCD_init();

//LCD_write_string(0,0,szStr); //LCD_write_string(0,1,szStr); //LCD_write_string(0,1,\"abc\"); while(1) {

delay_nms(200);

GetWS(); LATE |= 0x08; delay_nms(200); delay_nms(200);

delay_nms(200); delay_nms(200);

LATE &= ~(0X08); delay_nms(200); delay_nms(200); delay_nms(200); delay_nms(200); } }

因篇幅问题不能全部显示,请点此查看更多更全内容