Saturday, November 10, 2012

Your DelayMs() Is...Delayed

I'm working like crazy to get more modules and documentation added to the EasyPIC project and as part of that I was working on a tutorial that illustrates a delay. In the delay module of EasyPIC I use the Delay.c and Delay.h files from Microchip. There's only one problem, the functions don't work properly in the free version of the compiler. They're too slow. Like almost exactly twice as slow. That means a call to DelayMs(1000) produces a 2 second delay instead of a 1 second delay.

Solutions

There are a couple of ways to get around this besides paying for a version of the compiler that provides the necessary optimizations. The first, and simplest, is to write your own delay function using the built-in __delay_ms() function. I have found it to be accurate but it doesn't take large arguments so you will have to wrap it in a loop like:

i = 200;
while(i--) __delay_ms(10);

The other option is to use a timer and either trigger an interrupt on overflow or just poll the interrupt flag. This does mean you'd have one less timer available to your application but if that's alright this will provide a very accurate timer.

No comments:

Post a Comment

Keep it clean and civil. That's all I ask.