DogFlu66
Member
Hello; this time I leave you the procedure to read the RHT03 humidity and temperature sensor.
It is a sensor that has a single-wire port, which is used to request the reading and also receive the reading from it.
All the lines of the function are commented and indicate the port reading procedure with their corresponding times.
Timer1 is used to convert the data frame generated by the sensor to bits. It is used to measure the width of the pulses that the RHT03 generates on its port.
The function has been tested working the microcontroller at 4 and 8MHz.
Function:
Call _Read_RHT03(HR, TMP, CHK, CLOCK, "C") 'Read sensor.
'Call _Read_RHT03(HR, TMP, CHK, CLOCK, "F") 'Read sensor.
HR: returns relative humidity.
TMP: returns temperature.
CHK: returns checksum = 0 = OK.
CLOCK: System MHz.
C or F: reading in ºC or ºF.
I leave the download link of the sensor data sheet.
It is a sensor that has a single-wire port, which is used to request the reading and also receive the reading from it.
All the lines of the function are commented and indicate the port reading procedure with their corresponding times.
Timer1 is used to convert the data frame generated by the sensor to bits. It is used to measure the width of the pulses that the RHT03 generates on its port.
The function has been tested working the microcontroller at 4 and 8MHz.
Function:
Call _Read_RHT03(HR, TMP, CHK, CLOCK, "C") 'Read sensor.
'Call _Read_RHT03(HR, TMP, CHK, CLOCK, "F") 'Read sensor.
HR: returns relative humidity.
TMP: returns temperature.
CHK: returns checksum = 0 = OK.
CLOCK: System MHz.
C or F: reading in ºC or ºF.
I leave the download link of the sensor data sheet.
Code:
'**********************************************************************************
'Relative humidity and temperature reading function for the RHT03 sensor with TMR1.
'PicEBasic Board, By dogflu66, 03/2023.
'Basic Pic Simulator IDE (PSI), v6.92, Pic16F88.
'**********************************************************************************
Define CONF_WORD = 0x2f50
Define CONF_WORD_2 = 0x3ffc
Define CLOCK_FREQUENCY = 8
'Define SIMULATION_WAITMS_VALUE = 1 'Activate for simulation.
Define SINGLE_DECIMAL_PLACES = 1
'-----------------------------------------
'Include "_ProcSetUpEBasic.bas"
'Include "_FuncionesGeneralLibrary.bas"
'Include "_FuncionesPic16F88.bas"
'-----------------------------------------
'Clock internal.
OSCCON.SCS0 = 0
OSCCON.SCS1 = 0
'8Mhz
OSCCON.IRCF0 = 1
OSCCON.IRCF1 = 1
OSCCON.IRCF2 = 1
'4Mhz
'OSCCON.IRCF0 = 0
'OSCCON.IRCF1 = 1
'OSCCON.IRCF2 = 1
'Pin configuration
AllDigital
ConfigPin PORTA = Output
ConfigPin PORTB = Output
'Configuration lcd ports. --------------------------------------------
Define LCD_BITS = 4 'Data bus length to 4bits
Define LCD_DREG = PORTA 'Data bus: puerto A
Define LCD_DBIT = 0 'Least significant bit of the data bus.
Define LCD_RSREG = PORTB 'Rs control bit on port B
Define LCD_RSBIT = 7 'RS bit on RB7 pin.
Define LCD_EREG = PORTB 'E control bit on port B.
Define LCD_EBIT = 6 'E pin on RB6 pin
Define LCD_COMMANDUS = 2000 'Wait after each command in uSec.
Define LCD_DATAUS = 100 'Wait after sending a data to the LCD in uSec.
Define LCD_INITMS = 50 'Wait during display initialization in mSec.
'---------------------------------------------------------------------
Hseropen 4800 'Initialize port RS232
Lcdinit 'Initialize lcd
'-----------------------------------------
Lcdout "Test P. RHT03" 'Print text on lcd
Call _SetUp_RHT03() 'Initialize
Symbol _Led_RHT03 = RB1 'Pin is assigned to the RHTO3 adapter board led
ConfigPin _Led_RHT03 = Output 'Led
Dim CLOCK As Byte
CLOCK = 8 'System clock in Mhz
Dim HR As Single 'Relative Humidity
Dim TMP As Single 'Temperature
Dim CHK As Byte 'Checksum
WaitMs 2000 'Pause 2Sec.
While True 'Loop
_Led_RHT03 = True
Call _Read_RHT03(HR, TMP, CHK, CLOCK, "C") 'Read sensor.
'Call _Read_RHT03(HR, TMP, CHK, CLOCK, "F") 'Read sensor.
_Led_RHT03 = False
Lcdcmdout LcdLine1Home 'Cursor to beginning of line1
Lcdout "HR:", #HR, " T:", #TMP, "C " 'Data output on lcd (Europe)
'Lcdout "HR:", #HR, " T:", #TMP, "F " 'Data output on lcd
Lcdcmdout LcdLine2Home 'Cursor to beginning of line2
Lcdout "Checksum: " 'Print checksum
Select Case CHK
Case 0
Lcdout "OK. " 'Reading ok
Case Else
Lcdout "ERROR. " 'Read error
EndSelect
WaitMs 500 'Pause in mSec.
Wend
End
'Config pin datos port RHT03
Symbol _Pin_Data_RHT03 = RB4 'Assigns the data pin for the sensor RHT03
Symbol _Tmr1If = PIR1.TMR1IF 'Timer1 overflow indicator flag.
'Config pin as input and timer1
Proc _SetUp_RHT03()
ConfigPin _Pin_Data_RHT03 = Input
'Set timer 1
T1CON = %00000000 'Assigns oscillator type (_Tmr1_Internal)
T1CON.T1CKPS1 = 0 'Assign divisor x1
T1CON.T1CKPS0 = 0
T1CON.TMR1ON = 1 'Activate modulo
End Proc
'****************************************************************************************************
'Rading function for the RHT03 sensor, returns values by reference of Relative Humidity,
'temperature and checksum.
'_HR: passes the velue for reference (relative humidity).
'_TMP: passes the value for reference (temperature).
'_CHK: pass the value by reference (checksum: = 1 errror, = 0 Ok).
'Input argument _CLK = Mhz of the system clock.
'_TMP = (_TMP * 9 / 5) + 32 'ºF
'_tmr1if = PIR1.TMR1IF 'Label the timer overflow flag.
'****************************************************************************************************
Proc _Read_RHT03(ByRef _HR As Single, ByRef _TMP As Single, ByRef _CHK As Byte, _CLK As Byte, _SYS As Byte)
Dim _chk_aux As Byte
Dim _HR03 As Word
Dim _TMP03 As Word
Dim _Signo As Bit
Dim _n As Byte
Dim _x As Byte
Dim _v_aux(40) As Word
'Initialization variables.
_CLK = _CLK * 10 'calculates and assigns the duration of the pulse width corresponding
_HR = 0
_TMP = 0
_chk_aux = 0
_HR03 = 0
_TMP03 = 0
_Signo = 0
_CHK = 1 'Checksum error is activated by defaul
'Waits for the sensor communication port to become free.
ConfigPin _Pin_Data_RHT03 = Input 'Sensor port as unput.
Call _Clear_Timer1() 'Reset counter of timer1 and tmr1if.
While _Pin_Data_RHT03 = False And _Tmr1If = False 'Wait until the port becomes free.
Wend
If _Tmr1If = True Then Exit 'No response, communication error.
'MCU generates read start request to sensor (1 to 10mSec.)
ConfigPin _Pin_Data_RHT03 = Output
_Pin_Data_RHT03 = False
WaitMs 5
'MCU waits for the sensor to finish processing the read request (20 to 40uSec.).
ConfigPin _Pin_Data_RHT03 = Input 'Port as read.
Call _Clear_Timer1() 'Reset counter of timer1 and tmr1if.
While _Pin_Data_RHT03 = True And _Tmr1If = False 'Waiting answer.
Wend
If _Tmr1If = True Then Exit 'No response, communication error.
'MCU esperando que el sensor confirmte la petición de inicio de lectura (80uSeg.)
Call _Clear_Timer1() 'Reset counter of timer1 and tmr1if.
While _Pin_Data_RHT03 = False And _Tmr1If = False 'Waiting answer.
Wend
If _Tmr1If = True Then Exit 'No response, communication error.
'MCU waiting for sensor to acknowledge read start request (80uSeg.)
Call _Clear_Timer1() 'Reset counter of timer1 and tmr1if.
While _Pin_Data_RHT03 = True And _Tmr1If = False 'Waiting answer.
Wend
If _Tmr1If = True Then Exit 'No response, communication error.
'MCU Read datas en Bits (RH, TMP y Checksum), 26 a 28uSeg. = 0, 70uSeg. = 1
_n = 0
While _n < 40
'Waiting for synchronization state to end to read the bit.
Call _Clear_Timer1() 'Reset counter of timer1 and tmr1if.
While _Pin_Data_RHT03 = False And _Tmr1If = False 'Waiting for synchronization state to end to raad the bit.
Wend
If _Tmr1If = True Then Exit 'No response, communication error.
'Measuring the duration of the significant pulse of the bit
Call _Clear_Timer1() 'Reset counter of timer1 and tmr1if.
While _Pin_Data_RHT03 = True And _Tmr1If = False 'Measuring pulse duration.
Wend
If _Tmr1If = True Then Exit 'No response, communication error.
_v_aux(_n) = _Get_Timer1() 'Assign timer counter.
_n++ 'Increments the index of the vector.
Wend
'Converts the captured times to bits.
_n = 0
While _n < 40
Select Case _n
Case <= 15 'Relative humidity.
If _v_aux(_n) <= _CLK Then
_HR03 = _ClearBit32(_HR03, 15 - _n)
Else
If _v_aux(_n) >= _CLK Then _HR03 = _SetBit32(_HR03, 15 - _n)
Endif
Case <= 31 'Temperatura ºC
If _v_aux(_n) <= _CLK Then
_TMP03 = _ClearBit32(_TMP03, 31 - _n)
Else
If _v_aux(_n) >= _CLK Then _TMP03 = _SetBit32(_TMP03, 31 - _n)
Endif
Case Else 'Received checksum
If _v_aux(_n) <= _CLK Then
_CHK = _ClearBit32(_CHK, 39 - _n)
Else
If _v_aux(_n) >= _CLK Then _CHK = _SetBit32(_CHK, 39 - _n)
Endif
EndSelect
_n++
Wend
'Checks the received checksum with the computed checksum.
_chk_aux = _HR03.HB + _HR03.LB + _TMP03.HB + _TMP03.LB 'Calculated Checksum.
If _chk_aux <> _CHK Then 'If they are distinct.
_HR = 0
_TMP = 0
_CHK = 1
Else
_CHK = 0 'Correct reading.
_HR = _HR03 / 10 'Assigns decimal part.
'Assign sign.
If _TMP03.15 = 1 Then
_TMP03.15 = 0
_Signo = 1
Endif
_TMP = _TMP03 / 10 'Assigns decimal part.
If _Signo = 1 Then _TMP = _TMP * -1 'Assigns sign.
If _SYS = "F" Or _SYS = "f" Then _TMP = _TMP * 1.8 + 32 'ºF
Endif
End Proc
'**************************************************************************
'Returns value: Long
'Changes to 1 the value of a specified bit of a register up to Long.
'_reg: Is the unput variable or regiser, by value.
'_bit: Bit to modify, 0 to 31.
'_SetBit32: Image of the variable with the modified bit.
Function _SetBit32(_reg As Long, _bit As Byte) As Long
If _bit > 31 Then _bit = 31
_SetBit32 = 1
_SetBit32 = ShiftLeft(_SetBit32, _bit)
_SetBit32 = _reg Or _SetBit32
End Function
'Returns value: Long
'Changes to 0 the value of a specified bit of a register up to Long.
'_reg: Is then unput variable or regiser, by value.
'_bit: Bit to modify, 0 to 31.
'_ClearBit32: Image of the variable with the modified bit.
Function _ClearBit32(_reg As Long, _bit As Byte) As Long
If _bit > 31 Then _bit = 31
_ClearBit32 = 1
_ClearBit32 = ShiftLeft(_ClearBit32, _bit)
_ClearBit32 = Not _ClearBit32
_ClearBit32 = _reg And _ClearBit32
End Function
'Returns counter of Tmr1
Function _Get_Timer1() As Word
_Get_Timer1.HB = TMR1H 'Load the high byte.
_Get_Timer1.LB = TMR1L 'Load the low byte.
End Function
'Clear Tmr1 register and Tmr1If flag.
Proc _Clear_Timer1()
TMR1H = 0 'clear high byte
TMR1L = 0 'Clear low byte
PIR1.TMR1IF = 0 'clear overflow Flag.
End Proc
Attachments
Last edited: