基于PIC18系列单片机的DHT11温湿度采集系统设计

描述

基于PIC18系列(PIC18F4520)单片机+DHT11的温湿度采集系统的设计与制作(Proteus仿真部分)

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	
}
//process.c

#include "prototyper.h"

void DelayFor18TCY (void)		
{
	Delay10TCYx (2);		        // a delay of 20 TCY
}

void DelayPORXLCD (void) 		
{
	Delay1KTCYx (15);     			// a delay of 15ms at 4 MHZ
}
//doubixiaohanhan
void DelayXLCD (void) 			
{
	Delay1KTCYx (5);				// a delay of 5ms at 4 MHZ  
}


void serialTx (unsigned char rx_data)
{
	TXREG = rx_data;
	Delay10TCY();	
}

void DHT11_Start (void)
{
	Data_Dxn = 0; 				// DataPin is set as an Output pin 
	
	Data_Out = 0; 				// Pull the DataBus line Low 
	Delay1KTCYx (20); 			// A Delay of At least 18ms @4MHZ 
		
	Data_Out = 1; 
	Delay10TCYx (3);			// A Delay of atleast 30us @4MHZ
	 	
	Data_Dxn = 1; 				// DataPin set as an Input pin//doubixiaohanhan
}

short int  DHT11_Response (void)
{	
	Delay10TCYx (4);			// A Delay of 40us @ 4MHZ
	
	if (Data_In == 0)			// If Data line is low 
	{
		Delay10TCYx (8);		// A Delay of 80us @ 4MHZ
		
		if (Data_In == 1)		// If Data Line is High 
		{
			Delay10TCYx (5);	// A Delay of 50us @ 4MHZ
			return 1; 
		}
	}	
}

unsigned char DHT11_Read (void)
{
	unsigned char i; 
	unsigned char data = 0x00; 
	unsigned char Time = 0; 
	
	for (i = 0; i < 8; i++)
	{
		while (!Data_In)			// Wait Till Line goes High
		{
			Time++; 
			if (Time > 100)
			{	
				break; 
			}
			
			Delay1TCY (); 			// A Delay of 1us @ 4MHZ
		}//doubixiaohanhan
		
		Delay10TCYx (3); 			// Wait for atleast 30us to check if bit is either 1 or 0
		
		if (!Data_In)				// Bit is 0				
		{
			data = (data < < 1) | 0; 
		}
		
		else 
		{
			data = (data < < 1) | 1; // Bit is 1
			
			Time = 0; 
			while (Data_In)			// Wait till Databus goes Low 
			{
				Time++; 
				if (Time > 100)
				{
					break; 
				}
				
				Delay1TCY (); 
			}		
		}
	}
	
	return data; 
}

unsigned char Check_Parity (unsigned char parity, unsigned char RHI, unsigned char RHD, unsigned char TI, unsigned char TD)
{	
	if (parity == (RHI + RHD + TI + TD))
	{
		return 1; 
	}
	else 
	{
		return 0; 
	}
}


void Humidity_Serial (unsigned char Int_Part, unsigned char Float_Part)
{
	unsigned char Tens; 
	unsigned char Ones; 
	unsigned char Dec; 
	
	Tens = Int_Part / 10; 
	Ones = Int_Part % 10;
	Dec = Float_Part;  
	
	Tens += 48;				// Convet To ASCII Counterparts
	Ones += 48;  			//
	Dec  += 48; 			// 
	
	serialTx ('H');
	Delay1KTCYx (2);
	serialTx ('\\r');
	
	serialTx (Tens);
	Delay1KTCYx (2);
	serialTx (Ones);
	Delay1KTCYx (2);
	serialTx ('.');
	Delay1KTCYx (2);
	serialTx (Dec);
	serialTx ('\\r');
	Delay1KTCYx (2);
	serialTx ('\\r');
	Delay1KTCYx (2);
}

void Temp_Serial (unsigned char Int_Part, unsigned char Float_Part)
{
	unsigned char Tens; 
	unsigned char Ones; 
	unsigned char Dec; 
	
	Tens = Int_Part / 10; 
	Ones = Int_Part % 10;
	Dec = Float_Part;  
	
	Tens += 48;				// Convet To ASCII Counterparts
	Ones += 48;  			//
	Dec  += 48; 			// 
	
	serialTx ('T');
	Delay1KTCYx (2);
	serialTx ('\\r');
	
	serialTx (Tens);
	Delay1KTCYx (2);
	serialTx (Ones);
	Delay1KTCYx (2);
	serialTx ('.');
	Delay1KTCYx (2);
	serialTx (Dec);
	serialTx ('\\r');
	Delay1KTCYx (2);
	serialTx ('\\r');
	Delay1KTCYx (2);
}

void ParityError (void)
{
	unsigned char i = 0; 
	unsigned char Parity [] = "Parity Error";
	
	while (Parity [i] != '\\0')
	{
		serialTx (Parity [i]);
		i++; 
		Delay1KTCYx (2);
	}
	
	serialTx ('\\r');
	Delay1KTCYx (2);
	serialTx ('\\r');
	Delay1KTCYx (2);
	
	Delay10KTCYx (50);
}

void SensorUnresponsive (void)
{
	unsigned char i = 0; 
	unsigned char Response [] = "Sensor Unresponsive"; 
	
	while (Response [i] != '\\0')
	{
		serialTx (Response [i]);
		i++; 
		Delay1KTCYx (2);
	}
	
	serialTx ('\\r');
	Delay1KTCYx (2);
	serialTx ('\\r');
	Delay1KTCYx (2);
	
	Delay10KTCYx (50);
}

void DisplayTemp (unsigned char Int_Part , unsigned char Float_Part)
{
	unsigned char Tens; 
	unsigned char Ones; 
	unsigned char Dec; 
	
	Tens = Int_Part / 10; 
	Ones = Int_Part % 10;
	Dec = Float_Part;
	
	while (BusyXLCD());
	WriteCmdXLCD (0xC7);
	
	if (Tens != 0)
	{
		while (BusyXLCD());	
		WriteDataXLCD (Tens + 48);
	}
	
	else 
	{
		while (BusyXLCD());
		putrsXLCD (" "); 		// Write nothing 	
	}
	
	while (BusyXLCD());	
	WriteDataXLCD (Ones + 48);
	
	while (BusyXLCD());
	WriteCmdXLCD (0xCA);
	
	while (BusyXLCD());	
	WriteDataXLCD (Dec + 48);	
}

void DisplayHumidity (unsigned char Int_Part , unsigned char Float_Part)
{
	unsigned char Tens; 
	unsigned char Ones; 
	unsigned char Dec; 
	
	Tens = Int_Part / 10; 
	Ones = Int_Part % 10;
	Dec = Float_Part;
	
	while (BusyXLCD());
	WriteCmdXLCD (0x9B);
	

	while (BusyXLCD());	
	WriteDataXLCD (Tens + 48);
		
	while (BusyXLCD());	
	WriteDataXLCD (Ones + 48);
	
	while (BusyXLCD());
	WriteCmdXLCD (0x9E);
	
	while (BusyXLCD());	
	WriteDataXLCD (Dec + 48);
}

void Parity_Error (void)
{
	while (BusyXLCD());
	WriteCmdXLCD (CLRSCR); 		// Clear Screen 
	
	while (BusyXLCD());
	putrsXLCD ("Parity Error!!");
	
	while (BusyXLCD());
	WriteCmdXLCD (CLRSCR); 		// Clear Screen 
}

void Sensor_Unresponsive (void)
{	
	while (BusyXLCD());
	WriteCmdXLCD (CLRSCR); 		// Clear Screen 
	
	while (BusyXLCD());
	putrsXLCD ("Sensor Unresponsive!");
	
	while (BusyXLCD());
	WriteCmdXLCD (CLRSCR); 		// Clear Screen 
}
//prototyper.h

#include < p18f4520.h >
#include < delays.h >
#include < xlcd.h >

#define CLRSCR 		0x01
#define DISPLAY_ON 	0x0C
#define DISPLAY_OFF 0x08


#define Data_Dxn TRISCbits.TRISC1
#define Data_Out LATCbits.LATC1
#define Data_In  PORTCbits.RC1
//doubixiaohanhan
//doubixiaohanhan
void DHT11_Start (void);
short int DHT11_Response (void);
unsigned char DHT11_Read (void);
unsigned char Check_Parity (unsigned char, unsigned char, unsigned char, unsigned char, unsigned char); 


void Port_Init (void);
void SerialInit (void); 
void serialTx (unsigned char); 

void Temp_Serial (unsigned char, unsigned char);
void Humidity_Serial (unsigned char, unsigned char);

void ParityError (void);
void SensorUnresponsive (void);

/*************************************** LCD Functions ************************************///doubixiaohanhan

void LCD_Init (void);
void LCD_Stuff (void); 

void DisplayTemp (unsigned char, unsigned char);
void DisplayHumidity (unsigned char, unsigned char);

void Parity_Error (void);
void Sensor_Unresponsive (void);
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分