You are on page 1of 8

LED Brightness Control Using PWM Of LPC2138

ESD Lab Mini-Project

LED Brightness
Control Using PWM
Of LPC2138

Pritam Kondekar (2013BEN005)


Aditya Gadgil (2013BEN007)
Pavan Chopade(2013BEN008)
11-2-2015

LED Brightness Control Using PWM Of LPC2138

Problem StatementTo control the brightness of the LED using the PWM, duty cycle of which is
varied by using the pot connected to LPC2138.

PWM of LPC2138PWM (Pulse Width Modulation) enables the user to produce the rectangular wave of
different and desired duty cycles. The match facility in the LPC2138 is used for producing the
output. There are two types of the PWM- Single edge controlled and dual edge controlled. The
single edge controlled PWM, as the name suggests, the user can control only one edge, and thus
can control only the ON Time of the PWM Wave. While the dual edge controlled facilitates the
control of both ON Time and OFF Time.

Steps To Control the LED BrightnessUsing ADC to input the value1. Select the proper pin function for the ADC using PINSEL.
2. Turn on the ADC, and select divider value and channel number using the ADxCR.
3. Connect the potmeter to the ADC input pin, the value of the pot will be multiplied by the
step size to calculate the cycles in the ON time.

Enabling and Operating PWM


1. Select the PWM function for the PIN on which you need the PWM output using
PINSEL0/1 register.
2. Select Single Edge or Double Edge Mode using PWMPCR. By default its Single Edge
Mode.
3. Assign the Calculated value to PR.
4. Set the Value for PWM Period in PWMMR0.
5. Set the Values for other Match Registers i.e. the Pulse Widths.
6. Set appropriate bit values in PWMMCR.
7. Set Latch Enable Bits for the Match Registers that youve used. This is important!
8. Then Enable PWM outputs using PWMPCR.
9. Now Reset PWM Timer using PWMTCR.
10. Enable Timer Counter and PWM Mode using PWMTCR.
1|Page

LED Brightness Control Using PWM Of LPC2138

CalculationsThe ON time of the PWM is decided by the value of the pot, which is connected to the
ADC input of the LPC2138. The ADC value is multiplied by the step size calculated. We keep
MR0 (Match Register 0 which holds the total PWM period) at 60000.
Step Size = 60,000/1024
ON Time value = step size*the ADC value

2|Page

LED Brightness Control Using PWM Of LPC2138

Code#include<lpc213x.h>
void delay()
{
int i,j;
for(i=0;i<200;i++)
{ for(j=0;j<105;j++){}
}
}
void ADC_init()
{
PINSEL0=0X300000; //Selected the ADC 1
AD1CR=0X200104; //ADC 1 Enabled
}
void genpwm(unsigned int ton)
{
PINSEL0=0x02;
//P0.0 as PWM
PWMPCR=0x200;
//Single edge
PWMMCR=0x002;
//Reset on MR0
PWMMR0=60000;
//Total period
PWMMR1=ton;
//On time
PWMLER=0x01;
//For MR0
PWMTCR=0x02;
//Reset timer
PWMTCR=0x09;
//Enable timer and pwm
}
int ADC_read(unsigned char ch)
{
unsigned int value;
AD1CR|=1<<ch;
//channel 2
delay();
AD1DR&=0X7FFFFFFF;//clear done bit
AD1CR|=0X01000000;//start conversion
while(!(AD1DR&0X80000000));//wait for eoc
value=(AD1DR&0XFCC0)>>6;//store acquired value
return value;
}
int main()
{
unsigned int value,ton;
3|Page

LED Brightness Control Using PWM Of LPC2138

ADC_init();//Init ADC
while(1)
{
value=ADC_read(2);
ton=((60000*value)/1024);//ON time
genpwm(ton);
}

ObservationsPin Connect Block-

4|Page

LED Brightness Control Using PWM Of LPC2138

ADC Control-

PWM Wave-

5|Page

LED Brightness Control Using PWM Of LPC2138

Circuit Diagram-

6|Page

LED Brightness Control Using PWM Of LPC2138

7|Page

You might also like