• AVR Freaks

Hot!Assembly Code to Read Humidity Sensor

Page: 12345.. > >> Showing page 1 of 7
Author
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
2019/03/10 18:45:45 (permalink)
0

Assembly Code to Read Humidity Sensor

It's been a while since I haven't used the PIC Microcontrollers. I would like to read the data sent by a Humidity Sensor (DHT22: https://www.sparkfun.com/...Temperature/DHT22.pdf) and then display it in an LCD. 
(See photo 1)
Apparently what I have to read is this: (See photo 2)
 
I have problems doing the digital analysis of U1(DATA) (it doesn't show anything), so I think I am doing something wrong in the Assembly code
(See photo 3) 
My code is the next:
LIST P=16f877A 
     INCLUDE "P16F877A.INC"
     __CONFIG _FOSC_XT & _WDTE_OFF & _PWRTE_OFF & _BOREN_ON & _LVP_ON & _CPD_OFF & _WRT_OFF & _CP_OFF
 
ORG 0x00
     RESET
     GOTO Initiation
     
ORG 0x10

Initiation BCF STATUS,RP1 ; Selection of memory bank 1
     BSF STATUS,RP0
     CLRF TRISB ; Configuration of PORT B as output (Data pins LCD)
     CLRF TRISD ; Configuration of PORT D as output (RD0-R/S, RD1-E)
     
     MOVLW b'11111111'
     MOVWF TRISC ; Configuration of PORT C as input (DHT22 Data)

     MOVLW b'00000111'
     MOVWF OPTION_REG ; Configuration of Option Register (TMR0 Rate = 1:256)

     BCF STATUS,RP0 ; Selection of memory bank 0
     CLRF PORTB ; Setting PORTB to "0000000"
     CLRF PORTD ; Setting PORTD to "0000000"
     CLRF PORTC ; Setting PORTC to "0000000"
     
     CALL InitiateLCD
     
MainLoop CALL PRINTCHAR
     
     GOTO MainLoop

;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;///// LCD RUTINA ////////////////////////////////////////////////////////////////////////////////////////////////
;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      
InitiateLCD BCF PORTD,RD0 ; Setting RS as 0 (Sends commands to LCD)
     
     CALL DELAY_5ms

     MOVLW b'00000001' ; Clearing display
     MOVWF PORTB
     CALL Enable
     CALL DELAY_5ms

     MOVLW b'00111000' ; Funtion set
     MOVWF PORTB
     CALL Enable
     CALL DELAY_5ms

     MOVLW b'00001111' ; Display on off
     MOVWF PORTB
     CALL Enable
     CALL DELAY_5ms

     MOVLW b'00000110' ; Entry mod set
     MOVWF PORTB
     CALL Enable
     CALL DELAY_5ms
     
     RETURN

PRINTCHAR BCF PORTD,RD0 ; Setting RS as 0 (Sends commands to LCD)

     MOVLW b'00000010' ; Set cursor home
     MOVWF PORTB
     CALL Enable
     CALL DELAY_5ms
     
     BSF PORTD,RD0 ; Setting RS as 1 (Sends information to LCD)
     
     CALL DELAY_5ms

     MOVLW d'72' ; Print character "H"
     MOVWF PORTB
     CALL Enable
     CALL DELAY_5ms

     RETURN

Enable BSF PORTD,RD1 ; E pin is high, (LCD is processing the incoming data)
     NOP
     BCF PORTD,RD1 ; E pin is low, (LCD does not care what is happening)
     RETURN

DELAY_5ms MOVLW .5 ; Delay of 5 ms
     MOVWF TMR0

LOOP BTFSS INTCON,2
     GOTO LOOP
     BCF INTCON,2
     RETURN

;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
END

 
I think I still don't understand how to configure PORTx ... I understand that if I set a bit (=1) in the TRISx register it means that the corresponding PORTx is going to be an input, if I clear a bit (=0) is going to be an output. But how do I have to set or clear the bits in the PORT register?  Thanks
 
 

Attached Image(s)

#1

120 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/10 19:19:11 (permalink)
    0
    delfindelfin
    I think I still don't understand how to configure PORTx ... I understand that if I set a bit (=1) in the TRISx register it means that the corresponding PORTx is going to be an input, if I clear a bit (=0) is going to be an output.

    That is correct
     

    But how do I have to set or clear the bits in the PORT register?

    0 (zero) if you want the pin to be low, or 1 if you want it to be high after your initialisation code has run.
     
    There's one big problem with your circuit and code now.
    You are using the RB3/PGM pin to control the LCD, but this CONFIG setting " _LVP_ON " says that pin should act as "PGM" not as "RB3".
    Change " _LVP_ON " to " _LVP_OFF " before you try anything else.

    Nearly there...
    #2
    delfindelfin
    Super Member
    • Total Posts : 387
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/10 20:00:28 (permalink)
    0
    What does it mean for the pin to be low or high?
    #3
    pcbbc
    Super Member
    • Total Posts : 1381
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/10 20:10:39 (permalink)
    0
    low = 0 volts (same potential as Vss pin)
    high = + volts (same potential as Vdd pin)
    #4
    delfindelfin
    Super Member
    • Total Posts : 387
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/10 20:42:01 (permalink)
    0
    That would make sense when it is configurated as output. But in my case that is input, I have to put it as low? What happens if I put it as high? Is it still going to read?
    #5
    pcbbc
    Super Member
    • Total Posts : 1381
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/10 21:11:14 (permalink)
    0
    Setting or clearing the bits in the PORT register makes zero sense (and does nothing) if the pin is configured as an input via TRIS=1. So it does not matter if you write 0 or 1 to the bits defined as input, the value you write will be ignored (edit: unless you subsequently set TRIS bit = 0, at which point the 0/1 value you previously wrote to PORT will take immediate effect at the output pin).
    You need to read (or test) the bits in the PORT register in that case. The value of the bit will indicate the logic level at the input:
    1=high
    0=low
    post edited by pcbbc - 2019/03/10 21:16:44
    #6
    delfindelfin
    Super Member
    • Total Posts : 387
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/11 00:56:51 (permalink)
    0
    I have already changed _LVP_ON for _LVP_OFF (Although I don't know why the LCD was working fine). 
    So, the part related with TRIS and PORT was right. I set TRISC as input (Since I want to read in RC5 the value of U1(DATA) sended by the sensor). I set PORTC whatever I want since it doesn't matter. But in the Digital Analyzer of Proteus it doesn't simulate that trace
    #7
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/11 02:05:06 (permalink)
    0
    delfindelfin
    I have already changed _LVP_ON for _LVP_OFF (Although I don't know why the LCD was working fine).

    Probably because you are just running in Proteus.
    No simulator is ever a perfect emulation of the hardware.
     

    So, the part related with TRIS and PORT was right. I set TRISC as input (Since I want to read in RC5 the value of U1(DATA) sended by the sensor). I set PORTC whatever I want since it doesn't matter. But in the Digital Analyzer of Proteus it doesn't simulate that trace

    That is because you have not read the operation instructions correctly.
    The very first step says the the microprocessor has to drive the data pin low for at least one millisecond, and then high. The DHT22 will not send anything until you do this.
    That means you must temporarily change the RC5 pin into an output, drive it low for one ms, drive it high, then immediately change it back into an input. THEN you will get data from the sensor.
     
     

    Nearly there...
    #8
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3333
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/11 06:04:31 (permalink)
    0
    You will need to understand TRISx when using this single line serial device.
    TRISx is used to change the communication direction.
    1. Set TRISx low
    2. send pulse to LATx to activate device
    3. Set TRISx high
    4. receive string of 40bits from PORTx
    5. checksum ok or did not timeout?
    post edited by Gort2015 - 2019/03/11 06:08:37

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #9
    mpgmike
    Super Member
    • Total Posts : 330
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/12 21:03:50 (permalink)
    0
    I used the DH11 referenced.  I am including the PBP3 Basic code that worked.  I am trying to help, you'll have to follow the bouncing ball & translate it into XC8 or ASM if needed.  Please don't bash me because it's in Basic.  Here it is:
     

    Clear
    define OSC 16
    ;Configurations:
    #CONFIG
    __config _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _MCLRE_OFF & _CLKOUTEN_OFF & _FCMEN_OFF & _IESO_OFF & _BOREN_ON & _PWRTE_ON
    __config _CONFIG2, _PLLEN_OFF & _LVP_OFF & _STVREN_ON & _BORV_LO
    #ENDCONFIG
    ;Aliases:
    DH11 var LATA.4 ;Serial In/Out From/To DH11 TH_Sensor, Pin 8
    ;CLKOUT VAR PORTA.4 ;Pin 3, CLKOUTEN, Fosc/4
    rLED VAR LATC.2 ;Pin 8
    gLED VAR LATC.3 ;Pin 7
    ;Special Function Registers:
    OSCCON = %01111010 ;INTOSC 16 MHz
    WDTCON = %00010011 ;512 ms, .0 = 1/0
    TRISA = %00011010 ;Initialize DH11 as Input
    TRISC = %11110011
    ANSELA = 0
    ANSELC = 0
    ; -=- Timer 1 Related SFRs, Capture Input from DH11 -=-
    T1CON = %00010000 ;.0 = 1/0
    T1GCON = %11010000 ;.3 = GO/DONE, .2 = T1GVAL
    TMR1H = 0
    TMR1L = 0
    PIE1.0 = 0 ;TMR1IE
    PIE1.7 = 0 ;TMR1GIE
    ;Variables:
    b0 var byte ;Used in For/Next Loops
    b1 var byte ;Bit Counter in HD11 For/Next Loops
    b2 var byte ;Byte Counter in HD11 For/Next Loops
    CKc var byte ;Calculated Check Sum from DH11
    CKa var byte ;Actual Check Sum from DH11
    Dbit var byte
    Dbit0 var Dbit.0 ;Deciphers DH11 Data from bits to bytes
    Dbit1 var Dbit.1 ;Deciphers DH11 Data from bits to bytes
    Dbit2 var Dbit.2 ;Deciphers DH11 Data from bits to bytes
    Dbit3 var Dbit.3 ;Deciphers DH11 Data from bits to bytes
    Dbit4 var Dbit.4 ;Deciphers DH11 Data from bits to bytes
    Dbit5 var Dbit.5 ;Deciphers DH11 Data from bits to bytes
    Dbit6 var Dbit.6 ;Deciphers DH11 Data from bits to bytes
    Dbit7 var Dbit.7 ;Deciphers DH11 Data from bits to bytes
    DH1 var byte ;Timing mechanism to determine if bit = 1/0
    Hum var byte ;From DH11, humidity whole value
    Hum1 var byte ;From DH11, 2nd byte of data, 0.XXX
    Rot VAR BYTE ;Rotates Duties
    Temp var byte ;Input from DH11, 1st byte
    Temp1 var byte ;Input from DH11, 2nd byte
    TempF var byte ;Converted Temp From DH11, C >> F
    Work VAR WORD BANK0 SYSTEM ;Source of Working Bits
    THvalue VAR Work.4 ;To decipher input from DH11, one bit at a time
    Match VAR Work.6 ;CKa = CKc
    Main:
    DO
    LOOP UNTIL OSCSTAT.4 = 1 ;HFINTOSC Ready
    Work = 0
    rLED = 1 ;Indicate Start Condition
    PAUSE 150
    rLED = 0 ;Indicate End of Start Condition
    PAUSE 1000 ;DH11 needs at Least 1 second to Initialize
    INTCON = %11000000 ;Enable PIE & GIE
    DO
    GOSUB Get_Temp ;From DH11
    PAUSE 3000
    LOOP
    ; --- *** DH11 Subroutines: *** ---
    Get_Temp:
    TRISA.4 = 0 ;DH11 = Output
    DH11 = 0 ;LATA.4 = 0
    pause 19 ;DH11 Requires Minimum 18 mS
    DH11 = 1 ;LATA.4 = 1
    TRISA.4 = 1 ;DH11 = Input
    do
    loop UNTIL PORTA.4 = 0 ;Wait for DH11 Initialize Low
    do
    loop while PORTA.4 = 0 ;Wait for DH11 Initialize High
    DO
    loop while PORTA.4 = 1 ;Wait for DH11 to Begin Message
    for b1 = 1 to 5
    for b0 = 7 to 0 STEP -1
    PIR1.7 = 0 ;Clear TGIF
    TMR1H = 0
    TMR1L = 0
    T1CON = 1 ;T1ON
    T1GCON.3 = 1 ;GO/DONE
    DO
    LOOP while T1GCON.3 = 1 ;T1GIE
    PIR1.7 = 0
    DH1 = TMR1L ;
    T1CON = 0
    gosub Get_THvalue
    select case b0
    case 0 : Dbit0 = THvalue
    case 1 : Dbit1 = THvalue
    case 2 : Dbit2 = THvalue
    case 3 : Dbit3 = THvalue
    case 4 : Dbit4 = THvalue
    case 5 : Dbit5 = THvalue
    case 6 : Dbit6 = THvalue
    case 7 : Dbit7 = THvalue
    end select
    next b0
    if b1 = 1 then
    Hum.0 = Dbit0
    Hum.1 = Dbit1
    Hum.2 = Dbit2
    Hum.3 = Dbit3
    Hum.4 = Dbit4
    Hum.5 = Dbit5
    Hum.6 = Dbit6
    Hum.7 = Dbit7
    endif
    if b1 = 2 then
    Hum1.0 = Dbit0
    Hum1.1 = Dbit1
    Hum1.2 = Dbit2
    Hum1.3 = Dbit3
    Hum1.4 = Dbit4
    Hum1.5 = Dbit5
    Hum1.6 = Dbit6
    Hum1.7 = Dbit7
    endif
    if b1 = 3 then
    Temp.0 = Dbit0
    Temp.1 = Dbit1
    Temp.2 = Dbit2
    Temp.3 = Dbit3
    Temp.4 = Dbit4
    Temp.5 = Dbit5
    Temp.6 = Dbit6
    Temp.7 = Dbit7
    endif
    if b1 = 4 then
    Temp1.0 = Dbit0
    Temp1.1 = Dbit1
    Temp1.2 = Dbit2
    Temp1.3 = Dbit3
    Temp1.4 = Dbit4
    Temp1.5 = Dbit5
    Temp1.6 = Dbit6
    Temp1.7 = Dbit7
    endif
    if b1 = 5 then
    CKa.0 = Dbit0
    CKa.1 = Dbit1
    CKa.2 = Dbit2
    CKa.3 = Dbit3
    CKa.4 = Dbit4
    CKa.5 = Dbit5
    CKa.6 = Dbit6
    CKa.7 = Dbit7
    endif
    next b1
    CKc = Hum + Hum1 + Temp + Temp1
    IF CKc = CKa THEN
    Match = 1
    TempF = ((Temp * 18) / 10) + 32
    ELSE
    Match = 0
    ENDIF
    return
    Get_THvalue:
    if DH1 < 90 then
    THvalue = 1
    ELSE
    THvalue = 0
    ENDIF
    @ CLRWDT
    return

    post edited by mpgmike - 2019/03/13 07:39:13
    #10
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/12 21:13:34 (permalink)
    0
    The "code" tags must be in lower case to work.
     

    Nearly there...
    #11
    delfindelfin
    Super Member
    • Total Posts : 387
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/13 22:11:44 (permalink)
    0
    I did what qhb said and now it works. Now I am going to think how to read and convert that
     

    #12
    delfindelfin
    Super Member
    • Total Posts : 387
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/13 23:23:02 (permalink)
    0
    Altough now that I have noticed there is something strange in the image. The parts that are supposed to be high, are not completely. They are like undefined. I think that would be a problem if I want the PIC to read that
    #13
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/13 23:24:16 (permalink)
    +1 (1)
    It's a pretty simple protocol.
    The datasheet tells you the sensor sends a 50us low pulse, then a 26-28us high pulse for a "0", or a 70us pulse for a "1".
    So, you could simply wait for the rising edge, delay 50us, then check if the signal is now low or high.
    Rinse and repeat for all 40 bits.
     

    Nearly there...
    #14
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/13 23:26:38 (permalink)
    0
    delfindelfin
    Altough now that I have noticed there is something strange in the image. The parts that are supposed to be high, are not completely. They are like undefined. I think that would be a problem if I want the PIC to read that

    You might need a pullup resistor on the DATA signal. Try 1k.
     

    Nearly there...
    #15
    delfindelfin
    Super Member
    • Total Posts : 387
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/14 00:50:04 (permalink)
    0
    But if I place the pull-up resistor, are not going to be problems when I use RC5 as an output instead of input?
    post edited by delfindelfin - 2019/03/14 01:10:46
    #16
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/14 02:26:08 (permalink)
    0
    No. The PIC output can easily drive a 1k resistor low.
     

    Nearly there...
    #17
    delfindelfin
    Super Member
    • Total Posts : 387
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/14 02:55:10 (permalink)
    0
    No, it is wrong, I don't know why but when I place a 1k resistor it doesn't read the sensor, when I place a 4.7k it reads the sensor but only once. What do you mean exactly by drive? My native language is Spanish and I am sure about how should I translate that word
    #18
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/14 02:58:18 (permalink)
    0
    Are you still just using Proteus?
     
    "Drive" is what an output pin does. It drives the pin high or low.
     
    How exactly did you connect the 1k resistor? Can you show what your circuit looks like now?

    Nearly there...
    #19
    mpgmike
    Super Member
    • Total Posts : 330
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: offline
    Re: Assembly Code to Read Humidity Sensor 2019/03/14 08:48:02 (permalink)
    0
    I used a PIC16F1824 with no pull-up/down resistors.  One thing I did find is that the checksum often didn't match.  In the software, I only used values with matching checksums.  Usually they were only off a small amount, but not always.
    #20
    Page: 12345.. > >> Showing page 1 of 7
    Jump to:
    © 2019 APG vNext Commercial Version 4.5