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

    关注

    6002

    文章

    43982

    浏览量

    620907
  • Proteus
    +关注

    关注

    76

    文章

    1688

    浏览量

    105601
  • 仿真
    +关注

    关注

    50

    文章

    3872

    浏览量

    132166
  • DHT11
    +关注

    关注

    19

    文章

    264

    浏览量

    57179
  • 温湿度采集系统

    关注

    0

    文章

    6

    浏览量

    6079
收藏 人收藏

    评论

    相关推荐

    DHT11温湿度计 实时温湿度记录曲线 记录存盘 上下限报警 .....

    ``DHT11温湿度计 实时温湿度记录曲线 记录存盘 上下限报警单片机采用超小SOP-8单片机,型号STC15F104W。上位
    发表于 04-29 11:15

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

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

    51单片机dht11温湿度传感器

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

    DHT11温湿度数据的采集

    用的控制器是STM32F103C8T6,如果你用STMF103的其他芯片来跑这个代码也能跑通,基本配置都是一样的。先介绍DHT11温湿度数据的采集,有两个文件,一个DHT11.c,还有
    发表于 07-16 06:24

    DHT11温湿度传感器介绍

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

    基于STM32开发板实现传感数据采集-DHT11温湿度采集

    基于STM32开发板实现传感数据采集-DHT11温湿度采集项目简介:本次项目是基于STM32开发板实现传感数据采集-
    发表于 08-10 07:41

    如何利用STC89C52和DHT11实现温湿度检测仪的设计

    系统由STC89C52单片机DHT11传感器模块、1602液晶显示屏模块、按键模块、报警模块构成。具体功能:1、DHT11数字温湿度传感器
    发表于 11-10 09:14

    通过使用AT89C52和DHT11设计的温湿度控制系统资料分享

    【资源下载】下载地址:点击下载 1343 百度网盘本文通过使用AT89C52单片机DHT11传感器模块、1602液晶显示屏模块以及继电器控制模块。简单明了的实现的温湿度的控制要求。DHT11
    发表于 11-10 07:02

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

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

    PIC16F887 DHT11温湿度程序

    PIC16F877A 单片机 PIC16F887 DHT11 温湿度 程序
    发表于 11-24 06:25

    DHT11温湿度传感器简介

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

    DHT11数字温湿度传感器的相关资料推荐

    STM32采集DHT11温湿度关于DHT11相关参数代码篇接线和实验结果总结关于DHT11DHT11是一款数字
    发表于 02-21 07:34

    温湿度DHT11资料

    温湿度DHT11资料汇总 DHT11是一款有已校准数字信号输出的温湿度传感器。 其精度湿度+-5%RH, 温度+-2℃,量程
    发表于 11-29 17:28 24次下载

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

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

    PIC16F877A 单片机 PIC16F887 DHT11 温湿度 程序

    电子发烧友网站提供《PIC16F877A 单片机 PIC16F887 DHT11 温湿度 程序.pdf》资料免费下载
    发表于 11-16 16:51 34次下载
    <b class='flag-5'>PIC</b>16F877A <b class='flag-5'>单片机</b> <b class='flag-5'>PIC</b>16F887 <b class='flag-5'>DHT11</b> <b class='flag-5'>温湿度</b> 程序