Pulse-width modulation or PWM is a method of varying output DC voltage by varying the width of the signal. The average value of voltage fed to the load is controlled by turning the switch between supply and load on and off at a fast rate. The time for which the signal is high to the total time period is called the Duty Cycle.
In below example, I have kept 50% Duty Cycle, which means, the signal will be on 50% of the time and off for remaining 50% of the time.
The registers used to generate a PWM Output are, PWMLER (PWM Latch Enable Register), PWMPCR (PWM Control Register), PWMTCR(PWM Timer Control Register) to reset and enable the timer/counter, PWMPR (PWM Prescale Counter Register), PWMMCR (PWM Match Control Register).
// GENERATING PWM OUTPUT FROM A PIN
#include<lpc214x.h>
// CHANNEL 2 IS BEING USED HERE
void pwm_init()
{
PWMPCR=0X0000;
PWMPR=240000; // For 50% Duty Cycle
PWMMCR|=(1<<1);
PWMMCR&=~(1<<1);
PWMLER&=~(1<<1);
}
void pwm_output()
{
PWMPCR|=(1<<10);
PWMTCR|=(1<<1);
PWMTCR|=0X09;
}
int main()
{
PINSEL0=0X00008000;
pwm_init();
PWMLER|=(1<<2);
pwm_output();
while(1);
}
No comments:
Post a Comment