Professional Documents
Culture Documents
PWM Interfacing With PIC16F877A Primer
PWM Interfacing With PIC16F877A Primer
PWM Interfacing With PIC16F877A Primer
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
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.
OUTPUTS
PIC16F Lines
PWM1
PORTC.1
PWM3
PORTC.2
Connections
Output: Connect a CRO and measure the pulse width and duty cycle
MCLR
12
31
VDD
VDD
VSS
VSS
1
MCLR/Vpp
U22
11
32
0.1uF
C43
RC1/T1OSI/CCP2
PIC16F877A
C42
22pF
22pF
13
Y 11
10 Mhz
14
RC2/CCP1
16
17
PWM1
PWM2
U14
OSC1/CLKIN
OSC2/CLKOUT
C41
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.
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
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;
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;
//Timer2 Prescale is 16
//Turn ON timer2
}
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;
//Delay Routine
while(Ms>0)
{
Ms--;
for(delay_cnst = 0;delay_cnst <220;delay_cnst++);
//delay constant for 1Ms @10Mhz
}
}
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.
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.