Sign In

PIC18F Deep Sleep Wake Up Trouble

Nikita B nsbankar501@gmail.com India

Hardware: PIC18F97J94 Plug In Module On Internal Clock, RTCC Is DS3231 Software: MPLAB X IDE For Programming, XC8 Compiler, MPLAB IPE For Programming With PICkit3

I am trying to save power by putting PIC in deep sleep mode and wake it up on external interrupt by RTCC at RB0/INT0 or by DSWDT interrupt. I have tried normal sleep mode too, and that works just fine with WDT or RTCC interrupt. but PIC doesnt wake up from Deep Sleep mode on INT0 or DSWDT. I have initialized all the registers required to do so, but cant get it working. i have pasted my main code below for DSWDT source, please let me know if I have missed something here.
Thanks.

Images

Program

// PIC18F97J94 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1L
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset (Enabled)
#pragma config XINST = OFF      // Extended Instruction Set (Disabled)

// CONFIG1H
#pragma config BOREN = ON       // Brown-Out Reset Enable (Controlled with SBOREN bit, disabled in Deep Sleep)
#pragma config BORV = 1         // Brown-out Reset Voltage (1.8V)
#pragma config CP0 = OFF        // Code Protect (Program memory is not code-protected)

// CONFIG2L
#pragma config FOSC = FRC       // Oscillator (Fast RC Oscillator (FRC))
#pragma config SOSCSEL = LOW    // T1OSC/SOSC Power Selection Bits (Low Power T1OSC/SOSC circuit selected)
#pragma config CLKOEN = ON      // Clock Out Enable Bit (CLKO output enabled on the RA6 pin)
#pragma config IESO = ON        // Internal External Oscillator Switch Over Mode (Enabled)

// CONFIG2H
#pragma config PLLDIV = NOPLL   // PLL Frequency Multiplier Select bits (No PLL used - PLLGO bit not available to user)

// CONFIG3L
#pragma config POSCMD = MS      // Primary Oscillator Select (MS oscillator mode selected(3.5 MHz -10 MHz))
#pragma config FSCM = CSECME    // Clock Switching and Monitor Selection Configuration bits (Clock switching is enabled, fail safe clock monitor is enabled)

// CONFIG3H

// CONFIG4L
#pragma config WPFP = WPFP255   // Write/Erase Protect Page Start/End Boundary (Write Protect Program Flash Page 255)

// CONFIG4H
#pragma config WPDIS = WPDIS    // Segment Write Protection Disable (Disabled)
#pragma config WPEND = WPENDMEM // Segment Write Protection End Page Select (Write Protect from WPFP to the last page of memory)
#pragma config WPCFG = WPCFGDIS // Write Protect Configuration Page Select (Disabled)

// CONFIG5L
#pragma config T5GSEL = T5G     // TMR5 Gate Select bit (TMR5 Gate is driven by the T5G input)
#pragma config CINASEL = DEFAULT// CxINA Gate Select bit (C1INA and C3INA are on their default pin locations)
#pragma config EASHFT = ON      // External Address Shift bit (Address Shifting enabled)
#pragma config ABW = MM         // Address Bus Width Select bits (8-bit address bus)
#pragma config BW = 16          // Data Bus Width (16-bit external bus mode)
#pragma config WAIT = OFF       // External Bus Wait (Disabled)

// CONFIG5H
#pragma config IOL1WAY = ON     // IOLOCK One-Way Set Enable bit (Once set, the IOLOCK bit cannot be cleared)
#pragma config LS48MHZ = SYSX2  // USB Low Speed Clock Select bit (Divide-by-2 (System clock must be 12 MHz))
#pragma config MSSPMSK2 = MSK7  // MSSP2 7-Bit Address Masking Mode Enable bit (7 Bit address masking mode)
#pragma config MSSPMSK1 = MSK7  // MSSP1 7-Bit Address Masking Mode Enable bit (7 Bit address masking mode)

// CONFIG6L
#pragma config WDTWIN = PS25_0  // Watch Dog Timer Window (Watch Dog Timer Window Width is 25 percent)
#pragma config WDTCLK = FRC     // Watch Dog Timer Clock Source (Use FRC when WINDIS = 0, system clock is not INTOSC/LPRC and device is not in Sleep; otherwise, use INTOSC/LPRC)
#pragma config WDTPS = 8192     // Watchdog Timer Postscale (1:8192)

// CONFIG6H
#pragma config WDTEN = ON       // Watchdog Timer Enable (WDT controlled by SWDTEN bit setting)
#pragma config WINDIS = WDTSTD  // Windowed Watchdog Timer Disable (Standard WDT selected; windowed WDT disabled)
#pragma config WPSA = 128       // WDT Prescaler (WDT prescaler ratio of 1:128)

// CONFIG7L
#pragma config RETEN = ON       // Retention Voltage Regulator Control Enable (Retention controlled by SRETEN during Sleep)
#pragma config VBTBOR = ON      // VBAT BOR Enable (VBAT BOR is enabled)
#pragma config DSBOREN = OFF    // Deep Sleep BOR Enable (BOR disabled in Deep Sleep (does not affect operation in non-Deep Sleep modes))
#pragma config DSBITEN = ON     // DSEN Bit Enable bit (Deep Sleep is controlled by the register bit DSEN)

// CONFIG7H

// CONFIG8L
#pragma config DSWDTPS = DSWDTPSD// Deep Sleep Watchdog Timer Postscale Select (1:262114 (8.456 Secs))

// CONFIG8H
#pragma config DSWDTEN = ON     // Deep Sleep Watchdog Timer Enable (DSWDT Enabled)
#pragma config DSWDTOSC = LPRC  // DSWDT Reference Clock Select (DSWDT uses LPRC as reference clock)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include "Clock.h"
#include "interrupt.h"
#include "I2C.h"
#include "UART.h"
#include "RTCC.h"
#include<pic18.h>
#define _XTAL_FREQ 4000000

#define RS232_1ON LATDbits.LATD3 = 1; LATDbits.LATD4 = 0;
#define RS232_1OFF LATDbits.LATD3 = 0; LATDbits.LATD4 = 1;

#define RS232_2ON LATFbits.LATF7 = 1; LATEbits.LATE6 = 0;
#define RS232_2OFF LATFbits.LATF7 = 0; LATEbits.LATE6 = 1;

void initPeripheralPins() 
{
  GIE = 0;
  PEIE = 0;
  IOLOCK = 0;
  INTCON2bits.INTEDG0 = 0; //Falling Edge Interrupt
  INTCONbits.INT0IE = 1; //Enable External Interrupts
  RPINR0_1 = 0x3A; //MAP UART1RX to RP43
  RPOR14_15 = 0x01; //MAP UART1TX to RP14
  RPOR42_43 = 0x10;
  RPINR2_3 = 0x00; //Map UART2RX to RP2
  RPOR2_3 = 0x22; //map UART2TX to RP3
  RPINR4_5 = 0x00; //Map UART3RX to RP0
  RPOR0_1 = 0x22; //Map UART3TX to RP1
  RPINR6_7 = 0x11; //Map UART4RX to RP4
  RPOR4_5 = 0x33; //Map UART4TX to RP5
  RPINR12_13 = 0xF2; //Map SDI2 to RP9
  RPOR12_13 = 0x04; //Map SDO2 to RP12
  RPOR18_19 = 0x05; //Map SCK2 to RP18
  IOLOCK = 1;
  GIE = 1;
  PEIE = 1;
  
}
void delay(int d)
{
    for(int n = 1; n <= d; n++)
    {   __delay_ms(100); }
}
    
void DS()
{   GIE = 0; PEIE = 0;
    RS232_1OFF;
    RS232_2OFF;
  PMD0 = 0xff;
  PMD1 = 0xff;
  PMD2 = 0xff;
  PMD3 = 0xff;
  PMD4 = 0xff;
  INTCONbits.INT0E =1;
  INTCONbits.T0IE = 1;
  PIE3bits.RTCCIE = 1;
  IPR3bits.RTCCIP = 1;
  RCONbits.IPEN = 1;
    RCON2bits.SWDTEN = 1;
    DSWAKELbits.DSWDT = 1;
    DSWAKELbits.DSWDT = 1;
    DSCONL = 0;
    DSCONL = 0;
    DSCONH = 0;
    DSCONH = 0;
   
    IDLEN = 0;
    IDLEN = 0;
    
    DSEN = 1; DSEN = 1;
    
    SLEEP();
    PEIE = 1; GIE = 1;
}


void main()
{   char data1[20]; 
    char data0[25];
    char data2[50];   
    char data3[20];
    char data4[20];
initClock(INTERNAL);

    TRISJbits.TRISJ1 = 0; //Output for DE 485
    TRISEbits.TRISE5 = 0; //Output for RE 485
    TRISDbits.TRISD4 = 0; //Output for DD 232
    TRISDbits.TRISD3 = 0; //Output for SD 232
    TRISFbits.TRISF7 = 0; //Output for SD 232
    TRISEbits.TRISE6 = 0; //Output for DD 232
    TRISBbits.TRISB5 = 1; // to set GPIO3 as input
    TRISBbits.TRISB0 = 1; // set INT0 as input
    PMD2bits.TMR1MD = 0;
    DSWAKEHbits.DSINT0 = 1;
    DSWAKEHbits.DSINT0 = 1;
    PIE3bits.RTCCIE = 1;
    INTCONbits.INT0IF = 0;
    
    initPeripheralPins();
    RS232_1ON;
    RS232_2ON;
    UARTInitialize(9600 , 2);
    UARTInitialize(9600 , 3);
    UARTInitialize(9600 , 4);
    


    sprintf(data0 , "reading RTC   1...");
    transmitString(data0 , 2);
    transmitString(data0 , 3);
    transmitString(data0 , 4);
    
    initRTCC();
    
    sprintf(data3 , "2...");
    transmitString(data3 , 2);
    transmitString(data3 , 3);
    transmitString(data3 , 4);
    
    
    sprintf(data4 , "3...t");
    transmitString(data4 , 2);
    transmitString(data4 , 3);
    transmitString(data4 , 4);
    
    I2CInit();
    I2CStart();
   
   unlockRTC();
    Time rtc;
   ReadRTC(&rtc);
    
    
    //if(rtc.hour == 11 || rtc.hour == 12){
    sprintf(data2, "%02d_%02d_%02d_||%02d:%02d:%02dn", rtc.date, rtc.month, rtc.year, rtc.hour, rtc.minute, rtc.second);
    transmitString(data2 , 2);
    transmitString(data2 , 3);
    transmitString(data2 , 4); 
   /* }   else{   
    sprintf(data1 , "IIIIn");
    transmitString(data1 , 2);
    transmitString(data1 , 3);
    transmitString(data1 , 4);}*/
    
   delay(50);
   
   DS();
}

Replies

R erval87
2017-10-20 15:15:38
Hi,

you are clearing the global interrupt bit GIE before calling the sleep function. What your doing is disabling all type of interrupts in your program. Also, try clearing the priority enable bit IPEN before entering deep sleep mode.


Post a reply to Nikita B

Think you can help Nikita B ? Then post your thoughts that might help Nikita B. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.