Sunteți pe pagina 1din 9

2012

Generating PWM on LPCXpresso - LPC1769

Author: Manoj

PWM Pulse Width Modulation, or PWM, is a well known technique used in power controlling delivering the preferred amount of power to the load. It outputs the analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This onoff pattern can simulate voltages in between full on (3.3 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. The longer the ON period compared to the OFF period, the higher the power supplied to the load is. To get varying analog values, you change, or modulate, that pulse width. If you repeat this onoff pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 3.3v controlling the brightness of the LED. RGB LED RGB led is nothing but three primary color LEDs namely Red, Green, Blue are cascaded in a single package. This LED's are widely used to obtain the desired colors by the additive mixing of these colors.

Figure: RGB LED

Figure: RGB LED pin out

The RGB LEDs are available either in common anode or common cathode mode If these three LEDs share the same positive (anode) terminal, which means that this RGB LED has a "common anode" connection. To control each color, simply connect its cathode pin to ground (through a resistor as a current limiter), and it will light up.

Demo: Controlling the Brightness of the RGB LED with PWM outputs from the controller For this demo, we are connecting a tri-color RGB LED to the microcontroller with PWM ("virtual analog") outputs to control the brightness of each color, the PWM values are continuously varied using simple looping statements within the coding hence you can see the different colors at the output.

Figure: Schematic

Code:
/* ============================================================================ Name : main.c Author : Vinayak R Version : 1.0 Copyright : Copyright (C) Description : main definition ============================================================================ */ #ifdef __USE_CMSIS #include "LPC17xx.h" #endif #include <cr_section_macros.h> #include <NXP/crp.h> // Variable to store CRP value in. Will be placed automatically // by the linker when "Enable Code Read Protect" selected. // See crp.h header for more information __CRP const unsigned int CRP_WORD = CRP_NO_CRP ; // TODO: insert other include files here // TODO: insert other definitions and declarations here // Control whether debug printf are displayed via semihosting to the // debugger console window #define SEMIHOSTING_CONSOLE TRUE

#ifdef SEMIHOSTING_CONSOLE #include <stdio.h> #define DBG printf #else #define DBG(x ...) #endif #define PCPWM1 (1 << 6) #define PCLK_PWM1_BY8 ((1 << 13)|(1 << 12)) #define PCLK_PWM1_BY4 ~(PCLK_PWM1_BY8) #define PCLK_PWM1 ((0 << 13)|(1 << 12)) #define PCLK_PWM1_BY2 ((1 << 13)|(0 << 12)) #define LER0_EN 1 << 0 #define LER1_EN 1 << 1 #define LER2_EN 1 << 2 #define LER3_EN 1 << 3 #define LER4_EN 1 << 4 #define LER5_EN 1 << 5 #define LER6_EN 1 << 6 #define PWMENA1 1 << 9 #define PWMENA2 1 << 10 #define PWMENA3 1 << 11 #define PWMENA4 1 << 12 #define PWMENA5 1 << 13 #define PWMENA6 1 << 14 #define TCR_CNT_EN 0x00000001 #define TCR_RESET 0x00000002 #define TCR_PWM_EN 0x00000008 #define INITPWMVAL 0 #define ENDPWMVAL 65500 #define STEP 400 // Function Declaration void InitPWM(); void SetPWM(int,char); void delay(int); int main(void) { int i, j, k; int iter = 0; // TODO: insert code here InitPWM(); for(i = INITPWMVAL;i <= ENDPWMVAL; i += STEP) { for(j = INITPWMVAL;j <= ENDPWMVAL; j += STEP) { for(k = INITPWMVAL;k <= ENDPWMVAL; k += STEP) { SetPWM(i,'R'); SetPWM(j,'G'); SetPWM(k,'B'); delay(5); DBG("PWM value i = %d, j = %d, k = %d\n", i, j, k); } for(k = ENDPWMVAL ;k >= INITPWMVAL ; k -= STEP) { SetPWM(i,'R');

} } return 0 ; } }

SetPWM(j,'G'); SetPWM(k,'B'); delay(5); DBG("PWM value i = %d, j = %d, k = %d\n", i, j, k);

void InitPWM() { //enable PWM1 Power LPC_SC ->PCON |= PCPWM1; //PWM peripheral clk = PCLK LPC_SC ->PCLKSEL0 &= (PCLK_PWM1_BY4); //Put P0.26 in Hi-Z so it can be shorted to PWM1.2 (P2.2) // LPC_PINCON->PINMODE1 &= ~(0x3<<20); // LPC_PINCON->PINMODE1 |= 0x2<<20; // LPC_GPIO0->FIODIR &= ~(1<<26); //Pin select LPC_PINCON->PINSEL4 = (0x1<<0) | (0x1<<2) | (0x1<<4); // count frequency:Fpclk LPC_PWM1->PR = 0x00; //reset on MR0 LPC_PWM1->MCR = 1 << 1; // set PWM cycle LPC_PWM1->MR0 = ENDPWMVAL; //LEDs default to OFF LPC_PWM1->MR1 = 0; LPC_PWM1->MR2 = 0; LPC_PWM1->MR3 = 0; //Load Shadow register content LPC_PWM1->LER = LER0_EN | LER1_EN | LER2_EN| LER3_EN; //Enable PWM outputs LPC_PWM1->PCR = PWMENA1 | PWMENA2 | PWMENA3 | PWMENA4; //Enable PWM Timer LPC_PWM1->TCR = TCR_CNT_EN | TCR_PWM_EN; } void SetPWM(int PWMval, char rgb) { if(rgb == 'R') LPC_PWM1->MR1 = PWMval; else if(rgb == 'G') LPC_PWM1->MR2 = PWMval; else if(rgb == 'B') LPC_PWM1->MR3 = PWMval;

LPC_PWM1->LER = LER0_EN | LER1_EN | LER2_EN| LER3_EN; } void delay(int val) { int j,i; for(j = 0; j < val; j++) for(i = 0 ; i < 10000; i++); }

Demo Application: RGB LED is connected to the LPCXpresso LPC1769 board

The RGB LED is put into the casing ball for the clear visibility in the color variations.

The color variations; the color is continuously changing

S-ar putea să vă placă și