annotate src/i2c.asm @ 67:7815bf21d353

second debug mode page
author heinrichsweikamp
date Sat, 11 Jan 2014 22:36:17 +0100
parents 9b7dd3103545
children f3062a611eef
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
heinrichsweikamp
parents:
diff changeset
1 ;=============================================================================
heinrichsweikamp
parents:
diff changeset
2 ;
heinrichsweikamp
parents:
diff changeset
3 ; File i2c.asm
heinrichsweikamp
parents:
diff changeset
4 ;
heinrichsweikamp
parents:
diff changeset
5 ; I2C Interface to HMC5883L and MMA8452Q
heinrichsweikamp
parents:
diff changeset
6 ;
heinrichsweikamp
parents:
diff changeset
7 ; HMC5883L's read address (8-Bit): 0x3D
heinrichsweikamp
parents:
diff changeset
8 ; HMC5883L's write address (8-Bit): 0x3C
heinrichsweikamp
parents:
diff changeset
9 ;
heinrichsweikamp
parents:
diff changeset
10 ; MMA8452Q's read address (8-Bit): 0x39
heinrichsweikamp
parents:
diff changeset
11 ; MMA8452Q's write address (8-Bit): 0x38
heinrichsweikamp
parents:
diff changeset
12 ;
heinrichsweikamp
parents:
diff changeset
13 ; Copyright (c) 2012, JD Gascuel, HeinrichsWeikamp, all right reserved.
heinrichsweikamp
parents:
diff changeset
14 ;=============================================================================
heinrichsweikamp
parents:
diff changeset
15 ; HISTORY
heinrichsweikamp
parents:
diff changeset
16 ; 2012-08-22 : [mH] Creation
heinrichsweikamp
parents:
diff changeset
17
heinrichsweikamp
parents:
diff changeset
18
heinrichsweikamp
parents:
diff changeset
19 #include "ostc3.inc" ; Mandatory header
heinrichsweikamp
parents:
diff changeset
20 #include "wait.inc"
heinrichsweikamp
parents:
diff changeset
21
heinrichsweikamp
parents:
diff changeset
22 i2c CODE
heinrichsweikamp
parents:
diff changeset
23
heinrichsweikamp
parents:
diff changeset
24 WaitMSSP:
heinrichsweikamp
parents:
diff changeset
25 decfsz i2c_temp,F ; check for timeout during I2C action
heinrichsweikamp
parents:
diff changeset
26 bra WaitMSSP2
heinrichsweikamp
parents:
diff changeset
27 bra I2CFail ; timeout occured
heinrichsweikamp
parents:
diff changeset
28 WaitMSSP2:
heinrichsweikamp
parents:
diff changeset
29 btfss PIR1,SSPIF
heinrichsweikamp
parents:
diff changeset
30 bra WaitMSSP
heinrichsweikamp
parents:
diff changeset
31 clrf i2c_temp
heinrichsweikamp
parents:
diff changeset
32 bcf PIR1,SSPIF
heinrichsweikamp
parents:
diff changeset
33 nop
heinrichsweikamp
parents:
diff changeset
34 return
heinrichsweikamp
parents:
diff changeset
35
heinrichsweikamp
parents:
diff changeset
36 I2C_WaitforACK:
heinrichsweikamp
parents:
diff changeset
37 btfss SSPCON2,ACKSTAT ; checks for ACK bit from slave
heinrichsweikamp
parents:
diff changeset
38 return
heinrichsweikamp
parents:
diff changeset
39 I2CFail:
heinrichsweikamp
parents:
diff changeset
40 rcall I2CReset ; I2C Reset
heinrichsweikamp
parents:
diff changeset
41 bcf PIR1,SSPIF
heinrichsweikamp
parents:
diff changeset
42 clrf i2c_temp
heinrichsweikamp
parents:
diff changeset
43 return
heinrichsweikamp
parents:
diff changeset
44
heinrichsweikamp
parents:
diff changeset
45 I2CReset: ; Something went wrong (Slave holds SDA low?)
heinrichsweikamp
parents:
diff changeset
46 clrf SSP1CON1 ; wake-up slave and reset entire module
heinrichsweikamp
parents:
diff changeset
47 clrf SSP1CON2
heinrichsweikamp
parents:
diff changeset
48 clrf SSP1STAT
heinrichsweikamp
parents:
diff changeset
49 bcf TRISC,3 ; SCL OUTPUT
heinrichsweikamp
parents:
diff changeset
50 bsf TRISC,4 ; SDA Input
heinrichsweikamp
parents:
diff changeset
51 bcf PORTC,3
heinrichsweikamp
parents:
diff changeset
52 movlw d'9'
heinrichsweikamp
parents:
diff changeset
53 movwf i2c_temp ; clock-out 9 clock cycles manually
heinrichsweikamp
parents:
diff changeset
54 I2CReset_1:
heinrichsweikamp
parents:
diff changeset
55 bsf PORTC,3 ; SCL=1
heinrichsweikamp
parents:
diff changeset
56 nop
heinrichsweikamp
parents:
diff changeset
57 nop
heinrichsweikamp
parents:
diff changeset
58 nop
heinrichsweikamp
parents:
diff changeset
59 nop
heinrichsweikamp
parents:
diff changeset
60 btfsc PORTC,4 ; SDA=1?
heinrichsweikamp
parents:
diff changeset
61 bra I2CReset_2 ; =1, SDA has been released from slave
heinrichsweikamp
parents:
diff changeset
62 bcf PORTC,3 ; SCL=0
heinrichsweikamp
parents:
diff changeset
63 nop
heinrichsweikamp
parents:
diff changeset
64 nop
heinrichsweikamp
parents:
diff changeset
65 bcf PORTC,3
heinrichsweikamp
parents:
diff changeset
66 nop
heinrichsweikamp
parents:
diff changeset
67 nop
heinrichsweikamp
parents:
diff changeset
68 decfsz i2c_temp,F
heinrichsweikamp
parents:
diff changeset
69 bra I2CReset_1 ; check for nine clock cycles
heinrichsweikamp
parents:
diff changeset
70 I2CReset_2:
heinrichsweikamp
parents:
diff changeset
71 bsf TRISC,3 ; SCL Input
heinrichsweikamp
parents:
diff changeset
72 clrf SSP1CON1 ; setup I²C Mode
heinrichsweikamp
parents:
diff changeset
73 WAITMS d'10' ; Reset-Timeout for I2C devices
heinrichsweikamp
parents:
diff changeset
74 movlw b'00000000' ; with slew rate control
heinrichsweikamp
parents:
diff changeset
75 movwf SSPSTAT
heinrichsweikamp
parents:
diff changeset
76 movlw b'00101000'
heinrichsweikamp
parents:
diff changeset
77 movwf SSP1CON1
heinrichsweikamp
parents:
diff changeset
78 movlw b'00000000'
heinrichsweikamp
parents:
diff changeset
79 movwf SSP1CON2
heinrichsweikamp
parents:
diff changeset
80 movlw 0x27
heinrichsweikamp
parents:
diff changeset
81 movwf SSP1ADD
heinrichsweikamp
parents:
diff changeset
82 return
heinrichsweikamp
parents:
diff changeset
83
heinrichsweikamp
parents:
diff changeset
84 I2C_TX:
heinrichsweikamp
parents:
diff changeset
85 movwf SSP1BUF
heinrichsweikamp
parents:
diff changeset
86 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
87 bra I2C_WaitforACK ; Returns...
heinrichsweikamp
parents:
diff changeset
88
heinrichsweikamp
parents:
diff changeset
89 I2C_TwoBytesRX_div16: ; Get two bytes and devide lo:hi/16 (signed)
heinrichsweikamp
parents:
diff changeset
90 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
91 movff SSP1BUF,hi ; Data Byte
heinrichsweikamp
parents:
diff changeset
92 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
93 movff SSP1BUF,lo ; Data Byte
heinrichsweikamp
parents:
diff changeset
94 I2C_TwoBytesRX_div16_2: ; devide lo:hi/16 (signed) only
heinrichsweikamp
parents:
diff changeset
95 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
96 btfsc hi,7 ; Copy sign bit to carry
heinrichsweikamp
parents:
diff changeset
97 bsf STATUS,C
heinrichsweikamp
parents:
diff changeset
98 rrcf hi ; /2
heinrichsweikamp
parents:
diff changeset
99 rrcf lo
heinrichsweikamp
parents:
diff changeset
100 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
101 btfsc hi,7 ; Copy sign bit to carry
heinrichsweikamp
parents:
diff changeset
102 bsf STATUS,C
heinrichsweikamp
parents:
diff changeset
103 rrcf hi ; /4
heinrichsweikamp
parents:
diff changeset
104 rrcf lo
heinrichsweikamp
parents:
diff changeset
105 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
106 btfsc hi,7 ; Copy sign bit to carry
heinrichsweikamp
parents:
diff changeset
107 bsf STATUS,C
heinrichsweikamp
parents:
diff changeset
108 rrcf hi ; /8
heinrichsweikamp
parents:
diff changeset
109 rrcf lo
heinrichsweikamp
parents:
diff changeset
110 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
111 btfsc hi,7 ; Copy sign bit to carry
heinrichsweikamp
parents:
diff changeset
112 bsf STATUS,C
heinrichsweikamp
parents:
diff changeset
113 rrcf hi ; /16
heinrichsweikamp
parents:
diff changeset
114 rrcf lo
heinrichsweikamp
parents:
diff changeset
115 return
heinrichsweikamp
parents:
diff changeset
116
heinrichsweikamp
parents:
diff changeset
117 global I2C_RX_accelerometer
heinrichsweikamp
parents:
diff changeset
118 I2C_RX_accelerometer:
heinrichsweikamp
parents:
diff changeset
119 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
120 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
121 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
122 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
123 movlw 0x01
heinrichsweikamp
parents:
diff changeset
124 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
125 bsf SSP1CON2,RSEN ; Repeated start condition (!)
heinrichsweikamp
parents:
diff changeset
126 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
127 movlw 0x39 ; address
heinrichsweikamp
parents:
diff changeset
128 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
129
heinrichsweikamp
parents:
diff changeset
130 ; Chip orientation on the PCB requires
heinrichsweikamp
parents:
diff changeset
131 ; Original = Corrected
heinrichsweikamp
parents:
diff changeset
132 ; x = -x
heinrichsweikamp
parents:
diff changeset
133 ; y = -y
heinrichsweikamp
parents:
diff changeset
134 ; z = -z
heinrichsweikamp
parents:
diff changeset
135
heinrichsweikamp
parents:
diff changeset
136
heinrichsweikamp
parents:
diff changeset
137 rcall I2C_TwoBytesRX_div16 ; Get two bytes and devide /16 (signed)
heinrichsweikamp
parents:
diff changeset
138 comf hi ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
139 negf lo
heinrichsweikamp
parents:
diff changeset
140 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
141 incf hi,F ; YES: do it.
heinrichsweikamp
parents:
diff changeset
142 movff lo,accel_DX+0
heinrichsweikamp
parents:
diff changeset
143 movff hi,accel_DX+1 ; Copy result
heinrichsweikamp
parents:
diff changeset
144
heinrichsweikamp
parents:
diff changeset
145 rcall I2C_TwoBytesRX_div16 ; Get two bytes and devide /16 (signed)
heinrichsweikamp
parents:
diff changeset
146 comf hi ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
147 negf lo
heinrichsweikamp
parents:
diff changeset
148 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
149 incf hi,F ; YES: do it.
heinrichsweikamp
parents:
diff changeset
150 movff lo,accel_DY+0
heinrichsweikamp
parents:
diff changeset
151 movff hi,accel_DY+1 ; Copy result
heinrichsweikamp
parents:
diff changeset
152
heinrichsweikamp
parents:
diff changeset
153 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
154 movff SSP1BUF,hi ; Data Byte
heinrichsweikamp
parents:
diff changeset
155 bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents:
diff changeset
156 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
157 ; According to datasheet there should be no Master Acknowlegde for the last Byte (accel_DZ+0)...
heinrichsweikamp
parents:
diff changeset
158 movff SSP1BUF,lo ; Data Byte
heinrichsweikamp
parents:
diff changeset
159
heinrichsweikamp
parents:
diff changeset
160 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only
heinrichsweikamp
parents:
diff changeset
161 comf hi ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
162 negf lo
heinrichsweikamp
parents:
diff changeset
163 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
164 incf hi,F ; YES: do it.
heinrichsweikamp
parents:
diff changeset
165 movff lo,accel_DZ+0
heinrichsweikamp
parents:
diff changeset
166 movff hi,accel_DZ+1 ; Copy result
heinrichsweikamp
parents:
diff changeset
167
heinrichsweikamp
parents:
diff changeset
168 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
169 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
170 return
heinrichsweikamp
parents:
diff changeset
171
heinrichsweikamp
parents:
diff changeset
172 I2C_OneByteRX:
heinrichsweikamp
parents:
diff changeset
173 bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents:
diff changeset
174 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
175 bsf SSP1CON2,ACKEN ; Master acknowlegde
heinrichsweikamp
parents:
diff changeset
176 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
177 return
heinrichsweikamp
parents:
diff changeset
178
heinrichsweikamp
parents:
diff changeset
179 global I2C_RX_compass
heinrichsweikamp
parents:
diff changeset
180 I2C_RX_compass:
heinrichsweikamp
parents:
diff changeset
181 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
182 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
183 movlw 0x3C ; address
heinrichsweikamp
parents:
diff changeset
184 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
185 movlw 0x03
heinrichsweikamp
parents:
diff changeset
186 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
187 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
188 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
189
heinrichsweikamp
parents:
diff changeset
190 bcf PIR1,SSPIF
heinrichsweikamp
parents:
diff changeset
191 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
192 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
193 movlw 0x3D ; address
heinrichsweikamp
parents:
diff changeset
194 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
195
heinrichsweikamp
parents:
diff changeset
196 ; Compass IC sends data in following order:
heinrichsweikamp
parents:
diff changeset
197 ; x MSB
heinrichsweikamp
parents:
diff changeset
198 ; x LSB
heinrichsweikamp
parents:
diff changeset
199 ; z MSB
heinrichsweikamp
parents:
diff changeset
200 ; z LSB
heinrichsweikamp
parents:
diff changeset
201 ; y MSB
heinrichsweikamp
parents:
diff changeset
202 ; y LSB
heinrichsweikamp
parents:
diff changeset
203
heinrichsweikamp
parents:
diff changeset
204 ; Chip orientation on the PCB requires
heinrichsweikamp
parents:
diff changeset
205 ; Original = Corrected
heinrichsweikamp
parents:
diff changeset
206 ; x = -y
heinrichsweikamp
parents:
diff changeset
207 ; z = z
heinrichsweikamp
parents:
diff changeset
208 ; y = x
heinrichsweikamp
parents:
diff changeset
209
heinrichsweikamp
parents:
diff changeset
210 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
211 movff SSP1BUF,compass_DY+1; Data Byte
heinrichsweikamp
parents:
diff changeset
212 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
213 movff SSP1BUF,compass_DY+0; Data Byte
heinrichsweikamp
parents:
diff changeset
214 banksel compass_DY
heinrichsweikamp
parents:
diff changeset
215 comf compass_DY+1 ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
216 negf compass_DY+0
heinrichsweikamp
parents:
diff changeset
217 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
218 incf compass_DY+1,F ; YES: do it.
heinrichsweikamp
parents:
diff changeset
219 banksel common
heinrichsweikamp
parents:
diff changeset
220 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
221 movff SSP1BUF,compass_DZ+1; Data Byte
heinrichsweikamp
parents:
diff changeset
222 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
223 movff SSP1BUF,compass_DZ+0; Data Byte
heinrichsweikamp
parents:
diff changeset
224 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
225 movff SSP1BUF,compass_DX+1; Data Byte
heinrichsweikamp
parents:
diff changeset
226 bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents:
diff changeset
227 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
228 movff SSP1BUF,compass_DX+0; Data Byte
heinrichsweikamp
parents:
diff changeset
229 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
230 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
231 return
heinrichsweikamp
parents:
diff changeset
232
heinrichsweikamp
parents:
diff changeset
233 global I2C_init_compass
heinrichsweikamp
parents:
diff changeset
234 I2C_init_compass:
heinrichsweikamp
parents:
diff changeset
235 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
236 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
237 movlw 0x3C ; address
heinrichsweikamp
parents:
diff changeset
238 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
239 movlw 0x00
heinrichsweikamp
parents:
diff changeset
240 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
241 ; movlw b'01101001' ; ConfigA: 3Hz, 8 Samples averaged, Test Mode (Positive Bias)
heinrichsweikamp
parents:
diff changeset
242 movlw b'01101000' ; ConfigA: 3Hz, 8 Samples averaged
heinrichsweikamp
parents:
diff changeset
243 rcall I2C_TX
20
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
244 bra I2C_init_compass_common
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
245
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
246 global I2C_init_compass_fast
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
247 I2C_init_compass_fast:
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
248 bsf SSP1CON2,SEN ; Start condition
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
249 rcall WaitMSSP
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
250 movlw 0x3C ; address
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
251 rcall I2C_TX
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
252 movlw 0x00
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
253 rcall I2C_TX
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
254 movlw b'00111000' ; ConfigA: 75Hz, 2 Samples averaged
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
255 ; movlw b'00111001' ; ConfigA: 75Hz, 2 Samples averaged, Test Mode (Positive Bias)
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
256 rcall I2C_TX
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
257 I2C_init_compass_common:
18
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
258 movff opt_compass_gain,i2c_temp ; 0-7 (230LSB/Gauss to 1370LSB/Gaus)
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
259 swapf i2c_temp,F
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
260 comf i2c_temp,F
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
261 bcf STATUS,C
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
262 rlcf i2c_temp
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
263 movf i2c_temp,W
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
264 clrf i2c_temp
0
heinrichsweikamp
parents:
diff changeset
265 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
266 movlw b'00000000' ; Continous Mode
heinrichsweikamp
parents:
diff changeset
267 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
268 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
269 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
270 bsf compass_enabled
heinrichsweikamp
parents:
diff changeset
271 return
heinrichsweikamp
parents:
diff changeset
272
heinrichsweikamp
parents:
diff changeset
273 global I2C_sleep_compass
heinrichsweikamp
parents:
diff changeset
274 I2C_sleep_compass:
heinrichsweikamp
parents:
diff changeset
275 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
276 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
277 movlw 0x3C ; address
heinrichsweikamp
parents:
diff changeset
278 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
279 movlw 0x00
heinrichsweikamp
parents:
diff changeset
280 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
281 movlw b'01101000' ; ConfigA
heinrichsweikamp
parents:
diff changeset
282 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
283 movlw b'00100000' ; ConfigB
heinrichsweikamp
parents:
diff changeset
284 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
285 movlw b'00000010' ; Idle Mode
heinrichsweikamp
parents:
diff changeset
286 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
287 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
288 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
289 bcf compass_enabled
heinrichsweikamp
parents:
diff changeset
290 return
heinrichsweikamp
parents:
diff changeset
291
heinrichsweikamp
parents:
diff changeset
292
heinrichsweikamp
parents:
diff changeset
293 global I2C_init_accelerometer
heinrichsweikamp
parents:
diff changeset
294 I2C_init_accelerometer:
heinrichsweikamp
parents:
diff changeset
295 rcall I2C_sleep_accelerometer ; Regs can only be changed in St.By mode
heinrichsweikamp
parents:
diff changeset
296
heinrichsweikamp
parents:
diff changeset
297 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
298 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
299 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
300 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
301 movlw 0x0E ; XYZ_DATA_CFG
heinrichsweikamp
parents:
diff changeset
302 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
303 movlw b'00000000' ; High pass Filter=0 , +/- 2g range
heinrichsweikamp
parents:
diff changeset
304 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
305 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
306 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
307
heinrichsweikamp
parents:
diff changeset
308
heinrichsweikamp
parents:
diff changeset
309 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
310 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
311 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
312 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
313 movlw 0x2A ; CTRL_REG1
heinrichsweikamp
parents:
diff changeset
314 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
315 ; movlw b'00110000' ; CTRL_REG1: 160ms data rate, St.By Mode
heinrichsweikamp
parents:
diff changeset
316 movlw b'00110100' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode
heinrichsweikamp
parents:
diff changeset
317 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
318 movlw b'00000010' ; CTRL_REG2: High Res in Active mode
heinrichsweikamp
parents:
diff changeset
319 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
320 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
321 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
322
heinrichsweikamp
parents:
diff changeset
323 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
324 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
325 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
326 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
327 movlw 0x2A ; CTRL_REG1
heinrichsweikamp
parents:
diff changeset
328 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
329 ; movlw b'00110001' ; CTRL_REG1: 160ms data rate, Active Mode
heinrichsweikamp
parents:
diff changeset
330 movlw b'00110101' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode, Active Mode
heinrichsweikamp
parents:
diff changeset
331 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
332 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
333 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
334
heinrichsweikamp
parents:
diff changeset
335 return
heinrichsweikamp
parents:
diff changeset
336
heinrichsweikamp
parents:
diff changeset
337 global I2C_sleep_accelerometer
heinrichsweikamp
parents:
diff changeset
338 I2C_sleep_accelerometer:
heinrichsweikamp
parents:
diff changeset
339 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
340 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
341 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
342 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
343 movlw 0x2A ; CTRL_REG1
heinrichsweikamp
parents:
diff changeset
344 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
345 movlw b'00000000' ; St. By Mode
heinrichsweikamp
parents:
diff changeset
346 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
347 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
348 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
349 return
heinrichsweikamp
parents:
diff changeset
350
heinrichsweikamp
parents:
diff changeset
351
heinrichsweikamp
parents:
diff changeset
352 END