deep sleep and wake up using portB on change interrupt

Author
seth19
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2010/06/30 01:18:23
  • Location: 0
  • Status: offline
2010/08/18 03:10:36 (permalink)
0

deep sleep and wake up using portB on change interrupt

Hi,

i am working on the p18f46j50 microchip device and i try to make some test on the deep sleep mode. in order to do that i'm using the code example from microchip (the one remaining at 12/15/2009)

The code is working great and i'm able wake the device with all the wake exept the portB on change one and i don't know why.

i join you alll the code ( there's not a lot of code wink ). In my opinion the PortB-on-change interrupt is not correctly map to INT0.


If someone has an idea.


thanks.



#define USE_OR_MASKS
#include "p18cxxx.h"
#include "dpslp.h"
#include "portb.h"
#include "rtcc.h"
#define TRUE 1

//************Configuration in program*****************************

#if defined(__18F46J50) //If the selected device if PIC18F46J50, then apply below settings else user will have to set
#pragma config DSWDTOSC = INTOSCREF, RTCOSC = INTOSCREF, DSBOREN = ON,  DSWDTEN = ON, DSWDTPS = K32
#pragma config T1DIG = ON, LPT1OSC = OFF 
#pragma config OSC=INTOSCPLLO, FCMEN=ON, WDTEN=OFF, IESO=ON, XINST=OFF, CPUDIV=OSC1, PLLDIV = 3 
#endif


//Function Prototypes
void user_main(void);
unsigned char GetKey(void);
void DebounceDelay(void);
void SwitchInit(void);
void Blink_LED(unsigned int number_of_blink);
void RTCC_configure(void);
void EnableINT0(void);

//Global structures used in deep sleep library

SRC ptr;
CONTEXT read_state;
rtccTimeDate RtccTimeDate ,RtccAlrmTimeDate, Rtcc_read_TimeDate ;

//main function
void main(void)
{
if(IsResetFromDeepSleep()==0xFF) //if this is the reset after the deep_sleep wakup...then do this
{
ReadDSGPR( &read_state); //Read the deep sleep GPR
DeepSleepWakeUpSource(&ptr); //Check the deep sleep wakup soruce (if required)
ReleaseDeepSleep(); //Release the Deep sleep (IO configuration)
TRISEbits.TRISE0 = 0; 
PORTEbits.RE0 = 0; //configure the IO [TRIS and LAT register] to output ssignal for LED
SwitchInit(); //This function configures Switch IO as input pin
if( (read_state.Reg0!=0x67) || (read_state.Reg1!=0x7A) ) //0x67 & 0x7A represents the state of device before going to deep sleep
{
while(1)
{
Blink_LED(150); //Continuously Toggle LED to indicate the error in GPR reading. State of device corrupted
}
}

if((ptr.WK_SRC.DS_POR)==TRUE)
{
//deep sleep wakeup source is DSPOR
Blink_LED(1);
}
if(ptr.WK_SRC.DS_MCLR==TRUE)
{
//deep sleep wakeup source is MCLR
Blink_LED(2);
}
if(ptr.WK_SRC.DS_RTC==TRUE)
{
//deep sleep wakeup source is RTCC
Blink_LED(3);
}
if(ptr.WK_SRC.DS_WDT==TRUE)
{
//deep sleep wakeup source is DSWDT
Blink_LED(4);
}
if(ptr.WK_SRC.DS_FLT==TRUE)
{
//deep sleep wakeup source is Falut in deep sleep configuration
Blink_LED(5);
}
if(ptr.WK_SRC.DS_INT0==TRUE)
{
//deep sleep wakeup source is INT0
Blink_LED(6);
}
if(ptr.WK_SRC.DS_BOR==TRUE)
{
//deep sleep wakeup source is DSBOR
Blink_LED(7);
}
if(ptr.WK_SRC.DS_ULP==TRUE)
{
//deep sleep wakeup source is DSULP
Blink_LED(8);
}
user_main(); //call the "user_main" -- The User application program
}
else //else... this is the Normal (pure) Power_on Reset...do the normal init
{
//Normal POR init program here
TRISEbits.TRISE0 = 0; 
SwitchInit(); //This function configures Switch IO as input pin
Blink_LED(2);
PORTEbits.RE0 = 0;
user_main(); //call the "user_main" -- The User application program
}
while(1);
}

void user_main(void)
{
unsigned int config=0,i=0;
char Key;
PORTEbits.RE0 = 0;
while(1)
{
Key = !GetKey(); //check if Switch is pressed
if(Key) //if key pressed then call the deep sleep function
{

Write_DSGPR(0x67,0x7A); //Save state of system prior to deep sleep
RTCC_configure(); //Configure RTCC as one of sources of wake up
//*************** Charge the capacitor on RA0 for ultra low power wake up as source of wake up from deep sleep*****
//***************************
TRISAbits.TRISA0 = 0;
PORTAbits.RA0 = 1;
for(i = 0; i < 10000; i++) Nop();
//**************** configure INT0 with pullups enabled, falling edge *********************************
config = (PORTB_CHANGE_INT_ON | FALLING_EDGE_INT | PORTB_PULLUPS_ON);
   OpenRB0INT(config ); //configures INT0 & enables it
//*************** configure deep sleep wake up sources ********************************************
config = ( DPSLP_ULPWU_DISABLE | DPSLP_RTCC_WAKEUP_ENABLE);
GotoDeepSleep(config); //This function puts the device into deep sleep

}
}
}

void DebounceDelay(void)
{
unsigned int i = 0x00FF;
while(i--);    
}

unsigned char GetKey()
{
if(!PORTBbits.RB2) //this RB2 should be checked and changes as in the hardware
return 1; //TRUE;
else
{
while(PORTBbits.RB2); //this RB2 should be checked and changes as in the hardware
DebounceDelay();
return 0; //FALSE;
}
}

void SwitchInit(void)
{
TRISBbits.TRISB2 = 1; //  PORTB<2> as input, SW2 (on PIC Dem2+  board). This PORTB<2> should be checked and changes as in the hardware
ANCON1 = 0xFF; //configure ports to be digital

TRISBbits.TRISB0 = 0;
ANCON1bits.PCFG12 = 1;      // configure RB0/INT0/AN12 as digital input
TRISBbits.TRISB4 =1;    // config port RB4 et RB5 pour wake portB on change
TRISBbits.TRISB5 =1;

}

void Blink_LED(unsigned int number_of_blink)
{
unsigned int m=0,n=0;

// ************ BLINK LED *******************************
for(n=0;n<number_of_blink;n++)
{
LATE = 0x00;
for(m=0;m<60000;m++);
LATE = 0xFF;
for(m=0;m<60000;m++);
}
}


void RTCC_configure(void)
{
unsigned int i=0,j=0;


   RtccInitClock();       //turn on clock source 
for(i=0;i<4;i++) //Delay for Secondary oscillator to stabilize
for(j=0;j<60000;j++);

   RtccWrOn();             //write enable the rtcc registers  
   RtccTimeDate.f.hour = 9; //Set Date and time
   RtccTimeDate.f.min =  10;
   RtccTimeDate.f.sec =  9;
   RtccTimeDate.f.mday = 18;
   RtccTimeDate.f.mon =  1;
   RtccTimeDate.f.year = 8;   
   RtccAlrmTimeDate.f.hour = RtccTimeDate.f.hour;
   RtccAlrmTimeDate.f.min =  RtccTimeDate.f.min ;
   RtccAlrmTimeDate.f.sec =  RtccTimeDate.f.sec + 9;
   RtccAlrmTimeDate.f.mday = RtccTimeDate.f.mday;
   RtccAlrmTimeDate.f.mon =  RtccTimeDate.f.mon;
   RtccAlrmTimeDate.f.year = RtccTimeDate.f.year; 
   
   RtccWriteTimeDate(&RtccTimeDate,1); //write into registers
   RtccSetAlarmRpt(RTCC_RPT_TEN_SEC,1); //Set the alarm repeat to every 10 seconds
   RtccSetAlarmRptCount(5,1);
   RtccWriteAlrmTimeDate(&RtccAlrmTimeDate);

mRtccOn(); //enable the rtcc
mRtccAlrmEnable(); //enable the rtcc alarm to wake the device up from deep sleep
}  

void EnableINT0(void)
{
INTCON2bits.INTEDG0 = 0; // 0 - interrupt on falling edge
    INTCONbits.INT0IF = 0;      // clear INT0 interrupt flag
    INTCONbits.INT0IE = 1;      // enable INT0 interrupt
}




#1

6 Replies Related Threads

    DarioG
    Scheisse Menschen
    • Total Posts : 52256
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: hi there
    • Status: offline
    Re:deep sleep and wake up using portB on change interrupt 2010/08/18 04:30:55 (permalink)
    0
    Use LATx registers when writing, anyway.

    Dario Greggio
    --
    Wouldn't it be wonderful, if you lost one son for every promise you don't keep?
    #2
    seth19
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2010/06/30 01:18:23
    • Location: 0
    • Status: offline
    Re:deep sleep and wake up using portB on change interrupt 2010/08/18 04:46:56 (permalink)
    0

    thanks for your answer but whane you say:
    "Use LATx registers when writing, anyway. "



    where et what for should i use LATX? 
    #3
    DarioG
    Scheisse Menschen
    • Total Posts : 52256
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: hi there
    • Status: offline
    Re:deep sleep and wake up using portB on change interrupt 2010/08/18 06:08:45 (permalink)
    0
    This one for instance:

    TRISAbits.TRISA0 = 0;
    PORTAbits.RA0 = 1;

    becomes

    TRISAbits.TRISA0 = 0;
    LATAbits.LATA0 = 1;

    Dario Greggio
    --
    Wouldn't it be wonderful, if you lost one son for every promise you don't keep?
    #4
    seth19
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2010/06/30 01:18:23
    • Location: 0
    • Status: offline
    Re:deep sleep and wake up using portB on change interrupt 2010/08/18 07:55:11 (permalink)
    0
    I've done what you suggest but unfortunatly i don't see any change concerning the wake up using the portB on change interrupt.

    but thank you anyway
    #5
    fredmorton
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2007/04/16 21:54:05
    • Location: 0
    • Status: offline
    Re:deep sleep and wake up using portB on change interrupt 2010/08/18 09:25:00 (permalink)
    0
    Seth,

    It looks to me like you are configuring RB0 as an output in SwitchInit and never re-configuring it as an input prior to entering Deep Sleep.

    The function OpenRB0INT doesn't set TRISB0 = 1, so you will need to make sure it is set correctly yourself before calling it.

    Try that and let us know how it works.
    #6
    DarioG
    Scheisse Menschen
    • Total Posts : 52256
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: hi there
    • Status: offline
    Re:deep sleep and wake up using portB on change interrupt 2010/08/18 10:12:02 (permalink)
    0
    Yep, Seth, it was a suggestion valid for the future Smile

    Dario Greggio
    --
    Wouldn't it be wonderful, if you lost one son for every promise you don't keep?
    #7
    Jump to:
    © 2017 APG vNext Commercial Version 4.5