0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

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

CHANBAEK 来源:逗比小憨憨 作者:逗比小憨憨 2023-06-16 16:36 次阅读

基于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);
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 单片机
    +关注

    关注

    6034

    文章

    44547

    浏览量

    634376
  • Proteus
    +关注

    关注

    79

    文章

    1692

    浏览量

    106500
  • 仿真
    +关注

    关注

    50

    文章

    4066

    浏览量

    133526
  • DHT11
    +关注

    关注

    19

    文章

    277

    浏览量

    57577
  • 温湿度采集系统

    关注

    0

    文章

    6

    浏览量

    6095
收藏 人收藏

    评论

    相关推荐

    温湿度检测系统》+折线图显示DHT11温湿度数据

    ,与单片机之间,采用单总线通信方式。DHT11的数据结构:8bit湿度整数数据+8bit湿度小数数据+8bit温度整数数据+8bit温度小数数据+8bit校验和本人只保留了
    发表于 06-28 22:25

    51单片机dht11温湿度传感器

    设计、软件设计这三个方面来阐述。1.系统方案先来看一下整体的架构图:硬件部分由STC89C52单片机DHT11温湿度传感器、BT08蓝牙串口模块和Android手机组成。传感器将
    发表于 07-14 07:45

    DHT11温湿度传感器介绍

    DHT11温湿度传感器介绍,1.实物原理图2.模块说明2.1 DHT11产品概述DHT11数字温湿度传感器是一款含有已校准数字信号输出的
    发表于 07-21 09:04

    stm32单片机如何从DHT11获取到温湿度的呢

    stm32获取DHT11模块温湿度数值原理解析stm32单片机如何从DHT11获取到温湿度的呢?首先可以通过
    发表于 11-22 06:11

    DHT11温湿度传感器简介

    DHT11温湿度传感器1、DHT11简介DHT11数字温湿度传感器是一款含有已校准数字信号输出的温湿度
    发表于 02-16 06:55

    基于DHT11的多点温湿度报警系统设计

    单片机为控制核心,采用DHT11温湿度传感器,12864LCD显示模块,实现了实验室多点温湿度参数的实时采集、显示和超限报警功能。
    发表于 09-14 10:09 7817次阅读
    基于<b class='flag-5'>DHT11</b>的多点<b class='flag-5'>温湿度</b>报警<b class='flag-5'>系统</b>设计

    DHT11采集温湿度源程序

    DHT11采集温湿度并用LCD12864显示的源程序.可以使用的哈,分享给大家
    发表于 01-07 16:56 171次下载

    温湿度DHT11资料

    含有已校准数字信号输出的温湿度复合传感器,它应用专用的数字模块采集技术和温湿度传感技术,确保产品具有极高的可靠性和卓越的长期稳定性。传感器包括一个电阻式感湿元件和一个NTC测温元件,并与一个高性能8位
    发表于 11-29 17:28 27次下载

    DHT11温湿度传感器的AVR单片机例程

    DHT11温湿度传感器的AVR单片机例程
    发表于 05-16 14:46 26次下载
    <b class='flag-5'>DHT11</b><b class='flag-5'>温湿度</b>传感器的AVR<b class='flag-5'>单片机</b>例程

    使用51单片机实现DHT11温湿度检测的代码程序免费下载

    本文档的做作业内容详细介绍的是使用51单片机实现DHT11温湿度检测的代码程序免费下载。
    发表于 10-18 11:31 138次下载
    使用51<b class='flag-5'>单片机</b>实现<b class='flag-5'>DHT11</b><b class='flag-5'>温湿度</b>检测的代码程序免费下载

    51单片机DHT11温湿度ESP8266WiFi手机APP显示设计

    系统方案DHT11温湿度传感器采集数据传送给单片机单片机将数据处理之后通过ESP8266Wi
    发表于 11-04 16:21 119次下载
    51<b class='flag-5'>单片机</b><b class='flag-5'>DHT11</b><b class='flag-5'>温湿度</b>ESP8266WiFi手机APP显示设计

    基于51单片机+DHT11温湿度+LCD1602显示

    main.clcd1602.clcd1602.hdelay.cdelay.h项目展示DHT11温湿度相关介绍DHT11产品概述DHT11数字温湿度
    发表于 11-12 11:51 121次下载
    基于51<b class='flag-5'>单片机</b>+<b class='flag-5'>DHT11</b><b class='flag-5'>温湿度</b>+LCD1602显示

    687【毕设课设】基于单片机温湿度DHT11智能晾衣架系统

    687【毕设课设】基于单片机温湿度DHT11智能晾衣架系统
    发表于 11-13 10:36 20次下载
    687【毕设课设】基于<b class='flag-5'>单片机</b><b class='flag-5'>温湿度</b><b class='flag-5'>DHT11</b>智能晾衣架<b class='flag-5'>系统</b>

    stm32获取DHT11模块温湿度数据原理解析

    stm32获取DHT11模块温湿度数值原理解析stm32单片机如何从DHT11获取到温湿度的呢?首先可以通过
    发表于 11-13 20:06 55次下载
    stm32获取<b class='flag-5'>DHT11</b>模块<b class='flag-5'>温湿度</b>数据原理解析

    STM32单片机利用DHT11采集温湿度

    测量原理:DHT11内部集成了一个8位单片机、电阻湿度传感器和NTC来采集外部的温湿度单片机
    发表于 12-27 19:22 14次下载
    STM32<b class='flag-5'>单片机</b>利用<b class='flag-5'>DHT11</b><b class='flag-5'>采集</b><b class='flag-5'>温湿度</b>