Menu

Problem with waking up a 16F15323 during SLEEP

Help
Frank
2017-12-01
2017-12-04
  • Frank

    Frank - 2017-12-01

    Hi to all,

    I'm trying to program a 16F15323 for a battery application.
    I operate the µC with the internal 32 kHz low power oscillator (LFINTOSC).
    Sleep mode works, wake up via a button also....
    However, I would have to wake up the controller at fixed intervals (doesn't have to be too exact) about 1x per minute (preferably WITHOUT resetting), the variables should be preserved.

    Timer1 only seems to run in wake-up mode (in this case, timer interrupts also work).

    Does anyone have sample code for waking up from sleep mode? Or how can I use the watchdog for this? Can someone help me?

    Thank you very much!

    Frank

     
  • kent_twt4

    kent_twt4 - 2017-12-01

    Here is an example of using the software WDT. Will need adapting for your chip.

    #chip 12f683,4
    #config    MCLRE=ON
    #define Led GPIO.5
    dir GPIO.5 out
    
    Start:
      SET LED ON
      wait 1 s
      SET LED OFF
      SleepTime
    goto Start
    
    sub SleepTime
      'approximate 1 minute interval
      interval = 100
      'clear the interrupt register so that the program may continue
      GIE = 0
      'enable SWDTEN with 1:128 PS
      WDTCON = b'00000101'
      count = 0
      backtosleep:
      clrwdt 'clear the wdt before sleep
      asm sleep
      nop
      count = count + 1
      If count < interval then goto backtosleep
    end sub
    
     
  • Frank

    Frank - 2017-12-04

    Hi kent_twt4,

    thanks for your fast response and for your help! ...it seems that this chip needs more presettings...

    First a link to the page where I found useful information about the watchdog:
    http://embedded-lab.com/blog/lab-17-sleep-and-wake-pic-microcontrollers/

    "When the SLEEP instruction is being executed, the next instruction (PC + 1) is pre-fetched, so that on wake-up the processor could execute the next instruction after the SLEEP command." - this means that the µC is not reset after waking up but continues working directly after the SLEEP command!!!

    My circuit is driven by a 9V battery over a MCP1702 LDO. The complete power consumption in sleep mode is about 3.9µA!!!

    I'm not sure if all the presettings are really necessary.....

    Here is my code as basis for a battery powered device - You can wake up the µC at any time by pressing a button against GND on PORTA. 5.
    Otherwise the µC wakes up every 64 seconds, does its job and sleeps again!
    (Maybe someone finds this program useful or helps someone else...)

    Frank

    '******************************************************************
    '*                                                                *
    '*       *** (Compiled with GCB 0.98.010) ***                     *
    '*                                                                *
    '******************************************************************
    '
    
    '*** Initialisation ***
    
    'Select chip model and speed
    #chip 16F15323
    #config mclr_off, FCMEN_OFF, WDT_ON,WDTCCS_LFINTOSC,WDTE_SWDTEN, LPBOREN_ON, BOREN_OFF, RSTOSC_LFINT, FEXTOSC_OFF
    'Low-Power Internal 32 kHz Oscillator
    
    #OPTION Explicit                'All variables must first be declared!!!
    
    DIM Zaehler          AS Byte    'Temporary counter
    
    #define SWITCH       PORTA.5  'Switch to GND with a 470K pullup
    #define SoundOut     PORTC.5  'Piezo Buzzer
    #define Valve_open   PORTC.3  'Impulsdriven valve on H-bridge
    #define Valve_close  PORTC.4  'Impulsdriven valve on H-bridge
    
    WDTCON0 = b'00100001'         'Set Watchdog to 64s nominal and turn WDT on
    CLRWDT                        'Clear Watchdog timer
    
    'Setup an Interrupt event when PORTA.5 goes negative.
    IOCAN5 = 1
    On Interrupt PORTChange  Call InterruptHandler
    
    '*** beep 5 times on startup and wait 5 seconds ***
    for zaehler = 1 to 5
        Tone 2000, 5
        Wait 500 ms
    next
    wait 5 s
    
    '*** MAINLOOP ***
    do
    
      '*** beep 3 times ***
      for zaehler = 1 to 3
          Tone 4000, 5
          Wait 10 ms
      next
    
      '*** Valve activation ***
      PulseOut Valve_open, 5 ms
      Wait 3 s
      PulseOut Valve_close, 5 ms
      Wait 3 s
    
      CCP1CON = CCP1CON AND b'01111111' 
      'CCPx Module Enable bit=0 => CCPx is disabled
    
      TRISC   = b'00000000'
      'Define all ports as output and set to GND (exclusively SWITCH)
    
      '*** Preparation for deep sleep ***
      LATC    = b'00000000'
      TRISA   = b'00100000'
      LATA    = b'00000000'
      VREGCON = VREGCON OR b'00000010'
      'VREGPM=1 =>  Low-Power Sleep mode enabled in Sleep
      'Draws lowest current in Sleep, slower wake-up
    
      sleep                   'Sleep for 64s or push button on switch
    
      Tone 2000, 5
      wait 200 ms
    
    loop
    
    '    '*** Sound with PWM-Modul (alternative) ***
    '    TRISC   = b'11011111' 'RC5 as Output
    '
    '    'Module: CCP1 (created with "PIC PPS Tool for GCB"!
    '    RC5PPS = 0x0009     'CCP1 > RC5
    '    CCP1PPS = 0x0015    'RC5 > CCP1 (bi-directional)
    '
    '    for zaehler = 1 to 10
    '        #define PWM_Freq 4     'Set frequency in KHz
    '        #define PWM_Duty 50    'Set duty cycle to 50 %
    '        PWMOn                  'Turn on the PWM
    '        WAIT 300 ms            'Wait
    '        PWMOff                 'Turn off the PWM
    '        Wait 100 ms
    '    next
    
    '*** ON INTERRUPT ***
    sub InterruptHandler
      if IOCAF5 = 1 then                       'SWITCH was just pressed
          IOCAN5 = 0
          'Prevent the event from reentering the InterruptHandler routine
          IOCAF5 = 0                             
          'We must clear the flag in software
    
          wait 5 ms                              
          'debounce by waiting and seeing if still held down
    
          IOCAN5 = 1                             
          'ReEnable the InterruptHandler routine
      end if
    end sub
    
    End
    
     
  • kent_twt4

    kent_twt4 - 2017-12-04

    That's a really good power draw for sleep, nice job, and thanks for sharing the code. I know that nano amp is possible with these devices under certain situations, but probably not with the wdt enabled.

     
  • Frank

    Frank - 2017-12-04

    You are right! But not with a MCP1702 LDO - this LDO needs about 2-3µA for itself and the WDT also has its price. The µC is operated in my circuit with 5V - with 3V3 it should have an even lower current consumption.
    ...but I am satisfied with 3.9µA complete power consumption. My battery should have 230mAh - that should last for 6,7 years standby time...

    Frank

     
  • kent_twt4

    kent_twt4 - 2017-12-04

    Now I see that you were including the regulator, my mistake. The standby power for those MCP LDO regulators are amazing, I have used both the 5V and 3.3V models, and the TO92 packages are handy.

     

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.