Hot!Can't get to sleep

Page: 12 > Showing page 1 of 2
Author
swmcl
Super Member
  • Total Posts : 218
  • Reward points : 0
  • Joined: 2014/05/10 13:54:42
  • Location: Queensland
  • Status: offline
2018/03/05 22:27:47 (permalink)
0

Can't get to sleep

Hi,
 
I am trying to get to sleep.  Actually, I'm trying to go into a coma.
My application runs on battery.  When the battery is discharged down to a certain safe level, I want to turn everything off and go into a sleep from which there is no recovery.  The only way to recover is to change out the battery.
I'm using a PIC18F47K40 on MPLAB X IDE v4.10 MCC v.3.45.1 and XC8 1.45.
 
I'm still seeing an oscillation on the scope so I'm thinking I haven't done things right.
There is no WDT (not initialised - never used).
 
Here is my little coma routine so far ...

void Comatose (void)
{
FVRMD = 0;
__delay_us(1);
FVREN = 0; // Disable Fixed Voltage Reference
__delay_us(1);
DACMD = 0;
__delay_us(1);
DAC1EN = 0; // Disable Digital to Analogue Converter
__delay_us(1);
ADCMD = 0;
__delay_us(1);
ADON = 0; // Disable the Analogue to Digital Converter
__delay_us(1);
IOCMD = 0;
CRCMD = 0;
UART1MD = 0;
UART2MD = 0;
MSSP1MD = 0;
MSSP2MD = 0;

...

INTERRUPT_GlobalInterruptHighDisable();
__delay_us(1);
INTERRUPT_GlobalInterruptLowDisable();
__delay_us(1);
INTERRUPT_PeripheralInterruptDisable();
__delay_us(1);
SLEEP();

}

 
Any help appreciated.
Cheers.
#1

24 Replies Related Threads

    zdenekjs
    Super Member
    • Total Posts : 194
    • Reward points : 0
    • Joined: 2013/02/13 23:04:26
    • Location: 0
    • Status: offline
    Re: Can't get to sleep 2018/03/05 23:19:47 (permalink)
    0
    What are the config bits set for, particularly the WDT bits?
     
    Otherwise, what are the TO/PD bits when it wakes up?
    #2
    Dimitris
    Super Member
    • Total Posts : 299
    • Reward points : 0
    • Joined: 2017/11/03 10:29:26
    • Location: 0
    • Status: offline
    Re: Can't get to sleep 2018/03/06 00:08:56 (permalink)
    0
    Why dont you lead you project inside a 'case' which will not run anything
     
    #3
    1and0
    Access is Denied
    • Total Posts : 8525
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Can't get to sleep 2018/03/06 00:11:43 (permalink)
    +1 (1)
    In addition to the Watchdog Timer, have you disabled all the interrupt enable (IE) bits?  If you want to keep it in Sleep mode, try doing this:
    do {
        SLEEP();
    } while(1);

    #4
    swmcl
    Super Member
    • Total Posts : 218
    • Reward points : 0
    • Joined: 2014/05/10 13:54:42
    • Location: Queensland
    • Status: offline
    Re: Can't get to sleep 2018/03/06 13:09:13 (permalink)
    0
    The setup as configured by MCC is as follows but the WDT is OFF. I haven't initialised any interrupts - although I have them configured. The coma routine also specifically turns off the interrupts I thought.
    I am simply running the coma routine and then I flash an LED. I am flashing an LED so that means I'm passing straight through the coma routine.
    I'll try the infinite loop approach to the SLEEP() function next.

    #pragma config FEXTOSC = XT // External Oscillator mode Selection bits->XT (crystal oscillator) above 100 kHz, below 8 MHz; PFM set to medium power
    #pragma config RSTOSC = EXTOSC_4PLL // Power-up default value for COSC bits->EXTOSC with 4x PLL, with EXTOSC operating per FEXTOSC bits
    // CONFIG1H
    #pragma config CLKOUTEN = OFF // Clock Out Enable bit->CLKOUT function is disabled
    #pragma config CSWEN = ON // Clock Switch Enable bit->Writing to NOSC and NDIV is allowed
    #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit->Fail-Safe Clock Monitor enabled
    // CONFIG2L
    #pragma config MCLRE = EXTMCLR // Master Clear Enable bit->If LVP = 0, MCLR pin is MCLR; If LVP = 1, RE3 pin function is MCLR
    #pragma config PWRTE = OFF // Power-up Timer Enable bit->Power up timer disabled
    #pragma config LPBOREN = OFF // Low-power BOR enable bit->ULPBOR disabled
    #pragma config BOREN = SBORDIS // Brown-out Reset Enable bits->Brown-out Reset enabled , SBOREN bit is ignored
    // CONFIG2H
    #pragma config BORV = VBOR_2P45 // Brown Out Reset Voltage selection bits->Brown-out Reset Voltage (VBOR) set to 2.45V
    #pragma config ZCD = OFF // ZCD Disable bit->ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
    #pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit->PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle
    #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit->Stack full/underflow will cause Reset
    #pragma config DEBUG = OFF // Debugger Enable bit->Background debugger disabled
    #pragma config XINST = OFF // Extended Instruction Set Enable bit->Extended Instruction Set and Indexed Addressing Mode disabled
    // CONFIG3L
    #pragma config WDTCPS = WDTCPS_31 // WDT Period Select bits->Divider ratio 1:65536; software control of WDTPS
    #pragma config WDTE = OFF // WDT operating mode->WDT Disabled
    // CONFIG3H
    #pragma config WDTCWS = WDTCWS_7 // WDT Window Select bits->window always open (100%); software control; keyed access not required
    #pragma config WDTCCS = SC // WDT input clock selector->Software Control
    // CONFIG4L
    #pragma config WRT0 = OFF // Write Protection Block 0->Block 0 (000800-003FFFh) not write-protected
    #pragma config WRT1 = OFF // Write Protection Block 1->Block 1 (004000-007FFFh) not write-protected
    #pragma config WRT2 = OFF // Write Protection Block 2->Block 2 (008000-00BFFFh) not write-protected
    #pragma config WRT3 = OFF // Write Protection Block 3->Block 3 (00C000-00FFFFh) not write-protected
    #pragma config WRT4 = OFF // Write Protection Block 3->Block 4 (010000-013FFFh) not write-protected
    #pragma config WRT5 = OFF // Write Protection Block 3->Block 5 (014000-017FFFh) not write-protected
    #pragma config WRT6 = OFF // Write Protection Block 3->Block 6 (018000-01BFFFh) not write-protected
    #pragma config WRT7 = OFF // Write Protection Block 3->Block 7 (01C000-01FFFFh) not write-protected
    // CONFIG4H
    #pragma config WRTC = OFF // Configuration Register Write Protection bit->Configuration registers (300000-30000Bh) not write-protected
    #pragma config WRTB = OFF // Boot Block Write Protection bit->Boot Block (000000-0007FFh) not write-protected
    #pragma config WRTD = OFF // Data EEPROM Write Protection bit->Data EEPROM not write-protected
    #pragma config SCANE = ON // Scanner Enable bit->Scanner module is available for use, SCANMD bit can control the module
    #pragma config LVP = OFF // Low Voltage Programming Enable bit->HV on MCLR/VPP must be used for programming
    // CONFIG5L
    #pragma config CP = OFF // UserNVM Program Memory Code Protection bit->UserNVM code protection disabled
    #pragma config CPD = OFF // DataNVM Memory Code Protection bit->DataNVM code protection disabled
    // CONFIG6L
    #pragma config EBTR0 = OFF // Table Read Protection Block 0->Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
    #pragma config EBTR1 = OFF // Table Read Protection Block 1->Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
    #pragma config EBTR2 = OFF // Table Read Protection Block 2->Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
    #pragma config EBTR3 = OFF // Table Read Protection Block 3->Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
    #pragma config EBTR4 = OFF // Table Read Protection Block 4->Block 4 (010000-013FFFh) not protected from table reads executed in other blocks
    #pragma config EBTR5 = OFF // Table Read Protection Block 5->Block 5 (014000-017FFFh) not protected from table reads executed in other blocks
    #pragma config EBTR6 = OFF // Table Read Protection Block 6->Block 6 (018000-01BFFFh) not protected from table reads executed in other blocks
    #pragma config EBTR7 = OFF // Table Read Protection Block 7->Block 7 (01C000-01FFFFh) not protected from table reads executed in other blocks
    // CONFIG6H
    #pragma config EBTRB = OFF // Boot Block Table Read Protection bit->Boot Block (000000-0007FFh) not protected from table reads executed in other blocks

    #5
    jtemples
    Super Member
    • Total Posts : 11018
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Can't get to sleep 2018/03/06 13:56:46 (permalink)
    +1 (1)
    Clearing PEIE does not disable all wake-up interrupt sources.  You need to explicitly clear all of the *IE flags.
    #6
    swmcl
    Super Member
    • Total Posts : 218
    • Reward points : 0
    • Joined: 2014/05/10 13:54:42
    • Location: Queensland
    • Status: offline
    Re: Can't get to sleep 2018/03/06 19:24:35 (permalink)
    0
    @jtemples
    Examples are:
    TMR0IE
    IOCIE
    INT1IE
    etc. etc. ??
    I'll give it s go. I'm assuming just before the SLEEP() command.
    #7
    jtemples
    Super Member
    • Total Posts : 11018
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Can't get to sleep 2018/03/06 19:25:54 (permalink)
    0
    Right.
    #8
    swmcl
    Super Member
    • Total Posts : 218
    • Reward points : 0
    • Joined: 2014/05/10 13:54:42
    • Location: Queensland
    • Status: offline
    Re: Can't get to sleep 2018/03/06 21:26:29 (permalink)
    0
    Thanks.
    I've had some success. It is now turning off the external crystal oscillator and the current consumption has gone from around 2.5mA to around 36 to 40uA. I was hoping for a bit better than this but I'm at the limits of my ability to measure the current anyway. However, it wasn't just the interrupts somewhere it was also because Dimwit had turned ON the various modules rather than turning them OFF.
    So I turned off every module and disabled every interrupt en mass !!
    The important part of the code now reads:

    DOZEN = 1;
    IDLEN = 0;
    PMD0 = 0xFF;
    asm ("nop"); asm ("nop"); asm ("nop");
    PMD1 = 0xFF;
    asm ("nop"); asm ("nop"); asm ("nop");
    PMD2 = 0xFF;
    asm ("nop"); asm ("nop"); asm ("nop");
    PMD3 = 0xFF;
    asm ("nop"); asm ("nop"); asm ("nop");
    PMD4 = 0xFF;
    asm ("nop"); asm ("nop"); asm ("nop");
    PMD5 = 0xFF;
    asm ("nop"); asm ("nop"); asm ("nop");
    PIE1 = 0;
    asm ("nop"); asm ("nop"); asm ("nop");
    PIE2 = 0;
    asm ("nop"); asm ("nop"); asm ("nop");
    PIE3 = 0;
    asm ("nop"); asm ("nop"); asm ("nop");
    PIE4 = 0;
    asm ("nop"); asm ("nop"); asm ("nop");
    PIE5 = 0;
    asm ("nop"); asm ("nop"); asm ("nop");
    PIE6 = 0;
    asm ("nop"); asm ("nop"); asm ("nop");
    PIE7 = 0;
    asm ("nop"); asm ("nop"); asm ("nop");
    do
    {
    SLEEP();
    } while(1);

    And here endeth the functionality of the PIC - as I wanted.
    One last question ...
    on pp.60 of the datasheet section 6.2 it says to not leave the IO pins 'floating'. Floating means physically disconnected yes ?
    What I've done is set each pin to be an input or output as appropriate and then set its level (high or low for outputs) and then made it tri-state. Going tri-state on a board that has all pins connected is not 'floating' right ?
    #9
    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: Can't get to sleep 2018/03/06 22:01:52 (permalink)
    0
    You probably don't need all those NOPs.
    There's a macro for them anyway, instead of inline assembler.
     
    swmcl
    ...What I've done is set each pin to be an input or output as appropriate and then set its level (high or low for outputs) and then made it tri-state. Going tri-state on a board that has all pins connected is not 'floating' right ?

    Wrong.
    You make a pin "tri-state" by setting the bit in the TRISx register, which makes it an input.
    If nothing external is driving the pin, and no internal pullups are enabled, then the pin is indeed "floating".
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #10
    swmcl
    Super Member
    • Total Posts : 218
    • Reward points : 0
    • Joined: 2014/05/10 13:54:42
    • Location: Queensland
    • Status: offline
    Re: Can't get to sleep 2018/03/07 00:09:12 (permalink)
    0
    The NOPs are there because I've had some difficulties with timings after writing to registers.  I agree they are probably not necessary but at this stage it lessens my variables I think.
     
    Correct me if I'm wrong ...
    So if I tri-state a pin, it becomes 'floating'.  If the pin is either an input or output then it isn't floating.  So in trying to reduce power consumption as much as possible, convert as many pins as possible to inputs ?  Otherwise set them as outputs and drive them to their least-current scenario ...
     
    #11
    rpg7
    Super Member
    • Total Posts : 1365
    • Reward points : 0
    • Joined: 2003/11/07 12:47:35
    • Status: offline
    Re: Can't get to sleep 2018/03/07 00:41:55 (permalink)
    0
    swmcl
    Correct me if I'm wrong ...
    So if I tri-state a pin, it becomes 'floating'.  If the pin is either an input or output then it isn't floating.

     
    tri-state means that it can be high, low or high-impedance with no drive. This last mode is normally used for 'input'.
    I suggest: All pins that are inputs (i.e. have a connection that feeds in a signal) be left as inputs, all pins that are outputs are set high or low depending on which mode produce the lowest consumption. All pins with nothing connected should be set as outputs with low level being output. The last sentence should be the case when the project is running normally too.
     
    edit: closing bracket
    #12
    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: Can't get to sleep 2018/03/07 02:51:16 (permalink)
    0
    swmcl
    ...
    Correct me if I'm wrong ...
    So if I tri-state a pin, it becomes 'floating'.  If the pin is either an input or output then it isn't floating.

    I don't understand why (or how) you are treating "input" and "tri state" as different modes.
    How are you enabling "tri state" ?
     

     So in trying to reduce power consumption as much as possible, convert as many pins as possible to inputs ?

    No, unless you have some way to pull them high or low.
     

    Otherwise set them as outputs and drive them to their least-current scenario ...

    Yes.

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #13
    NKurzman
    A Guy on the Net
    • Total Posts : 16647
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Can't get to sleep 2018/03/07 06:38:30 (permalink)
    0
    A tri-stated pin is an input. The decision of a floating pin is an unconnected input.
    So if TRIS=1 it needs to be connected to something, pulled up or pulled down.
    #14
    coffee critic
    Super Member
    • Total Posts : 279
    • Reward points : 0
    • Status: offline
    Re: Can't get to sleep 2018/03/07 09:59:55 (permalink)
    0
    Add a NOP or 2 before the sleep command to insure all the hardware has had a chance to settle.

    n_*$
    #15
    swmcl
    Super Member
    • Total Posts : 218
    • Reward points : 0
    • Joined: 2014/05/10 13:54:42
    • Location: Queensland
    • Status: offline
    Re: Can't get to sleep 2018/03/07 13:21:35 (permalink)
    0
    Thanks for the help,
     
    My confusion is that I think of the tri-state being separated from internal circuitry as well as being separated from external circuitry.  To me, an input circuit has loading.  Anyways ... all good.
     
    In the end it became a lot simpler.
     
    1. Set all pins (except Vpp and OSC pretty much) to be either input or output and drive their output to the lowest current consuming level.
    2. Disable interrupts with:

     
    INTERRUPT_GlobalInterruptHighDisable();
    INTERRUPT_GlobalInterruptLowDisable();
    INTERRUPT_PeripheralInterruptDisable();
     

    3.  Important!  Disable Interrupt on change with:

     
    PIE0bits.IOCIE = 0;
     

    4.  Prepare for sleep with:

     
    DOZEN = 1;
    IDLEN = 0;
     

    5.  Turn off all modules with:

     
    PMD0 = 0xFF;
    PMD1 = 0xFF;
    PMD2 = 0xFF;
    PMD3 = 0xFF;
    PMD4 = 0xFF;
    PMD5 = 0xFF;
     

    6.  Go to sleep with:

     
    do
    {
        SLEEP();
    } while(1);
     

     
    I didn't need to disable all peripheral interrupts with:

    PIE1 = 0;
    PIE2 = 0;
    PIE3 = 0;
    PIE4 = 0;
    PIE5 = 0;
    PIE6 = 0;
    PIE7 = 0;

     
    My current consumption has dropped considerably by disabling modules and of course the oscillator is disabled.  The order I've done things in is probably not the best and there are perhaps some more refinements but I've come some way and things are working predictably.
     
    So I have seen the sleep in action and I'm a tiny bit better at it.
     
     
    #16
    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: Can't get to sleep 2018/03/07 13:37:13 (permalink)
    0
    swmcl
    ...
    My confusion is that I think of the tri-state being separated from internal circuitry as well as being separated from external circuitry.  To me, an input circuit has loading.  Anyways ... all good.

    Yes, there's some very confused thinking going on there.
    Maybe just STOP using the term "tri-state".
    The PIC has three possible states for a pin.
    Output driven low
    Output driven high
    Input.
    (I'm ignoring any built in weak pullup capability. That usually uses more power, so you don't want it in an ultra-low power configuration.)
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #17
    PStechPaul
    Super Member
    • Total Posts : 2093
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: online
    Re: Can't get to sleep 2018/03/07 17:06:29 (permalink)
    0
    AFAIK any GPIO pin can be read, no matter if it is selected as tristate (1) or output (0). If it is set as output (TRISx=0), the PORTx can still be read. That may be useful to see if the output is actually at the logic level that was applied by writing to PORTx or LATx. It may also be set as analog, in which case you can read the actual logic level of the output.
     
    The PIC is designed using CMOS technology, so under static conditions the outputs should not draw any appreciable current, except possibly if left floating. Sleep mode uses about 50 nA at 1.8V.

     
    #18
    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: Can't get to sleep 2018/03/07 17:19:45 (permalink)
    0
    PStechPaul
    ...
    It may also be set as analog, in which case you can read the actual logic level of the output.

    It's actually the other way around.
    On analog capable pins, the analog input is ALWAYS available.
    Putting the pin into analog mode just disables the digital input buffer (which is why it always reads as 0).
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #19
    swmcl
    Super Member
    • Total Posts : 218
    • Reward points : 0
    • Joined: 2014/05/10 13:54:42
    • Location: Queensland
    • Status: offline
    Re: Can't get to sleep 2018/03/08 16:28:14 (permalink)
    0
    Unfortunately, I am not 'there yet'.
     
    My main board has two daughterboards that are serial port devices for communicating to other devices. These two daughterboards are different in function. One is made by me. The other is a Chinese-made board that I cannot make.
     
    The Chinese board has caused me no end of difficulties. It doesn't live up to its hype and I haven't been able to find a replacement. Part of its difficulties is that it cannot accept 5V on all its inputs as it says it can. I've needed to make a separate 3V3 psu to accomodate it. To save power, I also completely turn off all power to this board. This means the pull-up resistors on the serial lines do not have any power in the off state. They slowly discharge to a point.
     
    When I have my daugtherboard and the mainboard connected, my mainboard goes to sleep. When I connect the Chinese board it continually wakes up. I've tried numerous combinations of settings on the pins connecting to the PIC. There are 5 lines: a TX an RX and three control lines that have pull-ups on the daugtherboard. These control lines are the lines that need to be operated at 3V3.
     
    My last known adventure was to disconnect (or try to) all the PPS connected devices inside the PIC because I think the EUSART RX line in particular might be the source of my continual re-booting.
     
    I just cannot seem to completely wipe out all possible ways that something could re-awaken my PIC from sleep !!
     
    I want a completely dead device - no questions asked. The device is obviously in a noisy environment.
     
    By the way, I have a reduced current consumption but not anything like 50nA !!
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2018 APG vNext Commercial Version 4.5