Tuesday 3 December 2013

PIC16F628A PWM code + Proteus simulation

This post provides the PWM code (using C language) and Proteus simulation for PIC16F628A microcontroller ( written using MPLAB and HI-TECH C compiler ). There are many uses for PWM signal, e-g you can control dc motor speed using PWM. CCP is the name for the built in PWM module in PIC16F628A. There is one built in PWM module available, namely CCP1. This code is using CCP1 module to generate PWM. You can download the complete project code andProteus simulation from the 'Downloads' section at the bottom of this page.

It is assumed that you know how to blink an LED with PIC16F628A microcontroller. If you don't then please read this page first, before proceeding with this article.

The following diagram (made in Proteus) shows the circuit diagram.
//
Figure 1.   PIC16F628A PWM generation circuit
A crystal of 20MHz is used here with PIC16F628A. As we know that any PIC microcontroller has an architecture which executes an instruction in 4 CPU cycles, hence this 20Mhz crystal makes this PIC16F628A run at 5MIPS (Million of instructions per second). In the above figure, it is clear that a PWM of approximately 4.9kHz frequency with 50% duty cycle is being generated at RB3/CCP1 pin.

Code

The code for main function is shown below.
//
Figure 2.   Main code for PC16F628A PWM
In the main function, PWM is initialized using the 'InitPWM()' function. Then PWM duty cycle is set to 50% ( You can verify this value from Figure 1 ). After that microcontroller is free and built in CCP1 module automatically generates desired PWM frequency. When you need to change PWM duty cycle, then just call SetPWMDutyCycle() function and change the duty cycle as per requirement. You can pass a value of 0(0% duty cycle) to 1023(100% duty cycle) in the argument of SetPWMDutyCycle() function.

The code for PWM functions is shown below.
///
Figure 3.   PWM function code for PIC16F628A
InitPWM() function initializes the PWM module CCP1 to generate PWM signal at 4.88KHz using 'Timer 2'. RB3 ( Pin 9 ) gives the output of the CCP1 module. CCP1 module has 10bit PWM resolution. 'SetPWMDutyCycle()' is the function which is used to set the duty cycle value for the PWM of CCP1. This function has 1 input argument which is the required value of the duty cycle of PWM. Write 0 here for 0% duty cycle and write 1023 here for 100% duty cycle. For example, to generate a PWM with 50% duty cycle write 'SetPWMDutyCycle( 512)' in your code.

You can calculate PWM frequency using the formula _XTAL_FREQ/(4*(PR2+1)*Prescalar). Where _XTAL_FREQ is the value of crystal frequency, in the above code it is defined to be 20000000. PR2 is Timer2 register, it's value is defined to be 0xFF ( In the InitPWM() function ). You can set PR2 value in between 0 to 0xFF only. Prescalar value is currently set to be 4 in InitPWM() function. Putting all these values, we get a PWM frequency of 20000000/(4*256*4) = 4.88kHz (This value can be verified from Figure1). You can modify these parameters to change PWM frequency.

As an example, You can use this PWM code to control dc motor with PIC16F628A controller. You will need some sort of dc motor driver circuitry in between PIC16F628A controller and the dc motor. Other than that, using this simple PWM controlling code, you can control dc motor speed with PIC16F628A controller easily. You can leave your comments in the comment section below.

Downloads

IWM code using PIC16F628A was compiled in MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10. To download code and Proteus simulation click here.

1 comment:

  1. If you had financial problems, then it is time for you to smile. You only need to contact Mr. Benjamin  with the amount you wish to borrow at the low rate of 2% ROI and the payment period that suits you and you will have your loan  within three working days. I just benefited for the sixth time a loan of 700 thousand dollars for a period of 180 months with the possibility of paying before the expiration date. Mr Benjamin has been helping me with the loan.Make contact with him and you will see that he is a very honest man with a good heart.His email is  247officedept@gmail.com   and his WhatApp phone number is + 1-989-394-3740 .

    ReplyDelete