16 bit conversion - AM2302


Closed Thread
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 16 bit conversion - AM2302

    tabsoft,

    Using your code and the line
    Code:
    LCDOut $FE,$80+9,"RH ",dec hum
    The LCD is displaying 6473, 6730, 6729, 6473, 6729 so I would assume that there is the odd misread now and again.

    What divider should I use of the variable hum to get a realistic figure, and is there any tweaks to the values in the for/next loop to clearly define the 1's and 0's to make the reading of the sensor more stable. I'm using an 18F4580 running at 40 Mhz (10mhz crystal with the HS_ppl option enabled)

    EDIT:

    For info using
    Code:
    LCDOut $FE,$80+9,"RH ",#hum
    the values jump around 18714, 18970,18970,18714,
    Last edited by Scampy; - 10th April 2015 at 17:12.

  2. #2
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 16 bit conversion - AM2302

    I'm assuming that for 100% humidity the value of hum would be 65535 and 0% would be 0 ?

    If so then using DEC hum/256 gives a reading of 73, 74 on the LCD - which seems a tad to high for the living room. The cheap simple dial hydrometer in the reptile enclosures read 37% and the sensor built into the weather station which is 18" away from the development board displaying 42%
    Last edited by Scampy; - 10th April 2015 at 17:25.

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 16 bit conversion - AM2302

    OK, just to check things I got my Arduino out and downloaded a sketch and library file. here's the results

    Name:  humidity.png
Views: 2156
Size:  81.4 KB

    which is close enough to the readings of both hydrometers in the reptile tanks.

    Now I don't understand the C style of these arduino sketches, but the link to the website is http://www.electroschematics.com/112...orial-library/ - maybe it might help in working out why this is such a pain to do in PBP

  4. #4
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: 16 bit conversion - AM2302

    Give this a try.

    Code:
    hum var word
    j var byte
    x var byte
    
    
    read_dht:
    
    TRISA.1 = 0 ' Make pin an input
    high dht_data   '250ms High
    pause 250
    
    low dht_data    '20ms Low
    pause 20' send 20ms low
    
    high dht_data   '40us High
    pauseus 40
    
    PulsIn PORTA.1, 1, dht
    if dht < 9 then goto badresponse 
    for x = 31 to 0 step-1
        PulsIn PORTA.1, 1, dht[x] ' 1
    next x
    
    hum = 0 'Clear hum variable
    'Stuff values into hum word variable based on pulsewidth value in dht byte arrary
    j = 15
    for x = 31 to 16 step - 1
       for x = 31 to 16 step - 1
        if dht(x)>=35 then hum.0[j] = 1
        j = j - 1    
    next x
    return
    and then to display try this.

    Code:
    LCDOut $FE,$80+9,"RH ",dec hum/10, "." dec hum //10
    Regards,
    TABSoft

  5. #5
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 16 bit conversion - AM2302

    Excellent !!

    I took a reading with the arduino code and it was giving me a value of 39.7 - 40.2 over a few minutes. Tried your suggestions and the LCD displayed 39.7 - 40.1 - so it's bang on and confirmed with the hydrometers I have.

    Ok now as I want to learn, can you explain what is happening in the code so I may learn from these examples.

  6. #6
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 16 bit conversion - AM2302

    Left it too long to edit the above post.. but here's my understanding - Please correct me if I'm wrong

    Code:
    read_dht:
    
    TRISA.1 = 0                         'make pin input
    high dht_data : pause 250           'take pin high for 250ms
    
    low dht_data : pause 20             'take pin low for 20ms 
    high dht_data : pauseus 40          'send 40us pulse
    
    PulsIn PORTA.1, 1, dht              'measure incomming high pulse and store in variable dht
    if dht < 9 then goto badresponse    'if pulse width is less than 9 got bad responce. 10mhz xtal should give 4us resolution 
    for x = 31 to 0 step -1             'loop to read pulse widths from bits 31 - 0
    PulsIn PORTA.1, 1, dht[x] ' 1       'measure incoming pulse width and add it to the array variable
    next x                              'go round the loop until 32 bits have been read
    
    hum=0                               'clear variable
    j = 15
    for x = 31 to 16 step - 1           'loop round the last 16 bits
    if dht(x)>=35 then hum.0[j] = 1     'if any value of dht(31 to 16) is equal or over 35 then ?????
    j = j-1                             'J is reduced by 1
    next x                              'go round until x = 16
    
    return
    
    badresponse:
     LCDOut $FE,$80+9,"RH N/C"
    return
    I'm stuck on the IF THEN section of the last for next loop

  7. #7
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: 16 bit conversion - AM2302

    I'm glad it worked for you.

    There were 3 areas I looked at to try to make it work.
    1. The sensor will send a "0" as a High pulse between 26us to 28us long, and a "1" as a High for 70us long.

    The original code was designed for a 20MHz PIC, so the PulsIn would return the pulsewidth in 2us increments.
    Your PIC 18F4580 was running at 40MHz so we needed to adjust the dht value test to accommodate the fact that PulsIn command was now returning the pulsewidth in 1us increments.
    The PulsIn command uses the PICs frequency to set the resolution level recorded into the variable.
    The way this works is with this formula: Resolution (us) = (1/(Fosc/4)) * 10
    For a 40Mhz OSC this would be: Res = (1/(40000000/4)) * 10 = (1/10000000) * 10 = (.0000001) * 10 = .000001 or 1us.
    So this means that the value in the PulsIn variable will be a count of 1us increments.
    The original code was testing for a zero by checking if the dht value was =>9 and <=21.
    With a 20MHz osc, this would would mean the "9" would be "9*2us = 18us" and the "21" would be "21*2us=42us", which would be close. (=>18 and <=42 us)
    With your 40MHz osc, it would be testing for 9*1us and 21*1us, which would not be correct (=>9 and <=21 us).

    2. With item 1 above and the fact that I "Zeroed" the "hum" variable before loading the bits into it from "dht", I simply tested for a "1" value from the sensor.
    "if dht(x)>=35 then hum.0[j] = 1"
    I chose "35us" since it is a value greater than a "0" value from the sensor (26-28us) and it is 1/2 of a "1" value from the sensor (70us).

    3. The sensor's initialization sequence that is defined in the datasheet you attached plus the DHT C library pushed me to make changes to the initialization sequence in the PBP program.


    I would also change the following line of code from "if dht < 9 then goto badresponse" to "if dht < 20 then goto badresponse"., given the 40MHz Osc.

    I commented the code below to help describe what is happening.

    Code:
    hum var word    'Holds final 16bit Humidity value (integral and decimal)
    j var byte
    x var byte
    
    
    read_dht:
    
    TRISA.1 = 0 ' Make pin an input
    
    '**Intialization sequence to sensor**
    high dht_data   '250ms High on data pin to sensor
    pause 250
    
    low dht_data    '20ms Low on data pin to sensor
    pause 20        ' send 20ms low
    
    high dht_data   '40us High
    pauseus 40
    
    'Read the sensor's acknowledgment of init sequence
    PulsIn PORTA.1, 1, dht  'Read a High pulse from the sensor data line
    if dht < 20 then goto badresponse   'Not a good signal, maybe noise? Change this to < 20   
    
    '**End of Initialization sequence"
    
    '**Load the data from the sensor into the dht byte array
    for x = 31 to 0 step-1  
        PulsIn PORTA.1, 1, dht[x]  'Read 32 High pulses (bits) from the sensor and store them as pulsewidth counts in individual bytes in the dht byte array.
    next x
    '**End of Load Data**
    
    hum = 0 'Clear hum word variable which holds the final 16bit humidity value
    
    '**Convert the dht byte array to a 16bit word variable
    'Stuff values into hum word variable based on the pulsewidth value in dht byte array
    'Use "hum.0(j)" to select a specific bit position in the hum word variable.
    '0.[j] is treated as an bit-offset into the hum variable. E.g. 0.[0] = hum.0, 0.[1] = hum.0+1, 0.[2] = hum.0+2, etc
    
    j = 15  'Used to select the correct bit in the hum word variable
    for x = 31 to 16 step - 1   'Read the 16 bytes for humidity in the dht byte array (humidity pulsewidth values)
        if dht(x)>=35 then hum.0[j] = 1 'If the pulsewidth is => 35us then the value is a "1".
        'otherwise, the value is a "0", so we do not need to do anything, all bits in "hum" were initialized to "0" with "hum = 0"
        j = j - 1    
    next x
    
    '**End Conversion**
    
    '**Display the result**
    'Send the ascii code that is the decimal representation of the humidity, "dec" modifier.
    'The 16bit hum variable includes both the integral and 1 digit decimal for the humidity (e.g. hum = 311 = 31.1 %RH).
    'So we need to display the value correctly by dividing the value by 10 (integral) and then displaying the decimal portion using the modulo "//" function.
    
    LCDOut $FE,$80+9,"RH ",dec hum/10, "." dec hum //10
    Hope this helps.
    Last edited by Tabsoft; - 11th April 2015 at 01:24.
    Regards,
    TABSoft

Similar Threads

  1. DHT22, AM2302 and pic18F2320
    By bitmaniac in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 15th November 2015, 00:42
  2. Replies: 10
    Last Post: - 8th March 2015, 19:26
  3. Replies: 17
    Last Post: - 22nd May 2014, 19:58
  4. Bit conversion
    By lerameur in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 5th November 2006, 11:11
  5. Conversion of 10-bit word into ASCII
    By Barry Johnson in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 16th January 2005, 14:26

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts