Page 1 of 1

Sleep mode / IDLE with PIC16F18345

PostPosted: Tue Jun 26, 2018 7:55 am
by jo7
Hello everyone,
I want to implement the operation in low consumption with my PIC16F18345. l can operate in 3 mode:, IDLE, DOZE, SLEEP
-the mode type is set with the IDLEN bit of the CPUDOZE register

But here is my problem, is:

I would like :
1. Press the button, the led lights up and it returns to sleep mode.
2. 2nd press, we return to normal mode and the led flashes.

Code: Select all
#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)

#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 = ON        // Watchdog Timer Enable bits (WDT enabled, 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)

#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.)

#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)

#define ICSPDAT         RA0                     // Port de programmation
#define POWER           RA1                     // Retour d etat charge batterie
#define MCLR            RA3                     // Port de programmation
#define Vactiv          RA5                     // Grille transistor ventilateur

#define ledR            RB7                     // +LED rouge

#define BP              RC5                     // Bouton poussoir

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

#define _XTAL_FREQ  8000000                     // Oscillateur réglé à 8MHz
#include <xc.h>

int control;            //

void interrupt Interrupt_bp (void)
          PIR0bits.INTF = 0;        // RAZ flag IT
          control = ~control;
          ledR = ~ledR;
          if (BP == 1){
void main(void) {
    OSCCON1 = 0b01100000;                       // Oscillateur interne
    OSCFRQ = 0b00000110;                        // Frequence oscillateur a 16MHz
    CPUDOZE = 0b00000000;                       // Choix DOZE/IDLE mode pour economie d energie en veille
    VREGCON = 0b00000010;                       // Choix DOZE/IDLE mode pour economie d energie en veille
     // Reglages des entrees/sorties
    TRISA = 0x0B;                         // Choix entrees/sorties
    LATA = 0x00;                          // RAZ des ports
    ANSELA = 0x00;                        // Choix mode analogique/numerique   
    WPUA = 0x02;                          // Resistances de pull-up interne
    TRISB = 0x00;                         // Choix entrees/sorties
    LATB = 0x80;                          // RAZ des ports
    ANSELB = 0x00;                        // Choix mode analogique/numerique
    WPUB = 0x00;                          // Resistances de pull-up interne
    TRISC = 0x20;                         // Choix entrees/sorties
    LATC = 0xC0;                          // RAZ des ports
    ANSELC = 0x18;                        // Choix mode analogique/numerique
    WPUC = 0x20;                          // Resistances de pull-up interne

    //Reglage des interuptions
    INTCON = 0b11000000;                 // Autorisation des interruptions
    //Reglage des peripheriques
    PIE0bits.TMR0IE = 0;                       // Valide l'interruption generee par le debordement du Timer0
    PIE0bits.IOCIE = 1;                        // Valide le changement d'etat
    PIE0bits.INTE = 1;                         // Autoise l'interruption sur un peripherique exterieur
    PIR0bits.TMR0IF = 0;                       // RAZ flag  IT
    PIR0bits.IOCIF = 1;                        // Activation du front descendant
    PIR0bits.INTF = 1;                         // Detection d'une interruption
    ledR = 0;
    control = 0;
        ledR = 1;
        ledR = 0;

Re: Sleep mode / IDLE with PIC16F18345

PostPosted: Tue Jun 26, 2018 10:38 am
by ric
You asked almost the same question in your first topic at viewtopic.php?f=62&t=643&p=3384#p3384
There was really no need to start a second topic. Also, you've ignored the advice in that topic, and are still writing to PORTx registers rather than LATx registers.

Re: Sleep mode / IDLE with PIC16F18345

PostPosted: Wed Jun 27, 2018 4:19 am
by AussieSusan
No idea if this is relevant but you are using the keywords "ON" and "OFF' in the #pragma config settings but also define "ON" to be 0 and "OFF" to be 1. I know that the compilers can treat the #pragma settings differently to other parts of the code file, but I would be concerned that this might lead to the wrong config settings on some cases.
You need to read up on the difference between the INT (external interrupt - Section 8.4 of the data sheet) function and the IOC (Interrupt On Change - Section 15) function. Wile you enable both interrupt sources, you only handle the INT source.
However, the comments look like you expect the INT source to be triggered when you push the button. (I've already mentioned that you need to handle contact bounce in the other thread.) As you have not altered the PPS defaults, the INT function is on RA2 whereas your schematic shows the button on RC2.
Please take the previous advice and start slowly. You can getting yourself into a whole lot of problems because you are trying to do more than your current state of knowledge will let you. The learning curve can be quite steep at the start but once you can 'flash a LED' at the desired frequency then you are over the worst and you can progress fairly quickly from there.