我正在尝试读取 PIC18f57Q84 上的 DHT11 并将其显示到 LCD 屏幕上。我在网上发现有人对 PIC18f4550 做了这个,并试图让它在我的 PIC18f57Q84 开发板上工作。我是非常新的 PIC 和 MPLAB X IDE,所以我基本上只是修改了代码,直到它构建完成。但这当然行不通。链接至网站 https://www.electronicwings.com/pic/dht11-sensor-interface-with-pic18f4550
我将 .c 和 .h 文件分别复制到项目的源文件夹和头文件夹中。我删除了 OSSCON 行,因为它不起作用,并且我更改了一些寄存器定义的语法以适应我习惯的方式。我还将 #include pic18f4550 更改为 #include pic18f57q84。我注意到的一件事是,即使我需要将 Configuration_Header_File.h 放置在项目的头文件夹中才能成功构建程序,但如果我 #include 它,该文件的大部分都无法识别并停止构建。我真正更改的唯一文件是湿度_temp.c 文件。我已经检查了 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 */
}
如果您不参考各自的数据表,从旧的 PIC 型号迁移到新型号可能会很痛苦。 PIC18F57Q84是最新的PIC18型号芯片之一,具有许多新功能。
好吧,如果我查看您的代码,我可以告诉您的第一件事是您不必显式包含
pic18f57q84
。如果您在 MPLABX IDE 中选择正确的芯片型号,xc.h
将自动引用所需的芯片标头。
其次,迁移代码中导致项目无法正常运行的实际错误是端口设置。
PIC18F57Q84中的
ADCON1
寄存器与控制GPIO的模拟功能无关。不过,它可能与较旧的芯片有关。因此,对于较新的芯片,您需要使用相应的 ANSELx
寄存器来禁用 GPIO 的模拟功能。在您的情况下,将修改 ANSELA
寄存器。根据我的理论,如果你改变这一行
ADCON1=0x0F; /* this makes all pins as a digital I/O pins */
以下
ANSELA = 0; /* this makes all PORTA pins as a digital I/O pins */
您的项目应该可以使用新芯片运行。