while(1)
{
// THREAD 0 ===========================================
while(thread == 0)
{
// this thread looks for a new second and updates the seconds
if(newsecond)
{
newsecond = 0;
secs++;
refresh_display = 1; // tell other thread to redraw LCD
}
}
// THREAD 1 ===========================================
while(thread == 1)
{
// this thread updates the minutes and hours
if(secs >= 60)
{
secs = 0;
mins++;
}
if(mins >= 60)
{
mins = 0;
hours++;
}
if(hours > 12) hours = 1;
}
while(1)
{
if(newsecond) // if "newsecond" isr flag
{ newsecond = 0; // reset "newsecond" isr flag
secs++; // and bump "secs"
if(secs >= 60) // if "secs" overflow
{ secs = 0; // reset "secs" var'
mins++; // and bump "mins"
}
if(mins >= 60) // if "mins" overflow
{ mins = 0; // reset "mins" var'
hours++; // and bump "hours"
}
if(hours > 12) // if "hours" overflow
{ hours = 1; // reset "hours" var'
}
ByteToStr(secs,txt); // format and display secs
txt[0] = ':';
if(txt[1] == ' ') txt[1] = '0';
RomanLCD_Out(0,6,txt);
ByteToStr(mins,txt); // format and display mins
txt[0] = ':';
if(txt[1] == ' ') txt[1] = '0';
RomanLCD_Out(0,3,txt);
ByteToStr(hours,txt); // format and display hours
RomanLCD_Out(0,0,txt);
}
if(thread2timer > 250) // if 250-msec interval
{ thread2timer = 0; // reset 250-msec timer
if(PORTC.F0) // if set mins button
{ mins++; //
mscount = 0; //
secs = 0; //
newsecond = 1; //
}
if(PORTC.F1) // if set hours button
{ hours++; //
mscount = 0; //
secs = 0; //
newsecond = 1; //
}
}
}
...
It seems you're adding a level of complexity to the program that just isn't necessary but perhaps I'm missing something more subtle? Is there an advantage to your "threaded" program over a simpler program running at any loop speed that I'm missing?
...
DirtyLude; said:...
I find it hard to believe that there are not a million RTOS's out there for the PIC already. FreeRTOS says it has ports for the PIC.
...
Why would you want to dedicate 30% or 20% or 10% of processing time to a task that doesn't require that amount of time? Why not just run it once when needed? Time-Slice "threading" is probably not the best way to use the limited resources on a small PIC mcu.Hi Mike, the first code examples on the page are multi-threading by dividing the processor time between tasks. So you can allocate 30% of time to task1, and 10% of the time to task2 etc.
Why would you want to dedicate 30% or 20% or 10% of processing time to a task that doesn't require that amount of time? ...
...
The Arduino guys were pretty clever in this regard. They have a 1-msec system timer that you can use to schedule tasks that run asynchronously (or synchronously) at any interval -- you can have one task that runs every 250-msecs, one that runs every 1250-msecs, and another one that runs every 60000-msecs (1 second), etc. And you still have event driven tasks running at the main loop speed and can use almost any method you like for linking dependant and/or synchronous tasks.
...
Unfortunately that's just too rigid and too inflexible a system to be useful for more than a few simple applications. If you have a single task that requires a large time slice you wouldn't be able to execute other tasks that require servicing at intervals that are shorter than your large time slice task.Because as a system it MIGHT be useful. Imagine if you need to do some massive calculations that will take a lot of time. You could allocate X% of the time to doing that task while other more urgent tasks get more of the timeslice.
Did everyone swallow a handful of grumpy pills over Christmas? Hmmmm.. maybe it's "Back to work Monday"...
It might be useful is not a reason to write code! You write code because you need it, or someone else needs it. If you don't have a specific application your code can fill then you're just writing code for the sake of writing code. There are many multitasking and RTOS systems out there for PIC as far as I know. Unless you're designing something for a specific need I'd question the usefulness of the project as there are no applications which require it. We're not talking about some ground breaking new method of multitasking here, just something that for possibly not obvious reasons has never been required on a PIC, just because it doesn't exist isn't a reason to write it.
Because as a system it MIGHT be useful. Imagine if you need to do some massive calculations that will take a lot of time. You could allocate X% of the time to doing that task while other more urgent tasks get more of the timeslice.
There's nothing to imagine there, universities and governments buy up PS3's by the score because of their parallel processing abilities, it's cheaper than buying over the counter super computing clusters =\ Such a system is not practical for micro controllers however unless an application could be imagined where it would be useful. The core multitasking idea is nothing without a supporting network of software that could intelligent transfer applications and data transparently. So it's a good idea, but needs a little more follow through. The processing power of even a network of a thousand micro controllers wouldn't even come close to a single more advanced processor. And distributed computing doesn't make sense on that scale.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?