Wednesday, December 10, 2014

MSP430 multitasking





     Hello everyone. I am vignesh raja. Here we are going to see about how to implement multitasking concept in msp430. Multi Tasking is defined as doing two or more function at same time but independently each other. One function doesn’t affect other function. Basically when we use delay function in the program of microcontroller when delay function runs we can’t do any other function and condition checking except interrupt. Because delay function use whole cpu. Instead of delay we are going to use millis() function from energia software’s pre de fined functions. Using this millis() function  we are going to achieve  multi tasking. The msp430 ic has inbuilt timer. What millis() function does is that running time on that timer when ic get powered. It prints the count of current millisecond. If ic runs for exactly one second from when it powered it print count of millisecond as 1000. Because the 1000ms is equal to one second. The milis() count approximately get counts for 53 day . After 53 days it get resetted  . but no problem the resetting doesn’t affect output.

Here in this video I programmed msp430 launch pad to perform two functions run at the same time. One function led blink with interval of one second and other function is when the push button pressed then it will print some string in serial monitor. Here what I did to provide delay between led blinking is just seeing the millis() function time using if condition . it just like we watch the time and do work as per time. At first The program set the millis() value into a variable (current_Millis)  and assign a variable(previous_Millis) with zero value. And IF condition check subtraction between current_Milis variable and previous_Millis variable equal to 1000. If the condition is true then it changes the current led state. If led is high then it become low and vice versa. So here we are didn’t using delay instead of that we are using condition checking for time. So at mean time of this function the cpu is free. So it can able to check other function also. So here I did other function push button checking.  So both function work at same time. Multi tasking is achieved in the microcontroller. Below I provide the code use it and share your opinion in comment.
                                                               


coding:


const int ledPin = P1_0;    // set P1.0 as led pin
int button = P1_3;            // P1.3 conneted with push button in msp430 board
int ledState = LOW;            // led state
long previousMillis = 0;         // hold last millis value
long interval = 1000;           // time intervel to provide delay for led function
 void setup() {
pinMode(ledPin, OUTPUT);           // set led pin as output
pinMode(button, INPUT);            // set button as input
Serial.begin(4800);              // start serial communication at the baud rate of 4800
}
void loop()
{
unsigned long currentMillis = millis(); // saving current millis() value to a variable
if(currentMillis - previousMillis > interval) { //comparing subtraction of current millis value to privious millis value with intervel 1000ms = 1 sec 
previousMillis = currentMillis;           // after comparision assigning current millis to previous one
if (ledState == LOW) //check led state if it is low make it as high
ledState = HIGH;
else
ledState = LOW;  // if it is high make it as low
digitalWrite(ledPin, ledState);   
}
if (digitalRead(button)== LOW){      // check button is pressed 
Serial.println("button pressed!");  // if true then print this string serial monitor
}
}



Thank you for reading.


  

1 comment:

  1. Hello vignesh indeed a very good example.thanks for sharing.

    narasimhaj
    narasimhajb@gmail.com

    ReplyDelete