LED Brightness Control Using PWM of LPC2138: ESD Lab Mini-Project
LED Brightness Control Using PWM of LPC2138: ESD Lab Mini-Project
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.
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
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
ADC_init();//Init ADC
while(1)
{
value=ADC_read(2);
ton=((60000*value)/1024);//ON time
genpwm(ton);
}
4|Page
ADC Control-
PWM Wave-
5|Page
Circuit Diagram-
6|Page
7|Page