Menu

Another watchdog/sleep query

Help
Q Thurtle
2008-07-15
2013-05-30
  • Q Thurtle

    Q Thurtle - 2008-07-15

    In order to save battery, I want to sleep my 16F819 and wake it up using the watchdog timer. I've played with the "5 minute sleep" sample code elsewhere on this forum but it has not worked for me.

    Virtually everything I do causes an endless sleep. The watchdog timer never wakes it up - it just freezes (with all the correct outputs active) requiring a power down.

    I'm stripped down to the following code now! If I put a 1 sec (1ms for me) wait insead of the five 'sleep' lines then all is well (and I see the watchdog timer resetting the 16F819 every 2.3 seconds - the led blinks momentarily - and if I insert a clrwdt command at the start of the loop, the blinking stops because the watchdog never times out).

    Any thougts on how to make the watchdog timer wake the long sleep?!

    Any help would be much appreciated. Thanks Q

    'I know the clock speed is 31.25 kHz
    'I specify a speed of 31.25MHz and reduce my timings (ie 1ms = 1sec)
    #chip 16f819,31.25
    #config WDT_ON, INTRC_IO
    #define Led PORTB.4
    dir PORTB.4 out 

    Main:
    'clrwdt
    SET LED ON 
    Wait 1 ms  ' one second for me
    SET LED OFF
    'wait 1ms

    GIE = 0
    OPTION_REG = b'00001111'
    clrwdt
    asm sleep
    nop

    goto Main

     
    • kent_twt4

      kent_twt4 - 2008-07-16

      There's a bug in GCBasic.  Even in the older version of system.h, the 31.25khz rc clock was never handled.  More importantly, below unity decimal representation of chipMhz (.5, .25, .125, .03125?) is not working correctly.  So the quick answer is to use an intrc osc of 1 mhz and above.

      You could put the option_reg statement before Main:, and the GIE is cleared automatically.

       
    • Q Thurtle

      Q Thurtle - 2008-07-16

      Thanks for that Kent. Actually, I'm cool with the chip MHz stuff - I soon realised that there was a problem and my method works fine for me - I hardly notice now!

      Its the Sleep problem that's got me baffled!

       
    • kent_twt4

      kent_twt4 - 2008-07-16

      Well, using anything other than standard internal rc values will mess up any wait/pause values that are generated by GCBasic.

      As far as the longer sleep values go, you will still need to use a counter inside a loop to determine that.  So from the previous example:

      'This is a program to put a device to sleep and re-awake
      'using the WDT.  The loop instructions and calibration
      'not taken into effect (i.e. very rough timing results).

      #chip 16f819,1
      #config WDT_ON, INTRC_IO
      #define Led PortA.1
      dir LED out
      #define interval 13  'interval is total wait in seconds divided by 2.3
      OPTION_REG = b'00001111'  'prescaler assigned to WDT with 1 to 128 prescaler

      Main:
      SET LED ON
      Wait 1 sec
      SET LED OFF
      SleepTime(interval)
      goto Main

      sub SleepTime(interval)
        count = 0
        backtosleep:
        clrwdt 'clear the wdt before sleep
        asm sleep
        count = count + 1
        If count < interval then goto backtosleep
      end sub

       
    • Q Thurtle

      Q Thurtle - 2008-07-16

      Thanks for the code, but it fails in just the way I found before. I get a single 1 sec flash on the LED and then nothing happens however long I wait. If I reduce the interval down to 2 (which should give a very brief delay) the same thing happens. My poor PIC never wakes up!

      I have NO other connections to the PIC, just the power lines to pins 5 and 14 and the LED on port A1. No pullups, no MCLR, etc (I've tried lots of circuit arrangements, all just hang!)

      Wherever I look on the net, everyone indicates that watchdog/sleep is easy - its got me stumped!

      Thanks for your help. Any other ideas? Best wishes Q

       
    • kent_twt4

      kent_twt4 - 2008-07-16

      I think you said you're using a PicKit 2, so be sure the Vdd box is checked if you are using it for power, and that 5.0v is showing in the window.  Other than that, if you happen to have another Pic, try that next.

      Have you installed the latest zip update file?  What assembler are you using, GCASM or MPASM?  My example works fine with MPASM.  If using GCASM, then try reinstalling GCBasic and check the MPASM option.  Might be a bad chip file or some such in GCASM.  Still no joy? then post your compiled.asm file for comparison.

       
    • Q Thurtle

      Q Thurtle - 2008-07-16

      Very many thanks for your help. Now I have code which works for you, I can fiddle around until I get it to go. I have installed the latest update zip. I'm using a JDM programmer and GCASM (I think). I will go for the MPASM compiler and see if it does the trick.

      Thanks - I'll let you know how I get on!

      Best wishes
      Q

       
    • Q Thurtle

      Q Thurtle - 2008-07-16

      I had high hopes! Swapped to MPASM! The hex file produced is identical to the one produced by GCASM and does the same - flashes for the first sec then hangs. Can I possibly have a chip which works in the other ways but not for sleep? Surely not??? Unfortunately, I havn't got another PIC to test - will get some asap!

      The compiled.asm file is displayed below. This is for your program on the earlier post.

      Can I just check again that no connections etc are needed? I have a three component circuit in a prototyping board. The PIC, the resistor and LED. All pins except 5 and 14 (power supply) and pin 18 (RA1) are unconnected.

      Thanks again for your help - much appreciated.

      Best wishes Q

      ;Program compiled by Great Cow BASIC (0.9 30/6/2008)
      ;Need help? See the GCBASIC forums at http://sourceforge.net/forum/?group_id=169286,
      ;check the documentation or email w_cholmondeley@users.sourceforge.net.

      ;********************************************************************************

      ;Set up the assembler options (Chip type, clock source, other bits and pieces)
      LIST p=16F819, r=DEC
      #include <P16F819.inc>
      __CONFIG _WDT_ON & _INTRC_IO & _LVP_OFF & _MCLR_OFF

      ;********************************************************************************

      ;Set aside memory locations for variables
      COUNT    equ    32
      DELAYTEMP    equ    33
      DELAYTEMP2    equ    34
      INTERVAL    equ    35
      SysWaitTempMS    equ    36
      SysWaitTempMS_H    equ    37
      SysWaitTempS    equ    38

      ;********************************************************************************

      ;Jump to initialisation code when PIC is reset
          ORG    0
          call    INITSYS
          goto    SystemInitialise

      ;********************************************************************************

      ;Interrupt vector
          ORG    4
      ;Various initialisation routines, automatically called by GCBASIC
      SystemInitialise

      ;********************************************************************************

      ;Start of the main program
          banksel    TRISA
          bcf    TRISA,1
          movlw    15
          movwf    OPTION_REG
      MAIN
          banksel    PORTA
          bsf    PORTA,1
          movlw    1
          movwf    SysWaitTempS
          call    Delay_S
          bcf    PORTA,1
          movlw    13
          movwf    INTERVAL
          call    SLEEPTIME
          goto    MAIN
      BASPROGRAMEND
          sleep
          goto    $

      ;********************************************************************************
      ;Subroutines included in program
      ;********************************************************************************

      Delay_MS
          incf    SysWaitTempMS_H, F
      DMS_START
          movlw    10
          movwf    DELAYTEMP2
      DMS_OUTER
          movlw    8
          movwf    DELAYTEMP
      DMS_INNER
          decfsz    DELAYTEMP, F
          goto    DMS_INNER
          decfsz    DELAYTEMP2, F
          goto    DMS_OUTER
          decfsz    SysWaitTempMS, F
          goto    DMS_START
          decfsz    SysWaitTempMS_H, F
          goto    DMS_START
          return

      ;********************************************************************************

      Delay_S
      DS_START
          movlw    232
          movwf    SysWaitTempMS
          movlw    3
          movwf    SysWaitTempMS_H
          call    Delay_MS
          decfsz    SysWaitTempS, F
          goto    DS_START
          return

      ;********************************************************************************

      INITSYS
          movlw    143
          banksel    OSCCON
          andwf    OSCCON,F
          movlw    64
          iorwf    OSCCON,F
          banksel    ADCON0
          bcf    ADCON0,ADON
          banksel    ADCON1
          bcf    ADCON1,ADFM
          bcf    ADCON1,PCFG3
          bsf    ADCON1,PCFG2
          bsf    ADCON1,PCFG1
          bcf    ADCON1,PCFG0
          banksel    PORTA
          clrf    PORTA
          clrf    PORTB
          return

      ;********************************************************************************

      SLEEPTIME
          clrf    COUNT
      BACKTOSLEEP
          clrwdt
          sleep
          incf    COUNT,F
          movlw    13
          subwf    COUNT,W
          btfss    STATUS, C
          goto    BACKTOSLEEP
          return

      ;********************************************************************************

      END

       
    • kent_twt4

      kent_twt4 - 2008-07-16

      Yea, the assembler matches mine.  Try putting a 10k pullup resistor on MCLR, that is the only hardware difference on my proto board.  After that...?

       
    • Q Thurtle

      Q Thurtle - 2008-07-17

      Well, I'm stumped. I've changed to a different prototyping board, gone from a power supply to batteries, tried various reprogrammings - all with no effect. I guess I have damaged a PART of the PIC leaving some of it functioning correctly?

      I have never come across this before - I've trashed plenty of CMOS and 74 ICs but never had them part work!

      I'll get hold of a few more PICs and check them out. I'll report back on my hoped-for success (or failure), but it will take a few days.

      Thanks again for your help. I have spent hours on this problem and my recent foray into PICs would probably have collapsed without some reassurance that what I was trying to do was not stupid. I appreciate the time you have spent on this.

      Best wishes Q

       
    • Q Thurtle

      Q Thurtle - 2008-07-19

      A new PIC arrived, and it sleeps and wakes up perfectly! I DID have a part-working PIC! Remarkable. I wonder what I did to damage part of it (or could it possibly be a manufacturing fault in the first PIC)?

      Thanks again. Q

       
    • kent_twt4

      kent_twt4 - 2008-07-19

      Glad you got it working.  I have a real knack for shorting the power supply.  Lost communication with a 16f877, when playing with a high side current monitor, in addition to excessive heat from the chip.  Will use a smaller/cheaper part when trying that again.  Have an lcd controller chip that's missing a few pixels, from .......you guessed it.

      No clue as to the failure rate of a PIC is. Its possible that you got a bad one, have yet to find one myself, but then that's an extremely small sample.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.