Hi Friends,
I want to run a loop(for..while...) for pre determined time in HI-TECH C.
For example,I want to run some while loop for 10mS.
How can I do This?
Thank in advance.
Run a Loop for specific time in HI-TECH C
Re: Run a Loop for specific time in HI-TECH C
Hello Nandika bro,
It is very nice that you are getting in to programming with micros. Very very nice.
With the previous topic (https://robot.lk/viewtopic.php?f=18&t=4042), I came to know that you have used timer interrupt and you also have mentioned a counter there. You could do a trick as below.
It is very nice that you are getting in to programming with micros. Very very nice.
With the previous topic (https://robot.lk/viewtopic.php?f=18&t=4042), I came to know that you have used timer interrupt and you also have mentioned a counter there. You could do a trick as below.
Code: Select all
int pre_time, time_to_run, time_diff;
pre_time = tmrofc;
time_to_run = 100; // this is as per your timer interrupt setting (if it is 1 second, then we will run the loop for 100 seconds
do {
// write your code here
time_diff = tmrofc - pre_time;
} while (time_diff < time_to_run);
Re: Run a Loop for specific time in HI-TECH C
Thanks Neo,
Timers is good way.But,I have only one timer for all.
Can't we do this using Delay loop?
Can I handle some thread?Like Java or .NET?
Timers is good way.But,I have only one timer for all.
Can't we do this using Delay loop?
Can I handle some thread?Like Java or .NET?
Re: Run a Loop for specific time in HI-TECH C
When you mention threads, there are two ways that the threads work in systems. As you know, unlike a process, the concept of a thread is a program that runs separately by sharing the same data space of the main program. A process would have a separate Code-Data-Stack block for each process.
In earlier days when CPUs had only one core, threads were running by sharing the same CPU with a time slice. However with latest CPU processing techniques, we have multidimensional processors, multi-threading processors, multiple core processors, etc... With these hardware level processing techniques, it is possible to assign threads to separate cores which gives the performance to each thread just as they run in separate machines individually.
When it comes to microcontrollers that we use today, having a single core (RISC hybrid pipeline architecture), having multiple threads means about having time slice per each thread. So there would be no performance advantage, just the advantage of having two tasks run in parallel.
With the PIC, you can use two timer interrupts to trigger in a defined time interval to run separate codes. If you consider you previous program, you will see that you are already using threads with main function running in background while the interrupts trigger with a high priority by preempting (http://en.wikipedia.org/wiki/Preemption_%28computing%29) the main routine (idle routine). Having delay loops will eat the CPU time unnecessarily. It is okay to use them in the idle loop but never inside interrupts. you need to service the interrupt routine as soon as possible by enabling some flags to be processed at the main loop.
If you are not clear, you may ask. Some stuff are hard to explain without a giving a long description
In earlier days when CPUs had only one core, threads were running by sharing the same CPU with a time slice. However with latest CPU processing techniques, we have multidimensional processors, multi-threading processors, multiple core processors, etc... With these hardware level processing techniques, it is possible to assign threads to separate cores which gives the performance to each thread just as they run in separate machines individually.
When it comes to microcontrollers that we use today, having a single core (RISC hybrid pipeline architecture), having multiple threads means about having time slice per each thread. So there would be no performance advantage, just the advantage of having two tasks run in parallel.
With the PIC, you can use two timer interrupts to trigger in a defined time interval to run separate codes. If you consider you previous program, you will see that you are already using threads with main function running in background while the interrupts trigger with a high priority by preempting (http://en.wikipedia.org/wiki/Preemption_%28computing%29) the main routine (idle routine). Having delay loops will eat the CPU time unnecessarily. It is okay to use them in the idle loop but never inside interrupts. you need to service the interrupt routine as soon as possible by enabling some flags to be processed at the main loop.
If you are not clear, you may ask. Some stuff are hard to explain without a giving a long description
- SemiconductorCat
- Major
- Posts: 455
- Joined: Mon Aug 22, 2011 8:42 pm
- Location: currently in hyperspace
Re: Run a Loop for specific time in HI-TECH C
have to mention one thing, the code that I've seen about microcontroller threads are almost always hardcoded.
For a example simply suppose you have two while loops in main as seperate threads like,
Then if you have two threads, I have seen they have exploited the '%' operators , if TMR0 is odd run the first
thread and if TMR0 is even then run the second. Like this.
Since it's completely hardcoded , you don't need to worry about deadlock issues or starvation issues at all.
Just only make sure that one loop code spends less than one overflow time. You could program to many
threads using a switch ,not my ideas just ideas that I've extracted through code reviewing[ that's why I always
tell code-reviewing is a good heaven thing ].
Simple code review will find you all this.
But if you really need to code a pure dynamic scheduler then THUMBS up you, I could hardly help you, I never done that
before for PIC like such small devices.
For a example simply suppose you have two while loops in main as seperate threads like,
Code: Select all
int main()
{
init();
while(){
check_adc();
check_button_pressed();
turn_on_led();
}
}
thread and if TMR0 is even then run the second. Like this.
Code: Select all
int main()
{
init();
while(true){
while(tmrofc%2){
// Thread 1 code //
}
while(!trmofc%2){
// thread 2 code //
}
}
}
Just only make sure that one loop code spends less than one overflow time. You could program to many
threads using a switch ,not my ideas just ideas that I've extracted through code reviewing[ that's why I always
tell code-reviewing is a good heaven thing ].
Code: Select all
while(true){
switch (tmrofc %n){
case 1:
//thread 1 code //
break;
case 2:
// thread 2 code //
break;
case 3:
// thread 3 code //
break;
//...
case n:
// thread n code//
break;
}
}
But if you really need to code a pure dynamic scheduler then THUMBS up you, I could hardly help you, I never done that
before for PIC like such small devices.
Re: Run a Loop for specific time in HI-TECH C
Thank Neo and Semi,
I can understand your explanation Neo.I know something about thread,CPU cores...
Thanks for Semi for example code.
I can understand your explanation Neo.I know something about thread,CPU cores...
Thanks for Semi for example code.