时间:2023-06-16 16:36
人气:
作者:admin
基于PIC18系列(PIC18F4520)单片机+DHT11的温湿度采集系统的设计与制作(Proteus仿真部分)

Proteus仿真连线图
//main.c
/*
* This Code is written to Developed to Extract Temperature and Humidity
* Data from the DHT11 Sensor using the one-wire Communication protocol.
* And Displays the Extracted Data on a 20x4 LCD Display.
* The Project uses an external XTAL of 4MHZ.
*
* Website: https://space.bilibili.com/314404732
//doubixiaohanhan
*/
#include "prototyper.h"
#pragma config OSC = HS
#pragma config LVP = OFF
#pragma config WDT = OFF
void main (void)
{
unsigned char RH_Integral;
unsigned char RH_Decimal;
unsigned char Temp_Integral;
unsigned char Temp_Decimal;
unsigned char Checksum;
unsigned char DataValid;
Port_Init ();
//doubixiaohanhan
LCD_Init ();
LCD_Stuff ();
while (1)
{
DHT11_Start ();
if (DHT11_Response ())
{
RH_Integral = DHT11_Read ();
RH_Decimal = DHT11_Read ();
Temp_Integral = DHT11_Read ();
Temp_Decimal = DHT11_Read ();
Checksum = DHT11_Read ();
DataValid = Check_Parity (Checksum, RH_Integral, RH_Decimal, Temp_Integral, Temp_Decimal);
if (DataValid)
{
DisplayTemp (Temp_Integral, Temp_Decimal);
DisplayHumidity (RH_Integral, RH_Decimal);
Delay10KTCYx (120); // A minimum Sample Period of 1.2 seconds @4MHZ
}
else
{
Parity_Error ();
}
} //doubixiaohanhan
else
{
Sensor_Unresponsive ();
}
}
}
//config.c
#include "prototyper.h"
void Port_Init (void)
{
TRISCbits.TRISC2 = 0; // Pin Used to Check Pwr on Ckt
LATCbits.LATC2 = 1; //
TRISD = 0x00; // Port used for LCD
LATD = 0x00;
}
//doubixiaohanhan
void LCD_Init (void)
{
OpenXLCD (FOUR_BIT & LINES_5X7);
while (BusyXLCD());
WriteCmdXLCD (DISPLAY_ON); // TURN DISPLAY ON cursor off
while (BusyXLCD());
WriteCmdXLCD (0x80); // begin at the first line
while (BusyXLCD());
WriteCmdXLCD (CLRSCR); // clear the lcd screen
}
void LCD_Stuff (void)
{
while (BusyXLCD());
putrsXLCD ("DHT11 Interfacing");
while (BusyXLCD());
WriteCmdXLCD (0xC0);
while (BusyXLCD());
putrsXLCD ("Temp = "); //doubixiaohanhan
while (BusyXLCD());
WriteCmdXLCD (0xC9);
while (BusyXLCD());
putrsXLCD (".");
while (BusyXLCD());
WriteCmdXLCD (0xCC);
while (BusyXLCD());
putrsXLCD ("deg");
while (BusyXLCD());
WriteCmdXLCD (0x94);
while (BusyXLCD());
putrsXLCD ("Humi = ");
while (BusyXLCD());
WriteCmdXLCD (0x9D);
while (BusyXLCD());
putrsXLCD (".");
while (BusyXLCD());
WriteCmdXLCD (0xA0);
while (BusyXLCD());
putrsXLCD ("%");
}
void SerialInit (void)
{
RCSTAbits.SPEN = 0; // disable serial port before configuring other parameters
TXSTAbits.SYNC = 0; // enable usart in asynchronous mode
//doubixiaohanhan
/******************* BaudRater Generation *************************/
BAUDCONbits.BRG16 = 0; // use the 8 bit spbrg protocol
TXSTAbits.BRGH = 1; // use High speed
SPBRG = 0x19; // to get a baud rate of 9600 bps @4MHZ
RCSTAbits.SPEN = 1; // enable serial port //doubixiaohanhan
TXSTAbits.TXEN = 1; // enable usart transmit
}