• AVR Freaks

Switching from INTOSC to TIMER1 oscillator for Sleep Mode

Author
Joel QO
New Member
  • Total Posts : 26
  • Reward points : 0
  • Joined: 2014/09/28 11:52:38
  • Location: 0
  • Status: offline
2018/11/06 06:52:28 (permalink)
0

Switching from INTOSC to TIMER1 oscillator for Sleep Mode

Hi, I'm using a PIC18F2550 that runs the main program with the internal oscillator at 4 MHz and it's using the Timer1 oscillator to wake up from Idle Mode.
 
However I want that the program enter in Sleep Mode and wake up with the Timer1. I've done this steps:
  • I've switched the IDLEN bit from 1 to 0.
  • I've changed from
    Sleep();
    to
    OSCCON = 0xE5;
    Sleep();
    OSCCON = 0xE6;
At this moment the program enters in Sleep Mode but never wakes up. 
 
Should I change something else?
I'm doing something wrong?
#1

15 Replies Related Threads

    mpgmike
    Super Member
    • Total Posts : 326
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: offline
    Re: Switching from INTOSC to TIMER1 oscillator for Sleep Mode 2018/11/06 08:15:57 (permalink)
    0
    Table 3.1 on page 35 of the data sheet explains a few different modes.  Depending on what other settings you have, SLEEP may ignore clocks altogether, where WAKE is triggered by an external event only.
    #2
    coffee critic
    Super Member
    • Total Posts : 378
    • Reward points : 0
    • Status: offline
    Re: Switching from INTOSC to TIMER1 oscillator for Sleep Mode 2018/11/06 08:46:34 (permalink)
    +1 (1)
    Add a couple NOP before sleep and one after sleep.

    n_*$
    #3
    Chris A
    Super Member
    • Total Posts : 841
    • Reward points : 0
    • Joined: 2010/07/20 04:37:07
    • Location: 0
    • Status: offline
    Re: Switching from INTOSC to TIMER1 oscillator for Sleep Mode 2018/11/06 09:31:52 (permalink)
    0
    I'm doing the same on at PIC18F2515. 
     
    I do have another instruction between OSCCON1 and Sleep and nop after.  I check the INT flags running at 32767Hz and might re-sleep with the for loop -  The point being that its faster than waiting for the INTOSC to startup as SOSC is already running all the time.
    GIE = 0;        // Disable ints and test flags in loop below
    OSCCON = 0x00;
    SWDTEN = 0;
     
    for(;;)
        {
        __sleep();
        __nop();
        // Check wake interrupt source flags
        if (TMR1IF)    {...}
        if (INT0IF)     {  break;  }
        }
     
    OSCCON = 0x60;                // Set 4MHz OSC
    __clear_watchdog_timer();
    WDTCON = 0xb;
    GIE = 1;

    Edit: Correct cut and paste of preprocessor conditional CPU type
    post edited by Chris A - 2018/11/06 10:07:05
    #4
    coffee critic
    Super Member
    • Total Posts : 378
    • Reward points : 0
    • Status: offline
    Re: Switching from INTOSC to TIMER1 oscillator for Sleep Mode 2018/11/06 09:41:12 (permalink)
    +1 (1)
    Chris A. 
    The reason for the nop before sleep is to allow any hardware changes to complete before the clock shuts off.  In your case I would look at the disassembly to see what instruction is being processed before the sleep instruction.  If it is anything to do with a SFR add a nop, if not take your chances.    

    n_*$
    #5
    Chris A
    Super Member
    • Total Posts : 841
    • Reward points : 0
    • Joined: 2010/07/20 04:37:07
    • Location: 0
    • Status: offline
    Re: Switching from INTOSC to TIMER1 oscillator for Sleep Mode 2018/11/06 09:50:15 (permalink)
    +1 (1)
    Good point - It is to allow the clock to change. I may need another pre-sleep nop, but I wrote this many many years ago and it works, however it is difficult to test if its actually doing what is expected as it could wake still on INTOSC and then switch to SOSC and appear to work!  Would have to time INT0 to IO toggle to be sure! I think at the time I carefully read the datasheet on doing this!
    post edited by Chris A - 2018/11/06 09:58:11
    #6
    coffee critic
    Super Member
    • Total Posts : 378
    • Reward points : 0
    • Status: offline
    Re: Switching from INTOSC to TIMER1 oscillator for Sleep Mode 2018/11/06 10:04:07 (permalink)
    0
    If it ain't broke...

    n_*$
    #7
    Joel QO
    New Member
    • Total Posts : 26
    • Reward points : 0
    • Joined: 2014/09/28 11:52:38
    • Location: 0
    • Status: offline
    Re: Switching from INTOSC to TIMER1 oscillator for Sleep Mode 2018/11/06 11:55:18 (permalink)
    0
    Hi, thaks for all your help. I've found my error: 
     
    I was enabling External Clock Input Synchronization bit in the T1CON
     T1CONbits.nT1SYNC = 0;
    I've just turned it to 1 and I've kept the rest of setup with out changes.
     
    coffee critic
    Add a couple NOP before sleep and one after sleep.

    I've just added it too.
    Chris A
    ... however it is difficult to test if its actually doing what is expected as it could wake still on INTOSC and then switch to SOSC and appear to work!...

    At this moment I have two ways to test it. I'm using a segmented LCD, if it changes its values slowly then the oscillator didn't back to INTOSC. The other way is measuring with an oscilloscope in the OSC1 and OSC2 pins there must be the instructions frecuency.
     
    For testing the Sleep mode I'm measuring the current without the LCD connected, but I'm not seeing any difference between Idle mode and Sleep Mode. 
     
    Any suggestion?
    #8
    coffee critic
    Super Member
    • Total Posts : 378
    • Reward points : 0
    • Status: offline
    Re: Switching from INTOSC to TIMER1 oscillator for Sleep Mode 2018/11/06 13:35:53 (permalink)
    +1 (1)
    If what you posted is correct OSCCON = 0xE5, SCS bits are 01 which will force the system clock use to timer 1 oscillator.  You will not likely be able to tell the difference between idle and sleep by current alone.  Try some other SCS setting.  
     
     

    n_*$
    #9
    Joel QO
    New Member
    • Total Posts : 26
    • Reward points : 0
    • Joined: 2014/09/28 11:52:38
    • Location: 0
    • Status: offline
    Re: Switching from INTOSC to TIMER1 oscillator for Sleep Mode 2018/11/06 13:53:52 (permalink)
    0
     Thank you. You're right.
    With the sleep mode working, I don't have to change the oscillator before sleep to get a minimum current.
     
    Now my Sleep Mode current is 90μA but the datasheet says that in Sleep Mode the current must be arround 0.1 μA and 1.1μA with the Timer1 Oscillator enabled, so I must figure out which is the actual power consumption source.
     
    I'll test the resistors for I2C.
     
     
     
    Could be possible that any other software setup could be causing this current consumption if I'm entering in Sleep Mode?

    Attached Image(s)

    #10
    coffee critic
    Super Member
    • Total Posts : 378
    • Reward points : 0
    • Status: offline
    Re: Switching from INTOSC to TIMER1 oscillator for Sleep Mode 2018/11/06 15:28:35 (permalink)
    +1 (1)
    Make sure all the unused pins are not floating.  Disconnect your programmer before taking any current measurements. 

    n_*$
    #11
    Joel QO
    New Member
    • Total Posts : 26
    • Reward points : 0
    • Joined: 2014/09/28 11:52:38
    • Location: 0
    • Status: offline
    Re: Switching from INTOSC to TIMER1 oscillator for Sleep Mode 2018/11/07 10:16:02 (permalink)
    0
    Hi, 
    I've based on the Chapter 2 of the DS01146B document. 
    • The non connected I/O pins were configured as outputs and puted a 0 on them.
    • The resistors for I2C were connected to an enable pin and I've aumented their resistance to 10KΩ.
    The current consumption have changed from 90μA to 30μA. 
     
    I would like to know if I can change anything else about it. The datasheet says that with the Timer1 Oscillator should work arrown 1.1μA.
     
    Thanks in advance.
    #12
    coffee critic
    Super Member
    • Total Posts : 378
    • Reward points : 0
    • Status: offline
    Re: Switching from INTOSC to TIMER1 oscillator for Sleep Mode 2018/11/07 10:30:56 (permalink)
    0
    This current number makes sense if you are in idle not sleep.  Look at table 25.2 Power-Down and Supply Current.
     
    Have you disabled BOR and WDT for this test?

    n_*$
    #13
    Joel QO
    New Member
    • Total Posts : 26
    • Reward points : 0
    • Joined: 2014/09/28 11:52:38
    • Location: 0
    • Status: offline
    Re: Switching from INTOSC to TIMER1 oscillator for Sleep Mode 2018/11/07 13:16:27 (permalink)
    0
    coffee critic
    This current number makes sense if you are in idle not sleep.  Look at table 25.2 Power-Down and Supply Current.
     
    Have you disabled BOR and WDT for this test?


     
    I suppose that you've meant the table 28.2 the only current values that match are in the RUN modes.

    I've checked BOR and WDT, both of them were turned off.
    In Idle Mode, I'm having 754μA during sleep.
    In Sleep Mode I'm having 26.9μA during sleep.
     
    Must be something else. I've checked a lot the circuit and I don't know if there is something else to get the ideal current.
    Any idea?
     
     
     
    #14
    coffee critic
    Super Member
    • Total Posts : 378
    • Reward points : 0
    • Status: offline
    Re: Switching from INTOSC to TIMER1 oscillator for Sleep Mode 2018/11/07 14:33:25 (permalink)
    0
    How about CONFIG2L VREGEN.

    n_*$
    #15
    Joel QO
    New Member
    • Total Posts : 26
    • Reward points : 0
    • Joined: 2014/09/28 11:52:38
    • Location: 0
    • Status: offline
    Re: Switching from INTOSC to TIMER1 oscillator for Sleep Mode 2018/11/07 14:56:32 (permalink)
    0
    No. In fact this is my configBits file.
    // CONFIG1L
    #pragma config PLLDIV = 1 
    #pragma config CPUDIV = OSC1_PLL2
    #pragma config USBDIV = 1
     

    // CONFIG1H
    #pragma config FOSC = INTOSCIO_EC
    #pragma config FCMEN = OFF 
    #pragma config IESO = OFF

    // CONFIG2L
    #pragma config PWRT = OFF 
    #pragma config BOR = OFF 
    #pragma config BORV = 3 
    #pragma config VREGEN = OFF
     
     
     
    // CONFIG2H
    #pragma config WDT = OFF 
    #pragma config WDTPS = 32768 

    // CONFIG3H
    #pragma config CCP2MX = ON 
    #pragma config PBADEN = OFF 
    #pragma config LPT1OSC = ON 
    #pragma config MCLRE = OFF 

    // CONFIG4L
    #pragma config STVREN = ON 
    #pragma config LVP = OFF 
    #pragma config XINST = OFF
     

    // CONFIG5L
    #pragma config CP0 = OFF 
    #pragma config CP1 = OFF
    #pragma config CP2 = OFF 
    #pragma config CP3 = OFF 

    // CONFIG5H
    #pragma config CPB = OFF 
    #pragma config CPD = OFF 

    // CONFIG6L
    #pragma config WRT0 = OFF 
    #pragma config WRT1 = OFF 
    #pragma config WRT2 = OFF 
    #pragma config WRT3 = OFF 

    // CONFIG6H
    #pragma config WRTC = OFF 
    #pragma config WRTB = OFF 
    #pragma config WRTD = OFF 

    // CONFIG7L
    #pragma config EBTR0 = OFF 
    #pragma config EBTR1 = OFF 
    #pragma config EBTR2 = OFF 
    #pragma config EBTR3 = OFF 

    // CONFIG7H
    #pragma config EBTRB = OFF 

     
    At this moment I'm using a DIP package. Could this be a reason?
    post edited by Joel QO - 2018/11/07 14:58:07
    #16
    Jump to:
    © 2019 APG vNext Commercial Version 4.5