Experimenting with blinking LEDs, I learned about creating delays with either a software loop or a timeout from a timer. I know that for a simple blinking LED program, where the processes aren't that complicated and timing isn't crucial, using either wouldn't matter. But in more of a time-crucial multitasking context what are the benefits and/or disadvantages of both?
Thanks heaps, here's how I set them up:
Software delay:
void softwaredelay()
{
int i;
for(i=0; i<1000; i++)
{/*Timer Stuff*/}
}
Hardware delay (specific for PIC16 mcu's):
void timerdelay()
{
OPTION_REGbits.PSA = 0;
OPTION_REGbits.PS. = 0b111;
OPTION_REGbits.T0CS = 0;
INTCONbits.T0IF = 0;
TMR0 = 0b11111000;
INTCONbits.T0IE = 1;
while(INTCONbits.T0IF==0)
{/*Timer Stuff*/}
}
Answer
You need to go to the bank (which is just around the corner), and do your laundry. Going to laundry, requires you to wait for the washer to finish, and then wait for the the dryer to finisher.
How would you go about performing these tasks ?
On a lazy day, you could just do the laundry, and then go to the bank. It's not that important. Who cares.
But on a busy day, where you have other things to do, like catching a movie, or studying or whatever task, the efficient thing to do, is you load the first part of the laundry, and then, go to the bank, and then load the second part of the laundry. Now you are free to do other things. That is maximizing your time so you can do other tasks.
Now for a microcontroller or any embedded system, the same is true. If you dont care, about power, time, or anything, then go with a software driven delay. It's easier to implement, and you can scale it as high as you want with great ease.
If you care about power, time, efficiency, then a hardware timer is the way to go. While you wait, you can either go to sleep, or perform another task. It's a bit more complicated to setup, and if multiple tasks require delays, you may run out of hardware timers and then have to resort to a more complicated system of managing time and delays.
If you care about anything other than ease of use : Hardware Timer
If you care only about ease of use : Software
No comments:
Post a Comment