Pages: [1]   Go Down
Author Topic: delayclockcycle()  (Read 707 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!
It would be a nice addition it there would be a delay smaller than delaymicroseconds() that deals directly with clock cycles.
Anybody else thinks so?
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 203
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Personally I wouldn't use clock cycles for timing since a clock rate can be changed, except for very small intervals for which rounding is an issue, and then I'm going to need to integrate it with operations around it.

You can't get in and out of a function in much under a microsecond, so if you are looking for better resolution it will need to be inline. For very short delays you can...
Code:
__asm__( "nop\n\t"
         "nop\n\t");
... for as many nops as you need at one cycle per nop. That could be a significant code size burden for longer delays. If you have a register to spare there are probably two cycle instructions that will make better code density. At some point it is better to loop and count.

If you are needing this sort of resolution, then you will also want to use objdump to disassemble your code and see what the compiler did. It has some amusing reordering rules that can put initialization computations inside your time critical areas.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Jims!
That is exactly what I was looking for.
Really helpful for precise timing protocols.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How would other ASM code be included? Could I write a byte to a port in one clock, then NOP for a while, and then loop, all in ASM?

I want to make very precise timing (I will be generating between 30 and 60 khz plus 60-120hz) for VGA, and if I have some spare uC time, I'd muck around with other stuff. I just want this stuff to be über efficient.



(I dont have freq counter so I have to use sillyscope)
« Last Edit: April 08, 2007, 06:38:09 pm by HL-SMG » Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 203
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, you can do such things when time is critical (turn off the interrupts while you are at it). Check the GCC Inline Assembler HOWTO. http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html and the Atmel AVR-8 instruction set.
Logged

Bangalore, India
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Check the GCC Inline Assembler HOWTO. http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html and the Atmel AVR-8 instruction set.

That link uses the x86 instruction set, which is potentially rather confusing smiley  (Or at least it had me scratching my head at 2am....)

The AVR-specific equivalent, include a list of opcodes and constraints is at http://www.nongnu.org/avr-libc/user-manual/inline_asm.html

Plus  more links and some of my ramblings about getting started with inline assembler over at http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1207370768/2#2
Logged

Pages: [1]   Go Up
Jump to: