Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

help: precison timing period in C

Status
Not open for further replies.

coolhand21

Junior Member level 2
Joined
May 17, 2010
Messages
24
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,448
Hi I would like to create timing period of 250ns.

then use multiples to create different intervals such as 1us or 10us

Using C is there a method? I can use nop, but for long intervals >100us it would not be pretty.

btw my clk 24mhz,

thanks cool
 

Try this this delay works with timer 0 and generate for 1 second and for your delay just change the values of TL0 and TH0, and check the result in performance analyzer in keil....

void TIMER() // timer function
{
unsigned int loop;
{
for(loop=0;loop<=40;loop++) //for 1sec.
{
TR0=1; // make timer 0 high
TL0=0x65; // Load tl0 with 0x65
TH0=0xa9; // and th0 with 0xa9 for 0.025second delay
while (TF0==0); // wait till timer over flow
TR0=0; // clear timer 0
TF0=0; // clear timer flag 0
}
}
}



Sharath
 

i don't know which compiler you use but usually there is a delay library which includes delay_ms() and delay_us().
These use a loop method calculated with the cpu clock frequency and they are accurate if you don't use any interrupt while they execute,
if an interrupt executes then total delay will be the interrupt duration + delay function

The second method is to use a timer but you usually do this to repeat a function in specific intervals,
for example check if a key is pressed every 20 ms.

Alex
 

Hi,

For precise time handling Assembly is the best; especially in ns and us level. First you should know instruction-cycle period (time required to execute one opcode); it is based on your uC and clock.

Then you can write assembly-snippet within your C code and call it; since that is assembly-opcode, you can find time required for calling+executing+returning it. Then you call it recursively for multiples of delay.

Wish you help this info.
 

Hi,

I have used Microchip PICs (16F628A, 16F877A, 18F4550) on that way.

Thanks
 

any other suggestions

So you don't want to use "nop" loops or the delay functions or the timers.
what other way are you looking for and why isn't any of the 3 above good enough?
You either have to use a timer or cpu clock cycles, there is no other timing parameter in an mcu.

Alex
 

There is a different way.

Use the WDT and set it to let say 1ms.
Now put the PIC to sleep with sleep instruction and
1ms later +/- 5% you will be woken-up.

wdtcon set to 1ms.
sleep

that is all, 2 cycles and you can make timings between 1ms and a few minutes on some PIC's. Check the WDT module for this !.

nop can be used for 1cycle and goto $+1 can be used for two cycle but one instruction saving you memory location.
 

Hi,
Another way is to use external timer; however accuracy should be guaranteed.
It's better if you try one of above method, then come up with difficulties and restrictions.

Thanks
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top