chiark / gitweb /
fixes during movpos wip
[trains.git] / iwjpictest / clockvaries.inc
1 ;----------------------------------------------------------------------
2 ; various definitions for clock variation
3
4  radix dec
5
6 ;----------------------------------------------------------------------
7 ; for serial port at 9600
8 ; according to table in datasheet top right p186
9
10  if mclock==32000
11 serial_brgh equ (1<<BRGH)
12 serial_spbrg equ 207
13  endif
14  if mclock==20000
15 serial_brgh equ (1<<BRGH)
16 serial_spbrg equ 129
17  endif
18  if mclock==1000
19 serial_brgh equ (1<<BRGH)
20 serial_spbrg equ 6 ; 7% error :-(
21  endif
22
23 bsc_txsta_brgh macro
24  if serial_brgh
25         bsf     TXSTA,2,0       ; set high baud rate
26  else
27         bcf     TXSTA,2,0       ; set low baud rate
28  endif
29  endm
30
31 movlw_movwf_spbrg macro
32  if serial_spbrg > 0
33  else
34   error "unsupported clock speed (serial_spbrg)"
35  endif
36         movlw   serial_spbrg
37         movwf   SPBRG,0
38         endm
39
40 ;----------------------------------------------------------------------
41 ; for i2c at 50kbit/s
42
43 i2c_kbitpersec equ 50
44 i2c_sspadd equ (mclock/(i2c_kbitpersec*4)) - 1 ; 50kbit/s
45
46 ;----------------------------------------------------------------------
47 ; NMRA at 50us per division
48 ; ... uh, this isn't going to work at 1MHz because that's only 12.5
49 ; insns per NMRA division
50
51
52 ;----------------------------------------------------------------------
53 ; busy-wait delay loop, originally from flasher.asm
54
55 implement_busywait_delay macro tclock
56
57 ; we always leave each loop when its counter has reached 0, so on
58 ; entry we assume it's already 0.  For the loops which want to
59 ; count all the way (256 iterations) that's good.
60
61         ; set a bit which says how fast the led
62         ; should flash and count down from 2^(that bit)
63  if tclock >= 3915
64   local fast=1
65   local medium_cycles=198000
66  else
67   local fast=0
68   local medium_cycles=768
69  endif
70                         ; now 1/(medium_period in ms)
71                         ; = tclock / (medium_cycles*4)
72         movlw           50 * tclock / medium_cycles ; 200ms
73         movwf           delay_countslow, 0
74
75  local delayslow_loop
76  local delaymedium_loop
77 delayslow_loop
78
79 delaymedium_loop
80
81  if fast
82  local delayfast_loop
83 delayfast_loop
84         decfsz          delay_countfast, 1, 0   ; 1 cycle
85         goto            delayfast_loop          ; 2 cycles (skipped or not)
86 ; exited delayfast_loop                         ; total: 3 * 256 = 768 cycles
87  endif
88
89
90         decfsz          delay_countmedium, 1, 0 ; 1 cycle
91         goto            delaymedium_loop        ; 2 cycles (skipped or not)
92 ; exited delaymedium_loop                       ; total: ~198000 cycles
93                                                 ; or     ~768 cycles (no fast)
94
95         decfsz          delay_countslow, 1, 0
96         goto            delayslow_loop
97 ; exited delayslow_loop
98
99         endm
100
101 define_busywait_delay macro
102 delay
103         implement_busywait_delay clock
104         return
105
106         endm