Hi Roman,
Forgive me but I don't see the advantage of the "thread" architecture in your examples. Take your "multi-thread3.c" program for example. I don't see why you would dedicate 1-msec of processing time to "thread 0" and 1-msec of processing time to "thread 1" when the task in "thread 0" is keyed to an interrupt flag that occurs once every second and the tasks in "thread 1" are keyed to the once-per-second update of the "secs" variable in "thread 0".
Code:
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;
}
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?
Code:
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; //
}
}
}