Вы находитесь на странице: 1из 12

PIC HOW-TO GUIDE

Interfacing PWM with PIC16F877A

Contents at a Glance
PIC16F/18F Primer Board .................................................3 PWM (Pulse Width Modulation) ......................................3 Interfacing PWM .............................................................4 Interfacing PWM with PIC16F877A...................................5 Pin Assignment with PIC16F877A .....................................5 Circuit Diagram to Interface PWM with PIC16F877A .........6 Source Code ....................................................................6 C Program to generate PWM in PIC16F877A.....................7 Testing the PWM with PIC16F877A ..................................9 General Information ...................................................... 10

Join the Technical Community Today! http://www.pantechsolutions.net

PIC16F/18F Primer Board The PIC16F/18F Primer board is specifically designed to help students to master the required skills in the area of embedded systems. The kit is designed in such way that all the possible features of the microcontroller will be easily used by the students. The kit supports in system programming (ISP) which is done through USB port. Microchips PIC (PIC16F877A), PIC16F/18F Primer Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 8-bit Microcontrollers. PWM (Pulse Width Modulation) Pulse width modulation (PWM) is a powerful technique for controlling analog circuits with a processor's digital outputs. PWM is employed in a wide variety of applications, ranging from measurement and communications to power control and conversion. Join the Technical Community Today! http://www.pantechsolutions.net

Interfacing PWM Figure 1 shows four different PWM signals. One is PWM output at a 25% duty cycle. That is, the signal is on for 25% of the period and off the other 75%. Next shows PWM output at 50%, 75% and 100% duty cycles, respectively. These three PWM outputs encode three different analog signal values, at 10%, 50%, and 90% of the full strength.

Fig. 1 PWM Outputs

Join the Technical Community Today! http://www.pantechsolutions.net

Interfacing PWM with PIC16F877A We now want to generate a PWM in PIC16F/18F Primer Board at a particular frequency. Pulse Width Modulation is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. Pin Assignment with PIC16F877A
PWMs OUTPUTS PWM1 PWM3 PIC16F Lines PORTC.1 PORTC.2 Connections

There are no connections on the board

Output: Connect a CRO and measure the pulse width and duty cycle

Join the Technical Community Today! http://www.pantechsolutions.net

Circuit Diagram to Interface PWM with PIC16F877A


MCLR
VDD 0.1uF C43

11 32

U22

MCLR/Vpp

VDD VDD

VSS VSS

12 31

RC1/T1OSI/CCP2 PIC16F877A C42 22pF 13 Y 11 10 Mhz 14 U14 OSC1/CLKIN RC2/CCP1

16 17

PWM1 PWM2

22pF C41

OSC2/CLKOUT

Source Code The Interfacing PWM with PIC16F877A program is very simple and straight forward, which generates a pulse pattern in a particular frequency. An ADC signal is used to varying the duty cycle of PWM signal. The C program is written in Mplab software & it executed with Hi-Tech C compiler.

Join the Technical Community Today! http://www.pantechsolutions.net

C Program to generate PWM in PIC16F877A


*************************************************************************************** Title : Program to generate PWM *************************************************************************************** #include<pic.h> //Define PIC registers

__CONFIG(0x3f72); //Select HS oscillator, Enable (PWRTE,BOREN), //Disable (CPD,CP,WDTEN,In-circuit Debugger) #define XTAL #define PWM_Freq #define TMR2_PRE #define PR2_Val #define Duty_Cyc unsigned int i; void PWM_init(void); void PWM_change(unsigned int); void DelayMs(unsigned int); void main(void) { PWM_init(); while(1) { i=0; PWM_change(i); DelayMs(10); while(i<PR2_Val) { i=i+1; 10000 1 16 //10Mhz=10000Khz //1Khz PWM frequency //Timer2 Prescale

((char)((XTAL/(4*TMR2_PRE*PWM_Freq))-1)) //Calculation for Period register PR2 (1Khz) PR2_Val*2

Join the Technical Community Today! http://www.pantechsolutions.net

PWM_change(i); DelayMs(200); } } } void PWM_init(void) { TRISC2=0; //PWM channel 1 and 2 configured as output TRISC1=0; PORTC = 0x00; CCP1CON=0x0c; //CCP1 and CCP2 are configured for PWM CCP2CON=0x0c; PR2=PR2_Val; //Move the PR2 value T2CON=0x03; TMR2=0x00; TMR2ON=1; } void PWM_change(unsigned int DTY) //Duty cycle change routine { CCPR1L=DTY; //Value is between 0 to 255 CCPR2L=DTY; } void DelayMs(unsigned int Ms) { int delay_cnst; while(Ms>0) { Ms--; for(delay_cnst = 0;delay_cnst <220;delay_cnst++); //delay constant for 1Ms @10Mhz } } //Delay Routine //Timer2 Prescale is 16 //Turn ON timer2

Join the Technical Community Today! http://www.pantechsolutions.net

To compile the above C code you need the Mplab software & Hi-Tech C Compiler. They must be properly set up and a project with correct settings must be created in order to compile the code. To compile the above code, the C file must be added to the project. In Mplab, you want to develop or debug the project without any hardware setup. You must compile the code for generating HEX file. In debugging Mode, you want to check the port output without PIC16F/18F Primer Board. The PICKIT2 software is used to download the hex file into your microcontroller IC PIC16F877A through USB port. Testing the PWM with PIC16F877A Give +12V power supply to PIC16F/18F Primer Board; the PWM port line is connected in PIC16F/18F Primer Board. When the program is downloading into PIC16F877A in Primer Board, the PWM output is generating at a particular frequency.

Join the Technical Community Today! http://www.pantechsolutions.net

If you are not reading any PWM output, then you just check the jumper connections. Otherwise you just check it with debugging mode in Mplab. If you want to see more details about debugging just see the videos in below link. How to create & Debug a Project in Mplab using PIC16F using Hi-Tech Compiler. General Information For proper working use the components of exact values as shown in Circuit file. Wherever possible use new components. Solder everything in a clean way. A major problem arises due to improper soldering, solder jumps and loose joints. Use the exact value crystal shown in schematic. More instructions are available in following articles, User Manual of PIC16F/18F Primer Board. Create & Debug a project in Mplab using PIC16F877A.

Join the Technical Community Today! http://www.pantechsolutions.net

Did you enjoy the read?


Pantech solutions creates information packed technical documents like this one every month. And our website is a rich and trusted resource used by a vibrant online community of more than 1, 00,000 members from organization of all shapes and sizes.

Join the Technical Community Today! http://www.pantechsolutions.net

What do we sell?
Our products range from Various Microcontroller development boards, DSP Boards, FPGA/CPLD boards, Communication Kits, Power electronics, Basic electronics, Robotics, Sensors, Electronic components and much more . Our goal is to make finding the parts and information you need easier and affordable so you can create awesome projects and training from Basic to Cutting edge technology.

Join the Technical Community Today! http://www.pantechsolutions.net

Вам также может понравиться