CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to

RHT03 Driver

Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message

Joined: 10 Dec 2011
Posts: 360
Location: Sofiq,Bulgariq

View user's profile Send private message

RHT03 Driver
PostPosted: Sat Mar 15, 2014 8:42 am     Reply with quote

In case you have to handle RHT03 (Temperature, Humidity) sensor, here is a driver which is tested. It works! The driver was developed thanks to these topics:
and the members involved in.

This is the code:

#INCLUDE <16F1508.h>
#USE delay(internal=16M, restart_wdt)
#USE rs232(BAUD=9600, UART1,ERRORS)

#define RHT_IO Pin_B6
#Define LED PIN_C7

#Include <RHT03.c>

void PrintData(int B1, int B2, int B3, int B4);

void main (void)
int8 i;
int8 ByteIndex[5];

output_high(led);    //Just for debugging WDT reset


While (True)
   SETUP_WDT(WDT_ON|WDT_32MS);                  //Prevent RHT03 Signal Stuck (will lead an infinite loop)
   RHT_Start();                                 //immediately after this line we must call reading of 40bits data
      for (i=0;i<5;i++)                         //5 bytes being read in this loop
         ByteIndex[i] = RHT_ReadData();
      printf("\r DATA Reception Succesful!");
      printf("\rDATA Garbage!");
   delay_ms(2300);                        //The process of data reception must last more than 2secs

void PrintData(int B1, int B2, int B3, int B4)
int16 humidity;
int16 temperature;
int1 TempSign;                         //the sign of the temperature (either minus or plus)
   TempSign = bit_test(B3,7);          //bit 7 determines if temerature is positive or negative, (=1 negative), (=0 positive)
   humidity = make16(B1,B2);           //assemble entire byte for humidity
   temperature = make16(B3,B4);        //assemble entire byte for temperature
   temperature &= 0b0111111111111111;  //exclude the sign of the temperature so I can convert it to a float value
   if(TempSign)                        //If temperature 15th bit is==1 the temperature value is negative
      printf(" T = -%.1f",(float)temperature/10);   //Note that I manualy put the minus sign
      printf(" T = %.1f",(float)temperature/10);
   printf(" RH = %.1f",(float)humidity/10);

And RHT03.C (driver):

void RHT03_Init(void)
   printf("\fRHT03 Initialization. . .");
   delay_ms(1500);                              //When power is supplied to sensor, don't send any instruction to the sensorwithin one second to pass unstable status
   printf("\fRHT03 Initialization done!");

void RHT_Start()
   output_low(RHT_IO);     // MCU pull low data bus  this process must beyond 1~10ms
   delay_ms(15);           // this process must beyond 1~10ms
   output_float(RHT_IO);   // MCU will pulls up and wait 20-40us for RHT03's response.
   while(input(RHT_IO));   // Wait for RHT03 pulls low the buss for 80uS as response signal
   while(!input(RHT_IO));   // Wait for RHT03 pulls up 80us for preparation to send data
   While(input(RHT_IO));   // RHT03 prepares data for being send (80uS)

int8 RHT_ReadData(void)    //RHT03 is sending data to MCU
int8 iIndex;
int8 iValue=0;
   for(iIndex = 0; iIndex < 8 ; iIndex++) //filling 8bit variable, bit by bit, according to RHT03 input state,
      while(!input(RHT_IO));     // every bit's transmission begin with low-voltage-level that last 50us
      shift_left(&iValue, 1, input(RHT_IO));
   return iValue;

You may change the timings if you consider as right.
Very important thing is not to forget the pull-up resistor 1K on RHT_IO pin!
A person who never made a mistake never tried anything new.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group