rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
RHT03 Driver |
Posted: Sat Mar 15, 2014 8:42 am |
|
|
Hello!
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:
http://www.ccsinfo.com/forum/viewtopic.php?t=51358&highlight=dht22
http://www.ccsinfo.com/forum/viewtopic.php?t=52068
and the members involved in.
This is the code:
Code: |
#INCLUDE <16F1508.h>
#FUSES INTRC_IO, NOMCLR, WDT_SW, PUT, NOBROWNOUT
#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
delay_ms(1000);
output_low(led);
RHT03_Init();
While (True)
{
SETUP_WDT(WDT_ON|WDT_32MS); //Prevent RHT03 Signal Stuck (will lead an infinite loop)
restart_wdt();
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();
SETUP_WDT(WDT_OFF);
if(ByteIndex[0]+ByteIndex[1]+ByteIndex[2]+ByteIndex[3]==ByteIndex[4])
printf("\r DATA Reception Succesful!");
else
printf("\rDATA Garbage!");
PrintData(ByteIndex[0],ByteIndex[1],ByteIndex[2],ByteIndex[3]);
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
else
printf(" T = %.1f",(float)temperature/10);
printf(" RH = %.1f",(float)humidity/10);
} |
And RHT03.C (driver):
Code: |
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
delay_us(35);
shift_left(&iValue, 1, input(RHT_IO));
while(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!
Enjoy! _________________ A person who never made a mistake never tried anything new. |
|