This is a small example for an RGB LED using PWM. An common anode RGB LED is connected with green to Pin 2.3, blue to pin 2.4 and red to pin 2.5. The example will slowly fade the colors from off to on and back off again. The example can be downloaded from our repository.

 

RGB LED


 

#include "lpc_types.h"
#include "LPC17xx.h"
#include "timer.h"
#include "pwm.h"
#include "GPIO.h"

int main(void)
{
	uint32_t green = 1000;
	uint32_t blue = 1000;
	uint32_t red = 1000;

	//PWM1.4 is green, 1.5 is blue, 1.6 is red.
	//When a color is set to 1000 the LED is off, 0 for on, 500 for 50%.
	PWM_Init(1, 4, 1000);
	PWM_Init(1, 5, 1000);
	PWM_Init(1, 6, 1000);
	PWM_Set(1, 4, 1000);
	PWM_Set(1, 5, 1000);
	PWM_Set(1, 6, 1000);

	PWM_Start(1);

    while(1)	//repeat for forever
    {
    	//Slowly fade the green LED from off to on
    	for (green = 1000; green > 1; green--)
    	{
    		PWM_Set(1, 4, green);
    		delayMs(0, 1);
    	}
    	//Slowly fade the blue LED from off to on
    	for (blue = 1000; blue > 1; blue--)
    	{
    		PWM_Set(1, 5, blue);
    		delayMs(0, 1);
    	}
    	//Slowly fade the red LED from off to on
    	for (red = 1000; red > 1; red--)
    	{
    		PWM_Set(1, 6, red);
    		delayMs(0, 1);
    	}
    	//Slowly fade the green LED from on to off
    	for (green = 0; green < 1000; green++)
    	{
    		PWM_Set(1, 4, green);
    		delayMs(0, 1);
    	}
    	//Slowly fade the blue LED from on to off
    	for (blue = 0; blue < 1000; blue++)
    	{
    		PWM_Set(1, 5, blue);
    		delayMs(0, 1);
    	}
    	//Slowly fade the red LED from on to off
    	for (red = 0; red < 1000; red++)
    	{
    		PWM_Set(1, 6, red);
    		delayMs(0, 1);
    	}
    }
}

 

Copyright 2011. Joomla 1.7 templates - Joomla template maker. The Simplecortex is developed by BRC-Electronics