annotate src/i2c.asm @ 3:72d9cb652604

interface description added
author heinrichsweikamp
date Mon, 06 May 2013 14:21:11 +0200
parents 11d4fc797f74
children 4e3f133dfbf4
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 ; movlw b'00111000' ; ConfigA: 75Hz, 2 Samples averaged
heinrichsweikamp
parents:
diff changeset
244 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
245 movlw b'00100000' ; ConfigB, 1090Gauss Gain
heinrichsweikamp
parents:
diff changeset
246 ; movlw b'10000000' ; ConfigB, 440Gauss Gain
heinrichsweikamp
parents:
diff changeset
247 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
248 movlw b'00000000' ; Continous Mode
heinrichsweikamp
parents:
diff changeset
249 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
250 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
251 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
252 bsf compass_enabled
heinrichsweikamp
parents:
diff changeset
253 return
heinrichsweikamp
parents:
diff changeset
254
heinrichsweikamp
parents:
diff changeset
255 global I2C_init_compass_fast
heinrichsweikamp
parents:
diff changeset
256 I2C_init_compass_fast:
heinrichsweikamp
parents:
diff changeset
257 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
258 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
259 movlw 0x3C ; address
heinrichsweikamp
parents:
diff changeset
260 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
261 movlw 0x00
heinrichsweikamp
parents:
diff changeset
262 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
263 movlw b'00111000' ; ConfigA: 75Hz, 2 Samples averaged
heinrichsweikamp
parents:
diff changeset
264 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
265 movlw b'00100000' ; ConfigB, 1090Gauss Gain
heinrichsweikamp
parents:
diff changeset
266 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
267 movlw b'00000000' ; Continous Mode
heinrichsweikamp
parents:
diff changeset
268 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
269 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
270 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
271 bsf compass_enabled
heinrichsweikamp
parents:
diff changeset
272 return
heinrichsweikamp
parents:
diff changeset
273
heinrichsweikamp
parents:
diff changeset
274
heinrichsweikamp
parents:
diff changeset
275 global I2C_sleep_compass
heinrichsweikamp
parents:
diff changeset
276 I2C_sleep_compass:
heinrichsweikamp
parents:
diff changeset
277 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
278 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
279 movlw 0x3C ; address
heinrichsweikamp
parents:
diff changeset
280 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
281 movlw 0x00
heinrichsweikamp
parents:
diff changeset
282 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
283 movlw b'01101000' ; ConfigA
heinrichsweikamp
parents:
diff changeset
284 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
285 movlw b'00100000' ; ConfigB
heinrichsweikamp
parents:
diff changeset
286 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
287 movlw b'00000010' ; Idle Mode
heinrichsweikamp
parents:
diff changeset
288 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
289 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
290 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
291 bcf compass_enabled
heinrichsweikamp
parents:
diff changeset
292 return
heinrichsweikamp
parents:
diff changeset
293
heinrichsweikamp
parents:
diff changeset
294
heinrichsweikamp
parents:
diff changeset
295 global I2C_init_accelerometer
heinrichsweikamp
parents:
diff changeset
296 I2C_init_accelerometer:
heinrichsweikamp
parents:
diff changeset
297 rcall I2C_sleep_accelerometer ; Regs can only be changed in St.By mode
heinrichsweikamp
parents:
diff changeset
298
heinrichsweikamp
parents:
diff changeset
299 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
300 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
301 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
302 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
303 movlw 0x0E ; XYZ_DATA_CFG
heinrichsweikamp
parents:
diff changeset
304 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
305 movlw b'00000000' ; High pass Filter=0 , +/- 2g range
heinrichsweikamp
parents:
diff changeset
306 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
307 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
308 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
309
heinrichsweikamp
parents:
diff changeset
310
heinrichsweikamp
parents:
diff changeset
311 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
312 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
313 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
314 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
315 movlw 0x2A ; CTRL_REG1
heinrichsweikamp
parents:
diff changeset
316 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
317 ; movlw b'00110000' ; CTRL_REG1: 160ms data rate, St.By Mode
heinrichsweikamp
parents:
diff changeset
318 movlw b'00110100' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode
heinrichsweikamp
parents:
diff changeset
319 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
320 movlw b'00000010' ; CTRL_REG2: High Res in Active mode
heinrichsweikamp
parents:
diff changeset
321 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
322 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
323 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
324
heinrichsweikamp
parents:
diff changeset
325 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
326 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
327 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
328 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
329 movlw 0x2A ; CTRL_REG1
heinrichsweikamp
parents:
diff changeset
330 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
331 ; movlw b'00110001' ; CTRL_REG1: 160ms data rate, Active Mode
heinrichsweikamp
parents:
diff changeset
332 movlw b'00110101' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode, Active Mode
heinrichsweikamp
parents:
diff changeset
333 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
334 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
335 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
336
heinrichsweikamp
parents:
diff changeset
337 return
heinrichsweikamp
parents:
diff changeset
338
heinrichsweikamp
parents:
diff changeset
339 global I2C_sleep_accelerometer
heinrichsweikamp
parents:
diff changeset
340 I2C_sleep_accelerometer:
heinrichsweikamp
parents:
diff changeset
341 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
342 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
343 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
344 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
345 movlw 0x2A ; CTRL_REG1
heinrichsweikamp
parents:
diff changeset
346 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
347 movlw b'00000000' ; St. By Mode
heinrichsweikamp
parents:
diff changeset
348 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
349 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
350 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
351 return
heinrichsweikamp
parents:
diff changeset
352
heinrichsweikamp
parents:
diff changeset
353
heinrichsweikamp
parents:
diff changeset
354 END