;; Delays for RS_TICKS less a fixed value (9 is the current value) ;; currently depends on 6Mhz clock! - possible bug in that it delays for ;; 70 cycles (total 79) where RS_TICKS is 78 (see next bit for reason) ;; For 10Mhz operation : ;; RS_TICKS will be 130.2 hence we either creep back by 0.2 or add an extra ;; cycle and creep forward 0.8. ;; Given how close Denis starts to the bits leading edge, I suspect that he ;; delibarately added the extra cycle at 6Mhz to cause forward creep rather ;; than back. ;; I shall do likewise and cause a delay for 122 cycles after 8 bits this only ;; puts us 6/1/2 cycles futher along along (8%) anyway. ;; I wont do this here tho! i shall add it to RS_TICKS at the top _delay_x_9: ;; Smallest delay possible with this routine is 19 cycles (38400 @ 4Mhz clock ;; is possible using 26 cycles so i dont think its an issue ) ;; Uses TEMP_0 and TEMP_1 and 7 instrctions ;; Delay comprises of ;; 9 - static value ;; 2 - call to get here ;; 6 - routine overhead ;; (n*3) - where n is number of loops required ;; -1 - (-1 coz last time round loop only executes 2 cycles not 3) ;; r - where r is (n mod 3) to get number of extras leftover ;; ;; We need to calculate sumber of times round loop which is RS_TICKS less the ;; static values (9+2+6-1)=16 WAIT_CYCLES set (RS_TICKS - 16) WAIT_LOOPS set (WAIT_CYCLES/3) if (WAIT_LOOPS < 1) error endif movwf TEMP_1 ; (1) store w to temp_1 movlw WAIT_LOOPS ; (1) get number of loops to do WAIT_CYCLES set (WAIT_CYCLES - (WAIT_LOOPS*3)); calc number of spare cycles movwf TEMP_0 ; (1) store literal in TEMP_0 movf TEMP_1,W ; (1) recover w from TEMP_1 _tx_delay: decfsz TEMP_0,F ; (1) decrement and jump if zero goto _tx_delay ; (2(1 if skipped)) go back round loop ; we are one cycle down coz the goto got missed ; have to account for it with a nop if (WAIT_CYCLES == 1) nop ; (1) endif if (WAIT_CYCLES == 2) goto _delay_x_9_next2 ; (2) _delay_x_9_next2: endif _delay4: return ; (2)
Hopefuly with all those comments its fairly self explanatory.