16 bit conversion - AM2302


Closed Thread
Results 1 to 13 of 13

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: 16 bit conversion - AM2302

    You could possibly do something like this...

    Code:
    hum var word
    j var byte
    x var byte
    
    read_dht:
    
    TRISA.1 = 0 '
    high dht_data
    low dht_data : pause 18' send 18ms low
    high dht_data : pauseus 30 ' send 30us hi
    TRISA.1 = 0
    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
    'For x = 31 to 0 step-1 
    '    if dht(x)>=9 and dht(x)<=21 then dht(x)=0 'if pulsewidth between 20 and 40uS then read as '0'
    '    if dht(x)>=29 and dht(x)<=41 then dht(x)=1 'if pulsewidth between 60 and 80uS then read as '1'
    'next x
    
    'Stuff values into hum word variable based on pulsewidth value in dht byte arrary
    j = 15
    for x = 31 to 16 step - 1
        if dht(x)>=9 and dht(x)<=21 then hum.0[j] = 0
        if dht(x)>=29 and dht(x)<=41 then hum.0[j] = 1
        j = j-1    
    next x
    Regards,
    TABSoft

  2. #2
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 16 bit conversion - AM2302

    Thanks Tabsoft,

    I'll give that a try. I did stumble across a similar thread (http://www.picbasic.co.uk/forum/showthread.php?t=17867) and amended the code to

    Code:
    read_dht:
    
    TRISA.1 = 0 '
    high dht_data
    low dht_data : pause 18' send 18ms low
    high dht_data : pauseus 30 ' send 30us hi
    TRISA.1 = 0
    PulsIn PORTA.1, 1, dht
    if dht < 9 then goto badresponse 
    for x = 0 to 31 
    PulsIn PORTA.1, 1, dht(x) ' 1
    next x
    For x = 0 to 31  
    if dht(x)>=9 and dht(x)<=21 then dht(x)=0 'if pulsewidth between 20 and 40uS then read as '0'
    if dht(x)>=29 and dht(x)<=41 then dht(x)=1 'if pulsewidth between 60 and 80uS then read as '1'
    next x
    hum=32768*dht(1)+16384*dht(2)+8192*dht(3)+4096*dht(4)+2048*dht(5)+1024*dht(6)+512*dht(7)+256*dht(8)+128*dht(9)+64*dht(10)+32*dht(11)+16*dht(12)+8*dht(13)+4*dht(14)+2*dht(15)+1*dht(16)           
    
    return
    with an amended display line, but get random digits all over the range from 6453% to 17% !!

    I'll give your example a try a and will get back

  3. #3
    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.

  4. #4
    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.

  5. #5
    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: 2637
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

  6. #6
    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

  7. #7
    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.

  8. #8
    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

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