DHT22, AM2302 and pic18F2320


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: DHT22, AM2302 and pic18F2320

    Well this guy got it working in C after playing with delays so it's most likely a timing issue.

    18F2320 can run at 40MHz internally, I'd try that first. Others mention waiting at least 2 seconds between samples.

    http://forum.43oh.com/topic/2239-sol...or-on-energia/

    Robert

  2. #2
    Join Date
    Jan 2004
    Location
    Thessaloniki , GREECE
    Posts
    61


    Did you find this post helpful? Yes | No

    Thumbs up SOLVED! DHT22, AM2302 and pic18F2320

    SUCCESS!!!

    After trying to increase crystal to 25Mhz and also 40Mhz I had the same problem of grabing only the first 21 pulses.... (problems with PULSIN speed...hmm....)

    Finally the solution was just to use PULSIN 40 times! in my code and not use the FOR NEXT LOOP !

    So there was timming speed problems and had to improve my code for speed - a little...

    Now I am getting correct results

    Here is the final code (the only thing left is to check checksum for errors!) ENJOY.

    Code:
    '***************************************
    '*  Name    : 18f2320_DHT22_AM2302.BAS *
    '*  Author  : bitmaniac   Alexandros Z.*
    '*  Notice  : Copyright (c) 2013       *
    '*  Date    : 5/4/2013                 *
    '***************************************
    
    ;include "ALLDIGITAL.pbp"
    ;@ __CONFIG _CONFIG1H, _INTIO2_OSC_1H & _IESO_OFF_1H ' use internal osc
    @ __CONFIG _CONFIG1H, _HS_OSC_1H & _IESO_OFF_1H ' use internal osc
    @ __CONFIG _CONFIG2L, _PWRT_OFF_2L & _BOR_OFF_2L
    @ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
    @ __CONFIG _CONFIG3H, _MCLRE_OFF_3H & _PBAD_DIG_3H ;& _CCP2MX_OFF_3H
    @ __CONFIG _CONFIG4L, _LVP_OFF_4L
    
    DEFINE  LCD_DREG        PORTC	' LCD Data Port
    DEFINE  LCD_DBIT        4		' Starting Data Bit
    DEFINE  LCD_RSREG       PORTC	' Register Select Port
    DEFINE  LCD_RSBIT       2		' Register Select Bit
    DEFINE  LCD_RWREG       PORTC   ' LCD read/write port 
    DEFINE  LCD_RWBIT       1       ' LCD read/write bit 
    DEFINE  LCD_EREG        PORTC	' Enable Port
    DEFINE  LCD_EBIT        0		' Enable Bit
    DEFINE  LCD_BITS		4		' Data Bus Size
    DEFINE  LCD_LINES		2		' Number of Lines on LCD
    DEFINE  LCD_COMMANDUS 2000
    DEFINE  LCD_DATAUS 50
    pause 300
    
    define osc 4
    
    ADCON0=0 
    ADCON1=00001111                  'All digital
    CMCON=7                          'Comparators OFF
    INTCON2.7=0                      'RBPU =0 , TURN ON PORTB PULL-UPS
    
    TRISA=%00000000
    TRISB=%00001111
    
    PORTA=0
    PORTB=0
    
    humread     var word             '16-bit var to store humidity
    tmpread     var word             '16-bit var to store temperature
    checksum    var BYTE             '8-bit var to store checksum
    x           var byte             'general var
    pulse       var byte [81]        'store pulse var
    p           var bit [81]         'store pulse var
    
    led         var LATA.0
    dht_port    var PORTB.4
    
    clear
    high dht_port
    high led
    
    ;---------- main program -------------
    high led
    LCDout $FE,$01,"DHT-22"
    LCDout $FE,$C0,"testing..."
    pause 2000
    
    start:
    ;---- check_DHT22 ----
    ;-== send StartSignal ==-
    TRISB.4 = 0                      'Data port is output
    high dht_port
    pauseus 10                       'send 10uS high pulse
    low dht_port                     
    pause 1                          'send 1mS high pulse
    high dht_port
    ;-== wait response from Sensor ==-
    TRISB.4 = 1                      'Data port is input
    while dht_port=1:wend            'wait for low from DHT
    while dht_port=0:wend            'wait for high FROM DHT
    high led
    ;-== Grab 40bits data from DHT22 ==-
    pulsin PORTB.4,1,pulse(1)        'Humidity HighBit data
    pulsin PORTB.4,1,pulse(2)
    pulsin PORTB.4,1,pulse(3)
    pulsin PORTB.4,1,pulse(4)
    pulsin DHT_port,1,pulse(5)
    pulsin DHT_port,1,pulse(6)
    pulsin DHT_port,1,pulse(7)
    pulsin DHT_port,1,pulse(8)
    pulsin DHT_port,1,pulse(9)
    pulsin DHT_port,1,pulse(10)
    pulsin DHT_port,1,pulse(11)
    pulsin DHT_port,1,pulse(12)
    pulsin DHT_port,1,pulse(13)
    pulsin DHT_port,1,pulse(14)
    pulsin DHT_port,1,pulse(15)
    pulsin DHT_port,1,pulse(16)      'Humidity LowBit data
    pulsin DHT_port,1,pulse(17)      'Temperature HighBit data (1 means below 0 degree!)
    pulsin DHT_port,1,pulse(18)
    pulsin DHT_port,1,pulse(19)
    pulsin DHT_port,1,pulse(20)
    pulsin DHT_port,1,pulse(21)
    pulsin DHT_port,1,pulse(22)
    pulsin DHT_port,1,pulse(23)
    pulsin DHT_port,1,pulse(24)
    pulsin DHT_port,1,pulse(25)
    pulsin DHT_port,1,pulse(26)
    pulsin DHT_port,1,pulse(27)
    pulsin DHT_port,1,pulse(28)
    pulsin DHT_port,1,pulse(29)
    pulsin DHT_port,1,pulse(30)
    pulsin DHT_port,1,pulse(31)
    pulsin DHT_port,1,pulse(32)      'Temperature LowBit data
    ;pulsin DHT_port,1,pulse(33)     'Checksum HighBit
    ;pulsin DHT_port,1,pulse(34)
    ;pulsin DHT_port,1,pulse(35)
    ;pulsin DHT_port,1,pulse(36)
    ;pulsin DHT_port,1,pulse(37)
    ;pulsin DHT_port,1,pulse(38)
    ;pulsin DHT_port,1,pulse(39)
    ;pulsin DHT_port,1,pulse(40)     'Checksum LowBit
    ;-== convert to '0' & '1' ==-
    for x=1 to 40
    if pulse(x)>=2 and pulse(x)<=4 then p(x)=0 'if pulsewidth between 20 and 40uS then read as '0' 
    if pulse(x)>=6 and pulse(x)<=8 then p(x)=1 'if pulsewidth between 60 and 80uS then read as '1'
    next x
    ;-== convert bin to dec ==-
    humread=32768*p(1)+16384*p(2)+8192*p(3)+4096*p(4)+2048*p(5)+1024*p(6)+512*p(7)+256*p(8)+128*p(9)+64*p(10)+32*p(11)+16*p(12)+8*p(13)+4*p(14)+2*p(15)+1*p(16)
    tmpread=16384*p(18)+8192*p(19)+4096*p(20)+2048*p(21)+1024*p(22)+512*p(23)+256*p(24)+128*p(25)+64*p(26)+32*p(27)+16*p(28)+8*p(29)+4*p(30)+2*p(31)+1*p(32)
    ;checksum=128*p(33)+64*p(34)+32*p(35)+16*p(36)+8*p(37)+4*p(38)+2*p(39)+1*p(40)
    ;-== show real values ==-
    LCDout $FE,$01,"RHdata  Tdata"
    LCDout $FE,$C0,dec humread/10,".",dec humread//10,"%   ",dec tmpread/10,".",dec tmpread//10,"oC" 'print integral part only
    low led
    pause 2000                       'give some time to DHT to stabilize!....
    goto start
    END
    Name:  2013-04-05 12.16.45.jpg
Views: 8106
Size:  63.0 KB

    Finnally I would like to say A BIG THANK YOU to demon for sending me the correct infos to search about that helped me to pinpoint my problem!
    Last edited by bitmaniac; - 5th April 2013 at 11:29.

  3. #3
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: SOLVED! DHT22, AM2302 and pic18F2320

    Congrats! Did you comfirm the readings with another device? Looks a little hot and dry where you live.

    Robert

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Default Re: DHT22, AM2302 and pic18F2320

    Hi, Alexandros

    did you find this one?
    http://embedded-lab.com/blog/?p=4333

    interesting explanations there.

    BTW ... How are you ???

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  5. #5
    Join Date
    Jan 2004
    Location
    Thessaloniki , GREECE
    Posts
    61


    Did you find this post helpful? Yes | No

    Default Re: DHT22, AM2302 and pic18F2320

    @ Demon I have not yet tested with another device... The photo wa taken before sensor given time to settle (as soon as I switched on!)

    @Acetronics Hi! my good friend! There has been a looong time since my last visit here! (Actually for the last year I was working on my retro collection hobby with local retro-exchibitions etc!)
    Yes I have seen this site infos....
    Last edited by bitmaniac; - 5th April 2013 at 13:56.
    Robotics , aeromodelling r/c , picprogramming , Retro Computer Collector

  6. #6
    Join Date
    Jul 2007
    Posts
    7


    Did you find this post helpful? Yes | No

    Default Re: DHT22, AM2302 and pic18F2320

    Hi,

    I want to use those codes to read values each 15 minutes via serial port. I need two sensors to read. One on board , the second 10 meters of wire. To replace LCD codes to serial is very easy. I wonder the include "ALLDIGITAL.pbp" not shown here . Finally what's is oscillator frequency ?

    The last version of PBP codes, seems from 5'th April 2013

    Thanks

  7. #7
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: DHT22, AM2302 and pic18F2320

    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Similar Threads

  1. PIC18F2320 RA4 output problem
    By Kristjan in forum General
    Replies: 2
    Last Post: - 31st May 2007, 00:56

Members who have read this thread : 1

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