Pic microcontroller SLEEP mode

Discussion in 'Embedded Systems and Microcontrollers' started by ecka333, Dec 23, 2010.

  1. ecka333

    Thread Starter Active Member

    Oct 1, 2009
    Hello, i am using PIC16F876A microcontroller. I am trying to put microcontroller in sleep mode to reduce power consumption. Generally my program's structure is:

    Code ( (Unknown Language)):
    2. void interrupt (){some code here}
    4. void main()
    5.     {initialisation of ports, LCD, etc.;
    6.     for (;;)
    7.         {some code here;
    8.          if(PORTB.B7==1){asm{sleep
    9.                                        nop
    10.                                        }
    11.                                 }
    12.           some code here;
    13.           }
    14.      }
    In the beginning program must initialise LCD, variables in RAM, microcontroller ports etc. This must be onlyon first turn on of microcontroller. After that the FOR statement begins (endless loop, in which microcontroller tests mains supply voltage presence. If blackout occured (there is level 1 on PORTB RB7 input) microcontroller must go to sleep mode. Wake up must occur on TMR1 interrupt. My question is: where the program will be executed wake up from sleep? I thought that program after interrupt will execute next line after sleep command. But in my case program after wake up from sleep returns in the very begining - initialisation. Maybe on sleep RAM goes to unknown state?
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    Put the initialization into a function of it's own. After initialization, set a variable/bit, then in main loop test for that bit being true, if not, initialize, if so, skip init.

    On wakeup, it will resume in the interrupt() routine.
  3. ecka333

    Thread Starter Active Member

    Oct 1, 2009
    Yes, i know that on wakeup microcontroller will resume in the interrupt routine but where program counter will be after interrupt routine?
  4. AlexR

    Well-Known Member

    Jan 16, 2008
    What happens on wake-up depends on what event causes the wake-up and whether interrupts are enabled.
    If the wake-up was caused by a reset (MCRL going low) then the PIC resets and begins the program from the beginning.
    If the wake-up is caused by a watchdog timer event or an interrupt event and the global interrupts (GIE bit) are disabled then the program just carries on from where is was when it went to sleep.
    If the wake-up is caused by an interrupt and the global interrupt bit is enabled then the PIC executes the instruction immediately after the sleep instruction and jumps to the ISR (interrupt service routine) in the normal manner. The return from interrupt in your case will come back to the instruction after the nop.