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 support@ccsinfo.com

RHT03 Driver

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rikotech8



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

View user's profile Send private message

RHT03 Driver
PostPosted: Thu Mar 13, 2014 2:27 pm     Reply with quote

Hello several weeks ago I requested for RHT03 driver. Some of you pointed me to this link:
www.ccsinfo.com/forum/viewtopic.php?t=51358&highlight=dht22
I modified the code so, to respond my output expectations.
Here is the driver I made:
Code:

void RHT03_Init(void)
{
   printf("\fRHT03 Initialization. . .");
   delay_ms(1500);                              //When power is supplied to sensor, don't send any instruction to the sensor within 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);             
      if(input(RHT_IO))
      {
         iValue = iValue |(1<<(7 - iIndex));
         while(input(RHT_IO));
      }
   }
   return iValue;
}


And the program to handle with:
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 <C:\Users\Riko\Desktop\Leonid_Silistra\CCS\RHT-03 driver\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 debuging 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 humidity
   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);
}


I tested the program and the data I receive is correct. Also when data_IO hang the controller resets the communication.

My question is:
Is my driver adequate for RHT03 handling, and should I post it in code library ?
_________________
A person who never made a mistake never tried anything new.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Mar 14, 2014 6:32 pm     Reply with quote

Congratulations on completing your driver and thanks for sharing it with us!

A few small notes:
- Add #case to your program. You are now mixing upper and lower case in non consistent ways. With #case the compiler will help you to follow the C rules.
- I see you use other timings than in the example program on the Sparkfun website. I didn't check these. Just noted a difference. https://www.sparkfun.com/products/10167

Code:
#Include <C:\Users\Riko\Desktop\Leonid_Silistra\CCS\RHT-03 driver\RHT03.c>
It's better practice to have relative paths in your code, like:
Code:
#Include "RHT03.c"
Note the use of "" instead of <>, this makes the compiler search for the file in the same directory as the current source file. <> will cause the compiler to search in the standard library locations.

Your RHT_ReadData function can be made 9 assembly instructions smaller by using the same method as used in the CCS example programs for bit banged communications. Instead of special handling for the '1' input we now always sample and shift the input.
Code:
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 begins with low-voltage-level that lasts 50us

      delay_us(35);                       // Sample in middle of bit time
      shift_left(&iValue, 1, input(RHT_IO));

      while(input(RHT_IO));               // Wait for level to go low again (if applicable)
   }
   return iValue;
}
rikotech8



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

View user's profile Send private message

PostPosted: Sat Mar 15, 2014 7:43 am     Reply with quote

Thank you very much for the advices! Since I am not a professional programmer, and there is not large C programming community around me, I very much appreciate the help you giving to me! I know the forum designation is rather code troubleshooting, than getting know-how, but thanks to your collaboration, the things go better for me. I am about to modify the driver and source code according to tips, given to me.
Thank you again!
_________________
A person who never made a mistake never tried anything new.
rikotech8



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

View user's profile Send private message

PostPosted: Sat Mar 15, 2014 8:24 am     Reply with quote

And the evidences ckielstra was right. I Haven't doubted!

Quote:

.................... shift_left(&iValue, 1, input(RHT_IO));
00F4: MOVLB 01
00F5: BSF 0D.6
00F6: MOVLB 00
00F7: BTFSC 0D.6
00F8: GOTO 0FB
00F9: BCF 03.0
00FA: GOTO 0FC
00FB: BSF 03.0
00FC: RLF 2A,F


Quote:

.................... iValue = iValue |(1<<(7 - iIndex));
00F9: MOVF 29,W
00FA: SUBLW 07
00FB: MOVWF 78
00FC: MOVLW 01
00FD: MOVWF 77
00FE: MOVF 78,F
00FF: BTFSC 03.2
0100: GOTO 104
0101: LSLF 77,F
0102: DECFSZ 78,F
0103: GOTO 101
0104: MOVF 77,W
0105: IORWF 2A,F[/b]

_________________
A person who never made a mistake never tried anything new.
esa



Joined: 31 Jan 2008
Posts: 19

View user's profile Send private message

PostPosted: Sat Mar 22, 2014 2:29 pm     Reply with quote

Thank a lot for the code.
I was able to read the device using your code in less than 10 minutes.
Now, I will analyze it to understand how it works.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion 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