Pic microcontroller SLEEP mode

Thread Starter

ecka333

Joined Oct 1, 2009
76
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:

Rich (BB code):
void interrupt (){some code here}
 
void main()
    {initialisation of ports, LCD, etc.;
    for (;;)
        {some code here;
         if(PORTB.B7==1){asm{sleep
                                       nop
                                       }
                                }
          some code here;
          }
     }
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?
 

thatoneguy

Joined Feb 19, 2009
6,359
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.
 

Thread Starter

ecka333

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

AlexR

Joined Jan 16, 2008
732
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.
 
Top