• AVR Freaks

Hot!C Code to Interface Humidity Sensor (DHT22)

Page: < 12345 Showing page 5 of 5
Author
1and0
Access is Denied
  • Total Posts : 9615
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: C Code to Interface Humidity Sensor (DHT22) 2019/04/24 07:24:54 (permalink)
+1 (1)
(to stop the flashy)
 
Edit: Wow... 5 pages already to read a sensor.  :(
 
post edited by 1and0 - 2019/04/24 07:26:18
#81
Eng. Daniel D. Albino
Engineer
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2019/02/28 10:17:53
  • Location: 0
  • Status: offline
Re: C Code to Interface Humidity Sensor (DHT22) 2019/04/24 17:35:29 (permalink)
+2 (2)

[Portugues]
Olá delfindelfin, tudo bem?
Bem, acredito que posso contribuir um pouco com este post.
Segue a minha lib, adaptada da versão para Arduino para aplicar ao meu PICCININO UNO com PIC18F25K22.
A lib contempla a familia de sensores DHT11,22 ...
e compilei para versão XC8.
O codigo de exemplo foi gerado em conjunto com o MCC e pode ajudar no seu desenvolvimento.
...
No exemplo , configurei um timer para executar overflow a cada 1 milisegundo, para:
1- atualizar a variavel de tempo do sensor.
2- atualizar a saida da serial a cada 5 segundos, setando uma flag.
Como pode observar, no loop prinipal é executada a verificação da flag para atualizar a porta serial com os valores de leitura da temperatura e humidade.
Espero ter ajudado.
Eng. Daniel Donaire Albino

[English]

Hello delfindelfin, how are you?
Well, I believe I can contribute a bit with this post.
Here is my lib, adapted from the version for Arduino to apply to my PICCININO UNO with PIC18F25K22.
A lib contemplates the family of sensors DHT11,22 ...
and compiled for XC8 version.
The sample code was generated in conjunction with the MCC and can assist in its development.
...
In the example, I set up a timer to run overflow every 1 millisecond, to:
1- Update the time variable of the sensor.
2- Update the serial output every 5 seconds, setting a flag.
As you can see, in the main loop the flag verification is performed to update the serial port with the temperature and humidity reading values.
Hope this helps.
Eng. Daniel Donaire Albino
post edited by Eng. Daniel D. Albino - 2019/04/24 17:37:28

Attached Image(s)

#82
mlp
boots too small
  • Total Posts : 794
  • Reward points : 0
  • Joined: 2012/09/10 15:12:07
  • Location: previously Microchip XC8 team
  • Status: offline
Re: C Code to Interface Humidity Sensor (DHT22) 2019/04/25 06:46:51 (permalink)
+1 (1)
1and0
Wow... 5 pages already to read a sensor.

I think the existence of a dialog is slightly more useful than OP just ghosting.

Mark (this opinion available for hire)
#83
InvalidApple
Super Member
  • Total Posts : 296
  • Reward points : 0
  • Joined: 2011/05/17 23:36:35
  • Location: Melbourne, Australia
  • Status: offline
Re: C Code to Interface Humidity Sensor (DHT22) 2019/04/25 21:25:45 (permalink)
0
This may be off topic. But do you know any good forums about C language and Linux?

For a good c language specific forum, look at
https://cboard.cprogramming.com/c-programming/
 
[edit]I did not realise that this was 5 pages long![/edit]
post edited by InvalidApple - 2019/04/25 21:28:13
#84
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: C Code to Interface Humidity Sensor (DHT22) 2019/06/04 04:45:03 (permalink)
0
Well, this last code works fine:
Main
 

Here I would post my code, if the firewall of this frigging forum would let me

 
DHT22 

// Including C Standard Libraries
#include <stdint.h>
// Including XC8 Compiler Library
#include <xc.h>
// Including User's Libraries
#include "dht22.h"
// Defining Oscillator Frequency
#define _XTAL_FREQ 8000000
// Binary to BCD to ASCII Converter ///////////////////////////////////////////////////////////////// 
void Binary_BCD_ASCIIConverter(char *Sen_Decim, char *Sen_Ones, char *Sen_Tens, uint16_t binary )
{

uint8_t Hundreds = 0;
uint8_t Tens = 0;
uint8_t Ones = 0;

uint8_t i;

for(i=0; i<16; i++) { // Dabble Double Algorithm to Convert Binary into BCD
Ones <<= 1;
Tens <<= 1;
Hundreds <<= 1;

if (binary & 0x8000) {
Ones++;
}
if (Ones >= 10) {
Ones -= 10;
Tens++;
}
if (Tens >= 10) {
Tens -= 10;
Hundreds++;
}
if (Hundreds >= 10) {
Hundreds -= 10;
}
binary <<= 1;
}

*Sen_Decim = Ones; // Assigning BCD Values to Sensor Reading Values
*Sen_Ones = Tens;
*Sen_Tens = Hundreds;

*Sen_Decim += '0'; // convert to ASCII
*Sen_Ones += '0';
*Sen_Tens += '0';
}
// Humidity Sensor Initiation ////////////////////////////////////////////////////
void Init_Humid_Sensor()
{
TRISCbits.TRISC2 = 0; // RC2 as Output PIN

PORTCbits.RC2 = 0;
__delay_ms(18); // RC2 Low for 18 ms

PORTCbits.RC2 = 1;
__delay_us(30); // RC2 High for 30 us

TRISCbits.TRISC2 = 1; // RC2 as Input PIN 

}
 
void Read_Humid_Sensor(char *Humid_Decim, char *Humid_Ones, char *Humid_Tens, char *Temp_Decim, char *Temp_Ones, char *Temp_Tens, char *Temp_Sign, char *Data_Corruption_Bit)
{
volatile uint8_t j;

//// Resetting Values ///////////////////////////////////////////////////////////////////////
uint16_t humid = 0 ; // Sensor Humid Data
uint16_t temp = 0 ; // Sensor Temperature Data
uint8_t checksum = 0 ; // Sensor Checksum

//// Skipping Sensor Response Signal 
__delay_us(80); // DHT22 Responds Low for 80 uS 
__delay_us(80); // DHT22 Responds High for 80 uS
//// Humidity Data Bus Reading ///////////////////////////////////////////////////////////////
for(j=0;j<40;j+=1)
{

while (PORTCbits.RC2 != 0); // Wait until RC2 is Low 
while (PORTCbits.RC2 != 1); // Wait until RC2 is high

__delay_us(30);

if(j<16){ // Select Data Humidity 
humid <<= 1;
if (PORTCbits.RC2 == 1) humid |= 1;
}
else if(j<32){ // Select Data Temperature
temp <<= 1;
if (PORTCbits.RC2 == 1) temp |= 1;
}
else{
checksum <<= 1; 
if (PORTCbits.RC2 == 1) checksum |= 1;
}



//// Checking if Data Corruption has Occurred /////////////////////////////////////////////////////
char humid_high = ( humid & 0xFF00 ) >> 8 ; // ( HHHHHHHH LLLLLLLL & 11111111 00000000 ) >> = HHHHHHHH 
char humid_low = humid & 0x00FF ; // ( HHHHHHHH LLLLLLLL & 00000000 11111111 ) >> = LLLLLLLL 
char temp_high = ( temp & 0xFF00 ) >> 8; 
char temp_low = temp & 0x00FF ;

char theor_checksum = humid_high + humid_low + temp_high + temp_low ; // Computing Theoretical Checksum

if( checksum == theor_checksum )
{
*Data_Corruption_Bit = 0;

char Humid_DecimX = 0;
char Humid_OnesX = 0;
char Humid_TensX = 0;

//// Humidity Conversions ///////////////////////////////////////////////////
Binary_BCD_ASCIIConverter(&Humid_DecimX, &Humid_OnesX, &Humid_TensX, humid);
 
*Humid_Decim = Humid_DecimX ;
*Humid_Ones = Humid_OnesX ;
*Humid_Tens = Humid_TensX ;

//// Temperature Conversion ////////////////////////////////////////////////
uint8_t temp_bit_sign = temp >> 15; // Checking Temperature Bit Sign

if(temp_bit_sign == 1){ 
*Temp_Sign = '-' ; 
}else{
*Temp_Sign = ' ' ; 
}

char Temp_DecimX = 0;
char Temp_OnesX = 0;
char Temp_TensX = 0;
 
 
 
Binary_BCD_ASCIIConverter(&Temp_DecimX, &Temp_OnesX, &Temp_TensX, temp); // Converting Binary to BCD to ASCII 

*Temp_Decim = Temp_DecimX;
*Temp_Ones = Temp_OnesX;
*Temp_Tens = Temp_TensX;
}
else 
{
*Data_Corruption_Bit = 1;

}
}

 
I don't know why, but when I change in the main function this:

__delay_ms(350);

 The LCD is refreshing the values ... So, shot in the dark, was it the sensor sampling rate the problem?
post edited by delfindelfin - 2019/06/04 04:49:33

MPLAB X IDE v5.05
XC8 2.00
#85
1and0
Access is Denied
  • Total Posts : 9615
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: C Code to Interface Humidity Sensor (DHT22) 2019/06/04 07:33:13 (permalink)
+3 (3)
delfindelfin
Well, this last code works fine:

... which will lockup in infinite loop if and when the sensor does not respond.
 
 
I don't know why, but when I change in the main function this:

__delay_ms(350);

The LCD is refreshing the values ... So, shot in the dark, was it the sensor sampling rate the problem?

Sometimes I feel I'm wasting my time with you. :(  Anyway, I know definitely I've mentioned the sampling rate of this sensor somewhere in this thread.
#86
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: C Code to Interface Humidity Sensor (DHT22) 2019/06/04 09:06:26 (permalink)
0
I am tired, I need to sleep .. I am going to check the code sharply to fix those subtleties but the main problem has already been fixed, it was the sampling rate as you mention it .. Thanks anyway
post edited by delfindelfin - 2019/06/04 09:33:49

MPLAB X IDE v5.05
XC8 2.00
#87
PStechPaul
Super Member
  • Total Posts : 2375
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: C Code to Interface Humidity Sensor (DHT22) 2019/06/04 10:04:17 (permalink)
+2 (2)
One way to implement a one second timeout to avoid getting stuck in an infinite loop due to lack of response:

int timeout = 1000;

while (PORTCbits.RC2 != 0 && timeout > 0) {

  timeout--;

  __delay_ms(1); }// Wait until RC2 is Low

timeout = 1000;
while (PORTCbits.RC2 != 1 && timeout > 0) {

  timeout--;

  __delay_ms(1); } // Wait until RC2 is high



 
#88
1and0
Access is Denied
  • Total Posts : 9615
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: C Code to Interface Humidity Sensor (DHT22) 2019/06/04 13:00:28 (permalink)
0
PStechPaul
One way to implement a one second timeout to avoid getting stuck in an infinite loop due to lack of response:

int timeout = 1000;
while (PORTCbits.RC2 != 0 && timeout > 0) {
  timeout--;
  __delay_ms(1); }// Wait until RC2 is Low
timeout = 1000; 

while (PORTCbits.RC2 != 1 && timeout > 0) {
  timeout--;
  __delay_ms(1); } // Wait until RC2 is high



Usage of 1 ms delays is way too long, as the pulse duration of the signal on RC2 is less than 100 us.
#89
PStechPaul
Super Member
  • Total Posts : 2375
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: C Code to Interface Humidity Sensor (DHT22) 2019/06/04 20:52:16 (permalink)
+1 (1)
That was just an illustration of how it can be done. I saw the OP's mention of a __delay_ms(350) and figured this is a way to avoid getting stuck. There should also be some sort of timeout handler. But I don't intend to do work that the OP should be doing.

 
#90
Page: < 12345 Showing page 5 of 5
Jump to:
© 2019 APG vNext Commercial Version 4.5