chiark / gitweb /
dfefe58b65e46bdb76f11cbee2951366a99a96f0
[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_sspadd equ (mclock/(50*4)) - 1 ; 50kbit/s
44
45 ;----------------------------------------------------------------------
46 ; NMRA at 50us per division
47 ; ... uh, this isn't going to work at 1MHz because that's only 12.5
48 ; insns per NMRA division
49
50
51 ;----------------------------------------------------------------------
52 ; busy-wait delay loop, originally from flasher.asm
53
54 implement_busywait_delay macro tclock
55
56 ; we always leave each loop when its counter has reached 0, so on
57 ; entry we assume it's already 0.  For the loops which want to
58 ; count all the way (256 iterations) that's good.
59
60         ; set a bit which says how fast the led
61         ; should flash and count down from 2^(that bit)
62  if tclock >= 3915
63   local fast=1
64   local medium_cycles=198000
65  else
66   local fast=0
67   local medium_cycles=768
68  endif
69                         ; now 1/(medium_period in ms)
70                         ; = tclock / (medium_cycles*4)
71         movlw           50 * tclock / medium_cycles ; 200ms
72         movwf           delay_countslow, 0
73
74  local delayslow_loop
75  local delaymedium_loop
76 delayslow_loop
77
78 delaymedium_loop
79
80  if fast
81  local delayfast_loop
82 delayfast_loop
83         decfsz          delay_countfast, 1, 0   ; 1 cycle
84         goto            delayfast_loop          ; 2 cycles (skipped or not)
85 ; exited delayfast_loop                         ; total: 3 * 256 = 768 cycles
86  endif
87
88
89         decfsz          delay_countmedium, 1, 0 ; 1 cycle
90         goto            delaymedium_loop        ; 2 cycles (skipped or not)
91 ; exited delaymedium_loop                       ; total: ~198000 cycles
92                                                 ; or     ~768 cycles (no fast)
93
94         decfsz          delay_countslow, 1, 0
95         goto            delayslow_loop
96 ; exited delayslow_loop
97
98         endm
99
100 define_busywait_delay macro
101 delay
102         implement_busywait_delay clock
103         return
104
105         endm