.include "tn13adef.inc" ; real target //.include "m2560def.inc" ; my test board /**************************************************************************** * Fractional divider using bresenham algorithm * Maximum output jitter is 1 input clock cycle * Integer divider ratios are jitter free * * fuses * * L: 0x78 ;use external clock * H: 0xF9 ;5V supply BOD configuration * * clock input : PB3 (CLKI) * clock output : PB4 * * f_in / f_out = N / M * * Notes: * * 1) M is limited to range 1-32767 * 2) N / M must be >= 14 * 3) N / M must be <= 524000 * 4) to calculate M and N, write down the fraction of f_out / f_in * and reduce it until M is <= 32767 * * Example: * * f_in / f_out = N / M = 11,000,000 / 310,000 = 1,100,000 / 31,000 * * ****************************************************************************/ .equ M = 31000 .equ N = 11000000 .equ ticks_per_half_cycle = N / (2*M) .equ rest = N % (2*M) .if (M>32767 || M<1) .error "M must be within 1-32767" .endif .if (ticks_per_half_cycle < 7) .error "N/M must be >= 14" .endif .if (ticks_per_half_cycle > 262000 ) .error "N/M must be <= 524000" .endif .def tmp = r16 .def cnt_l = r17 .def cnt_h = r18 .def m_l = r19 .def m_h = r20 .def r_l = r21 .def r_h = r22 .def delay_cnt_l = r24 ; r25:r24 .def delay_cnt_h = r25 ;############################################################################ .macro my_delay .if (@0) > 262140 .error "max. delay 262140 cycles!" .endif .if (@0) < 0 .error "delay negative!" .endif .if (@0 == 1) nop .endif .if (@0 == 2) nop nop .endif .if (@0 == 3) nop nop nop .endif .if (@0 == 4) nop nop nop nop .endif .if @0 >= 5 .if ((@0-1) % 4) == 1 nop .endif .if ((@0-1) % 4) == 2 nop nop .endif .if ((@0-1) % 4) == 3 nop nop nop .endif ldi delay_cnt_l, low((@0-1) / 4) ldi delay_cnt_h, high((@0-1) / 4) loop: sbiw delay_cnt_l, 1 brne loop .endif .endm . ;switch off all unnecessary hardware stuff ################################## ldi tmp,(1<