In: Electrical Engineering
Write a program for the msp430 to make led1 and led 2 alternate blinking 2. repeat the program to make each led blink 5 times in a sequence, then 5 times simultaneously
Solution:
Two LEDs are connected to two Pins of Port-2
LED1- is connected to P2.2 (Port -2) and it is active high means that when the bit is high LED will glow.
LED2- is connected to P2.3 (Port -2) and it is active high means that when the bit is high LED will glow.
The below program is implementation of alternative blinking of Two LEDs with delay of 352msec.
Program1:
/******** Progrman for alternate blinking of two leds *******/
#include "msp430x14x.h"
#define BIT2 0x04u
#define BIT3 0x08u
void delay_352us(unsigned int i);//352us delay program statement
//*************************************
int main( void )
{
WDTCTL = WDTPW + WDTHOLD;// Stop watchdog timer to prevent time out reset
P2DIR |= BIT2; //Port 2.2 as output port for led1
P2DIR |= BIT3; //Port 2.3 as output port for led2
P2OUT &= ~BIT2; //led1 off
P2OUT &= ~BIT3; //led2 off
while(1)
{
P2OUT |= BIT2; //led1 on
delay_352us(1000);
P2OUT &= ~BIT2; //led1 off
P2OUT |= BIT3; //led2 on
delay_352us(1000);
P2OUT &= ~BIT3; //led2 off
}
}
}
//******************************************************
//352 ms delay procedure
void delay_352us(unsigned int i)//352us delay procedures, the calculation of delay time in front of already mentioned
{
unsigned char j;
while(i--)
{
for(j=0;j<255;j++)
{
_NOP();
_NOP();
_NOP();
_NOP();
}
}
}
/******************end of program********/
Program2:
It is an implementation of following sequences for the two LEDs
/******** Progrman for alternate blinking of two leds *******/
#include "msp430x14x.h"
#define BIT2 0x04u
#define BIT3 0x08u
void delay_352us(unsigned int i);//352us delay program statement
void blinkled1();
void blinkled2();
void blinkled1and2();
//*************************************
int main( void )
{
WDTCTL = WDTPW + WDTHOLD;// Stop watchdog timer to prevent time out reset
P2DIR |= BIT2; //Port 2.2 as output port for led1
P2DIR |= BIT3; //Port 2.3 as output port for led2
P2OUT &= ~BIT2; //led1 off
P2OUT &= ~BIT3; //led2 off
while(1)
{
blinkled1();
blinkled2();
blinkled1and2();
}
}
/*******Blink LED1*****/
void blinkled1()
{
unsigned char i=0;
for(i=0;i<5;i++)
{
P2OUT |= BIT2; //led1 on
delay_352us(1000);
P2OUT &= ~BIT2; //led1 off
}
}
/*******Blink LED2*****/
void blinkled2()
{
unsigned char i=0;
for(i=0;i<5;i++)
{
P2OUT |= BIT3; //led2 on
delay_352us(1000);
P2OUT &= ~BIT3; //led2 off
}
}
/*******Blink LED1*****/
void blinkled1and2()
{
unsigned char i=0;
for(i=0;i<5;i++)
{
P2OUT |= (BIT2|BIT3); //led1&2 on
delay_352us(1000);
P2OUT &= ~(BIT2|BIT3); //led1&2 off
}
}
//******************************************************
//352 ms delay procedure
void delay_352us(unsigned int i)//352us delay procedures, the calculation of delay time in front of already mentioned
{
unsigned char j;
while(i--)
{
for(j=0;j<255;j++)
{
_NOP();
_NOP();
_NOP();
_NOP();
}
}
}
/*****end of Program***************/
Three functions are defined for the three steps and it is repeated forever.