Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 1 month ago.
how to create two tasks using round robin scheduling?
i need to create two tasks using round robin scheduling.. task 1 : print ABCDE on lcd display(in first row) task 2 : print 12345 on lcd 2nd row the tasks should execute in the order A1B2C3D4C5.. can anyone suggest how to proceed and write the code using mbed kit..??
3 Answers
9 years, 1 month ago.
I show an example code for you.
Main routine is a modification from DigitalOut_HelloWorld (blink LED).
Task1 and task2 are realized by using ’writeLcd’ function. They are invoked in round robin with 200 millisecond intervals in infinite loop.
The code to display data on LCD is varied significantly according to variety of LCD devices, so I write signature of 'writeLcd' function without code body.
Code Sample
"include "mbed.h"
Routine to show strings 'aLine' to LCD at the 'rn' row. int writeLcd(char* aLine, int rn){ put code for the LCD device you are using. }
int main(void) {
while(1); infinite loop{ writelcd("abcde",1); task1 wait(0.2); waiting 200 millisecond before task2
writelcd("12345”,2); task2 wait(0.2); waiting 200 millisecond before task1 }
}
9 years, 1 month ago.
You can check out http://developer.mbed.org/cookbook/Wiki-Syntax for tips on how to use <<code>> <</code>>
tags so that the code looks like this:
Task snippet
"include "mbed.h" int main(void) { while(1); //infinite loop writelcd("abcde",1); //task1 wait(0.2); //waiting 200 milisecond before task2 writelcd("12345",2); //task2 wait(0.2); //waiting 200 milisecond before task1 }
When using wait(0.2), the code is "blocking" in that the microcontroller pauses or stops at that line of code. This may be acceptable for some programs.
You might want to look at using a Ticker instead of wait: http://developer.mbed.org/handbook/Ticker
The following is a good LCD Cookbook: http://developer.mbed.org/cookbook/Text-LCD-Enhanced
You could try the following LCD library: http://developer.mbed.org/users/simon/code/TextLCD_HelloWorld/file/ad0b044d0a10/main.cpp
If you use Ticker the code might look like this:
#include "mbed.h" #include "TextLCD.h" TextLCD lcd(p15, p16, p17, p18, p19, p20); // needs to match your wiring and platform Ticker lcdTicker; int lcdTaskIndex; //this is a global variable void setLCD(void) { //You can also use switch, case, break instead of if, else if if (lcdTaskIndex == 1) { //writelcd("abcde",1); //task1 lcd.printf("abcde\n"); lcdTaskIndex = 2; } else if (lcdTaskIndex == 2) { //writelcd("12345",2); //task2 lcd.printf("12345\n"); lcdTaskIndex = 1; } } int main() { lcdTaskIndex = 1; lcdTicker.attach(&setLCD, 0.2); // the function and the interval (0.2 seconds) while(1) { } }
There is also a Task Manager library available:
http://developer.mbed.org/users/Phlaphead/code/TaskManager/
Thank you for your kind suggestion. I'm new in mbed site, and I had not know about <code> tag.
posted by 19 Mar 2015This code does not use a global variable for the task index position, which is better.
#include "mbed.h" #include "TextLCD.h" TextLCD lcd(p15, p16, p17, p18, p19, p20); // needs to match your wiring and platform Ticker lcdTicker; void setLCD(void) { static int lcdTaskIndex = 1; // task index position //You can also use switch, case, break instead of if, else if if (lcdTaskIndex == 1) { //writelcd("abcde",1); //task1 lcd.printf("abcde\n"); lcdTaskIndex = 2; } else if (lcdTaskIndex == 2) { //writelcd("12345",2); //task2 lcd.printf("12345\n"); lcdTaskIndex = 1; } } int main() { lcdTicker.attach(&setLCD, 0.2); // the function and the interval (0.2 seconds) // while(1) { } }
9 years, 1 month ago.
The example given above will print ABCDE and then 12345. If you want the order to be A1B2C3... then you need to define the tasks in a way that outputs each letter independently.
You can use the keyword static to ensure that a variable that is local to a function keeps its value between calls to that function, very handy when you need to keep track of this sort of thing without having to make everything global.
Something along the lines of the code below will call the two tasks in turn, how much they do each call is up to the tasks.
This is a very basic way of doing things, if you need a true task scheduler then you need to use an RTOS of some sort, either the mbed one or something similar.
void nextTask(void) { static int taskID = 0; if (taskID == 0) { task1(); taskID = 1; } else { task2(); taskID = 0; } } void task1(void) { const int NumberOfStates = 2; static int state = 0; switch (state) { case 0: lcd.putc('A'); break; case 1: lcd.putc('B'); break; default: state = 0; break; } state++; if (state == NumberOfStates) state = 0; } void task2(void) { } main() { while (true) { nextTask(); } }
Since it seemed like a quick little project I extended the code above so that you can add and remove tasks on the fly. If you want to give a task a higher priority add it to the list twice.
http://developer.mbed.org/users/AndyA/code/SimpleTaskSwitchDemo/
posted by 19 Mar 2015