适配PIC18F57Q84的DHT11读取代码无法运行求助
PIC18F57Q84读取DHT11数据并显示至LCD故障排查
我在PIC18F57Q84开发板上尝试读取DHT11传感器数据并显示到LCD屏幕,参考了适配PIC18F4550的开源代码。作为PIC及MPLAB X IDE新手,我已将代码修改至可编译状态,但程序无法正常运行。
修改内容如下:
- 移除OSSCON相关代码
- 调整寄存器定义语法
- 将头文件从
pic18f4550.h替换为pic18f57q84.h
另外发现,项目需包含Configuration_Header_File.h才能编译,但引入该文件会导致编译失败。已排查LCD与DHT11的接线,确认无问题。
修改后的代码:
#include <pic18f57q84.h> #include <xc.h> #include <stdio.h> #include "LCD_16x2_8-bit_Header_File.h" #define Data_Out LATAbits.LATA0 /* assign Port pin for data*/ #define Data_In PORTAbits.RA0 /* read data from Port pin*/ #define Data_Dir TRISAbits.TRISA0 /* Port direction */ #define _XTAL_FREQ 8000000 /* define _XTAL_FREQ for using internal delay */ void DHT11_Start(); void DHT11_CheckResponse(); char DHT11_ReadData(); void main() { char RH_Decimal,RH_Integral,T_Decimal,T_Integral; char Checksum; char value[10]; LCD_Init(); /* initialize LCD16x2 */ ADCON1=0x0F; /* this makes all pins as a digital I/O pins */ while(1) { DHT11_Start(); /* send start pulse to DHT11 module */ DHT11_CheckResponse(); /* wait for response from DHT11 module */ /* read 40-bit data from DHT11 module */ RH_Integral = DHT11_ReadData(); /* read Relative Humidity's integral value */ RH_Decimal = DHT11_ReadData(); /* read Relative Humidity's decimal value */ T_Integral = DHT11_ReadData(); /* read Temperature's integral value */ T_Decimal = DHT11_ReadData(); /* read Relative Temperature's decimal value */ Checksum = DHT11_ReadData(); /* read 8-bit checksum value */ /* convert humidity value to ascii and send it to display*/ sprintf(value,"%d",RH_Integral); LCD_String_xy(0,0,value); sprintf(value,".%d ",RH_Decimal); LCD_String(value); LCD_Char('%'); /* convert temperature value to ascii and send it to display*/ sprintf(value,"%d",T_Integral); LCD_String_xy(1,0,value); sprintf(value,".%d",T_Decimal); LCD_String(value); LCD_Char(0xdf); LCD_Char('C'); sprintf(value,"%d ",Checksum); LCD_String_xy(1,8,value); /* check addition of humidity and temperature value equals to checksum */ if(Checksum != (RH_Integral + RH_Decimal + T_Integral + T_Decimal)) LCD_String_xy(0,8,"Error"); else LCD_String_xy(0,8,"No Error"); MSdelay(500); } } char DHT11_ReadData() { char i,data = 0; for(i=0;i<8;i++) { while(!(Data_In & 1)); /* wait till 0 pulse, this is start of data pulse */ __delay_us(30); if(Data_In & 1) /* check whether data is 1 or 0 */ data = ((data<<1) | 1); else data = (data<<1); while(Data_In & 1); } return data; } void DHT11_Start() { Data_Dir = 0; /* set as output port */ Data_Out = 0; /* send low pulse of min. 18 ms width */ __delay_ms(18); Data_Out = 1; /* pull data bus high */ __delay_us(20); Data_Dir = 1; /* set as input port */ // LED = 14; } void DHT11_CheckResponse() { while(Data_In & 1); /* wait till bus is High */ while(!(Data_In & 1)); /* wait till bus is Low */ while(Data_In & 1); /* wait till bus is High */ }
内容的提问来源于stack exchange,提问作者yui1236
相关产品推荐
相关产品推荐

