Friday 28 August 2020

MSP430 with RGB LED - Pulse Width Modulation (PWM)

This post is related to the Pulse width modulation using MSP430 launchpad and RGB LED. 

All the details for the implementation of this project are provided on My YouTube channel : https://www.youtube.com/watch?v=o-tf9ZC-rSQ

Do Subscribe to the channel to see more content related to embedded systems.


Below is the CCS code:

#include <msp430.h>

#define MAX_TIME (1000)
#define MAX_COUNT (50)
int rv = 750; //red
int gv = 0;   //green
int bv = 0;   //blue
int fac = 50;  //change by a factor of 50

int c = 0;
int d = 0;

inline void update_colors(void)
{
if(d<15) 
{
rv = rv - fac;
bv = bv + fac;
gv = 0;
d++;

TA1CCR2 = rv;
TA1CCR1 = bv;
TA0CCR1 = gv;
}

if(d>14 && d<30) 
{
rv = 0;
bv = bv - fac;
gv = gv + fac;

d++;

TA1CCR2 = rv;
TA1CCR1 = bv;
TA0CCR1 = gv;

}

if(d>29 && d<45) 
{
bv = 0;
rv = rv + fac;
gv = gv - fac;

d++;

TA1CCR2 = rv;
TA1CCR1 = bv;
TA0CCR1 = gv;
}

if(d==45) 

d=0;

}


int main()
{
WDTCTL = WDTPW + WDTHOLD;

P2DIR |= (BIT1 | BIT5);
P2SEL |= (BIT1 | BIT5);

TA1CCR0 = MAX_TIME;
TA1CCTL1 = OUTMOD_7;
TA1CCTL2 = OUTMOD_7;
TA1CCR1 = rv;
TA1CCR2 = bv;
//Start the timer 1
TA1CTL = TASSEL_2 | MC_1;


P1DIR |= BIT6;
P1SEL |= BIT6;
TA0CCR0 = MAX_TIME;
TA0CCTL1 = OUTMOD_7;
TA0CTL |= TAIE;
TA0CCR1 = gv;

//Start timer 0
TA0CTL |= TASSEL_2 | MC_1;

//Turn off CPU and interrupt enabled
_BIS_SR((LPM0_bits | GIE));

while(1) {}
return 0;
}

__attribute__((interrupt(TIMER0_A1_VECTOR)))
void tim0_a1_isr(void)
{
++c;
if(c >= MAX_COUNT)
{
update_colors();
c=0;
}
TA0CTL &= ~TAIFG;
}