And we have fading LEDs. Hello and in this video I'm going to be covering how to use
the PWM module. First let's go over the hardware connection. As per usual my LEDs are connected
to port B and then RC1 as my PWM output on CCP2. Now the LEDs are connected to transistor and then
multiplexed to the single PWM pin. Let's have a look at the datasheet. Now the first register we
need to go over for the PWM is timer 2. This is used to determine the timings of the PWM.
The postscalar we are not going to use but we are going to set it anyways to a 1 to 1. Then
timer on which is going to use to start the PWM. Timer prescaler is going to be used to determine
the clock frequency of the timer and I'm going to use it as a 1 to 1 as well and let's have a look
at the block diagram. Then in the block diagram we have our prescalar. Then timer 2 and then a
comparator and then a comparator goes straight to the PWM module. So it bypasses the post scalar.
That is why we are not using it. Now let's have a look at the PWM module. In this PIC we are going
to use CCP2CON as CCP1 is implemented as an enhanced module on the 40 pin and 44 pin
devices. As this is a 40 pin device. Now in the register we have 7 and 6 as unimplemented and
then our least significant bit to control the duty cycle of the PWM is located in bit 5 and 4
and then most significant bits is in the CCPR2L register and then in our CCP2 mode select we
are going to select PWM mode and now in the PWM we have to configure the CCP2 pin as a output
and then it is multiplexed in the configuration bits to Port C RC1. Our PWM period is done by
writing to the PR2 register and this is the formula for calculating the PWM period in time
scale in seconds. Just one over the PWM period we get the frequency. I'm going to target for a
100 kilohertz frequency and the PWM duty cycle is calculated using this formula. Now back to the PWM
period the value we are going to require here is PR2. Since all other values are known. Let me
refer to my paint. This is the formula described in the datasheet this is the manipulated and we
get a value of 19 for PR at the PWM frequency of 100 kilohertz with an oscillator frequency
over here at 8 megahertz and then the prescaler set to a 1 to 1. Now let's get to coding. A quick
summary of what's in the code. The oscillator is set to 8 megahertz. TRISB is it as output. Then
the entire B port is driven high to switch on the transistor. Now the first thing we want to do is
set the CCP pin on RC1 as output so TRISC bits member RC1 equals to 0. Next what we do
is we setup timer2. We say T2CON bits member T2OUTPS equals to 0. Too set the postscaler as a 1
to 1. Then we set the prescaler bit. So we copied T2CON bits with a T2CKPS is equal to 0.
Too set the prescaler as 1 to 1. So we start with the PR2 register. PR2 is equals to
19 that we calculated earlier. Then we add in CCP2CON bits member DC2B is equal to 0 and then
copy CCP2CON. We say CCP2 mode is equals to 0xC0 to set it in PWM. Now a register I didn't cover
in the datasheet is CCPR2L equals to 0. Now this is your most significant bit for the
duty cycle. Then we end off the configuration by enabling timer2. Copy-paste the T2CON
bits. Then we say TMR2ON equals to 1. That's the configuration for the PWM. Next
we go to the top and we add a function of type void we call it PWM underscores set pass it a value
of uint16 and we call it duty. Now next what we do is. We copy the least significant bits of the
duty cycle. Past it here. We type cast it to uint8 and we say duty and 0x0003
to parse out the... to parse out the two least significant bits of the duty cycle and then
next we copy the most significant bits. We say as equals to type cast uint8 and it's
duty left shift by 2. Now for the demonstration code. I am going to fade some LEDs. So I'm going
to add two variables of uint16 PWM count equals to zero and uint16 PWM mode equals to 0.
Now PWM count will be the duty cycle and PWM mode will be either up counting or down counting but
first let's just demonstrate up counting. First we add in PWM set and the variable PWM count
passed to it and after PWM set. We increment PWM count and if PWM count greater or equal to 0x03FF.
Which is the maximum of a 10 bit register. PWM count is reset to 0. Then we add a delay... of
25 milliseconds between each count. Just so we can see the LEDs fading. Program the PIC. So I
found a mistake the PWM mode should be 0C and not C0 and I'm going to adjust the
reset to 4F otherwise the PIC takes too long to reset the values. Programmed the PIC. Now as
you can see the LEDs fade in. On the up counting and then reset to 0. Also judging by the volt
meter showing that the voltage increases. Now next what I'm going to add in is. Is a fade in
and out. So at max brightness fade down and at min brightness fade in. Now to implement the
fade in and out. We change PWM count to PWM mode equals to 1 in our if statement. Then we add if PWM
count is equivalent to zero PWM mode equals zero. Then we say if PWM mode is equivalent to
0 increment the PWM counter and above set PWM we add in if PWM mode is equivalent 1
decrement the counter. Now we program the PIC. As you can see the LEDs are fading in and out.
And that's a basic introduction of using the PWM module on a PIC. Any parts that I have used will
be linked down in the description. Any code that I've written it will be uploaded to my Git.
A like share comment and subscribe. Would be greatly appreciated. Thank you have a nice day