comparison src/i2c.asm @ 626:be8787f2034d

compass3 support for 3.01 branch
author heinrichsweikamp
date Sun, 23 Jun 2019 15:21:16 +0200
parents 1ad0531e9078
children
comparison
equal deleted inserted replaced
625:5c2ca77ce2df 626:be8787f2034d
1 ;============================================================================= 1 ;=============================================================================
2 ; 2 ;
3 ; File i2c.asm V2.99c 3 ; File i2c.asm
4 ; 4 ;
5 ; I2C Interface 5 ; I2C Interface
6 ;
7 ; Compass0
8 ; HMC5883L's read address (8-Bit): 0x3D
9 ; HMC5883L's write address (8-Bit): 0x3C
10 ; MMA8452Q's read address (8-Bit): 0x39
11 ; MMA8452Q's write address (8-Bit): 0x38
12 ;
13 ; Compass1
14 ; LSM303D's read address (8-Bit): 0x3D
15 ; LSM303D's write address (8-Bit): 0x3C
16 ;
17 ; Compass2
18 ; LSM303AGR's Compass read address (8-Bit): 0x3D
19 ; LSM303AGR's Compass write address (8-Bit): 0x3C
20 ; LSM303AGR's Acceleration read address (8-Bit): 0x33
21 ; LSM303AGR's Acceleration write address (8-Bit): 0x32
22 ;
23 ; Compass3
24 ; LSM303C's Compass read address (8-Bit): 0x3D
25 ; LSM303C's Compass write address (8-Bit): 0x3C
26 ; LSM303C's Acceleration read address (8-Bit): 0x3B
27 ; LSM303C's Acceleration write address (8-Bit): 0x3A
28 ;
29 ; RX Circuity
30 ; RX Circuity read address (8-Bit): 0x51
31 ; RX Circuity write address (8-Bit): 0x50
32 ;
6 ; 33 ;
7 ; Copyright (c) 2012, JD Gascuel, HeinrichsWeikamp, all right reserved. 34 ; Copyright (c) 2012, JD Gascuel, HeinrichsWeikamp, all right reserved.
8 ;============================================================================= 35 ;=============================================================================
9 ;
10 ; Compass0
11 ; --------
12 ; HMC5883L's read address (8-Bit): 0x3D
13 ; HMC5883L's write address (8-Bit): 0x3C
14 ; MMA8452Q's read address (8-Bit): 0x39
15 ; MMA8452Q's write address (8-Bit): 0x38
16 ;
17 ; Compass1
18 ; --------
19 ; LSM303D's read address (8-Bit): 0x3D
20 ; LSM303D's write address (8-Bit): 0x3C
21 ;
22 ; Compass2
23 ; --------
24 ; LSM303AGR's Compass read address (8-Bit): 0x3D
25 ; LSM303AGR's Compass write address (8-Bit): 0x3C
26 ; LSM303AGR's Acceleration read address (8-Bit): 0x33
27 ; LSM303AGR's Acceleration write address (8-Bit): 0x32
28 ;
29 ; RX Circuity
30 ; -----------
31 ; RX Circuity read address (8-Bit): 0x51
32 ; RX Circuity write address (8-Bit): 0x50
33 ;
34 ;
35
36 ; HISTORY 36 ; HISTORY
37 ; 2012-08-22 : [mH] Creation 37 ; 2012-08-22 : [mH] Creation
38 ; 2018-02-18 : [mH] Sync with hwOS Sport release 38
39 39
40 40 #include "hwos.inc" ; Mandatory header
41 #include "hwos.inc" ; Mandatory header
42 #include "wait.inc" 41 #include "wait.inc"
43 #include "math.inc" 42 #include "math.inc"
44 #include "external_flash.inc" 43 #include "external_flash.inc"
45 44
46 45 i2c CODE
47 i2c CODE
48
49 ;=============================================================================
50 46
51 I2C_TX: 47 I2C_TX:
52 movwf SSP1BUF 48 movwf SSP1BUF
53 rcall WaitMSSP 49 rcall WaitMSSP
54 bra I2C_WaitforACK ; returns... 50 bra I2C_WaitforACK ; Returns...
55 51
56 I2C_TwoBytesRX_div16: ; get two bytes and divide lo:hi/16 (signed) 52 I2C_TwoBytesRX_div16: ; Get two bytes and devide lo:hi/16 (signed)
57 rcall I2C_OneByteRX ; get one byte 53 rcall I2C_OneByteRX ; Get one byte
58 movff SSP1BUF,hi ; data byte 54 movff SSP1BUF,hi ; Data Byte
59 rcall I2C_OneByteRX ; get one byte 55 rcall I2C_OneByteRX ; Get one byte
60 movff SSP1BUF,lo ; data byte 56 movff SSP1BUF,lo ; Data Byte
61 I2C_TwoBytesRX_div16_2: ; divide lo:hi/16 (signed) only 57 I2C_TwoBytesRX_div16_2: ; devide lo:hi/16 (signed) only
62 bcf STATUS,C 58 bcf STATUS,C
63 btfsc hi,7 ; copy sign bit to carry 59 btfsc hi,7 ; Copy sign bit to carry
64 bsf STATUS,C 60 bsf STATUS,C
65 rrcf hi ; /2 61 rrcf hi ; /2
66 rrcf lo 62 rrcf lo
67 I2C_TwoBytesRX_div8_2: ; divide lo:hi/8 (signed) only 63 I2C_TwoBytesRX_div8_2: ; devide lo:hi/8 (signed) only
68 bcf STATUS,C 64 bcf STATUS,C
69 btfsc hi,7 ; copy sign bit to carry 65 btfsc hi,7 ; Copy sign bit to carry
70 bsf STATUS,C 66 bsf STATUS,C
71 rrcf hi ; /4 67 rrcf hi ; /4
72 rrcf lo 68 rrcf lo
73 bcf STATUS,C 69 bcf STATUS,C
74 btfsc hi,7 ; copy sign bit to carry 70 btfsc hi,7 ; Copy sign bit to carry
75 bsf STATUS,C 71 bsf STATUS,C
76 rrcf hi ; /8 72 rrcf hi ; /8
77 rrcf lo 73 rrcf lo
78 bcf STATUS,C 74 bcf STATUS,C
79 btfsc hi,7 ; copy sign bit to carry 75 btfsc hi,7 ; Copy sign bit to carry
80 bsf STATUS,C 76 bsf STATUS,C
81 rrcf hi ; /16 77 rrcf hi ; /16
82 rrcf lo 78 rrcf lo
83 return 79 return
84 80
85 global I2C_RX_accelerometer 81 global I2C_RX_accelerometer
86 I2C_RX_accelerometer: 82 I2C_RX_accelerometer:
87 btfsc compass_type2 ; compass2 ? 83 btfsc compass_type3 ; compass3
88 bra I2C_RX_accelerometer_compass2 ; YES 84 bra I2C_RX_accelerometer_compass3 ; yes
89 btfsc compass_type ; compass1 ? 85 btfsc compass_type2 ; compass2
90 bra I2C_RX_accelerometer_compass1 ; YES 86 bra I2C_RX_accelerometer_compass2 ; yes
87 btfsc compass_type ; compass1?
88 bra I2C_RX_accelerometer_compass1 ; yes
91 I2C_RX_accelerometer_compass0: 89 I2C_RX_accelerometer_compass0:
92 bsf SSP1CON2,SEN ; start condition 90 bsf SSP1CON2,SEN ; Start condition
93 rcall WaitMSSP 91 rcall WaitMSSP
94 movlw 0x38 ; address 92 movlw 0x38 ; address
95 rcall I2C_TX 93 rcall I2C_TX
96 movlw 0x00 94 movlw 0x00
97 rcall I2C_TX 95 rcall I2C_TX
98 bsf SSP1CON2,RSEN ; repeated start condition 96 bsf SSP1CON2,RSEN ; Repeated start condition (!)
99 rcall WaitMSSP 97 rcall WaitMSSP
100 movlw 0x39 ; address 98 movlw 0x39 ; address
101 rcall I2C_TX 99 rcall I2C_TX
102 100
103 rcall I2C_OneByteRX ; get status byte 101 rcall I2C_OneByteRX ; Get Status Byte
104 movf SSP1BUF,W 102 movf SSP1BUF,W
105 103
106 ; Non-flipped screen: 104 ; Non-flipped screen:
107 ; Chip orientation on the PCB requires 105 ; Chip orientation on the PCB requires
108 ; Original = corrected 106 ; Original = Corrected
109 ; x = -x 107 ; x = -x
110 ; y = -y 108 ; y = -y
111 ; z = -z 109 ; z = -z
112 110
113 ; Flipped screen: 111 ; Flipped screen:
114 ; Chip orientation on the PCB requires 112 ; Chip orientation on the PCB requires
115 ; Original = corrected 113 ; Original = Corrected
116 ; x = x 114 ; x = x
117 ; y = y 115 ; y = y
118 ; z = -z 116 ; z = -z
119 117
120 rcall I2C_TwoBytesRX_div16 ; get two bytes and divide /16 (signed) 118 rcall I2C_TwoBytesRX_div16 ; Get two bytes and devide /16 (signed)
121 btfsc flip_screen ; 180° rotation ? 119 btfsc flip_screen ; 180° rotation ?
122 bra I2C_RX_accelerometer2 ; YES 120 bra I2C_RX_accelerometer2 ; Yes
123 comf hi ; 16 bit sign change 121 comf hi ; 16bit sign change.
124 negf lo 122 negf lo
125 btfsc STATUS,C ; carry to propagate ? 123 btfsc STATUS,C ; Carry to propagate ?
126 incf hi,F ; YES - do it 124 incf hi,F ; YES: do it.
127 I2C_RX_accelerometer2: 125 I2C_RX_accelerometer2:
128 movff lo,accel_DX+0 126 movff lo,accel_DX+0
129 movff hi,accel_DX+1 ; Copy result 127 movff hi,accel_DX+1 ; Copy result
130 128
131 rcall I2C_TwoBytesRX_div16 ; Get two bytes and divide /16 (signed) 129 rcall I2C_TwoBytesRX_div16 ; Get two bytes and devide /16 (signed)
132 btfsc flip_screen ; 180° rotation ? 130 btfsc flip_screen ; 180° rotation ?
133 bra I2C_RX_accelerometer3 ; Yes 131 bra I2C_RX_accelerometer3 ; Yes
134 comf hi ; 16bit sign change. 132 comf hi ; 16bit sign change.
135 negf lo 133 negf lo
136 btfsc STATUS,C ; Carry to propagate ? 134 btfsc STATUS,C ; Carry to propagate ?
137 incf hi,F ; YES: do it. 135 incf hi,F ; YES: do it.
138 I2C_RX_accelerometer3: 136 I2C_RX_accelerometer3:
139 movff lo,accel_DY+0 137 movff lo,accel_DY+0
140 movff hi,accel_DY+1 ; Copy result 138 movff hi,accel_DY+1 ; Copy result
141 139
142 rcall I2C_OneByteRX ; Get one byte 140 rcall I2C_OneByteRX ; Get one byte
143 movff SSP1BUF,hi ; Data Byte 141 movff SSP1BUF,hi ; Data Byte
144 bsf SSP1CON2, RCEN ; Enable receive mode 142 bsf SSP1CON2, RCEN ; Enable recieve mode
145 rcall WaitMSSP 143 rcall WaitMSSP
146 ; According to data sheet there should be no Master Acknowledge for the last Byte (accel_DZ+0)... 144 ; According to datasheet there should be no Master Acknowlegde for the last Byte (accel_DZ+0)...
147 movff SSP1BUF,lo ; Data Byte 145 movff SSP1BUF,lo ; Data Byte
148 146
149 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only 147 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only
150 comf hi ; 16bit sign change. 148 comf hi ; 16bit sign change.
151 negf lo 149 negf lo
152 btfsc STATUS,C ; Carry to propagate ? 150 btfsc STATUS,C ; Carry to propagate ?
153 incf hi,F ; YES: do it. 151 incf hi,F ; YES: do it.
154 movff lo,accel_DZ+0 152 movff lo,accel_DZ+0
155 movff hi,accel_DZ+1 ; Copy result 153 movff hi,accel_DZ+1 ; Copy result
156 154
157 bsf SSP1CON2,PEN ; Stop condition 155 bsf SSP1CON2,PEN ; Stop condition
158 bra WaitMSSP ; (And return) 156 bra WaitMSSP ; (And return)
159 157
160 I2C_RX_accelerometer_compass1: 158 I2C_RX_accelerometer_compass1:
161 bsf SSP1CON2,SEN ; Start condition 159 bsf SSP1CON2,SEN ; Start condition
162 rcall WaitMSSP 160 rcall WaitMSSP
163 movlw 0x3C ; address 161 movlw 0x3C ; address
164 rcall I2C_TX 162 rcall I2C_TX
165 movlw b'10101000' ; 0x28 with auto-increment (MSB=1) 163 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
166 rcall I2C_TX 164 rcall I2C_TX
167 bsf SSP1CON2,RSEN ; Repeated start condition (!) 165 bsf SSP1CON2,RSEN ; Repeated start condition (!)
168 rcall WaitMSSP 166 rcall WaitMSSP
169 movlw 0x3D ; address 167 movlw 0x3D ; address
170 I2C_RX_accelerometer_compass1_xx: ; compass2 continues here... 168 I2C_RX_accelerometer_compass1_xx: ; compass2 and 3 continue here...
171 rcall I2C_TX 169 rcall I2C_TX
172 170 ; Non-flipped screen:
173 ; Non-flipped screen: 171 ; Chip orientation on the PCB requires
174 ; Chip orientation on the PCB requires 172 ; Original = Corrected
175 ; Original = Corrected 173 ; x = -x (Compass 1)
176 ; x = -x (Compass 1) 174 ; x = x (Compass 2)
177 ; x = x (Compass 2) 175 ; y = -y
178 ; y = -y 176 ; z = -z
179 ; z = -z 177
180 178 ; Flipped screen:
181 ; Flipped screen: 179 ; Chip orientation on the PCB requires
182 ; Chip orientation on the PCB requires 180 ; Original = Corrected
183 ; Original = Corrected 181 ; x = x (Compass 1)
184 ; x = x (Compass 1) 182 ; x = -x (Compass 2)
185 ; x = -x (Compass 2) 183 ; y = y
186 ; y = y 184 ; z = -z
187 ; z = -z 185
188 186 ; Dump the accelerator data
189 ; Dump the accelerator data 187 rcall I2C_OneByteRX
190 rcall I2C_OneByteRX 188 movff SSP1BUF,lo ;accel_DX+0
191 movff SSP1BUF,lo ; accel_DX+0 189 rcall I2C_OneByteRX
192 rcall I2C_OneByteRX 190 movff SSP1BUF,hi ;accel_DX+1
193 movff SSP1BUF,hi ;accel_DX+1 191 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only
194 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only 192 btfss compass_type2 ; compass 2?
195 btfss compass_type2 ; compass 2? 193 bra I2C_RX_accelerometer1_c1 ; No, compass 1
196 bra I2C_RX_accelerometer1_c1 ; No, compass 1 194 ; compass 2
197 ; compass 2 195 btfss flip_screen ; 180° rotation ?
198 btfss flip_screen ; 180° rotation ? 196 bra I2C_RX_accelerometer2_c1 ; No, continue with normal compass1 routines for Y and Z
199 bra I2C_RX_accelerometer2_c1 ; No, continue with normal compass1 routines for Y and Z 197 ; flipped compass 2, negate x
200 ; flipped compass 2, negate x 198 comf hi ; 16bit sign change.
201 comf hi ; 16bit sign change. 199 negf lo
202 negf lo 200 btfsc STATUS,C ; Carry to propagate ?
203 btfsc STATUS,C ; Carry to propagate ? 201 incf hi,F ; YES: do it.
204 incf hi,F ; YES: do it. 202 bra I2C_RX_accelerometer2_c1 ; continue with normal compass1 routines for Y and Z
205 bra I2C_RX_accelerometer2_c1 ; continue with normal compass1 routines for Y and Z 203
206 204 I2C_RX_accelerometer1_c1:
207 I2C_RX_accelerometer1_c1: 205 btfsc flip_screen ; 180° rotation ?
208 btfsc flip_screen ; 180° rotation ? 206 bra I2C_RX_accelerometer2_c1 ; Yes
209 bra I2C_RX_accelerometer2_c1 ; Yes 207 ; non-flipped compass 1, negate x
210 ; non-flipped compass 1, negate x 208 comf hi ; 16bit sign change.
211 comf hi ; 16bit sign change. 209 negf lo
212 negf lo 210 btfsc STATUS,C ; Carry to propagate ?
213 btfsc STATUS,C ; Carry to propagate ? 211 incf hi,F ; YES: do it.
214 incf hi,F ; YES: do it.
215 I2C_RX_accelerometer2_c1: 212 I2C_RX_accelerometer2_c1:
216 ; flipped compass 1, non-flipped compass 2 213 ; flipped compass 1, non-flipped compass 2
217 movff lo,accel_DX+0 214 movff lo,accel_DX+0
218 movff hi,accel_DX+1 ; Copy result 215 movff hi,accel_DX+1 ; Copy result
219 rcall I2C_OneByteRX 216 rcall I2C_OneByteRX
220 movff SSP1BUF,lo ; accel_DY+0 217 movff SSP1BUF,lo ;accel_DY+0
221 rcall I2C_OneByteRX 218 rcall I2C_OneByteRX
222 movff SSP1BUF,hi ; accel_DY+1 219 movff SSP1BUF,hi ;accel_DY+1
223 220
224 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only 221 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only
225 btfsc flip_screen ; 180° rotation ? 222 btfsc flip_screen ; 180° rotation ?
226 bra I2C_RX_accelerometer3_c1 ; Yes 223 bra I2C_RX_accelerometer3_c1 ; Yes
227 comf hi ; 16bit sign change. 224 comf hi ; 16bit sign change.
228 negf lo 225 negf lo
229 btfsc STATUS,C ; Carry to propagate ? 226 btfsc STATUS,C ; Carry to propagate ?
230 incf hi,F ; YES: do it. 227 incf hi,F ; YES: do it.
231 I2C_RX_accelerometer3_c1: 228 I2C_RX_accelerometer3_c1:
232 movff lo,accel_DY+0 229 movff lo,accel_DY+0
233 movff hi,accel_DY+1 ; Copy result 230 movff hi,accel_DY+1 ; Copy result
234 231
235 rcall I2C_OneByteRX 232 rcall I2C_OneByteRX
236 movff SSP1BUF,lo ;accel_DZ+0 233 movff SSP1BUF,lo ;accel_DZ+0
237 bsf SSP1CON2, RCEN ; Enable receive mode 234 bsf SSP1CON2, RCEN ; Enable recieve mode
238 rcall WaitMSSP 235 rcall WaitMSSP
239 ; According to data sheet there should be no Master Acknowledge for the last Byte (accel_DZ+1)... 236 ; According to datasheet there should be no Master Acknowlegde for the last Byte (accel_DZ+1)...
240 movff SSP1BUF,hi ;accel_DZ+1 237 movff SSP1BUF,hi ;accel_DZ+1
241 bsf SSP1CON2,PEN ; Stop condition 238 bsf SSP1CON2,PEN ; Stop condition
242 rcall WaitMSSP 239 rcall WaitMSSP
243 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only 240 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only
244 comf hi ; 16bit sign change for Z 241 comf hi ; 16bit sign change for Z
245 negf lo 242 negf lo
246 btfsc STATUS,C ; Carry to propagate ? 243 btfsc STATUS,C ; Carry to propagate ?
247 incf hi,F ; YES: do it. 244 incf hi,F ; YES: do it.
248 movff lo,accel_DZ+0 245 movff lo,accel_DZ+0
249 movff hi,accel_DZ+1 ; Copy result 246 movff hi,accel_DZ+1 ; Copy result
250 return 247 return
251 248
252 I2C_RX_accelerometer_compass2: 249 I2C_RX_accelerometer_compass2:
253 bsf SSP1CON2,SEN ; Start condition 250 bsf SSP1CON2,SEN ; Start condition
254 rcall WaitMSSP 251 rcall WaitMSSP
255 movlw 0x32 ; address 252 movlw 0x32 ; address
256 rcall I2C_TX 253 rcall I2C_TX
257 movlw b'10101000' ; 0x28 with auto-increment (MSB=1) 254 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
258 rcall I2C_TX 255 rcall I2C_TX
259 bsf SSP1CON2,RSEN ; Repeated start condition (!) 256 bsf SSP1CON2,RSEN ; Repeated start condition (!)
260 rcall WaitMSSP 257 rcall WaitMSSP
261 movlw 0x33 ; address 258 movlw 0x33 ; address
262 bra I2C_RX_accelerometer_compass1_xx 259 bra I2C_RX_accelerometer_compass1_xx
263 260
261 I2C_RX_accelerometer_compass3:
262 bsf SSP1CON2,SEN ; Start condition
263 rcall WaitMSSP
264 movlw 0x3A ; address
265 rcall I2C_TX
266 movlw 0x28 ; 0x28 (OUT_X_L_A)
267 rcall I2C_TX
268 bsf SSP1CON2,RSEN ; Repeated start condition (!)
269 rcall WaitMSSP
270 movlw 0x3B ; address
271 bra I2C_RX_accelerometer_compass1_xx
272
264 I2C_OneByteRX: 273 I2C_OneByteRX:
265 bsf SSP1CON2,RCEN ; Enable receive mode 274 bsf SSP1CON2, RCEN ; Enable recieve mode
266 rcall WaitMSSP 275 rcall WaitMSSP
267 bsf SSP1CON2,ACKEN ; Master acknowledge 276 bsf SSP1CON2,ACKEN ; Master acknowlegde
268 bra WaitMSSP ; And return! 277 bra WaitMSSP ; And return!
269 278
270 global I2C_RX_compass 279 global I2C_RX_compass
271 I2C_RX_compass: 280 I2C_RX_compass:
272 btfsc compass_type2 ; compass2 281 btfsc compass_type3 ; compass3
273 bra I2C_RX_compass2 ; yes 282 bra I2C_RX_compass3 ; yes
274 btfsc compass_type ; compass1? 283 btfsc compass_type2 ; compass2
275 bra I2C_RX_compass1 ; yes 284 bra I2C_RX_compass2 ; yes
285 btfsc compass_type ; compass1?
286 bra I2C_RX_compass1 ; yes
276 I2C_RX_compass0: 287 I2C_RX_compass0:
277 bsf SSP1CON2,SEN ; Start condition 288 bsf SSP1CON2,SEN ; Start condition
278 rcall WaitMSSP 289 rcall WaitMSSP
279 movlw 0x3C ; address 290 movlw 0x3C ; address
280 rcall I2C_TX 291 rcall I2C_TX
281 movlw 0x03 292 movlw 0x03
282 rcall I2C_TX 293 rcall I2C_TX
283 bsf SSP1CON2,PEN ; Stop condition 294 bsf SSP1CON2,PEN ; Stop condition
284 rcall WaitMSSP 295 rcall WaitMSSP
285 296
286 bcf PIR1,SSP1IF 297 bcf PIR1,SSP1IF
287 bsf SSP1CON2,SEN ; Start condition 298 bsf SSP1CON2,SEN ; Start condition
288 rcall WaitMSSP 299 rcall WaitMSSP
289 movlw 0x3D ; address 300 movlw 0x3D ; address
290 rcall I2C_TX 301 rcall I2C_TX
291 302
292 ; Compass IC sends data in following order: 303 ; Compass IC sends data in following order:
293 ; x MSB 304 ; x MSB
294 ; x LSB 305 ; x LSB
295 ; z MSB 306 ; z MSB
296 ; z LSB 307 ; z LSB
297 ; y MSB 308 ; y MSB
298 ; y LSB 309 ; y LSB
299 310
300 ; Non-flipped screen 311 ; Non-flipped screen
301 ; Chip orientation on the PCB requires 312 ; Chip orientation on the PCB requires
302 ; Original = Corrected 313 ; Original = Corrected
303 ; x = -y 314 ; x = -y
304 ; z = z 315 ; z = z
305 ; y = x 316 ; y = x
306 317
307 ; Flipped screen 318 ; Flipped screen
308 ; Chip orientation on the PCB requires 319 ; Chip orientation on the PCB requires
309 ; Original = Corrected 320 ; Original = Corrected
310 ; x = y 321 ; x = y
311 ; z = z 322 ; z = z
312 ; y = -x 323 ; y = -x
313 324
314 rcall I2C_OneByteRX ; Get one byte 325 rcall I2C_OneByteRX ; Get one byte
315 movff SSP1BUF,compass_DY+1 ; Data Byte 326 movff SSP1BUF,compass_DY+1; Data Byte
316 rcall I2C_OneByteRX ; Get one byte 327 rcall I2C_OneByteRX ; Get one byte
317 movff SSP1BUF,compass_DY+0 ; Data Byte 328 movff SSP1BUF,compass_DY+0; Data Byte
318 btfsc flip_screen ; 180° rotation ? 329 btfsc flip_screen ; 180° rotation ?
319 bra I2C_RX_compass0_2 ; Yes 330 bra I2C_RX_compass0_2 ; Yes
320 banksel compass_DY 331 banksel compass_DY
321 comf compass_DY+1 ; 16bit sign change. 332 comf compass_DY+1 ; 16bit sign change.
322 negf compass_DY+0 333 negf compass_DY+0
323 btfsc STATUS,C ; Carry to propagate ? 334 btfsc STATUS,C ; Carry to propagate ?
324 incf compass_DY+1,F ; YES: do it. 335 incf compass_DY+1,F ; YES: do it.
325 I2C_RX_compass0_2: 336 I2C_RX_compass0_2:
326 banksel common 337 banksel common
327 rcall I2C_OneByteRX ; Get one byte 338 rcall I2C_OneByteRX ; Get one byte
328 movff SSP1BUF,compass_DZ+1 ; Data Byte 339 movff SSP1BUF,compass_DZ+1; Data Byte
329 rcall I2C_OneByteRX ; Get one byte 340 rcall I2C_OneByteRX ; Get one byte
330 movff SSP1BUF,compass_DZ+0 ; Data Byte 341 movff SSP1BUF,compass_DZ+0; Data Byte
331 rcall I2C_OneByteRX ; Get one byte 342 rcall I2C_OneByteRX ; Get one byte
332 movff SSP1BUF,compass_DX+1 ; Data Byte 343 movff SSP1BUF,compass_DX+1; Data Byte
333 bsf SSP1CON2, RCEN ; Enable receive mode 344 bsf SSP1CON2, RCEN ; Enable recieve mode
334 rcall WaitMSSP 345 rcall WaitMSSP
335 movff SSP1BUF,compass_DX+0 ; Data Byte 346 movff SSP1BUF,compass_DX+0; Data Byte
336 bsf SSP1CON2,PEN ; Stop condition 347 bsf SSP1CON2,PEN ; Stop condition
337 rcall WaitMSSP 348 rcall WaitMSSP
338 btfss flip_screen ; 180° rotation ? 349 btfss flip_screen ; 180° rotation ?
339 return ; No, done. 350 return ; No, done.
340 ; Yes, flip X 351 ; Yes, flip X
341 banksel compass_DX 352 banksel compass_DX
342 comf compass_DX+1 ; 16bit sign change. 353 comf compass_DX+1 ; 16bit sign change.
343 negf compass_DX+0 354 negf compass_DX+0
344 btfsc STATUS,C ; Carry to propagate ? 355 btfsc STATUS,C ; Carry to propagate ?
345 incf compass_DX+1,F ; YES: do it. 356 incf compass_DX+1,F ; YES: do it.
346 banksel common 357 banksel common
347 return 358 return
348 359
349 I2C_RX_compass1: ; New compass 360 I2C_RX_compass1: ; New compass
350 bsf SSP1CON2,SEN ; Start condition 361 bsf SSP1CON2,SEN ; Start condition
351 rcall WaitMSSP 362 rcall WaitMSSP
352 movlw 0x3C ; address 363 movlw 0x3C ; address
353 rcall I2C_TX 364 rcall I2C_TX
354 movlw b'10001000' ; 0x08 with auto-increment (MSB=1) 365 movlw b'10001000' ; 0x08 with auto-increment (MSB=1)
355 rcall I2C_TX 366 rcall I2C_TX
356 bsf SSP1CON2,RSEN ; Repeated start condition (!) 367 bsf SSP1CON2,RSEN ; Repeated start condition (!)
357 rcall WaitMSSP 368 rcall WaitMSSP
358 movlw 0x3D ; address 369 movlw 0x3D ; address
359 rcall I2C_TX 370 rcall I2C_TX
360 ;rcall WaitMSSP ; Needed? (mH) 371 ;rcall WaitMSSP ; Needed? (mH)
361 rcall I2C_OneByteRX ; Get one byte 372 rcall I2C_OneByteRX ; Get one byte
362 movff SSP1BUF,lo ; Data Byte 373 movff SSP1BUF,lo ; Data Byte
363 rcall I2C_OneByteRX ; Get one byte 374 rcall I2C_OneByteRX ; Get one byte
364 movff SSP1BUF,hi ; Data Byte 375 movff SSP1BUF,hi ; Data Byte
365 rcall I2C_TwoBytesRX_div8_2 376 rcall I2C_TwoBytesRX_div8_2
366 movff lo,compass_DX+0 377 movff lo,compass_DX+0
367 movff hi,compass_DX+1 378 movff hi,compass_DX+1
368 btfss flip_screen ; 180° rotation ? 379 btfss flip_screen ; 180° rotation ?
369 bra I2C_RX_compass1_1 ; Yes 380 bra I2C_RX_compass1_1 ; Yes
370 ; Yes, flip X 381 ; Yes, flip X
371 banksel compass_DX 382 banksel compass_DX
372 comf compass_DX+1 ; 16bit sign change. 383 comf compass_DX+1 ; 16bit sign change.
373 negf compass_DX+0 384 negf compass_DX+0
374 btfsc STATUS,C ; Carry to propagate ? 385 btfsc STATUS,C ; Carry to propagate ?
375 incf compass_DX+1,F ; YES: do it. 386 incf compass_DX+1,F ; YES: do it.
376 banksel common 387 banksel common
377 I2C_RX_compass1_1: 388 I2C_RX_compass1_1:
378 rcall I2C_OneByteRX ; Get one byte 389 rcall I2C_OneByteRX ; Get one byte
379 movff SSP1BUF,lo ; Data Byte 390 movff SSP1BUF,lo ; Data Byte
380 rcall I2C_OneByteRX ; Get one byte 391 rcall I2C_OneByteRX ; Get one byte
381 movff SSP1BUF,hi ; Data Byte 392 movff SSP1BUF,hi ; Data Byte
382 rcall I2C_TwoBytesRX_div8_2 393 rcall I2C_TwoBytesRX_div8_2
383 movff lo,compass_DY+0 394 movff lo,compass_DY+0
384 movff hi,compass_DY+1 395 movff hi,compass_DY+1
385 btfss flip_screen ; 180° rotation ? 396 btfss flip_screen ; 180° rotation ?
386 bra I2C_RX_compass1_2 ; Yes 397 bra I2C_RX_compass1_2 ; Yes
387 ; Yes, flip Y 398 ; Yes, flip Y
388 banksel compass_DY 399 banksel compass_DY
389 comf compass_DY+1 ; 16bit sign change. 400 comf compass_DY+1 ; 16bit sign change.
390 negf compass_DY+0 401 negf compass_DY+0
391 btfsc STATUS,C ; Carry to propagate ? 402 btfsc STATUS,C ; Carry to propagate ?
392 incf compass_DY+1,F ; YES: do it. 403 incf compass_DY+1,F ; YES: do it.
393 I2C_RX_compass1_2: 404 I2C_RX_compass1_2:
394 banksel common 405 banksel common
395 rcall I2C_OneByteRX ; Get one byte 406 rcall I2C_OneByteRX ; Get one byte
396 movff SSP1BUF,lo ; Data Byte 407 movff SSP1BUF,lo ; Data Byte
397 bsf SSP1CON2, RCEN ; Enable receive mode 408 bsf SSP1CON2, RCEN ; Enable recieve mode
398 rcall WaitMSSP 409 rcall WaitMSSP
399 movff SSP1BUF,hi ; Data Byte 410 movff SSP1BUF,hi ; Data Byte
400 rcall I2C_TwoBytesRX_div8_2 411 rcall I2C_TwoBytesRX_div8_2
401 movff lo,compass_DZ+0 412 movff lo,compass_DZ+0
402 movff hi,compass_DZ+1 413 movff hi,compass_DZ+1
403 bsf SSP1CON2,PEN ; Stop condition 414 bsf SSP1CON2,PEN ; Stop condition
404 bra WaitMSSP ;(And return) 415 bra WaitMSSP ;(And return)
405 416
406 I2C_RX_compass2: ; newest compass 417 I2C_RX_compass2: ; newest compass
407 bsf SSP1CON2,SEN ; Start condition 418 bsf SSP1CON2,SEN ; Start condition
408 rcall WaitMSSP 419 rcall WaitMSSP
409 movlw 0x3C ; address 420 movlw 0x3C ; address
410 rcall I2C_TX 421 rcall I2C_TX
411 movlw 0xE8 ; 0x68 with auto-increment (MSB=1) 422 movlw 0xE8 ; 0x68 with auto-increment (MSB=1)
412 rcall I2C_TX 423 rcall I2C_TX
413 bsf SSP1CON2,RSEN ; Repeated start condition (!) 424 bsf SSP1CON2,RSEN ; Repeated start condition (!)
414 rcall WaitMSSP 425 rcall WaitMSSP
415 movlw 0x3D ; address 426 movlw 0x3D ; address
416 rcall I2C_TX 427 rcall I2C_TX
417 ; rcall WaitMSSP 428 I2C_RX_compass2_xx: ; compass 3 enters here
418 rcall I2C_OneByteRX ; Get one byte 429 ; rcall WaitMSSP
419 movff SSP1BUF,lo ; Data Byte 430 rcall I2C_OneByteRX ; Get one byte
420 rcall I2C_OneByteRX ; Get one byte 431 movff SSP1BUF,lo ; Data Byte
421 movff SSP1BUF,hi ; Data Byte 432 rcall I2C_OneByteRX ; Get one byte
422 ; rcall I2C_TwoBytesRX_div8_2 433 movff SSP1BUF,hi ; Data Byte
423 btfsc flip_screen ; 180° rotation ? 434 ; rcall I2C_TwoBytesRX_div8_2
424 bra I2C_RX_compass2_1 ; Yes, do nothing with X 435 btfsc flip_screen ; 180° rotation ?
425 ; No, flip X 436 bra I2C_RX_compass2_1 ; Yes, do nothing with X
426 comf hi ; 16bit sign change. 437 ; No, flip X
427 negf lo 438 comf hi ; 16bit sign change.
428 btfsc STATUS,C ; Carry to propagate ? 439 negf lo
429 incf hi,F ; YES: do it. 440 btfsc STATUS,C ; Carry to propagate ?
430 I2C_RX_compass2_1: 441 incf hi,F ; YES: do it.
431 movff lo,compass_DX+0 442 I2C_RX_compass2_1:
432 movff hi,compass_DX+1 443 movff lo,compass_DX+0
433 rcall I2C_OneByteRX ; Get one byte 444 movff hi,compass_DX+1
434 movff SSP1BUF,lo ; Data Byte 445 rcall I2C_OneByteRX ; Get one byte
435 rcall I2C_OneByteRX ; Get one byte 446 movff SSP1BUF,lo ; Data Byte
436 movff SSP1BUF,hi ; Data Byte 447 rcall I2C_OneByteRX ; Get one byte
437 ; rcall I2C_TwoBytesRX_div8_2 448 movff SSP1BUF,hi ; Data Byte
438 btfss flip_screen ; 180° rotation ? 449 ; rcall I2C_TwoBytesRX_div8_2
439 bra I2C_RX_compass2_2 ; No, do nothing with Y 450 btfss flip_screen ; 180° rotation ?
440 ; Yes, flip Y 451 bra I2C_RX_compass2_2 ; No, do nothing with Y
441 comf hi ; 16bit sign change. 452 ; Yes, flip Y
442 negf lo 453 comf hi ; 16bit sign change.
443 btfsc STATUS,C ; Carry to propagate ? 454 negf lo
444 incf hi,F ; YES: do it. 455 btfsc STATUS,C ; Carry to propagate ?
456 incf hi,F ; YES: do it.
445 I2C_RX_compass2_2: 457 I2C_RX_compass2_2:
446 movff lo,compass_DY+0 458 movff lo,compass_DY+0
447 movff hi,compass_DY+1 459 movff hi,compass_DY+1
448 rcall I2C_OneByteRX ; Get one byte 460 rcall I2C_OneByteRX ; Get one byte
449 movff SSP1BUF,lo ; Data Byte 461 movff SSP1BUF,lo ; Data Byte
450 rcall I2C_OneByteRX ; Get one byte 462 rcall I2C_OneByteRX ; Get one byte
451 movff SSP1BUF,hi ; Data Byte 463 movff SSP1BUF,hi ; Data Byte
452 ; rcall I2C_TwoBytesRX_div8_2 464 ;rcall I2C_TwoBytesRX_div8_2
453 movff lo,compass_DZ+0 465 movff lo,compass_DZ+0
454 movff hi,compass_DZ+1 466 movff hi,compass_DZ+1
455 bsf SSP1CON2,PEN ; Stop condition 467 bsf SSP1CON2,PEN ; Stop condition
456 bra WaitMSSP ;(And return) 468 bra WaitMSSP ;(And return)
457 469
458 470 I2C_RX_compass3:
459 global I2C_init_compass 471 bsf SSP1CON2,SEN ; Start condition
472 rcall WaitMSSP
473 movlw 0x3C ; address
474 rcall I2C_TX
475 movlw 0xA8 ; 0x28 with auto-increment (MSB=1)
476 rcall I2C_TX
477 bsf SSP1CON2,RSEN ; Repeated start condition (!)
478 rcall WaitMSSP
479 movlw 0x3D ; address
480 rcall I2C_TX
481 bra I2C_RX_compass2_xx
482
483 global I2C_init_compass
460 I2C_init_compass: 484 I2C_init_compass:
461 bsf compass_enabled 485 bsf compass_enabled
462 bcf compass_type2 486 bcf compass_type2
463 ; probe compass 2 487 bcf compass_type3
464 bsf SSP1CON2,SEN ; Start condition 488
465 rcall WaitMSSP 489 ; probe compass 3
466 movlw 0x32 ; Address byte + Write bit 490 bsf SSP1CON2,SEN ; Start condition
467 movwf SSP1BUF ; control byte 491 rcall WaitMSSP
468 rcall WaitMSSP 492 movlw 0x3A ; Address byte + Write bit
469 btfss SSP1CON2,ACKSTAT ; ACK? 493 movwf SSP1BUF ; control byte
470 bsf compass_type2 ; ACK send. compass2 present 494 rcall WaitMSSP
471 bsf SSP1CON2,PEN ; Stop condition 495 btfss SSP1CON2,ACKSTAT ; ACK?
472 rcall WaitMSSP 496 bsf compass_type3 ; ACK send. compass2 present
473 497 bsf SSP1CON2,PEN ; Stop condition
474 btfsc compass_type2 498 rcall WaitMSSP
475 bra I2C_init_compass2 ; Compass2 499
476 ; Check for compass0 or compass1... 500 btfsc compass_type3
477 bsf compass_type ; set flag 501 bra I2C_init_compass3 ; Compass3
478 bsf SSP1CON2,SEN ; Start condition 502
479 rcall WaitMSSP 503 ; probe compass 2
480 movlw 0x3C ; address 504 bsf SSP1CON2,SEN ; Start condition
481 rcall I2C_TX 505 rcall WaitMSSP
482 movlw 0x0F 506 movlw 0x32 ; Address byte + Write bit
483 rcall I2C_TX 507 movwf SSP1BUF ; control byte
484 bsf SSP1CON2,PEN ; Stop condition 508 rcall WaitMSSP
485 rcall WaitMSSP 509 btfss SSP1CON2,ACKSTAT ; ACK?
486 bcf PIR1,SSP1IF 510 bsf compass_type2 ; ACK send. compass2 present
487 bsf SSP1CON2,SEN ; Start condition 511 bsf SSP1CON2,PEN ; Stop condition
488 rcall WaitMSSP 512 rcall WaitMSSP
489 movlw 0x3D ; address 513
490 rcall I2C_TX 514 btfsc compass_type2
491 rcall I2C_OneByteRX ; Get one byte 515 bra I2C_init_compass2 ; Compass2
492 movlw 0x49 ; 0x49 = Compass1 516 ; Check for compass0 or compass1...
493 cpfseq SSP1BUF 517 bsf compass_type ; set flag
494 bcf compass_type ; clear flag 518 bsf SSP1CON2,SEN ; Start condition
495 bsf SSP1CON2,PEN ; Stop condition 519 rcall WaitMSSP
496 rcall WaitMSSP 520 movlw 0x3C ; address
497 521 rcall I2C_TX
498 btfsc compass_type ; compass1? 522 movlw 0x0F
499 bra I2C_init_compass1 ; yes 523 rcall I2C_TX
524 bsf SSP1CON2,PEN ; Stop condition
525 rcall WaitMSSP
526 bcf PIR1,SSP1IF
527 bsf SSP1CON2,SEN ; Start condition
528 rcall WaitMSSP
529 movlw 0x3D ; address
530 rcall I2C_TX
531 rcall I2C_OneByteRX ; Get one byte
532 movlw 0x49 ; 0x49 = Compass1
533 cpfseq SSP1BUF
534 bcf compass_type ; clear flag
535 bsf SSP1CON2,PEN ; Stop condition
536 rcall WaitMSSP
537
538 btfsc compass_type ; compass1?
539 bra I2C_init_compass1 ; yes
500 ; init compass0 540 ; init compass0
501 bsf SSP1CON2,SEN ; Start condition 541 bsf SSP1CON2,SEN ; Start condition
502 rcall WaitMSSP 542 rcall WaitMSSP
503 movlw 0x3C ; address 543 movlw 0x3C ; address
504 rcall I2C_TX 544 rcall I2C_TX
505 movlw 0x00 545 movlw 0x00
506 rcall I2C_TX 546 rcall I2C_TX
507 ; movlw b'01101001' ; ConfigA: 3Hz, 8 Samples averaged, Test Mode (Positive Bias) 547 ; movlw b'01101001' ; ConfigA: 3Hz, 8 Samples averaged, Test Mode (Positive Bias)
508 movlw b'01101000' ; ConfigA: 3Hz, 8 Samples averaged 548 movlw b'01101000' ; ConfigA: 3Hz, 8 Samples averaged
509 rcall I2C_TX 549 rcall I2C_TX
510 I2C_init_compass_common: 550 I2C_init_compass_common:
511 movff opt_compass_gain,i2c_temp1 ; 0-7 (230LSB/Gauss to 1370LSB/Gauss) 551 movff opt_compass_gain,i2c_temp1 ; 0-7 (230LSB/Gauss to 1370LSB/Gauss)
512 swapf i2c_temp1,F 552 swapf i2c_temp1,F
513 comf i2c_temp1,F 553 comf i2c_temp1,F
514 bcf STATUS,C 554 bcf STATUS,C
515 rlcf i2c_temp1 555 rlcf i2c_temp1
516 movf i2c_temp1,W 556 movf i2c_temp1,W
517 clrf i2c_temp1 557 clrf i2c_temp1
518 rcall I2C_TX 558 rcall I2C_TX
519 movlw b'00000000' ; Continuous Mode 559 movlw b'00000000' ; Continous Mode
520 rcall I2C_TX 560 rcall I2C_TX
521 bsf SSP1CON2,PEN ; Stop condition 561 bsf SSP1CON2,PEN ; Stop condition
522 bra WaitMSSP ; (And return) 562 bra WaitMSSP ; (And return)
523 563
524 I2C_init_compass1: 564 I2C_init_compass1:
525 bsf SSP1CON2,SEN ; Start condition 565 bsf SSP1CON2,SEN ; Start condition
526 rcall WaitMSSP 566 rcall WaitMSSP
527 movlw 0x3C ; address 567 movlw 0x3C ; address
528 rcall I2C_TX 568 rcall I2C_TX
529 movlw 0x9F ; 1F with auto-increment (MSB=1) 569 movlw 0x9F ; 1F with auto-increment (MSB=1)
530 rcall I2C_TX 570 rcall I2C_TX
531 movlw b'00000000' ; CTRL0 571 movlw b'00000000' ; CTRL0
532 rcall I2C_TX 572 rcall I2C_TX
533 movlw b'00101111' ; CTRL1 (6,25Hz, BDU=0, x,y,z = ON) 573 movlw b'00101111' ; CTRL1 (6,25Hz, BDU=0, x,y,z = ON)
534 rcall I2C_TX 574 rcall I2C_TX
535 movlw b'11000000' ; CTRL2 (50Hz, +/-2g, 575 movlw b'11000000' ; CTRL2 (50Hz, +/-2g,
536 rcall I2C_TX 576 rcall I2C_TX
537 movlw b'00000000' ; CTRL3 577 movlw b'00000000' ; CTRL3
538 rcall I2C_TX 578 rcall I2C_TX
539 movlw b'00000000' ; CTRL4 579 movlw b'00000000' ; CTRL4
540 rcall I2C_TX 580 rcall I2C_TX
541 movlw b'01100100' ; CTRL5 HIGH res, 6,25Hz 581 movlw b'01100100' ; CTRL5 HIGH res, 6,25Hz
542 rcall I2C_TX 582 rcall I2C_TX
543 init_compass1_common: 583 init_compass1_common:
544 movff opt_compass_gain,i2c_temp1 ; 0-7 (230LSB/Gauss to 1370LSB/Gauss) +++ 584 movff opt_compass_gain,i2c_temp1 ; 0-7 (230LSB/Gauss to 1370LSB/Gauss)
545 movlw b'01100000' ; CTRL6 Full scale (+/-12 Gauss -> 2730LSB/Gauss) 585 movlw b'01100000' ; CTRL6 Full scale (+/-12 Gauss -> 2730LSB/Gauss)
546 dcfsnz i2c_temp1,F ; = 1? 586 dcfsnz i2c_temp1,F ; = 1?
547 movlw b'01100000' ; Yes, CTRL6 Full scale (+/-12 Gauss -> 2730LSB/Gauss) 587 movlw b'01100000' ; Yes, CTRL6 Full scale (+/-12 Gauss -> 2730LSB/Gauss)
548 dcfsnz i2c_temp1,F ; = 2? 588 dcfsnz i2c_temp1,F ; = 2?
549 movlw b'01000000' ; Yes, CTRL6 (+/-8 Gauss) 589 movlw b'01000000' ; Yes, CTRL6 (+/-8 Gauss)
550 dcfsnz i2c_temp1,F ; = 3? 590 dcfsnz i2c_temp1,F ; = 3?
551 movlw b'01000000' ; Yes, CTRL6 (+/-8 Gauss) 591 movlw b'01000000' ; Yes, CTRL6 (+/-8 Gauss)
552 dcfsnz i2c_temp1,F ; = 4? 592 dcfsnz i2c_temp1,F ; = 4?
553 movlw b'00100000' ; Yes, CTRL6 (+/-4 Gauss) 593 movlw b'00100000' ; Yes, CTRL6 (+/-4 Gauss)
554 dcfsnz i2c_temp1,F ; = 5? 594 dcfsnz i2c_temp1,F ; = 5?
555 movlw b'00100000' ; Yes, CTRL6 (+/-4 Gauss) 595 movlw b'00100000' ; Yes, CTRL6 (+/-4 Gauss)
556 dcfsnz i2c_temp1,F ; = 6? 596 dcfsnz i2c_temp1,F ; = 6?
557 movlw b'00000000' ; Yes, CTRL6 (+/-2 Gauss) 597 movlw b'00000000' ; Yes, CTRL6 (+/-2 Gauss)
558 dcfsnz i2c_temp1,F ; = 7? 598 dcfsnz i2c_temp1,F ; = 7?
559 movlw b'00000000' ; Yes, CTRL6 (+/-2 Gauss) 599 movlw b'00000000' ; Yes, CTRL6 (+/-2 Gauss)
560 rcall I2C_TX 600 rcall I2C_TX
561 movlw b'00000000' ; CTRL7 Continuous Mode 601 movlw b'00000000' ; CTRL7 Continuous Mode
562 rcall I2C_TX 602 rcall I2C_TX
563 bsf SSP1CON2,PEN ; Stop condition 603 bsf SSP1CON2,PEN ; Stop condition
564 bra WaitMSSP ; (And return) 604 bra WaitMSSP ; (And return)
565 605
566 I2C_init_compass2: 606 I2C_init_compass2:
567 bsf SSP1CON2,SEN ; Start condition 607 bsf SSP1CON2,SEN ; Start condition
568 rcall WaitMSSP 608 rcall WaitMSSP
569 movlw 0x3C ; address 609 movlw 0x3C ; address
570 rcall I2C_TX 610 rcall I2C_TX
571 movlw 0xE0 ; 0x60 with auto-increment (MSB=1) 611 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
572 rcall I2C_TX 612 rcall I2C_TX
573 movlw b'00000000' ; CFG_REG_A_M (10Hz, Continuous) 0x60 613 movlw b'10000000' ; CFG_REG_A_M (10Hz, Continuous) 0x60
574 rcall I2C_TX 614 rcall I2C_TX
575 movlw b'00000000' ; CFG_REG_B_M (Low-Pass Filter off) 0x61 (set pulse is released every 63 ODR) 615 movlw b'00000001' ; CFG_REG_B_M (Low-Pass Filter enabled) 0x61 (set pulse is released every 63 ODR)
576 rcall I2C_TX 616 rcall I2C_TX
577 movlw b'00000000' ; CFG_REG_C_M BDU=0 0x62 617 movlw b'00010000' ; CFG_REG_C_M BDU=1 0x62
578 rcall I2C_TX 618 rcall I2C_TX
579 bsf SSP1CON2,PEN ; Stop condition 619 bsf SSP1CON2,PEN ; Stop condition
580 bra WaitMSSP ;(And return) 620 bra WaitMSSP ;(And return)
581 621
582 622 I2C_init_compass3:
583 global I2C_sleep_compass
584 I2C_sleep_compass:
585 bcf compass_enabled
586 btfsc compass_type2 ; compass2?
587 bra I2C_sleep_compass2 ; yes
588 btfsc compass_type ; compass1?
589 bra I2C_sleep_compass1 ; yes
590 I2C_sleep_compass0:
591 bsf SSP1CON2,SEN ; Start condition
592 rcall WaitMSSP
593 movlw 0x3C ; address
594 rcall I2C_TX
595 movlw 0x00
596 rcall I2C_TX
597 movlw b'01101000' ; ConfigA
598 rcall I2C_TX
599 movlw b'00100000' ; ConfigB
600 rcall I2C_TX
601 movlw b'00000010' ; Idle Mode
602 rcall I2C_TX
603 bsf SSP1CON2,PEN ; Stop condition
604 bra WaitMSSP ; (And return)
605
606 I2C_sleep_compass1:
607 bsf SSP1CON2,SEN ; Start condition
608 rcall WaitMSSP
609 movlw 0x3C ; address
610 rcall I2C_TX
611 movlw 0x20 ; CTRL_REG1
612 rcall I2C_TX
613 movlw b'00000000' ; data for CTRL_REG1: acceleration sensor Power-down mode
614 rcall I2C_TX
615 bsf SSP1CON2,PEN ; Stop condition
616 rcall WaitMSSP
617 bsf SSP1CON2,SEN ; Start condition
618 rcall WaitMSSP
619 movlw 0x3C ; address
620 rcall I2C_TX
621 movlw 0x26 ; CTRL_REG7
622 rcall I2C_TX
623 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor Power-down mode
624 rcall I2C_TX
625 bsf SSP1CON2,PEN ; Stop condition
626 bra WaitMSSP ;(And return)
627
628 I2C_sleep_compass2:
629 ; magnetic
630 bsf SSP1CON2,SEN ; Start condition 623 bsf SSP1CON2,SEN ; Start condition
631 rcall WaitMSSP 624 rcall WaitMSSP
632 movlw 0x3C ; address 625 movlw 0x3C ; address
633 rcall I2C_TX 626 rcall I2C_TX
634 movlw 0xE0 ; 0x60 with auto-increment (MSB=1) 627 movlw 0xA0 ; 0x20 with auto-increment (MSB=1)
635 rcall I2C_TX 628 rcall I2C_TX
636 movlw b'00000011' ; CFG_REG_A_M (Idle mode) 0x60 629 movlw b'01110000' ; CTRL_REG1_M (10Hz) 0x20
637 rcall I2C_TX 630 rcall I2C_TX
638 movlw b'00000100' ; CFG_REG_B_M 0x61 (set pulse is released only at power-on after PD condition) 631 movlw b'01100000' ; CTRL_REG2_M (Full-scale: +/- 16gauss) 0x21
639 rcall I2C_TX 632 rcall I2C_TX
640 movlw b'01010001' ; CFG_REG_C_M 0x62 633 movlw b'01000000' ; CTRL_REG3_M (Continuous) 0x22
641 rcall I2C_TX 634 rcall I2C_TX
642 movlw b'00000000' ; INT_CTRL_REG_M 0x63 635 movlw b'00000000' ; CTRL_REG4_M (Z in Low-power mode) 0x23
643 rcall I2C_TX 636 rcall I2C_TX
644 637 movlw b'00000000' ; CTRL_REG5_M 0x24
638 rcall I2C_TX
639 movlw b'00000000' ; CTRL_REG5_M 0x24
640 rcall I2C_TX
645 bsf SSP1CON2,PEN ; Stop condition 641 bsf SSP1CON2,PEN ; Stop condition
646 bra WaitMSSP ; (And return) 642 bra WaitMSSP ;(And return)
647 643
648 I2C_sleep_accelerometer2: 644
649 ; accelerometer
650 bsf SSP1CON2,SEN ; Start condition
651 rcall WaitMSSP
652 movlw 0x32 ; address
653 rcall I2C_TX
654 movlw 0x9F ; 1F with auto-increment (MSB=1)
655 rcall I2C_TX
656 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off) 0x1F
657 rcall I2C_TX
658 movlw b'00000000' ; CTRL_REG1_A (All off) 0x20
659 rcall I2C_TX
660 bsf SSP1CON2,PEN ; Stop condition
661 bra WaitMSSP ; (And return)
662
663 WaitMSSP: 645 WaitMSSP:
664 decfsz i2c_temp1,F ; check for timeout during I2C action 646 decfsz i2c_temp1,F ; check for timeout during I2C action
665 bra WaitMSSP2 647 bra WaitMSSP2
666 bra I2CFail ; timeout occurred 648 bra I2CFail ; timeout occured
667 WaitMSSP2: 649 WaitMSSP2:
668 btfss PIR1,SSP1IF 650 btfss PIR1,SSP1IF
669 bra WaitMSSP 651 bra WaitMSSP
670 clrf i2c_temp1 652 clrf i2c_temp1
671 bcf PIR1,SSP1IF 653 bcf PIR1,SSP1IF
672 return 654 return
673 655
674 I2C_WaitforACK: 656 I2C_WaitforACK:
675 btfss SSP1CON2,ACKSTAT ; checks for ACK bit from slave 657 btfss SSP1CON2,ACKSTAT ; checks for ACK bit from slave
676 return 658 return
677 I2CFail: 659 I2CFail:
678 rcall I2CReset ; I2C Reset 660 rcall I2CReset ; I2C Reset
679 bcf PIR1,SSP1IF 661 bcf PIR1,SSP1IF
680 clrf i2c_temp1 662 clrf i2c_temp1
681 bsf i2c_error_flag ; set error flag 663 bsf i2c_error_flag ; set error flag
682 return 664 return
683 665
684 I2CReset: ; something went wrong (slave holds SDA low?) 666 I2CReset: ; Something went wrong (Slave holds SDA low?)
685 clrf SSP1CON1 ; wake-up slave and reset entire module 667 clrf SSP1CON1 ; wake-up slave and reset entire module
686 clrf SSP1CON2 668 clrf SSP1CON2
687 clrf SSP1STAT 669 clrf SSP1STAT
688 bcf TRISC,3 ; SCL OUTPUT 670 bcf TRISC,3 ; SCL OUTPUT
689 bsf TRISC,4 ; SDA input 671 bsf TRISC,4 ; SDA Input
690 bcf PORTC,3 672 bcf PORTC,3
691 movlw d'9' 673 movlw d'9'
692 movwf i2c_temp1 ; clock-out 9 clock cycles manually 674 movwf i2c_temp1 ; clock-out 9 clock cycles manually
693 I2CReset_1: 675 I2CReset_1:
694 bsf PORTC,3 ; SCL = 1 676 bsf PORTC,3 ; SCL=1
695 nop 677 nop
696 nop 678 nop
697 nop 679 nop
698 nop 680 nop
699 btfsc PORTC,4 ; SDA = 1 ? 681 btfsc PORTC,4 ; SDA=1?
700 bra I2CReset_2 ; YES - =1, SDA has been released from slave 682 bra I2CReset_2 ; =1, SDA has been released from slave
701 bcf PORTC,3 ; NO - set SCL = 0 683 bcf PORTC,3 ; SCL=0
702 nop 684 nop
703 nop 685 nop
704 bcf PORTC,3 686 bcf PORTC,3
705 nop 687 nop
706 nop 688 nop
707 decfsz i2c_temp1,F 689 decfsz i2c_temp1,F
708 bra I2CReset_1 ; check for nine clock cycles 690 bra I2CReset_1 ; check for nine clock cycles
709 I2CReset_2: 691 I2CReset_2:
710 bsf TRISC,3 ; SCL Input 692 bsf TRISC,3 ; SCL Input
711 clrf SSP1CON1 ; setup I²C mode 693 clrf SSP1CON1 ; setup I²C Mode
712 WAITMS d'10' ; reset-timeout for I2C devices 694 WAITMS d'10' ; Reset-Timeout for I2C devices
713 movlw b'00000000' ; with slew rate control 695 movlw b'00000000' ; with slew rate control
714 movwf SSP1STAT 696 movwf SSP1STAT
715 movlw b'00101000' 697 movlw b'00101000'
716 movwf SSP1CON1 698 movwf SSP1CON1
717 movlw b'00000000' 699 movlw b'00000000'
718 movwf SSP1CON2 700 movwf SSP1CON2
719 movlw 0x27 701 movlw 0x27
720 movwf SSP1ADD 702 movwf SSP1ADD
721 return 703 return
722 704
723 global I2C_init_accelerometer 705
706 global I2C_sleep_compass
707 I2C_sleep_compass:
708 bcf compass_enabled
709 btfsc compass_type3 ; compass3?
710 bra I2C_sleep_compass3 ; yes
711 btfsc compass_type2 ; compass2?
712 bra I2C_sleep_compass2 ; yes
713 btfsc compass_type ; compass1?
714 bra I2C_sleep_compass1 ; yes
715 I2C_sleep_compass0:
716 bsf SSP1CON2,SEN ; Start condition
717 rcall WaitMSSP
718 movlw 0x3C ; address
719 rcall I2C_TX
720 movlw 0x00
721 rcall I2C_TX
722 movlw b'01101000' ; ConfigA
723 rcall I2C_TX
724 movlw b'00100000' ; ConfigB
725 rcall I2C_TX
726 movlw b'00000010' ; Idle Mode
727 rcall I2C_TX
728 bsf SSP1CON2,PEN ; Stop condition
729 bra WaitMSSP ; (And return)
730
731 I2C_sleep_compass1:
732 bsf SSP1CON2,SEN ; Start condition
733 rcall WaitMSSP
734 movlw 0x3C ; address
735 rcall I2C_TX
736 movlw 0x20 ; CTRL_REG1
737 rcall I2C_TX
738 movlw b'00000000' ; data for CTRL_REG1: acceleration sensor Power-down mode
739 rcall I2C_TX
740 bsf SSP1CON2,PEN ; Stop condition
741 rcall WaitMSSP
742 bsf SSP1CON2,SEN ; Start condition
743 rcall WaitMSSP
744 movlw 0x3C ; address
745 rcall I2C_TX
746 movlw 0x26 ; CTRL_REG7
747 rcall I2C_TX
748 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor Power-down mode
749 rcall I2C_TX
750 bsf SSP1CON2,PEN ; Stop condition
751 bra WaitMSSP ;(And return)
752
753
754 I2C_sleep_compass2:
755 ; magnetic
756 bsf SSP1CON2,SEN ; Start condition
757 rcall WaitMSSP
758 movlw 0x3C ; address
759 rcall I2C_TX
760 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
761 rcall I2C_TX
762 movlw b'00000011' ; CFG_REG_A_M (Idle mode) 0x60
763 rcall I2C_TX
764 movlw b'00000100' ; CFG_REG_B_M 0x61 (set pulse is released only at power-on after PD condition)
765 rcall I2C_TX
766 movlw b'01010001' ; CFG_REG_C_M 0x62
767 rcall I2C_TX
768 movlw b'00000000' ; INT_CTRL_REG_M 0x63
769 rcall I2C_TX
770
771 bsf SSP1CON2,PEN ; Stop condition
772 bra WaitMSSP ; (And return)
773
774 I2C_sleep_compass3:
775 ; magnetic
776 bsf SSP1CON2,SEN ; Start condition
777 rcall WaitMSSP
778 movlw 0x3C ; address
779 rcall I2C_TX
780 movlw 0xA2 ; 0x22 with auto-increment (MSB=1)
781 rcall I2C_TX
782 movlw b'01000010' ; CTRL_REG3_M (Power-down) 0x22
783 rcall I2C_TX
784 bsf SSP1CON2,PEN ; Stop condition
785 bra WaitMSSP ;(And return)
786
787 I2C_sleep_accelerometer2:
788 ; accelerometer
789 bsf SSP1CON2,SEN ; Start condition
790 rcall WaitMSSP
791 movlw 0x32 ; address
792 rcall I2C_TX
793 movlw 0x9F ; 1F with auto-increment (MSB=1)
794 rcall I2C_TX
795 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off) 0x1F
796 rcall I2C_TX
797 movlw b'00000000' ; CTRL_REG1_A (All off) 0x20
798 rcall I2C_TX
799 bsf SSP1CON2,PEN ; Stop condition
800 bra WaitMSSP ; (And return)
801
802 I2C_sleep_accelerometer3:
803 ; accelerometer
804 bsf SSP1CON2,SEN ; Start condition
805 rcall WaitMSSP
806 movlw 0x3A ; address
807 rcall I2C_TX
808 movlw 0x20
809 rcall I2C_TX
810 movlw b'00000000' ; CTRL_REG1_A (100Hz, x,y,z = OFF) 0x20
811 rcall I2C_TX
812 bsf SSP1CON2,PEN ; Stop condition
813 bra WaitMSSP ; (And return)
814
815 global I2C_init_accelerometer
724 I2C_init_accelerometer: 816 I2C_init_accelerometer:
725 btfsc compass_type2 ; compass2? 817 btfsc compass_type3 ; compass3?
726 bra I2C_init_accelerometer2 ; Yes. 818 bra I2C_init_accelerometer3 ; Yes.
727 819 btfsc compass_type2 ; compass2?
728 btfsc compass_type ; compass1? 820 bra I2C_init_accelerometer2 ; Yes.
729 return ; yes, ignore 821 btfsc compass_type ; compass1?
730 822 return ; yes, ignore
731 rcall I2C_sleep_accelerometer ; Regs can only be changed in St.By mode 823
732 824 rcall I2C_sleep_accelerometer ; Regs can only be changed in St.By mode
733 bsf SSP1CON2,SEN ; Start condition 825
734 rcall WaitMSSP 826 bsf SSP1CON2,SEN ; Start condition
735 movlw 0x38 ; address 827 rcall WaitMSSP
736 rcall I2C_TX 828 movlw 0x38 ; address
737 movlw 0x0E ; XYZ_DATA_CFG 829 rcall I2C_TX
738 rcall I2C_TX 830 movlw 0x0E ; XYZ_DATA_CFG
739 movlw b'00000000' ; High pass Filter=0 , +/- 2g range 831 rcall I2C_TX
740 rcall I2C_TX 832 movlw b'00000000' ; High pass Filter=0 , +/- 2g range
741 bsf SSP1CON2,PEN ; Stop condition 833 rcall I2C_TX
742 rcall WaitMSSP 834 bsf SSP1CON2,PEN ; Stop condition
743 835 rcall WaitMSSP
744 bsf SSP1CON2,SEN ; Start condition 836
745 rcall WaitMSSP 837
746 movlw 0x38 ; address 838 bsf SSP1CON2,SEN ; Start condition
747 rcall I2C_TX 839 rcall WaitMSSP
748 movlw 0x2A ; CTRL_REG1 840 movlw 0x38 ; address
749 rcall I2C_TX 841 rcall I2C_TX
750 ; movlw b'00110000' ; CTRL_REG1: 160ms data rate, St.By Mode 842 movlw 0x2A ; CTRL_REG1
751 movlw b'00110100' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode 843 rcall I2C_TX
752 rcall I2C_TX 844 ; movlw b'00110000' ; CTRL_REG1: 160ms data rate, St.By Mode
753 movlw b'00000010' ; CTRL_REG2: High Res in Active mode 845 movlw b'00110100' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode
754 rcall I2C_TX 846 rcall I2C_TX
755 bsf SSP1CON2,PEN ; Stop condition 847 movlw b'00000010' ; CTRL_REG2: High Res in Active mode
756 rcall WaitMSSP 848 rcall I2C_TX
757 849 bsf SSP1CON2,PEN ; Stop condition
758 bsf SSP1CON2,SEN ; Start condition 850 rcall WaitMSSP
759 rcall WaitMSSP 851
760 movlw 0x38 ; address 852 bsf SSP1CON2,SEN ; Start condition
761 rcall I2C_TX 853 rcall WaitMSSP
762 movlw 0x2A ; CTRL_REG1 854 movlw 0x38 ; address
763 rcall I2C_TX 855 rcall I2C_TX
764 ; movlw b'00110001' ; CTRL_REG1: 160ms data rate, Active Mode 856 movlw 0x2A ; CTRL_REG1
765 movlw b'00110101' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode, Active Mode 857 rcall I2C_TX
766 rcall I2C_TX 858 ; movlw b'00110001' ; CTRL_REG1: 160ms data rate, Active Mode
767 bsf SSP1CON2,PEN ; Stop condition 859 movlw b'00110101' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode, Active Mode
768 bra WaitMSSP ; (And return) 860 rcall I2C_TX
769 861 bsf SSP1CON2,PEN ; Stop condition
862 bra WaitMSSP ; (And return)
863
770 I2C_init_accelerometer2: 864 I2C_init_accelerometer2:
771 bsf SSP1CON2,SEN ; Start condition 865 bsf SSP1CON2,SEN ; Start condition
772 rcall WaitMSSP 866 rcall WaitMSSP
773 movlw 0x32 ; address 867 movlw 0x32 ; address
774 rcall I2C_TX 868 rcall I2C_TX
775 movlw 0x9F ; 1F with auto-increment (MSB=1) 869 movlw 0x9F ; 1F with auto-increment (MSB=1)
776 rcall I2C_TX 870 rcall I2C_TX
777 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off) 871 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off) 0x1F
778 rcall I2C_TX 872 rcall I2C_TX
779 movlw b'01010111' ; CTRL_REG1_A (100Hz, x,y,z = ON) 873 movlw b'01010111' ; CTRL_REG1_A (100Hz, x,y,z = ON) 0x20
780 rcall I2C_TX 874 rcall I2C_TX
781 movlw b'00000000' ; CTRL_REG2_A 875 movlw b'00000000' ; CTRL_REG2_A 0x21
782 rcall I2C_TX 876 rcall I2C_TX
783 ; movlw b'00000000' ; CTRL_REG3_A 877 movlw b'00000000' ; CTRL_REG3_A 0x22
784 ; rcall I2C_TX 878 rcall I2C_TX
785 ; movlw b'00000000' ; CTRL_REG4_A (BDU=0, +/-2g, 879 movlw b'00000000' ; CTRL_REG4_A (BDU=0, +/-2g) 0x23
786 ; rcall I2C_TX 880 rcall I2C_TX
787 ; movlw b'00000000' ; CTRL_REG5_A 881 movlw b'00000000' ; CTRL_REG5_A 0x24
788 ; rcall I2C_TX 882 rcall I2C_TX
789 bsf SSP1CON2,PEN ; Stop condition 883 bsf SSP1CON2,PEN ; Stop condition
790 bra WaitMSSP ; (And return) 884 bra WaitMSSP ; (And return)
791 885
792 global I2C_sleep_accelerometer 886 I2C_init_accelerometer3:
887 bsf SSP1CON2,SEN ; Start condition
888 rcall WaitMSSP
889 movlw 0x3A ; address
890 rcall I2C_TX
891 movlw 0x20
892 rcall I2C_TX
893 movlw b'10011111' ; CTRL_REG1_A (10Hz, x,y,z = ON) 0x20
894 rcall I2C_TX
895 movlw b'00000000' ; CTRL_REG2_A 0x21
896 rcall I2C_TX
897 movlw b'00000000' ; CTRL_REG3_A 0x22
898 rcall I2C_TX
899 movlw b'11001100' ; CTRL_REG4_A 0x23
900 rcall I2C_TX
901 bsf SSP1CON2,PEN ; Stop condition
902 bra WaitMSSP ; (And return)
903
904
905 global I2C_sleep_accelerometer
793 I2C_sleep_accelerometer: 906 I2C_sleep_accelerometer:
794 btfsc compass_type2 ; Compass2 907 btfsc compass_type3 ; Compass3
795 bra I2C_sleep_accelerometer2 ; Yes 908 bra I2C_sleep_accelerometer3 ; Yes
796 btfsc compass_type ; compass1? 909 btfsc compass_type2 ; Compass2
797 return ; yes, ignore 910 bra I2C_sleep_accelerometer2 ; Yes
798 911 btfsc compass_type ; compass1?
799 bsf SSP1CON2,SEN ; Start condition 912 return ; yes, ignore
800 rcall WaitMSSP 913 ; compass 0
801 movlw 0x38 ; address 914 bsf SSP1CON2,SEN ; Start condition
802 rcall I2C_TX 915 rcall WaitMSSP
803 movlw 0x2A ; CTRL_REG1 916 movlw 0x38 ; address
804 rcall I2C_TX 917 rcall I2C_TX
805 movlw b'00000000' ; St. By Mode 918 movlw 0x2A ; CTRL_REG1
806 rcall I2C_TX 919 rcall I2C_TX
807 bsf SSP1CON2,PEN ; Stop condition 920 movlw b'00000000' ; St. By Mode
808 bra WaitMSSP ; (And return) 921 rcall I2C_TX
809 922 bsf SSP1CON2,PEN ; Stop condition
923 bra WaitMSSP ; (And return)
924
810 lt2942_init_again: 925 lt2942_init_again:
811 clrf i2c_temp1 926 clrf i2c_temp1
812 movlw 0x02 ; Point to accumulated charge registers 927 movlw 0x02 ; Point to accumulated charge registers
813 rcall I2C_TX_GAUGE 928 rcall I2C_TX_GAUGE
814 movff battery_acumulated_charge+1,SSP1BUF ; Data Byte 929 movff battery_acumulated_charge+1,SSP1BUF ; Data Byte