18f4550 PWM Example
18f4550 PWM Example
//Description:
//
//
//This example shows that PWM function from PIC18F4550. There are 2 channels which can be used as
PWM outputs.
//See from the schematic of PIC18F4550, these channels are noticed as CCP1 and CCP2. The PWM uses
timer2 as time base.
//Before PWM works, timer2 must be set.
//
//The PWM period is calculated by:
//PWM Period = [period+1]*4*Tosc*(TMR2 Prescaler).
//PWM Dutycycle = (DCx<9:0>) x TOSC x (TMR2 prescaler).
//where DCx<9:0> is the 10-bit value specified in the call to this function.
//
//This example is using to PWM from both 2 channels to make fading LEDs.
//
//
#include <p18f4550.h>
#include <pwm.h> // include the PWM library
#include <delays.h> // include the delay library
#include <timers.h> // include the timer library
}
void Delay10mS(int x)
{//Pre: Delay library is included.
//Post:Delay for x*10ms.
int i;
for (i=0; i<x; i++) Delay10KTCYx(12);
}
void Delay100mS(int x)
{//Pre: Delay library is included.
//Post:Delay for x*100ms.
int i;
for (i=0; i<x; i++) Delay10KTCYx(120);
}
void Delay1S(int x)
{//Pre: Delay library is included.
//Post:Delay for x*1s.
int i;
for (i=0; i<10*x; i++) Delay10KTCYx(120);
}
1
E:\Internship project PIC18F4550\standard library examples\PWM\PWM.c
OpenTimer2(T2_PS_1_16 & TIMER_INT_OFF); // Timer2 is used for the time base of the PWM. set
timer2 before PWM works.
// Set timer2 prescaler to 1:16, and set interrupts OFF