Indicating that the sleep mode has triggered. Hello and in this video I'm going to be covering
the watchdog timer. The schematic for this tutorial is exactly the same as the previous
one with the comparator and interrupt based programming. So the watchdog timer is an
independent timer from the PIC itself. It does not use any of the peripherals and is independent of
your sleep mode. It only requires the internal oscillator to be running. The nominal period of
the watchdog timer is 4 milliseconds with the same stability as the internal oscillator. Now
the watchdog timer is multiplied by a 16-bit post scaler and any output from the watchdog
timer post scaler is selected by a multiplexer controlled by the configuration register H2.
We have a maximum of 131.072 seconds. Which is 2.8 minutes for a period of the watchdog
time reset. Any sleep instruction we send or clear watchdog timer instruction we send will
reset the watchdog timer counter. The watchdog timer is mainly used to wake the microcontroller
from sleep. Now here we have a functional diagram of the watchdog timer. You see it
first gets divided by 128 and then it goes to the programmable prescaler. Which you preset with the
configuration bits. Then we have the reset change. It's also reset if you have a clock failure or a
clock source change. Here you can see it comes from sleep mode as soon as it overflows. Which
is referred to as the reset it wakes up from any power manage mode it is currently in. Then next
we have the WDTCON register or watchdog timer control register and it has a single bit in
it which is bit 0. Which is referred to as the software watchdog timer enable control bit. If
it's 1 turn on the watchdog timer. If it's 0 turn off the watchdog timer. Now we are not going to
use this bit since this bit has no effect when WDTEN is enabled. Which I am going to enable
via the other configuration bits. Then we have the configuration register 2 high which controls
the post scaler of the watchdog timer. I'm going to use a 1 to 64 post scalar for about a 1 second
timing and then we have the watchdog enable bit if it's 1 enable the bit. If it's zero disabled
the bit. Now this is a bit different in code but we'll cover that when we get there. Now we have
a block diagram of how the watchdog timer works in the oscillator configuration chapter of the
datasheet. So we first have an oscillator. Which is an 8 MHz source and then it can output 31
kHz internal RC output and that directly clocks the watchdog timer. So this gives you the
ability to have your CPU power down and your peripherals power down while the watchdog timer
is running in the background. Ok now let's look at the timings of the watchdog timer. So the watchdog
timers period starts at 4 milliseconds and ends at 131.072 seconds. Now what I'm going to use is
a post scalar of 64 to give me an approximate of 1 second. Now let's have a look at the code. Ok
now the color I'm using is the exact same code from the last video with minor modifications.
Now to enable the watchdog timer we go to the conbits dot H folder. We open up the memory views and
we go to configuration bits. Now in configuration bits all we need to do is turn the watchdog timer
on in this tab and then we set the watchdog timers prescaler to 64 or you can go to the
other side and then use the drop down menu which corresponds better to the datasheet. The watchdog
timer is enabled and it's set to a 1 to 64 post scalar and then you can click generate code to
output and then we just look for the watchdog timer configurations. Which is over here. So when
you set configuration that's it's not exactly like setting a register. Like you can see the watchdog
timer enable is equals to on and then the post scalar is set to the value of 64. So more human
readable. Now to the main code I still have the sleep mode set to be in the RC State with your
CPU is off but your peripherals are running in the background. Then next thing I do is I disable
timer 2 because it is not necessary anymore to trigger the ADC reads. The main problem for using
the watchdog timer is I couldn't use the time because the timer only had a maximum of 1
millisecond intervals for its interrupts. So it's much more useful to use it likes like I'm using
it for every second and then another major change is your delay functions turn from say underscore
underscore delay MS to delay WDT which is delay watchdog timer if you use the standard delay you
can't get away with it with short periods but long periods like this for two seconds just hangs the
CPU and it doesn't want to go anywhere I was continuously getting stuck at this point. Then
here you can see timer 2 is off and then the main change here in the main loop is if there's no ADC
data from the interrupt start an ADC read and then we have ADC done. Which just prints out the value
of the ADC and then just a LED toggling on and off to show the intervals at which the code is
running and then we stop the timer 2 from going on. Now the other things we can do is we can swap
between deep sleep mode. Which is idle en equals to 0 and RC idle mode. Which is idle en equal
to 1 but I found for my purposes it work because I'm constantly monitoring the comparator pins and
that only triggers when idle en is equal to 1. Here you can enable the deep sleep modes and
check it out for yourself from the GitHub link and then we just enter sleep mode and then I just
took away the code that runs inside the timer 2 low priority interrupt and this code stays pretty
much the same. So an execution example of this code would be. When the code starts start an ADC read go
to or see idle mode. The ADC read trigger goes to the main loop sees ADC done is true. We go to
if and then we print out the voltage value to the UART. We set ADC done as false. We toggle the LED
to change from a previous state and then sleep and then this will wait the full 1 second for
the watchdog timer to time out and then we repeat the process as like... as from boot where we go ADC
false. Trigger a read. Go to sleep. Wake when ADC is done and then print ADC value. Toggle LED. Go to
sleep for one second and then one other thing is the comparator is continuously monitored. So if
there's a trigger on a comparator it will swap the two LEDs to indicate when the comparator
is triggered. This interrupt will continuously trigger if... if you set between the voltage trigger values.
Now let's look at the practical demonstration. Okay now you can see the comparators in the high
state with the red LED being on and the green LED indicating that the sleep mode has triggered. So
you can see they're flashing about every 1 second. When we take the comparator low. Okay
now the comparator is in low state with the LED triggering every second and I'm going to put the
voltage between the two states of the comparator and you'll see the change in the LED... The comparator
interrupts continuously waking the CPU. Now you can see the comparator in between two states. And
that's a basic introduction to using the watchdog timer to wake up microcontroller from sleep. Any
code that I've written will be linked on my Git. Any parts that I've used will linked down in the description.
A like share comment and subscribe would be greatly appreciated. Thank you have a nice day.