Other Parts Discussed in Thread: MSP430G2231
Hi.
The code below just fades some LEDs in a sequence. The "start" of the LED action is given by the pushbutton (S2) on the launchpad. Also, the "stop" of the LED fading/blinking should be done with a push of the same button. The "start" is just fine. The "stop" on the other hand acts weird. For example it won't turn off the LEDs until the whole while(1) loop is complete and also, sometimes when I press the button, one of the LEDs will stay ON no matter what part of the program is executed. Any ideas?
What I am looking for is something like this: push button --> LEDs blink&fade; push button again --> all LEDs OFF.
Thanks.
This is the code I am currently running (I apologize for the length, but I wanted you to see if there is something wrong with what I'm doing) :
#include <msp430g2231.h>
#define LED_OUT P1OUT
#define LED_DIR P1DIR
#define P0 BIT0 // white led
#define P1 BIT1 // Red led
#define P2 BIT2 // white led
#define BUTTON BIT3 // button
void Delay_us(unsigned int us);
unsigned int blink = 0;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Set WDT to stop
LED_DIR |= (P0 + P1); // Set P1.0 and P1.6 to output direction
LED_OUT &= ~(P0 + P1); // Set the LEDs off
P1IE |= BUTTON;
__enable_interrupt();
LED_DIR |= P2;
LED_OUT &= ~P2;
int j, k, i, m, n;
i=0; // used to vary the "speed" of the LEDs
n=0; // not currently used
while(1)
{
if(blink>0)
{
while(1)
{
for(j = 0; j < 255; j++)
{ // fades LED out
for(k = i; k < 12; k++)
{
Delay_us(255-j);
P1OUT ^=P0;
Delay_us(j);
P1OUT ^=P0;
}
}
for(j = 0; j < 255; j++)
{ // fades LED in
for(k = i; k < 6; k++)
{
Delay_us(j);
P1OUT ^=P0;
Delay_us(255-j);
P1OUT ^=P0;
}
}
for(j = 0; j < 255; j++)
{ // fades LED out
for(k = i; k < 2; k++)
{
Delay_us(255-j);
P1OUT ^=P0;
Delay_us(j);
P1OUT ^=P0;
}
}
for(j = 0; j < 255; j++)
{ // fades LED in
for(k = i; k < 2; k++)
{
Delay_us(j);
P1OUT ^=P0;
Delay_us(255-j);
P1OUT ^=P0;
}
}
for(j = 0; j < 255; j++)
{ // fades LED out
for(k = i; k < 3; k++)
{
Delay_us(255-j);
P1OUT ^=P0;
Delay_us(j);
P1OUT ^=P0;
}
}
for(j = 0; j < 255; j++)
{ // fades LED in
for(k = i; k < 3; k++)
{
Delay_us(j);
P1OUT ^=P0;
Delay_us(255-j);
P1OUT ^=P0;
}
}
__delay_cycles(100000);
for(j = 0; j < 255; j++)
{ // fades LED out
for(k = i; k < 12; k++)
{
Delay_us(255-j);
P1OUT ^=P1;
P1OUT ^=P0;
Delay_us(j);
P1OUT ^=P1;
P1OUT ^=P0;
}
}
for(j = 0; j < 255; j++)
{ // fades LED in
for(k = i; k < 12; k++)
{
Delay_us(j);
P1OUT ^=P1;
P1OUT ^=P0;
Delay_us(255-j);
P1OUT ^=P1;
P1OUT ^=P0;
}
}
__delay_cycles(100000);
for(j = 0; j < 255; j++)
{ // fades LED out
for(k = i; k < 12; k++)
{
Delay_us(255-j);
P1OUT ^=P2;
P1OUT ^=P1;
Delay_us(j);
P1OUT ^=P2;
P1OUT ^=P1;
}
}
for(j = 0; j < 255; j++)
{ // fades LED in
for(k = i; k < 12; k++)
{
Delay_us(j);
P1OUT ^=P2;
P1OUT ^=P1;
Delay_us(255-j);
P1OUT ^=P2;
P1OUT ^=P1;
}
}
__delay_cycles(100000);
for(j = 0; j < 255; j++)
{ // fades LED out
for(k = i; k < 12; k++)
{
Delay_us(255-j);
P1OUT ^=P1;
Delay_us(j);
P1OUT ^=P1;
}
}
for(j = 0; j < 255; j++)
{ // fades LED in
for(k = i; k < 6; k++)
{
Delay_us(j);
P1OUT ^=P1;
Delay_us(255-j);
P1OUT ^=P1;
}
}
for(j = 0; j < 255; j++)
{ // fades LED out
for(k = i; k < 3; k++)
{
Delay_us(255-j);
P1OUT ^=P1;
Delay_us(j);
P1OUT ^=P1;
}
}
for(j = 0; j < 255; j++)
{ // fades LED in
for(k = i; k < 3; k++)
{
Delay_us(j);
P1OUT ^=P1;
Delay_us(255-j);
P1OUT ^=P1;
}
}
__delay_cycles(100000);
i=i+2;
if (i>11)
{
i=0;
}
n=n+1;
}
}
}
}
void Delay_us(unsigned int us) // delay function
{
while(us--){
__delay_cycles(1);
}
}
// Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
blink ^= 0x01;
P1IFG &= ~BUTTON; // P1.3 IFG cleared
LED_OUT &= ~(P0 + P1 + P2); // Clear the LEDs so they start in OFF state
}