5.LPC2148 - PWM Programming
5.LPC2148 - PWM Programming
5.LPC2148 - PWM Programming
In this tutorial, we are going to discuss the PWM module of LPC2148. First, we will see how to
configure the PWM registers to generate signals of required PWM, at the end, we will see how to
use the Explore Emdedded PWM library.
Register Description
PWMIR Interrupt Register: The IR can be read to identify which of eight possible interrupt
sources are pending. Writing Logic-1 will clear the corresponding interrupt.
PWMTCR Timer Control Register: The PWMTCR is used to control the Timer Counter
functions (enable/disable/reset).
PWMTC Timer Counter: The 32-bit TC is incremented every PR+1 cycles of PCLK. The
PWMTC is controlled through the PWMTCR.
PWMPR Prescaler Register: This is used to specify the Prescaler value for incrementing the
PWMTC.
PWMPC Prescale Counter: The 32-bit PC is a counter which is incremented to the value
stored in PR. When the value in PR is reached, the TC is incremented.
PWMMCR Match Control Register: The PWMMCR is used to control the reseting of PWMTC
and generating of interrupt whenever a Match occurs.
PWMMR0 Match Register: This register hold the max cycle Time(Ton+Toff).
PWMMR1- Match Registers: These registers holds the Match value(PWM Duty) for
PWMMR6 corresponding PWM channels(PWM1-PWM6).
PWMPCR PWM Control Register: PWM Control Register. Enables PWM outputs and selects
PWM channel types as either single edge or double edge controlled.
PWMLER Load Enable Register: Enables use of new PWM values once the match occurs.
Register Configuration
The below table shows the registers associated with LPC2148 PWM.
PWMTCR
31:4 3 2 1 0
PWM
MCR
31:21 20 19 18 - 5 4 3 2 1 0
Reserv PWM PWMM PWM - PWM PWMM PWM PWM PWMM PWM
ed MR6S R6R MR6I MR1S R1R MR1I MR0S R0R MR0I
PWMMRxI
This bit is used to Enable or Disable the PWM interrupts when the PWMTC matches PWMMRx
(x:0-6)
0- Disable the PWM Match interrupt
1- Enable the PWM Match interrupt.
PWMMRxR
This bit is used to Reset PWMTC whenever it Matches PWMRx(x:0-6)
0- Do not Clear.
1- Reset the PWMTC counter value whenever it matches PWMRx.
PWMMRxS
This bit is used to Stop the PWMTC,PWMPC whenever the PWMTC matches PWMMRx(x:0-
6).
0- Disable the PWM stop o match feature
1- Enable the PWM Stop feature. This will stop the PWM whenever the PWMTC reaches the
Match register value.
PWMPCR
PWMSELx
This bit is used to select the single edged and double edge mode form PWMx (x:2-6)
0- Single Edge mode for PWMx
1- Double Edge Mode for PWMx.
PWMENAx
This bit is used to enable/disable the PWM output for PWMx(x:1-6)
0- PWMx Disable.
1- PWMx Enabled.
PWMLER
31-7 6 5 4 3 2 1 0
PWM Working
After looking into the PWM registers, its time to see how the LPC1768 PWM module works.
The TC is continuously incremented and once it matches the MR1(Duty Cycle) the PWM pin is
pulled Low. TC still continues to increment and once it reaches the Cycle time(Ton+Toff) the
PWM module does the following things:
Example
Program to demonstrates the variable PWM signal generation on PWM_1-
PWM_4(P0_0,P0_7,P0_1,P0_8).
Connect the Leds to the pins P0_0,P0_7,P0_1,P0_8 and observe the led brigthness change
depending on the dutycycle.
#include <lpc214x.h>
#include "stdutils.h"
#include "systemInit.h"
#define SBIT_CNTEN 0
#define SBIT_PWMEN 3
#define SBIT_PWMMR0R 1
#define SBIT_LEN0 0
#define SBIT_LEN1 1
#define SBIT_LEN2 2
#define SBIT_LEN3 3
#define SBIT_LEN4 4
#define SBIT_PWMENA1 9
#define SBIT_PWMENA2 10
#define SBIT_PWMENA3 11
#define SBIT_PWMENA4 12
while(1)
{
for(dutyCycle=0; dutyCycle<100; dutyCycle++)
{
PWMMR1 = dutyCycle; /* Increase the dutyCycle from 0-100 */
PWMMR2 = dutyCycle;
PWMMR3 = dutyCycle;
PWMMR4 = dutyCycle;
/* Trigger the latch Enable Bits to load the new Match Values */
PWMLER = (1<<SBIT_LEN0) | (1<<SBIT_LEN1) | (1<<SBIT_LEN2) | (1<<SBIT_LEN3) |
(1<<SBIT_LEN4);
delay_ms(5);
}
#include <lpc214x.h>
#include "stdutils.h"
#include "systemInit.h"
#include "pwm.h"
#include "delay.h"
while(1)
{
for(dutyCycle=0;dutyCycle<CYCLE_TIME;dutyCycle++) /* Increase the Brightness of the
Leds */
{
PWM_SetDutyCycle(PWM_1,dutyCycle); //P0_0
PWM_SetDutyCycle(PWM_2,dutyCycle); //P0_7
PWM_SetDutyCycle(PWM_3,dutyCycle); //P0_1
PWM_SetDutyCycle(PWM_4,dutyCycle); //P0_8
DELAY_ms(5);
}