Serial Comms and Crystals


Closed Thread
Results 1 to 40 of 43

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Serial Comms and Crystals

    Richard,

    need some additional help. The following code compiled, but displayed RH 0.0 and tmr 0 on the LCD. I changed the values for badread and noise as suggested, but still have the same result. As mentioned I'm using a 10 mhz crystal with HS_ppl enabled to run the pic at 40 Mhz. Could you point me in the right direction as to what I'm doing wrong ?

    Code:
    ; config settings 18F4580, 10mhz crystal HSPPL
    
    ASM  
      __CONFIG    _CONFIG1H, _OSC_HSPLL_1H
      __CONFIG    _CONFIG2L, _PWRT_ON_2L  
      __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
      __CONFIG    _CONFIG3H, _PBADEN_OFF_3H
      __CONFIG    _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
    ENDASM
    
    DEFINE  OSC 40
    ADCON1 = $0F
    clear
    INCLUDE "alldigital.pbp"
    CMCON = 7 
    TRISA = %11001111
    
    DEFINE LCD_DREG  PORTB           ' LCD Data port
    DEFINE LCD_DBIT  0               ' starting Data bit (0 or 4)
    DEFINE LCD_EREG  PORTB           ' LCD Enable port
    DEFINE LCD_EBIT  5               '     Enable bit  (on EasyPIC 5 LCD)
    DEFINE LCD_RSREG PORTB           ' LCD Register Select port
    DEFINE LCD_RSBIT 4               '     Register Select bit   (on EasyPIC 5 LCD)
    DEFINE LCD_BITS  4               ' LCD bus size (4 or 8 bits)
    DEFINE LCD_LINES 4               ' number of lines on LCD
    DEFINE LCD_COMMANDUS 2000        ' Command delay time in us 
    DEFINE LCD_DATAUS 50             ' Data delay time in us 
    
    ASM       
    hum          = _dht+3    
    tempa         = _dht+1
    ENDASM 
    
    T1CON = %00000001                               ; free-running, 1:1 prescaler
    
    dht_data var PORTA.1
    dht_dir var TRISA.1
    dht var byte[5]
    
    tempa var word EXT
    bits var byte
    crc   var byte
    hum  var word EXT 
    
    main:
    gosub read_dht
    LCDOut $FE,$94,"rh ",#hum/10,".",dec1 hum//10
    pause 4000
    goto main
        
    read_dht:
         for bits=4 to 0 step-1 
           dht[bits]=0 
         next
        
         tmr2=0
         dht_dir=0
         dht_data=0
         pauseus 1000                   'start it up
         dht_data=1
         pauseus 30
         dht_dir=1
         pauseus 40                    'wait till middle of response pulse window
         if ( dht_data)then goto badread   'no response then  give up
         while (!dht_data)
         wend 
         t2con=6
         while ( dht_data) 
         wend 
         t2con=0
         if ((tmr2 <75)||(tmr2>85))then goto badread      ' confirm presence ?
         for  bits =39 to 0  step -1
             tmr2=0
             while (!dht_data) 
             wend 
             t2con=6
             while ( dht_data) 
             wend 
             t2con=0
             if ((tmr2>20)&&(tmr2<80)) then     ' noise ?
               if (tmr2 >50 ) then
                  dht.0[bits] =  1 
               endif   
               
             else
              goto badread          ' noise ?
             endif
         next 
         crc=0 
         for bits=1 to 4         
           crc=crc+ dht[bits]
           
         next
         if crc !=  dht[0] then goto badcrc    'crc
    
    return
      
    badread:
    LCDOut $FE,$94 +8,"tmr6 ",#tmr2
        for bits=4 to 0 step-1 
           dht[bits]=0 
        next
    return
    badcrc:
        for bits=4 to 0 step-1 
    LCDOut $FE,$94 +16,hex dht[bits],"," 
        next  
    return

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,666


    Did you find this post helpful? Yes | No

    Default Re: Serial Comms and Crystals

    you did not make the changes I suggested

    ; config settings 18F4580, 10mhz crystal HSPPL

    ASM
    __CONFIG _CONFIG1H, _OSC_HSPLL_1H
    __CONFIG _CONFIG2L, _PWRT_ON_2L
    __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
    __CONFIG _CONFIG3H, _PBADEN_OFF_3H
    __CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
    ENDASM

    DEFINE OSC 40
    ADCON1 = $0F
    clear
    INCLUDE "alldigital.pbp"
    CMCON = 7
    TRISA = %11001111

    DEFINE LCD_DREG PORTB ' LCD Data port
    DEFINE LCD_DBIT 0 ' starting Data bit (0 or 4)
    DEFINE LCD_EREG PORTB ' LCD Enable port
    DEFINE LCD_EBIT 5 ' Enable bit (on EasyPIC 5 LCD)
    DEFINE LCD_RSREG PORTB ' LCD Register Select port
    DEFINE LCD_RSBIT 4 ' Register Select bit (on EasyPIC 5 LCD)
    DEFINE LCD_BITS 4 ' LCD bus size (4 or 8 bits)
    DEFINE LCD_LINES 4 ' number of lines on LCD
    DEFINE LCD_COMMANDUS 2000 ' Command delay time in us
    DEFINE LCD_DATAUS 50 ' Data delay time in us

    ASM
    hum = _dht+3
    tempa = _dht+1
    ENDASM

    T1CON = %00000001 ; free-running, 1:1 prescaler

    dht_data var PORTA.1
    dht_dir var TRISA.1
    dht var byte[5]

    tempa var word EXT
    bits var byte
    crc var byte
    hum var word EXT

    main:
    gosub read_dht
    LCDOut $FE,$94,"rh ",#hum/10,".",dec1 hum//10
    pause 4000
    goto main

    read_dht:
    for bits=4 to 0 step-1
    dht[bits]=0
    next

    tmr2=0
    dht_dir=0
    dht_data=0
    pauseus 1000 'start it up
    dht_data=1
    pauseus 30
    dht_dir=1
    pauseus 40 'wait till middle of response pulse window
    if ( dht_data)then goto badread 'no response then give up
    while (!dht_data)
    wend
    t2con=6
    while ( dht_data)
    wend
    t2con=0
    if ((tmr2 <45)||(tmr2>55))then goto badread ' confirm presence ?
    for bits =39 to 0 step -1
    tmr2=0
    while (!dht_data)
    wend
    t2con=6
    while ( dht_data)
    wend
    t2con=0
    if ((tmr2>12)&&(tmr2<50)) then ' noise ?
    if (tmr2 >31 ) then

    dht.0[bits] = 1
    endif

    else
    goto badread ' noise ?
    endif
    next
    crc=0
    for bits=1 to 4
    crc=crc+ dht[bits]

    next
    if crc != dht[0] then goto badcrc 'crc

    return

    badread:
    LCDOut $FE,$94 +8,"tmr6 ",#tmr2
    for bits=4 to 0 step-1
    dht[bits]=0
    next
    return
    badcrc:
    for bits=4 to 0 step-1
    LCDOut $FE,$94 +16,hex dht[bits],","
    next
    return

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,666


    Did you find this post helpful? Yes | No

    Default Re: Serial Comms and Crystals

    if you don't have a oscilloscope or logic analyser add some more diagnostics to the program


    Code:
     ; config settings 18F4580, 10mhz crystal HSPPL
    
     ASM 
     __CONFIG _CONFIG1H, _OSC_HSPLL_1H
     __CONFIG _CONFIG2L, _PWRT_ON_2L 
     __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
     __CONFIG _CONFIG3H, _PBADEN_OFF_3H
     __CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
     ENDASM
    
     DEFINE OSC 40
     ADCON1 = $0F
     clear
     INCLUDE "alldigital.pbp"
     CMCON = 7 
     TRISA = %11001111
    
     DEFINE LCD_DREG PORTB ' LCD Data port
     DEFINE LCD_DBIT 0 ' starting Data bit (0 or 4)
     DEFINE LCD_EREG PORTB ' LCD Enable port
     DEFINE LCD_EBIT 5 ' Enable bit (on EasyPIC 5 LCD)
     DEFINE LCD_RSREG PORTB ' LCD Register Select port
     DEFINE LCD_RSBIT 4 ' Register Select bit (on EasyPIC 5 LCD)
     DEFINE LCD_BITS 4 ' LCD bus size (4 or 8 bits)
     DEFINE LCD_LINES 4 ' number of lines on LCD
     DEFINE LCD_COMMANDUS 2000 ' Command delay time in us 
     DEFINE LCD_DATAUS 50 ' Data delay time in us 
    
     ASM 
     hum = _dht+3 
     tempa = _dht+1
     ENDASM 
    
     T1CON = %00000001 ; free-running, 1:1 prescaler
    
     dht_data var PORTA.1
     dht_dir var TRISA.1
     dht var byte[5]
    
     tempa var word EXT
     bits var byte
     crc var byte
     hum var word EXT 
    
     main:
     gosub read_dht
     LCDOut $FE,$94,"rh ",#hum/10,".",dec1 hum//10
     pause 4000
     goto main
    
     read_dht:
     for bits=4 to 0 step-1 
     dht[bits]=0 
     next
    
     tmr2=0
     dht_dir=0
     dht_data=0
     pauseus 1000 'start it up
     dht_data=1
     pauseus 30
     dht_dir=1
     pauseus 40 'wait till middle of response pulse window
     if ( dht_data)then goto badresponce 'no response then give up
     while (!dht_data)
     wend 
     t2con=6
     while ( dht_data) 
     wend 
     t2con=0
     if ((tmr2 <45)||(tmr2>55))then goto badpresence ' confirm presence ?
     for bits =39 to 0 step -1
     tmr2=0
     while (!dht_data) 
     wend 
     t2con=6
     while ( dht_data) 
     wend 
     t2con=0
     if ((tmr2>12)&&(tmr2<50)) then ' noise ?
     if (tmr2 >31 ) then
     dht.0[bits] = 1 
     endif 
    
     else
     goto badread ' noise ?
     endif
     next 
     crc=0 
     for bits=1 to 4 
     crc=crc+ dht[bits]
    
     next
     if crc != dht[0] then goto badcrc 'crc
    
     return
    
     badread:
     LCDOut $FE,$94 +8,"tmr2 ",#tmr2
     
     next
     return
     badcrc:
     for bits=4 to 0 step-1 
     LCDOut $FE,$94 +16,hex dht[bits],"," 
    pause 5000
     next 
     return
    badresponse:
     LCDOut $FE,$94 +8,"nr "
     return
    
    badpresence:
     LCDOut $FE,$94 +8,"pr",#tmr2
     return

  4. #4
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Serial Comms and Crystals

    Quote Originally Posted by richard View Post
    you did not make the changes I suggested
    Quote Originally Posted by Scampy View Post
    I changed the values for badread and noise as suggested, but still have the same result.
    Sorry Richard, I must of confused you by posting the code with the values in the original code.

    I'll take a look at your suggested code tomorrow and let you know - cheers for your help

    EDIT - loaded the code as is and the LCD displays rh 0.0 nr
    I assume that it's not getting a response from the sensor ? I have checked the connections and they appear correct (+ve to 5v, GND to ground, and signal to PORT A pin 1) this is connected via a header to the EasyPIC5 board with the 10K pull up resistors enabled (also tried without) I have a Hantek PC scope which I can use to test. I'm guessing I should see pin A1 going from high to low for the initial handshake between PIC and sensor ??
    Last edited by Scampy; - 25th March 2015 at 00:39. Reason: update

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,666


    Did you find this post helpful? Yes | No

    Default Re: Serial Comms and Crystals

    expected dht response to start signal
    Attached Images Attached Images  

  6. #6
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Serial Comms and Crystals

    OK seems to of been a hardware issue. Replaced the sensor and now get a value on the lcd. The program is giving a reading of 870.4 - how would I turn this into a percentage

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


    Did you find this post helpful? Yes | No

    Default Re: Serial Comms and Crystals

    Just to get a sense of the raw data collected, perhaps you can change your "main" subroutine to the follow and post the results output on the LCD?

    Code:
    main:
     gosub read_dht
     'LCDOut $FE,$94,"rh ",#hum/10,".",dec1 hum//10
     lcdout $FE, $94, hex2 dht(0), " ", hex2 dht(1), " ",hex2 dht(2), " ", hex2 dht(3), " ", hex2 dht(4)
     pause 4000
     goto main
    Regards,
    TABSoft

  8. #8
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Serial Comms and Crystals

    LCDOut $FE,$94,"rh ",#hum/10,".",dec1 hum//10 this gives a value of 870.4

    lcdout $FE, $94, hex2 dht(0), " ", hex2 dht(1), " ",hex2 dht(2), " ", hex2 dht(3), " ", hex2 dht(4) produces 38 00 16 00 22

Similar Threads

  1. PC Serial comms
    By Bill Legge in forum Serial
    Replies: 7
    Last Post: - 13th December 2009, 23:37
  2. Simple Serial Comms.
    By koossa in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 23rd November 2007, 08:12
  3. Some comments on serial comms please.
    By muddy0409 in forum Schematics
    Replies: 1
    Last Post: - 15th June 2007, 09:53
  4. Serial Comms Buffer?
    By koossa in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 2nd December 2005, 01:29
  5. Serial comms / Bootloader
    By koossa in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 30th October 2005, 18:48

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