; ; wait_ns waittime in ns , cyles already used ; ; cycles already used will be subtracted from the delay ; the waittime resolution is 1 cycle (delay from exact to +1 cycle) ; the maximum delay at 20MHz (50ns/clock) is 38350ns ; .set Osc_Hz = 7372800 .set cyle_time_ns = (1000000000 / Osc_hz) .def wait_reg = r16 .macro wait_ns .set cycles = (@0 / cyle_time_ns + 1 - @1) .if (cycles > (255 * 3 + 2)) .error "MACRO wait_ns - too many cycles to burn" .else .if (cycles > 3) .set loop_cycles = (cycles / 3) ldi wait_reg,loop_cycles dec wait_reg brne pc-1 .set cycles = (cycles - (loop_cycles * 3)) .endif .if (cycles > 0) .if (cycles & 2) nop nop .endif .if (cycles & 1) nop .endif .endif .endif .endmacro ; ; examples ; example: wait_ns 1000,0 ;want to wait 1000ns after previous instruction ldi r17,42 wait_ns 500,1 ;want to wait 500ns including the previous instruction subi r17,42 rcall my_delay ;the wait is going to be re-used tst r17 rjmp example my_delay: wait_ns 2000,7 ;rcall/ret take 7 clocks ret