How to check value of delay in terms of seconds

Status
Not open for further replies.

haxan

New Member
Hi, How can i check the duration of a delay in terms of divisions of seconds (micro, mili etc)

I want to generate a function to delay100us but cannot calibrate it accurately (without use of timers).

For a clock of 4MHz,
Code:
void Delay100Us(unsigned char time)
{
	char x;
	for(x=0;x<time;x++)
		Delay10TCYx(9);
}

does the above code generate Delay100Us(1) 100Us ? Please help me.

P.S: I dont have any debugging tool kits, just proteus software.
 
Last edited:
Hi,

Use MPLABs Simulator function, it has a Stopwatch function that allows you to check the times from instruction level to seconds.
 
Ok thank you, the problem i am facing now is that i am unable to only run that particular function once. Its keeps on running and starts opening library files.
 
Hi,

Ok thank you, the problem i am facing now is that i am unable to only run that particular function once. Its keeps on running and starts opening library files.


Only do Assembler, but you should be able to use Breakpoints in your code, simply move the pointer to the first line of the delay code and right click and select a breakpoint.

Do the same but on the line of code after the delay routine - that defines the timing period for the stopwatch.

In Sim press F9 to get to the first breakpoint, reset the stopwatch, ensuring you have also selected the correct osc frequency, press F9 again at it will then do the delay routine and stop on its exit so you can see the actual time on the stopwatch.
 
Thank you Wp100, that works. One problem now is that how can i give a variable some value which is not to be included into the stopwatch time. For example, i have a loop which runs 't' times, how can i set the value of 't' for debugging.
 
Hi,
how can i give a variable some value which is not to be included into the stopwatch time.

If the setting the variable is part of the delay routine why exclude it ?

Setting that variable before your first breakpoint is the way to do it in Assembler but no idea how you do that in C - sure others can help though......
 
This sounded a bit of a challenge and so I had a play.

To make your delay accurate including the call etc you can do,
Code:
void Delay100Us(unsigned char time)
{
    char x;
    for(x=0;x<time-1;x++){
        Delay10TCYx(9);
        Delay1TCY();
        Delay1TCY();
        Delay1TCY();
    }  
    x=25;
    _asm
    movlw   x
loop:
    decfsz  PLUSW2,1,0
    goto    loop
    _endasm
}

The asm is the simplest way to get the timing spot on. It's not obvious what it is doing as it's using a local variable.

To test it in the simulator, put a breakpoint on the function call, clear the stopwatch and hit F8 to step over the line.

Mike.
 
I'm still wondering why you're doing this in the first place. Most MCU C compiler libraries come with fully calibrated delay routines.
 
C18 compiler from microchip has Delays library which provides delay in terms of clock cycles. They are not actually calibrated in terms of divisions of seconds.

The delay routines are like this:

Code:
 *               Delay10TCYx(x)
 *               Delay100TCYx(x)
 *               Delay1KTCYx(x)
 *               Delay10KTCYx(x)
 
So what, then just write an ultra basic routine that does the math to convert from the number of clock cycles to real world time based on the current chip speed.
 
The C18 delay routines provide a delay that is fixed at compile time. No variable delay is available and so the above is quite useful. Note, the above actually uses the fixed C18 delays.

Mike.
 
There's no wrapper or additional library code out there? I find that odd. I'm just starting to use AVR GCC for my AVR's and even stock off a fresh compile it comes with routines to provide us or ms delays.
 
In C18 its not like that The above code which Pommie sent will only work for 4MHz frequency. Hence if frequency is changed, will need to change the delay function little bit.

In other C compilers like BoostC and MicroC these functions are pre made.

I actually want to move to AVR soon after i am done with a simple project which requires TCP/IP Stack free from microchip
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…