TUTORIAL 3: HOW TO CONFIGURE THE PWM OF THE PIC16F877A.


 Still on our tutorial on the design of a micro based sine wave inverter, Today we will be discussing on how to configure the PWM module of the PIC16F877A.
At this point, I expected you to have gotten the necessary software I earlier spoke about in Tutorial 1, Proteus simulating software (Proteus 8.0), microchip IDE, and XC8 compiler by microchip. If not, Search for Proteus 8.0 on Google, you will find so many entries through which you can download for free. You can download the microchip IDE, and XC8 compiler on microchip website, WWW.MICROCHIP.COM . It is all for free.

I hope you are doing more studies, apart from the one been taught here. If you are not, I encourage you to start doing so. Believe me, everything cannot be taught here. Do not misquote me, I will do my possible best to stretch the explanations and the tutorial, but with all this, you still have to do your own personal researches, then ask questions on the areas you need clearer and better understanding. You have to be consistent with your research.

Back to the Tutorial. Configuring the PWM of the PIC16F877A is as simple as anything. The CCPXCON is the register necessary for setting up the PWM of the chip amongst other things. In fact, the CCPXCON controls the CCP module, Where X can be 1 or 2. CCP module stand for (Capture, Compare and Pulse width modulation) module.
I advise you download the PIC16F877A data sheet and navigate to page 58, there you find a comprehensive explanation of how the register works. You just have to do your own personal research. I advise you get it before continuing reading. The size of the one I have, is about 2.09MB.

In the datasheet, you will find out that, bit 0-3 is responsible for configuring the module for PWM function. Wondering what is the meaning of the word bit?

My tutorial on Embedded C will soon commence, but I am having a feeling that it will not be for free. I need only those who are so passionate about learning Embedded C.
You can sacrifice anything for your passion. I will soon tell you my story, of how I sacrificed, just to learn Electronics and Embedded C and I can say, it really worth it. I am so grateful that I sacrificed then.
This material I am giving for free, I spent months working on it’s practical implementation. Nobody wants to teach how to build an inverter for free, especially a Sine wave inverter. So, I had to do the research all by myself and even teach it for free. I can assure you, the tutorial will be highly loaded with examples and comprehensive explanations. It will soon be available.

I am sure at this point, you have questions. Type your questions as a comment, and I will give a reply.

In Pulse Width Modulation mode, the CCPx pin (capture, compare, PWM) where x, is either 1 or 2, produces up to a 10-bit resolution PWM output. Since the CCP1 pin is multiplexed with the PORTC data latch, the TRISC<2> bit must be cleared to make the CCP1 pin an output and since also the CCP2 pin is multiplex with the PORTC data latch, the TRISC<1> bit must be cleared to make the CCP2 pin an output. If you check the datasheet of the chip, the CCP pins are on the PORTC, and PORTC is controlled by TRISC. what does TRISC means? It means TRI State. The pins of the microcontroller can function in three states.



According to the datasheet, bit 0-3 of the CCPXCON register is set to 11xx, where x can be either 1 or 2.
Below shows the different bit selection to achieve any function on the CCP module.

bit 3-0    CCPxM3:CCPxM0: CCPx Mode Select
                bits

   0000 = Capture/Compare/PWM disabled  
                (resets CCPx module)
   0100 = Capture mode, every falling edge
   0101 = Capture mode, every rising edge
   0110 = Capture mode, every 4th rising edge
   0111 = Capture mode, every 16th rising edge
   1000 = Compare mode, set output on match
                 (CCPxIF bit is set)
   1001 = Compare mode, clear output on match
                 (CCPxIF bit is set)
   1010 = Compare mode, generate software
                 interrupt on match
                 (CCPxIF bit is set, CCPx pin is  
                  unaffected)
   1011 = Compare mode, trigger special event
                 (CCPxIF bit is set,  CCPx pin is
                  unaffected);CCP1resets TMR1; CCP2
                  resets TMR1 and starts an A/D  
                 conversion (if A/D module is enabled)
  11xx = PWM mode


So, after setting the CCP module for the PWM function, the next step is to set the pulse width modulation period and the duty cycle.

Below, shows the formula for setting the PWM period

 PWM period = [(PR2) + 1] • 4 • TOSC • (TMR2 prescale value).
         PWM frequency is defined as 1 / [PWM period].

I need you to place a careful attention from this point.

In the formula given above, there are three names; PWM period, PR2, TOSC, TMR2 prescale value.

PWM period: what does PWM period means, This means the time in secs required to complete a cycle of the PWM.

                     


PR2: The PR2 determines when the Timer2 interrupt occurs. The maximum value Timer2 can hold in the 16F series is 8 bits, which is 256, which after it overflows i.e. returns to zero, which after an interrupt occurs, if it is enabled. But if you want the timer to overflow at any value you wish, it has to be specified in the PR2 register. For example, if you want the Timer to overflow at decimal 100, all you have to do is to equate PR2 to 100,PR2=100. The values of TMR2 and PR2 are constantly compared and the TMR2 register keeps on being incremented until it matches the value in PR2. When the match occurs, the TMR2 register is automatically cleared to 00h.




TOSC: This means period of oscillation. For example, if the crystal oscillator used to clock the microcontroller is rated 4MHZ, then the TOSC is 1/4MHZ = 0.25us.



TMR2 prescale value: This is used to slowdown the rate at which the Timer is incremented. It is mainly used for the division of the clock input frequency. Timer 2 has a prescale value ranging from 1:1, 1:4 to 1:16.



In the next tutorial, I will be explaining more on how to configure the PWM of the PIC16f877A.
Please, study this tutorial well and Do your personal researches. Watch out for Tutorial 4. It will be available on Tuesday, 24th of May.

Look forward to the Embedded C Tutorial, It will start Tuesday, 24th of May.

Thanks for reading. Please share and tell your friends about it.
Your comments, questions and suggestions are all highly welcomed.

Thank you.

You can forward your questions to my email, trinnexelectronics@gmail.com or send it as a comment on the blog.


Comments