Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
29 jimmy 1
/*
2
   Precise Delay Functions
3
   V 0.5, Martin Thomas, 9/2004
4
 
5
   Inspired by the avr-libc's loop-code
6
*/
7
 
8
#ifndef _delay_h_
9
#define _delay_h_
10
 
11
#include <inttypes.h>
12
#include <avr/io.h>
13
 
14
/* delay function for microsec
15
   4 cpu cycles per loop + 1 cycles(?) overhead
16
   when a constant is passed. */
17
static inline void delayloop16(uint16_t count)
18
{
19
    asm volatile (  "cp  %A0,__zero_reg__ \n\t"  \
20
                     "cpc %B0,__zero_reg__ \n\t"  \
21
                     "breq L_Exit_%=       \n\t"  \
22
                     "L_LOOP_%=:           \n\t"  \
23
                     "sbiw %0,1            \n\t"  \
24
                     "brne L_LOOP_%=       \n\t"  \
25
                     "L_Exit_%=:           \n\t"  \
26
                     : "=w" (count)
27
                     : "0"  (count)
28
                   );                            
29
}
30
// delayloop16(x) eats 4 cycles per x
31
#define DELAY_US_CONV(us) ((uint16_t)(((((us)*1000L)/(1000000000/F_OSC))-1)/4))
32
#define delay_us(us)      delayloop16(DELAY_US_CONV(us))
33
 
34
/* delay function for millisec
35
  (6 cycles per x + 20(?) overhead) */
36
void delayloop32( uint32_t l); // not inline
37
#define DELAY_MS_CONV(ms) ( (uint32_t) (ms*(F_OSC/6000L)) ) 
38
#define delay_ms(ms)  delayloop32(DELAY_MS_CONV(ms))
39
 
40
/* mth 9/04:
41
   Remark uSeconds:
42
   Main Oscillator Clock given by F_OSC (makefile) in Hz
43
   one CPU-Cycle takes 1/F_OSC seconds => 1000000/F_OSC uSeconds
44
   so: 1 uSecond takes F_OSC/1000000 CPU-Cyles. The following code
45
   is inspired by the avr-libc delay_loop2 function.
46
   This it not "that precise" since it takes at least 4 cycles
47
   but should be o.k. with any parameter (even 0).
48
   Call function with delayloop(DELAYUS(dt [in uSeconds])).
49
*/
50
 
51
#endif