Powered By Blogger

Tuesday, June 9, 2015

CONTROLLED LED GLOW USING SWITCH

CONTROLLED LED GLOW USING SWITCH

The aim of the program is to make you familiar with :
1. basic commands
2. Interrupts
3. switches on developer board

OUTCOME

The outcome of the program will be like when you press button1 First Time at P4.5 LED 1 will GLOW and LED 0 will be OFF. When pressed again LED glowing reverses. The later button 2 at P1.0 works as a switch for the whole process of the glowing. If button 2 is once pressed the button 1 works , i.e. switching the glowing of LEDs at each press, and if button 2 is pressed again, button 1 becomes ineffective.

WORKING

The prime background working of this program is Interrupt Service Subroutine. MSP430FR5969 is a Ultra Low Power device. Running a program and checking the pin status always continously takes energy. So its wise to use interrupts instead. Interrupt is enabled at each button press and work is done accordingly.
Work of BUTTON 2 is to enable or disable BUTTON 1. So we make the value of a our main variable to TURNED OFF situation (here COUNT=2 ). And BUTTON 1 keeps increasing the value of COUNT at each Interrupt generation. When reached 3, it's reset to 0 (this is done since two LEDs are used. You can use many more LEDs and so let count reach a higher value accordingly).

POINTS TO EMPHASIZE

Command: WDTCTL = WDTPW + WDTHOLD

This command disables WATCHDOG TIMER.
Now what is this Whatchdog Timer. Allow me to explain this crucial point. It is a control that helps to prevent the code being stuck at a place where it shouldn't be like an infinite loop. If the control gets stuck in an infinite loop, it checks that the control hasn't come out of the loop in a certain time. Then it resets the device to free up all the memories and start over again.

We in this case don't want this to happen. Instead we have deliberately introduced an infinite loop.

Command : PM5CTL0 &= ~LOCKLPM5

This command disables the GPIO power on default high impedance state. In simpler language, it enables new code to be written on the controller each time we burn the code. Without this line program doesn't gets into memory and we would get worried that some logical or hardware error must have happened.


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//                                                 PROGRAM STARTS FROM HERE
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include

#define LED_0 BIT0
#define LED_1 BIT6
#define BUTTON BIT5
#define BUTTON2 BIT1

unsigned int count = 2;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void main(void)
{
WDTCTL = WDTPW + WDTHOLD;              // Stop watchdog timer
PM5CTL0 &= ~LOCKLPM5;                          // Disable the GPIO power-on default high-impedance                                                                           //mode to activate previously configured port settings
P1DIR |= (LED_0 + LED_1);                         // Set P1.0 and P1.6 to output direction
P1OUT &= ~(LED_0 + LED_1);                    // Set the LEDs off


P4REN |= BUTTON;                                         //Enables a puller-Resistor on the button-pin
P4OUT |= BUTTON;                                    //Writes a "1" to the portpin, tellling the resistor to pullup
P4IES |= BUTTON;                                   //Triggers when you PRESS the button :: Pick one
//P1IES &= ~BUTTON;                        // Triggers when you RELEASE the button :: or pick the other
P4IE |= BUTTON;                  //Enables the selector-mask for generating interrupts on the relevant pin


P1REN |= BUTTON2;                                      //Enables a puller-Resistor on the button-pin
P1OUT |= BUTTON2;                                 //Writes a "1" to the portpin, tellling the resistor to pullup
P1IES |= BUTTON2;                                //Triggers when you PRESS the button :: Pick one...
//P1IES &= ~BUTTON;                     // Triggers when you RELEASE the button :: ...or pick the other
P1IE |= BUTTON2;                //Enables the selector-mask for generating interrupts on the relevant pin

P1IFG &= ~BUTTON2;                                             //~~~~~~~~~ P1.1 IFG cleared
P4IFG &= ~BUTTON;                                               //~~~~~~~~~P4.5 IFG cleared

/* Clearing of (IFG) Interrupt Flag is important. If not done it will take any random value, may get triggered without button pressed at the first time and generate undesired value.*/


__enable_interrupt();                              // Interrupts get enabled *here* - they were disabled thus far..

for (;;)
{

if (count == 0)                                                       // Even button Press condition
{

       P1OUT |= (LED_0);                                        // led 0 is ON
       P1OUT &= ~(LED_1);                              // led 1 os OFF
}
else if (count == 1)                                                 // Odd button Press condition
{

P1OUT |= (LED_1);                                // led 1 is ON
P1OUT &= ~(LED_0);                             // led 0 is OFF
}
else                                                                           // Default Case..... (both LEDs OFF)
{
P1OUT &= ~(LED_0);                             // led 0 is OFF
P1OUT &= ~(LED_1);                             // led 1 is OFF
}
}
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//                                                 MAIN PROGRAM ENDS ABOVE
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


// Port 4 interrupt service routine

#pragma vector=PORT4_VECTOR
__interrupt void Port_4(void)
{
count++;                                                            //The value of count is increased on each interrupt
P4IFG &= ~BUTTON;                                             // P4.5 IFG cleared
if (count > 2)
{
count = 0;                                                         // count is reset when count =3

}

}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// Port 1 interrupt service routine

#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
P1IFG &= ~BUTTON2;                                               // P1.1 IFG cleared
   count = 2;
}

No comments:

Post a Comment