Run a Loop for specific time in HI-TECH C

Microcontroller Topics
Post Reply
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Run a Loop for specific time in HI-TECH C

Post by Nandika » Tue Aug 13, 2013 8:05 pm

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.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Run a Loop for specific time in HI-TECH C

Post by Neo » Tue Aug 13, 2013 11:47 pm

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.

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);
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: Run a Loop for specific time in HI-TECH C

Post by Nandika » Wed Aug 14, 2013 12:01 am

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? :x
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Run a Loop for specific time in HI-TECH C

Post by Neo » Wed Aug 14, 2013 12:47 am

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).
interrupt.jpg
interrupt.jpg (32.51 KiB) Viewed 6803 times
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 :)
User avatar
SemiconductorCat
Major
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

Post by SemiconductorCat » Wed Aug 14, 2013 4:33 am

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,

Code: Select all

int main()
{
  init();
  while(){
    check_adc();
    check_button_pressed();
    turn_on_led();
  }
}
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.

Code: Select all

int main()
{
  init();

  while(true){
     while(tmrofc%2){
       // Thread 1 code //
     }

    while(!trmofc%2){
      // thread 2 code //
    }
  }
}
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 8-) ].

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;
  }
}
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.
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: Run a Loop for specific time in HI-TECH C

Post by Nandika » Thu Aug 15, 2013 9:22 pm

Thank Neo and Semi,
I can understand your explanation Neo.I know something about thread,CPU cores...
Thanks for Semi for example code.
Post Reply

Return to “Microcontrollers”