Hot!Sleep Mode with PIC16F18345

Author
Jonathan_G
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2018/03/07 06:24:16
  • Location: 0
  • Status: offline
2018/07/02 00:09:53 (permalink)
0

Sleep Mode with PIC16F18345

Hello everyone,

I had a question about the sleep mode (SLEEP Mode) of the microcontroller to check the consumption of it.
I would just like to use my code activate the activation of the PIC old mode without button or interruption.
The problem is that the code does nothing and the consumption remains the same. Can you tell me my mistake? Thank you in advance !

// CONFIG1
#pragma config FEXTOSC = OFF // FEXTOSC External Oscillator mode Selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT1 // Power-up default value for COSC bits (HFINTOSC with 2x PLL (32MHz))
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is enabled; FOSC/4 clock appears at OSC2)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

// CONFIG2
#pragma config MCLRE = ON // Master Clear Enable bit (MCLR/VPP pin function is MCLR; Weak pull-up enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config WDTE = OFF // Watchdog Timer Enable bits (WDT disabled; SWDTEN is ignored)
#pragma config LPBOREN = OFF // Low-power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = SBOREN // Brown-out Reset Enable bits (Brown-out Reset enabled according to SBOREN)
#pragma config BORV = LOW // Brown-out Reset Voltage selection bit (Brown-out voltage (Vbor) set to 2.45V)
#pragma config PPS1WAY = OFF // PPSLOCK bit One-Way Set Enable bit (The PPSLOCK bit can be set and cleared repeatedly (subject to the unlock sequence))
#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will not cause a Reset)
#pragma config DEBUG = OFF // Debugger enable bit (Background debugger disabled)

// CONFIG3
#pragma config WRT = OFF // User NVM self-write protection bits (Write protection off)
#pragma config LVP = ON // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored.)

// CONFIG4
#pragma config CP = OFF // User NVM Program Memory Code Protection bit (User NVM code protection disabled)
#pragma config CPD = OFF // Data NVM Memory Code Protection bit (Data NVM code protection disabled)

#include <xc.h>

#define _XTAL_FREQ 3000000 // Oscillateur réglé à 1MHz

// PORT A
#define ICSPDAT RA0 // Port de programmation
#define POWER RA1 // Retour d etat charge batterie
#define MCLR RA3 // Port de programmation


/*// PORT B
#define ledR RB7 // +LED rouge

// PORT C
#define BP RC5 // Bouton poussoir*/



void main(void)
{

        // Reglages des entrees/sorties
    TRISA = 0b00001011; // Choix entrees/sorties
    LATA = 0b00000000; // RAZ des ports
    ANSELA = 0; // Choix mode analogique/numerique
    INLVLA = 0b00000010; // Niveaux TTL
    WPUA = 0b00000010; // Resistances de pull-up interne
    TRISB = 0b01010000; // Choix entrees/sorties
    LATB = 0b10000000; // RAZ des ports
    ANSELB = 0; // Choix mode analogique/numerique
    INLVLB = 0b10000000; // Niveaux TTL
    SLRCONB = 0b10101111; // Slew rate
    WPUB = 0; // Resistances de pull-up interne
    TRISC = 0b00111000; // Choix entrees/sorties
    LATC = 0b11000000; // RAZ des ports
    ANSELC = 0b00011000; // Choix mode analogique/numerique
    INLVLC = 0b11000111; // Niveaux TTL
    SLRCONC = 0b11111111; // Slew rate
    WPUC = 0b00100000; // Resistances de pull-up interne
    
    __delay_ms(2);
    
       
   
while(1){
    SLEEP(); // boucle infinie
    }
[/code]
#1

15 Replies Related Threads

    qɥb
    Monolothic Member
    • Total Posts : 3329
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: Sleep Mode with PIC16F18345 2018/07/02 00:13:57 (permalink)
    0
    Please do NOT put code tags around the text of your question.
    If you look at your post, you will see all that text is truncated.
    Here it is with the code tags removed.
    Jonathan_G
    Hello everyone,

    I had a question about the sleep mode (SLEEP Mode) of the microcontroller to check the consumption of it.
    I would just like to use my code activate the activation of the PIC old mode without button or interruption.
    The problem is that the code does nothing and the consumption remains the same. Can you tell me my mistake? Thank you in advance !




    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"
    #2
    brownt
    Super Member
    • Total Posts : 292
    • Reward points : 0
    • Joined: 2015/11/21 14:58:09
    • Location: 0
    • Status: offline
    Re: Sleep Mode with PIC16F18345 2018/07/03 05:41:00 (permalink)
    0
    Setup the oscillator, Like this
     

     // NOSC HFINTOSC; NDIV 4;
     OSCCON1 = 0x62;
     // CSWHOLD may proceed; SOSCPWR Low power; SOSCBE crystal oscillator;
     OSCCON3 = 0x00;
     // LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
     OSCEN = 0x00;
     // HFFRQ 1_MHz;
     OSCFRQ = 0x00;
     // HFTUN 0;
     OSCTUNE = 0x00;


    Then do a simple LED test first, to make sure the system is running. Then i think you need to set somethings before the sleep function is called. Like this
     

    VREGCON = 0b11; //lowest power mode but slowest wake up time
    IDLEN =0; //when set to 0, SLEEP() causes full pwr down mode. 1 = idle mode

     
    The Microchip Code Configurator (MCC) is worthwhile learning to use, if you are in it for the long run. It will generate the code for the oscillator and other bits.
     
     
    #3
    Jonathan_G
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/03/07 06:24:16
    • Location: 0
    • Status: offline
    Re: Sleep Mode with PIC16F18345 2018/07/12 02:40:49 (permalink)
    0
    Hello,
    The PIC I'm using has three power off modes: DOZE mode, IDLE mode, and standby mode (P113). I would like using the button to vary these three modes by an interruption.

    On my test plate, I put a push button connected to RC5 (at rest 5V on RC5 and if a press on the BP, 0V on RC5), I thus wish to use the interrupts and the different mode of consumption of the mode sleep of my PIC16F18345
    1 / BP at rest-> no interruption on RC5-> PIC in SLEEP mode
    2 / Press BP-> interrupt on RC5-> PIC in DOZE mode -> Program in operation
    3 / RE Press BP-> interrupt on RC5-> stop program-> PIC in IDLE Mode

    My current code does the following thing: power on -> sleep mode, press the button -> normal mode
    I can not vary the different modes. Thank you in advance ![quote=brownt]
    // CONFIG1
    #pragma config FEXTOSC = OFF // FEXTOSC External Oscillator mode Selection bits (Oscillator not enabled)
    #pragma config RSTOSC = HFINT1 // Power-up default value for COSC bits (HFINTOSC (1MHz))
    #pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; I/O or oscillator function on OSC2)
    #pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
    #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

    // CONFIG2
    #pragma config MCLRE = ON // Master Clear Enable bit (MCLR/VPP pin function is MCLR; Weak pull-up enabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config WDTE = OFF // Watchdog Timer Enable bits (WDT disabled; SWDTEN is ignored)
    #pragma config LPBOREN = OFF // Low-power BOR enable bit (ULPBOR disabled)
    #pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled)
    #pragma config BORV = LOW // Brown-out Reset Voltage selection bit (Brown-out voltage (Vbor) set to 2.45V)
    #pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (The PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
    #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a Reset)
    #pragma config DEBUG = OFF // Debugger enable bit (Background debugger disabled)

    // CONFIG3
    #pragma config WRT = OFF // User NVM self-write protection bits (Write protection off)
    #pragma config LVP = ON // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored.)

    // CONFIG4
    #pragma config CP = OFF // User NVM Program Memory Code Protection bit (User NVM code protection disabled)
    #pragma config CPD = OFF // Data NVM Memory Code Protection bit (Data NVM code protection disabled)


    // PORT A
    #define ICSPDAT RA0 // Port de programmation
    #define POWER RA1 // Retour d etat charge batterie
    #define MCLR RA3 // Port de programmation


    // PORT B
    #define ledR RB7 // +LED rouge

    // PORT C
    #define BP RC5 // Bouton poussoir


    #define ON 0 // Led tirée au +VCC
    #define OFF 1 // Led tiréé au gnd

    //#define _XTAL_FREQ 1000000 // Oscillateur réglé à 1MHz

    //int etat_Bp; // Variable bouton

    #include <xc.h>
    #include <pic16f18345.h>

    void interrupt ISR ()
    {
       if(PIE0bits.IOCIE) // IT changement d'etat bouton
        {

        CPUDOZEbits.IDLEN = 1;
        IOCCFbits.IOCCF5 = 0; // RAZ flag IT

       }
       else if (PIE0bits.IOCIE){

         CPUDOZEbits.DOZEN = 1;
         IOCCFbits.IOCCF5 = 0; // RAZ flag IT
          
       }

    }

    void main(void) {
        
        OSCCON1 = 0x37; // Oscillateur interne
        OSCFRQ = 0x06; // Frequence oscillateur a 16MHz
        CPUDOZE = 0x00; // Choix DOZE/IDLE mode pour economie d energie en veille
        VREGCON = 0x03; // Choix DOZE/IDLE mode pour economie d energie en veille
        
        // Reglages des entrees/sorties
        TRISA = 0x37; // Choix entrees/sorties
        LATA = 0x00; // RAZ des ports
        ANSELA = 0x37; // Choix mode analogique/numerique
        WPUA = 0x20; // Resistances de pull-up interne
        
        TRISB = 0xFF; // Choix entrees/sorties
        LATB = 0x00; // RAZ des ports
        ANSELB = 0x70; // Choix mode analogique/numerique
        WPUB = 0x00; // Resistances de pull-up interne
        
        TRISC = 0xFF; // Choix entrees/sorties
        LATC = 0x00; // RAZ des ports
        ANSELC = 0xDF; // Choix mode analogique/numerique
        WPUC = 0x20; // Resistances de pull-up interne
        
        //WDTCON = 0x3C;
        
        //Reglage des interuptions
        INTCON = 0b11000000; // Autorisation des interruptions : GIE=1, PEIE=1 , INTED=0 donc sur front descendant

        IOCCNbits.IOCCN5 = 1; // Interruption sur la pin RC5 negative activee
        PIE0bits.IOCIE = 1; // Valide le changement d'etat
        IOCCFbits.IOCCF5 = 0; // RAZ flag IT
        
        SLEEP();
        
        while(1){
          }
    }


    [/code]


    #4
    Jonathan_G
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/03/07 06:24:16
    • Location: 0
    • Status: offline
    Re: Sleep Mode with PIC16F18345 2018/07/12 02:43:01 (permalink)
    0
    http://ww1.microchip.com/downloads/en/DeviceDoc/40001795E.pdf
     
    P 113 to 118--> power-saving operation modes
     
    #5
    brownt
    Super Member
    • Total Posts : 292
    • Reward points : 0
    • Joined: 2015/11/21 14:58:09
    • Location: 0
    • Status: offline
    Re: Sleep Mode with PIC16F18345 2018/07/12 05:58:01 (permalink)
    0
    are you saying that your code now works. In that you can wake it from sleep mode with a button press, and it moves to normal operation?
     
    I am not sure why you have done the following twice
     

     
    if(PIE0bits.IOCIE)
     

     
    But regardless, the IOC being enabled is not to be checked, rather you need to check for the interrupt flag IOCDFbits.IOCCF5 within the interrupt, clear it, and then run your code.
    post edited by brownt - 2018/07/12 05:59:48
    #6
    Jonathan_G
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/03/07 06:24:16
    • Location: 0
    • Status: offline
    Re: Sleep Mode with PIC16F18345 2018/07/12 06:13:31 (permalink)
    0
    My program is not working properly I can not stop at the second press to return to normal mode
    #7
    brownt
    Super Member
    • Total Posts : 292
    • Reward points : 0
    • Joined: 2015/11/21 14:58:09
    • Location: 0
    • Status: offline
    Re: Sleep Mode with PIC16F18345 2018/07/12 06:23:35 (permalink)
    0
    What does the first press do?
     
    Can you confirm that the device is in sleep mode?
     
    Is the IOC flag being checked?
    #8
    Jonathan_G
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/03/07 06:24:16
    • Location: 0
    • Status: offline
    Re: Sleep Mode with PIC16F18345 2018/07/12 06:29:20 (permalink)
    0
    On power up, the μc goes directly into sleep mode. When I press a first time, it goes into normal mode.
    #9
    Jonathan_G
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/03/07 06:24:16
    • Location: 0
    • Status: offline
    Re: Sleep Mode with PIC16F18345 2018/07/12 06:30:31 (permalink)
    0
    What's CIO please ?
     
    #10
    brownt
    Super Member
    • Total Posts : 292
    • Reward points : 0
    • Joined: 2015/11/21 14:58:09
    • Location: 0
    • Status: offline
    Re: Sleep Mode with PIC16F18345 2018/07/12 06:34:09 (permalink)
    +1 (1)
    CIO, I don't know what you mean. Do you mean IOC?
     
    When the button is pressed the interrupt routine will run. Within the routine the IOC flag should be checked, if it is set, then you know the button has been pressed. You then clear the flag and run your code, until the button is pressed again.
    post edited by brownt - 2018/07/12 06:38:02
    #11
    Jonathan_G
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/03/07 06:24:16
    • Location: 0
    • Status: offline
    Re: Sleep Mode with PIC16F18345 2018/07/12 06:35:44 (permalink)
    0
    Interrupt On Change --> P.173
    #12
    brownt
    Super Member
    • Total Posts : 292
    • Reward points : 0
    • Joined: 2015/11/21 14:58:09
    • Location: 0
    • Status: offline
    Re: Sleep Mode with PIC16F18345 2018/07/12 06:42:59 (permalink)
    +1 (1)
    why PIE0bits.IOCIE twice?
     

    if(PIE0bits.IOCIE) // IT changement d'etat bouton
        {
       CPUDOZEbits.IDLEN = 1;
        IOCCFbits.IOCCF5 = 0; // RAZ flag IT
       }
       else if (PIE0bits.IOCIE){
         CPUDOZEbits.DOZEN = 1;
         IOCCFbits.IOCCF5 = 0; // RAZ flag IT
      }

     
    but anyway, it is not IOCIE that you want to check, it is IOCIF, the interrupt flag. You want to check if the flag has been set because the flag will be set when the button is pressed. You check if the flag is set, clear the flag, and run your code until the button is pressed again.
    #13
    Jonathan_G
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/03/07 06:24:16
    • Location: 0
    • Status: offline
    Re: Sleep Mode with PIC16F18345 2018/07/12 06:56:47 (permalink)
    0
     


    I can not change the mode now, when I press the button nothing happens





     
     
    void interrupt ISR ()
    {
    if(IOCCFbits.IOCCF5 == 1) // IT changement d'etat bouton
    {
    CPUDOZEbits.IDLEN = 1;
    IOCCFbits.IOCCF5 = 0; // RAZ flag IT

    }

    }

    #14
    JorgeF
    Super Member
    • Total Posts : 3216
    • Reward points : 0
    • Joined: 2011/07/09 11:56:58
    • Location: PT/EU @ Third rock from the Sun
    • Status: offline
    Re: Sleep Mode with PIC16F18345 2018/07/12 11:20:06 (permalink)
    +2 (2)
    Hi
     
    Made some suggestions here http://picforum.ric323.co...pic.php?f=11&t=649

    Best regards
    Jorge
     
    I'm here http://picforum.ric323.com too!
    And it works better....
    #15
    brownt
    Super Member
    • Total Posts : 292
    • Reward points : 0
    • Joined: 2015/11/21 14:58:09
    • Location: 0
    • Status: offline
    Re: Sleep Mode with PIC16F18345 2018/07/14 07:03:54 (permalink)
    0
    how are you determining that the system is in normal mode?
     
    You could test the interrupt like this. You might get some switch bouncing, but you should see something happening.
     

    void interrupt ISR ()
    {
         if(IOCCFbits.IOCCF5 == 1) // IT changement d'etat bouton
        {
           //LED = !LED; Each time BP is pressed the LED will change. Or measure with meter or oscilloscope

           IOCCFbits.IOCCF5 = 0; // RAZ flag IT
       }
     
    }



    #16
    Jump to:
    © 2018 APG vNext Commercial Version 4.5