Professional Documents
Culture Documents
Microprocessors and Microcontrollers Lab: Title: Components
Microprocessors and Microcontrollers Lab: Title: Components
Components:
Breadboard
LED
Resistors kit.
Capacitors Kit.
Jumper Wires Pack.
LM7805 Voltage Regulator (5v).
Crystal Oscillator.
PICkit2 or 3 Programmer.
9v Battery or DC Power Supply
Software:
MPLABX IDE
Proteus
Introduction:
. PWM
Signal .
PWM stands for Pulse Width Modulation. This feature provides microcontrollers by a mean
of, seemingly, outputting analog values of voltage between (0-5)v. Instead of outputting digital
values that are either Low (0v) or High (5v). The PWM signal plot looks something like as
shown in the diagram below. The plot for this signal captures two features: (Frequency & Duty
Cycle). And we’ll discuss each of them in more detail hereafter.
PWM Channel Port Pin Control Register Duty Cycle Period Register
Register
PWM Working
Before going to the PIC PWM module lets understand few terminologies wrt below image:
Duty Cycle: The percentage of time for which the PWM signal will be ON/HIGH. $$DutyCycle
= (Ton/(Ton+Toff))*100 = (Ton/Period) * 100 $$
Below diagram shows the PIC16f877a PWM block diagram and output signal generation.
PIC uses TIMER2 for generating the PWM signals. Lets relate the above concepts with the
PIC registers.
Period:PR2 register is used to specify the PWM Period. The PWM period can be calculated
using the following formula. $$PWM Period = [(PR2) + 1] * 4 * TOSC * (TMR2 Prescale
Value)$$
DutyCycle:CCPR1L and CCP1CON<5:4> are used to specify the DutyCycle.The CCPR1L
contains the eight MSbs and the CCP1CON<5:4> contains the two LSbs.
PWM Signal Generation: Once the PWM is configured and Timer2 is enabled, TMR2
starts incrementing depending on the prescalar. Once the TMR2 value is equal to
dutyCycle(CCPR1L+CCP1CON<5:4>) the PWM pin will be pulled LOW. The timer still
continues to increment till it matches with the period PR2. After which the PWM pin will
be pulled HIGH and TMR2 is reset for next cycle.
Register Description
CCPxCON This register is used to Configure the CCP module for Capture/Compare/PWM
opertaion.
CCPRxL This register holds the 8-Msb bits of PWM, lower 2-bits will be part of CCPxCON
register.
TMR2 Free running counter which will be compared with CCPR1L and PR2 for
generating the PWM output.
The below table shows the registers associated with PIC16F877A PWM.
Register Configuration
The below table shows the registers associated with PIC16f877a PWM.
7 6 5 4 3 2 1 0
Applications Of PWM:
Following are some applications:
TASK
Learn how to use CCP modules to generate PWM signals with a desired frequency
of 2kHz and variable duty cycle. We’ll use the output PWM signal to control the
brightness of a small LED in this LAB. The duty cycle should be gradually
increasing from 0 up-to 100% then it should start to gradually drop back to 0%
and so on.
CODE:
#include <xc.h>
#include <stdint.h>
#include "config.h"
void PWM1_Set_Duty(uint16_t);
void main(void)
CCP1M3 = 1;
CCP1M2 = 1;
PR2 = 124;
T2CKPS0 = 1;
T2CKPS1 = 0;
TMR2ON = 1;
uint16_t DC=0;
// Write The System's Main Routine
while(1)
{
while (DC<500)
{
PWM1_Set_Duty(DC);
DC++;
__delay_ms(2); }
__delay_ms(100);
while(DC>0)
{
PWM1_Set_Duty(DC);
DC--;
__delay_ms(2); }
__delay_ms(100); }
return;}
// Check The DC Value To Make Sure it's Within 10-Bit Range
if(DC<1024)
{
CCP1Y = DC & 1;
CCP1X = DC & 2;
CCPR1L = DC >> 2;
}
Hardware:
Conclusion:
In this lab, we have learnt to generate PWM of some specific frequency to Configure CCP
Module of PIC 16F877A. We have studied about the PWM signal and its frequency and duty
cycle. Studied about the applications of PWM and observe CCP operation in PWM mode.
.END.