Hello and in today's video I'm going to be covering how to use the sleep function on a PIC
microcontroller. Now let's go over the hardware connections. I have the entire B port connected
to LEDs. Then a USB to Serial connected to the UART of the PIC and then a button connected to
the capture module with grounding resistors and then LEDs are power via transistor with the the B
Port pins connected to the base of the transistor and in a current limiting resistor. Now let's have
a look at the datasheet. Now the PIC has three power states with your normal state as run. Where
your CPU is on and where the peripherals are on. Then you have idle state where the CPU is off but
the peripherals are still running. Then you have sleep mode with the CPU and the peripherals off.
Now your run mode as stated in this datasheet typically around 11 micro amps and then idle
mode is 2.5 micro amps and sleep mode being at 100 nano amps and then there's these two depending on
what your clock source is. You have these... the 900 nano ampere range. When you're running off of time
oscillator 1 and then the 1.4 micro amp typically if you run for the watchdog timer. Unfortunately
I don't have the equipment to test these values, if they are accurate. Now the first register we
have to have a look at is the oscillator control register. Now in the oscillator control register
we use all of these bits. The most important ones are your idle enable bit and then your
internal oscillator block select bit. This will determine which type of sleep mode you go into
but for this example I'm going to use the internal oscillator block and idle en I'm going to show
both modes for it. Like for say if your idle en is 0. Then it enters the deep sleep mode and
then your idle 1. Then it enters the sleep mode. Where your clock is off but your peripherals are
still running in the background. Then we go to the section of selecting a power mode. The power modes
highlighted here or according to the idle en bit of the oscillator control register and the SCS bit
of the oscillator control register. Now across all the register your sleep mode is global and then
depending on your configuration you have these four different idle states. Your normal run state
for the current configuration I have is going to be the RC run state with the CPU being clocked and
the peripherals being clock. Again for the current configuration. The idle state will have the CPU
off and then the peripherals being clocked. Then we go further down. Then a description of the RC idle mode the CPU
is disabled but the peripherals are continued to being clocked from the internal oscillator
block using the internal multiplexer and here they state that the mode is entered by running the
sleep instruction. This will show in in code and then exiting an idle sleep mode. We can exit the
sleep mode and one of the following ways. The sleep mode can be exited with one of the following modes
which is either by an interrupt a hard reset or the watchdog timer being timed out. For this
example I'm going to use the interrupt. Now here they describe the process of exiting sleep mode
via interrupt. Any interrupt can cause the device to exit sleep mode. Now this is subjective since
if you are using a peripheral to exit sleep mode it needs to be in the idle state and not the deep
sleep state. And here they just tell you you have to have the interrupts enable bit set otherwise
you can't use... otherwise it will not work. They say the exit sequence is started by a fla... by an
interrupt flag being set. Here they just state you have to have your global interrupts enabled
before you enter sleep mode if you want to exit via interrupt. I have tested this. The PIC just
goes into a hang state where it does nothing and then if you start the exit sleep mode there is
a fixed delay between exiting sleep mode and code execution. Now the sleep is about 2 to 6
oscillator cycles but from what I can determine is this this is a fixed delay this in the PIC itself
and you don't need to do it via software. Then they describe how the watchdog timer is and exiting
by a hard reset. Now let's have a look at the code. Ok another code is a basic setup I just have
these two macros for reference and then the 2 high and low priority interrupts. Then I have a print buffer
to print out whatever I want and then a function to do running LEDs on the LATB register. So we're
just going to have the LEDs flicker from LATB0 to LATB7 and then repeat the process. Now here's the
important bit which is the idle en bit. When this idle en bit is set to 0. We enter deep sleep mode
with the whole PIC being off and then I set the oscillator to 8 megahertz. I set the... the entire
B port as output. Set the B port as low. Set a caption compare and as an input. So I'm going
to use this only as a external interrupt. So basically the same as INT1 and 0 and of the UART
initialized at 9600 baud as per usual. The caption compare module set as capture mode. Then the
timer setup which you can have a look at one of my previous videos and then the interrupt setups for
priority on the timer and capture timer 3 as low priority and capture as high priority. Then I do
the two enables for it and then the base interrupt enables. Then I have a startup delay. Printout tell
me my program is started. Then a variable which counts the number of times the running led code
has run. Then I turn timer 3 on and then in the infinite loop I run the running LEDs code.
I increment the sleep counter. I print out the current value of the sleep counter and then if the
sleep counter is equals to 2 turn LATB off because when you enter sleep mode it keeps the state of
the current pins. So if you have one of the pins high the LED will stay on and if the LED stays
on it's sort of defeats the purpose of having the PIC in sleep mode because that LED actually
draws... draws more current than the total of the whole PIC and then I set the sleep timer to zero
and then I call the sleep function. Now in some cases of some microcontrollers entering sleep
mode actually powers down the RAM for even more power saving. So it might be actually necessary
to store any important variables into the EEPROM or flash of the microcontroller if it's the case
where it powers down the RAM and then we enter sleep mode and then we have our two interrupts
which actually do nothing but they're there to wake up the PIC. Now I've pre-programmed the PIC.
So let's have a look at the code execution in the physical. Now the PIC is currently in deep sleep
mode. Now when I press the button it will run twice and then re-enter sleep mode and then we
run it again. Now to enter their idle mode instead of deep sleep mode we change the idle en bit to
1. Now we quickly program the PIC. Now as you can see the pic is running continuously. Now what you
don't see which I'm displaying on the UART is that the PIC is actually running an entering
sleep mode but on every interrupt trigger of timer 3 it wakes up again and executes code. And that's
a basic introduction to using sleep mode on a PIC microcontrollers. Any hardware that I use
will be linked down in the description. Any code that I've written will be shared on my
Git. A like share comment and subscribe would be greatly appreciated. Thank you have a nice day.