annotate src/i2c.asm @ 466:7f4c3b2e6bb7

battery gauge handling
author heinrichsweikamp
date Sat, 19 Nov 2016 18:37:47 +0100
parents 850c1f3c4824
children 6ca155fc1509
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 ;
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
13 ; LSM303D's read address (8-Bit): 0x3D
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
14 ; LSM303D's write address (8-Bit): 0x3C
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
15 ;
0
heinrichsweikamp
parents:
diff changeset
16 ; Copyright (c) 2012, JD Gascuel, HeinrichsWeikamp, all right reserved.
heinrichsweikamp
parents:
diff changeset
17 ;=============================================================================
heinrichsweikamp
parents:
diff changeset
18 ; HISTORY
heinrichsweikamp
parents:
diff changeset
19 ; 2012-08-22 : [mH] Creation
heinrichsweikamp
parents:
diff changeset
20
heinrichsweikamp
parents:
diff changeset
21
275
653a3ab08062 rename into hwOS
heinrichsweikamp
parents: 235
diff changeset
22 #include "hwos.inc" ; Mandatory header
0
heinrichsweikamp
parents:
diff changeset
23 #include "wait.inc"
113
heinrichsweikamp
parents: 20
diff changeset
24 #include "math.inc"
heinrichsweikamp
parents: 20
diff changeset
25
0
heinrichsweikamp
parents:
diff changeset
26 i2c CODE
heinrichsweikamp
parents:
diff changeset
27
heinrichsweikamp
parents:
diff changeset
28 WaitMSSP:
heinrichsweikamp
parents:
diff changeset
29 decfsz i2c_temp,F ; check for timeout during I2C action
heinrichsweikamp
parents:
diff changeset
30 bra WaitMSSP2
heinrichsweikamp
parents:
diff changeset
31 bra I2CFail ; timeout occured
heinrichsweikamp
parents:
diff changeset
32 WaitMSSP2:
heinrichsweikamp
parents:
diff changeset
33 btfss PIR1,SSPIF
heinrichsweikamp
parents:
diff changeset
34 bra WaitMSSP
heinrichsweikamp
parents:
diff changeset
35 clrf i2c_temp
heinrichsweikamp
parents:
diff changeset
36 bcf PIR1,SSPIF
heinrichsweikamp
parents:
diff changeset
37 nop
heinrichsweikamp
parents:
diff changeset
38 return
heinrichsweikamp
parents:
diff changeset
39
heinrichsweikamp
parents:
diff changeset
40 I2C_WaitforACK:
heinrichsweikamp
parents:
diff changeset
41 btfss SSPCON2,ACKSTAT ; checks for ACK bit from slave
heinrichsweikamp
parents:
diff changeset
42 return
heinrichsweikamp
parents:
diff changeset
43 I2CFail:
heinrichsweikamp
parents:
diff changeset
44 rcall I2CReset ; I2C Reset
heinrichsweikamp
parents:
diff changeset
45 bcf PIR1,SSPIF
heinrichsweikamp
parents:
diff changeset
46 clrf i2c_temp
heinrichsweikamp
parents:
diff changeset
47 return
heinrichsweikamp
parents:
diff changeset
48
heinrichsweikamp
parents:
diff changeset
49 I2CReset: ; Something went wrong (Slave holds SDA low?)
heinrichsweikamp
parents:
diff changeset
50 clrf SSP1CON1 ; wake-up slave and reset entire module
heinrichsweikamp
parents:
diff changeset
51 clrf SSP1CON2
heinrichsweikamp
parents:
diff changeset
52 clrf SSP1STAT
heinrichsweikamp
parents:
diff changeset
53 bcf TRISC,3 ; SCL OUTPUT
heinrichsweikamp
parents:
diff changeset
54 bsf TRISC,4 ; SDA Input
heinrichsweikamp
parents:
diff changeset
55 bcf PORTC,3
heinrichsweikamp
parents:
diff changeset
56 movlw d'9'
heinrichsweikamp
parents:
diff changeset
57 movwf i2c_temp ; clock-out 9 clock cycles manually
heinrichsweikamp
parents:
diff changeset
58 I2CReset_1:
heinrichsweikamp
parents:
diff changeset
59 bsf PORTC,3 ; SCL=1
heinrichsweikamp
parents:
diff changeset
60 nop
heinrichsweikamp
parents:
diff changeset
61 nop
heinrichsweikamp
parents:
diff changeset
62 nop
heinrichsweikamp
parents:
diff changeset
63 nop
heinrichsweikamp
parents:
diff changeset
64 btfsc PORTC,4 ; SDA=1?
heinrichsweikamp
parents:
diff changeset
65 bra I2CReset_2 ; =1, SDA has been released from slave
heinrichsweikamp
parents:
diff changeset
66 bcf PORTC,3 ; SCL=0
heinrichsweikamp
parents:
diff changeset
67 nop
heinrichsweikamp
parents:
diff changeset
68 nop
heinrichsweikamp
parents:
diff changeset
69 bcf PORTC,3
heinrichsweikamp
parents:
diff changeset
70 nop
heinrichsweikamp
parents:
diff changeset
71 nop
heinrichsweikamp
parents:
diff changeset
72 decfsz i2c_temp,F
heinrichsweikamp
parents:
diff changeset
73 bra I2CReset_1 ; check for nine clock cycles
heinrichsweikamp
parents:
diff changeset
74 I2CReset_2:
heinrichsweikamp
parents:
diff changeset
75 bsf TRISC,3 ; SCL Input
heinrichsweikamp
parents:
diff changeset
76 clrf SSP1CON1 ; setup I²C Mode
heinrichsweikamp
parents:
diff changeset
77 WAITMS d'10' ; Reset-Timeout for I2C devices
heinrichsweikamp
parents:
diff changeset
78 movlw b'00000000' ; with slew rate control
heinrichsweikamp
parents:
diff changeset
79 movwf SSPSTAT
heinrichsweikamp
parents:
diff changeset
80 movlw b'00101000'
heinrichsweikamp
parents:
diff changeset
81 movwf SSP1CON1
heinrichsweikamp
parents:
diff changeset
82 movlw b'00000000'
heinrichsweikamp
parents:
diff changeset
83 movwf SSP1CON2
heinrichsweikamp
parents:
diff changeset
84 movlw 0x27
heinrichsweikamp
parents:
diff changeset
85 movwf SSP1ADD
heinrichsweikamp
parents:
diff changeset
86 return
heinrichsweikamp
parents:
diff changeset
87
heinrichsweikamp
parents:
diff changeset
88 I2C_TX:
heinrichsweikamp
parents:
diff changeset
89 movwf SSP1BUF
heinrichsweikamp
parents:
diff changeset
90 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
91 bra I2C_WaitforACK ; Returns...
heinrichsweikamp
parents:
diff changeset
92
heinrichsweikamp
parents:
diff changeset
93 I2C_TwoBytesRX_div16: ; Get two bytes and devide lo:hi/16 (signed)
heinrichsweikamp
parents:
diff changeset
94 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
95 movff SSP1BUF,hi ; Data Byte
heinrichsweikamp
parents:
diff changeset
96 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
97 movff SSP1BUF,lo ; Data Byte
heinrichsweikamp
parents:
diff changeset
98 I2C_TwoBytesRX_div16_2: ; devide lo:hi/16 (signed) only
heinrichsweikamp
parents:
diff changeset
99 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
100 btfsc hi,7 ; Copy sign bit to carry
heinrichsweikamp
parents:
diff changeset
101 bsf STATUS,C
heinrichsweikamp
parents:
diff changeset
102 rrcf hi ; /2
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
103 rrcf lo
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
104 I2C_TwoBytesRX_div8_2: ; devide lo:hi/8 (signed) only
0
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 ; /4
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
109 rrcf lo
0
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 ; /8
heinrichsweikamp
parents:
diff changeset
114 rrcf lo
heinrichsweikamp
parents:
diff changeset
115 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
116 btfsc hi,7 ; Copy sign bit to carry
heinrichsweikamp
parents:
diff changeset
117 bsf STATUS,C
heinrichsweikamp
parents:
diff changeset
118 rrcf hi ; /16
heinrichsweikamp
parents:
diff changeset
119 rrcf lo
heinrichsweikamp
parents:
diff changeset
120 return
heinrichsweikamp
parents:
diff changeset
121
heinrichsweikamp
parents:
diff changeset
122 global I2C_RX_accelerometer
heinrichsweikamp
parents:
diff changeset
123 I2C_RX_accelerometer:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
124 btfsc compass_type ; compass1?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
125 bra I2C_RX_accelerometer_compass1 ; yes
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
126 ;I2C_RX_accelerometer_compass0:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
127 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
128 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
129 movlw 0x38 ; address
0
heinrichsweikamp
parents:
diff changeset
130 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
131 movlw 0x00
0
heinrichsweikamp
parents:
diff changeset
132 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
133 bsf SSP1CON2,RSEN ; Repeated start condition (!)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
134 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
135 movlw 0x39 ; address
0
heinrichsweikamp
parents:
diff changeset
136 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
137
158
683321c09cfa nicer boot into surfacemode
heinrichsweikamp
parents: 113
diff changeset
138 rcall I2C_OneByteRX ; Get Status Byte
683321c09cfa nicer boot into surfacemode
heinrichsweikamp
parents: 113
diff changeset
139 movf SSP1BUF,W
683321c09cfa nicer boot into surfacemode
heinrichsweikamp
parents: 113
diff changeset
140
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
141 ; Non-flipped screen:
0
heinrichsweikamp
parents:
diff changeset
142 ; Chip orientation on the PCB requires
heinrichsweikamp
parents:
diff changeset
143 ; Original = Corrected
heinrichsweikamp
parents:
diff changeset
144 ; x = -x
heinrichsweikamp
parents:
diff changeset
145 ; y = -y
heinrichsweikamp
parents:
diff changeset
146 ; z = -z
heinrichsweikamp
parents:
diff changeset
147
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
148 ; Flipped screen:
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
149 ; Chip orientation on the PCB requires
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
150 ; Original = Corrected
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
151 ; x = x
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
152 ; y = y
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
153 ; z = -z
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
154
0
heinrichsweikamp
parents:
diff changeset
155 rcall I2C_TwoBytesRX_div16 ; Get two bytes and devide /16 (signed)
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
156 btfsc flip_screen ; 180° rotation ?
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
157 bra I2C_RX_accelerometer2 ; Yes
0
heinrichsweikamp
parents:
diff changeset
158 comf hi ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
159 negf lo
heinrichsweikamp
parents:
diff changeset
160 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
161 incf hi,F ; YES: do it.
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
162 I2C_RX_accelerometer2:
0
heinrichsweikamp
parents:
diff changeset
163 movff lo,accel_DX+0
heinrichsweikamp
parents:
diff changeset
164 movff hi,accel_DX+1 ; Copy result
heinrichsweikamp
parents:
diff changeset
165
heinrichsweikamp
parents:
diff changeset
166 rcall I2C_TwoBytesRX_div16 ; Get two bytes and devide /16 (signed)
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
167 btfsc flip_screen ; 180° rotation ?
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
168 bra I2C_RX_accelerometer3 ; Yes
0
heinrichsweikamp
parents:
diff changeset
169 comf hi ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
170 negf lo
heinrichsweikamp
parents:
diff changeset
171 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
172 incf hi,F ; YES: do it.
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
173 I2C_RX_accelerometer3:
0
heinrichsweikamp
parents:
diff changeset
174 movff lo,accel_DY+0
heinrichsweikamp
parents:
diff changeset
175 movff hi,accel_DY+1 ; Copy result
heinrichsweikamp
parents:
diff changeset
176
heinrichsweikamp
parents:
diff changeset
177 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
178 movff SSP1BUF,hi ; Data Byte
heinrichsweikamp
parents:
diff changeset
179 bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents:
diff changeset
180 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
181 ; According to datasheet there should be no Master Acknowlegde for the last Byte (accel_DZ+0)...
heinrichsweikamp
parents:
diff changeset
182 movff SSP1BUF,lo ; Data Byte
heinrichsweikamp
parents:
diff changeset
183
heinrichsweikamp
parents:
diff changeset
184 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only
heinrichsweikamp
parents:
diff changeset
185 comf hi ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
186 negf lo
heinrichsweikamp
parents:
diff changeset
187 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
188 incf hi,F ; YES: do it.
heinrichsweikamp
parents:
diff changeset
189 movff lo,accel_DZ+0
heinrichsweikamp
parents:
diff changeset
190 movff hi,accel_DZ+1 ; Copy result
heinrichsweikamp
parents:
diff changeset
191
heinrichsweikamp
parents:
diff changeset
192 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
193 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
194 return
heinrichsweikamp
parents:
diff changeset
195
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
196 I2C_RX_accelerometer_compass1:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
197 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
198 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
199 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
200 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
201 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
202 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
203 bsf SSP1CON2,RSEN ; Repeated start condition (!)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
204 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
205 movlw 0x3D ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
206 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
207
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
208 ; Non-flipped screen:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
209 ; Chip orientation on the PCB requires
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
210 ; Original = Corrected
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
211 ; x = -x
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
212 ; y = -y
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
213 ; z = -z
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
214
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
215 ; Flipped screen:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
216 ; Chip orientation on the PCB requires
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
217 ; Original = Corrected
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
218 ; x = x
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
219 ; y = y
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
220 ; z = -z
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
221
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
222 ; Dump the accelerator data
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
223 rcall I2C_OneByteRX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
224 movff SSP1BUF,lo ;accel_DX+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
225 rcall I2C_OneByteRX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
226 movff SSP1BUF,hi ;accel_DX+1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
227 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
228 btfsc flip_screen ; 180° rotation ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
229 bra I2C_RX_accelerometer2_c1 ; Yes
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
230 comf hi ; 16bit sign change.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
231 negf lo
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
232 btfsc STATUS,C ; Carry to propagate ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
233 incf hi,F ; YES: do it.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
234 I2C_RX_accelerometer2_c1:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
235 movff lo,accel_DX+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
236 movff hi,accel_DX+1 ; Copy result
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
237
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
238 rcall I2C_OneByteRX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
239 movff SSP1BUF,lo ;accel_DY+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
240 rcall I2C_OneByteRX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
241 movff SSP1BUF,hi ;accel_DY+1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
242
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
243 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
244 btfsc flip_screen ; 180° rotation ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
245 bra I2C_RX_accelerometer3_c1 ; Yes
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
246 comf hi ; 16bit sign change.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
247 negf lo
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
248 btfsc STATUS,C ; Carry to propagate ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
249 incf hi,F ; YES: do it.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
250 I2C_RX_accelerometer3_c1:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
251 movff lo,accel_DY+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
252 movff hi,accel_DY+1 ; Copy result
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
253
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
254 rcall I2C_OneByteRX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
255 movff SSP1BUF,lo ;accel_DZ+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
256 bsf SSP1CON2, RCEN ; Enable recieve mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
257 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
258 ; According to datasheet there should be no Master Acknowlegde for the last Byte (accel_DZ+1)...
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
259 movff SSP1BUF,hi ;accel_DZ+1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
260 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
261 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
262 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
263 comf hi ; 16bit sign change for Z
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
264 negf lo
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
265 btfsc STATUS,C ; Carry to propagate ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
266 incf hi,F ; YES: do it.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
267 movff lo,accel_DZ+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
268 movff hi,accel_DZ+1 ; Copy result
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
269 return
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
270
0
heinrichsweikamp
parents:
diff changeset
271 I2C_OneByteRX:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
272 bsf SSP1CON2, RCEN ; Enable recieve mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
273 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
274 bsf SSP1CON2,ACKEN ; Master acknowlegde
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
275 bra WaitMSSP ; And return!
0
heinrichsweikamp
parents:
diff changeset
276
heinrichsweikamp
parents:
diff changeset
277 global I2C_RX_compass
heinrichsweikamp
parents:
diff changeset
278 I2C_RX_compass:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
279 btfsc compass_type ; compass1?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
280 bra I2C_RX_compass1 ; yes
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
281 ;I2C_RX_compass0:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
282 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
283 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
284 movlw 0x3C ; address
0
heinrichsweikamp
parents:
diff changeset
285 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
286 movlw 0x03
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
287 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
288 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
289 rcall WaitMSSP
0
heinrichsweikamp
parents:
diff changeset
290
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
291 bcf PIR1,SSPIF
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
292 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
293 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
294 movlw 0x3D ; address
0
heinrichsweikamp
parents:
diff changeset
295 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
296
heinrichsweikamp
parents:
diff changeset
297 ; Compass IC sends data in following order:
heinrichsweikamp
parents:
diff changeset
298 ; x MSB
heinrichsweikamp
parents:
diff changeset
299 ; x LSB
heinrichsweikamp
parents:
diff changeset
300 ; z MSB
heinrichsweikamp
parents:
diff changeset
301 ; z LSB
heinrichsweikamp
parents:
diff changeset
302 ; y MSB
heinrichsweikamp
parents:
diff changeset
303 ; y LSB
heinrichsweikamp
parents:
diff changeset
304
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
305 ; Non-flipped screen
0
heinrichsweikamp
parents:
diff changeset
306 ; Chip orientation on the PCB requires
heinrichsweikamp
parents:
diff changeset
307 ; Original = Corrected
heinrichsweikamp
parents:
diff changeset
308 ; x = -y
heinrichsweikamp
parents:
diff changeset
309 ; z = z
heinrichsweikamp
parents:
diff changeset
310 ; y = x
heinrichsweikamp
parents:
diff changeset
311
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
312 ; Flipped screen
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
313 ; Chip orientation on the PCB requires
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
314 ; Original = Corrected
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
315 ; x = y
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
316 ; z = z
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
317 ; y = -x
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
318
0
heinrichsweikamp
parents:
diff changeset
319 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
320 movff SSP1BUF,compass_DY+1; Data Byte
heinrichsweikamp
parents:
diff changeset
321 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
322 movff SSP1BUF,compass_DY+0; Data Byte
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
323 btfsc flip_screen ; 180° rotation ?
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
324 bra I2C_RX_compass2 ; Yes
0
heinrichsweikamp
parents:
diff changeset
325 banksel compass_DY
heinrichsweikamp
parents:
diff changeset
326 comf compass_DY+1 ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
327 negf compass_DY+0
heinrichsweikamp
parents:
diff changeset
328 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
329 incf compass_DY+1,F ; YES: do it.
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
330 I2C_RX_compass2:
0
heinrichsweikamp
parents:
diff changeset
331 banksel common
heinrichsweikamp
parents:
diff changeset
332 rcall I2C_OneByteRX ; Get one byte
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
333 movff SSP1BUF,compass_DZ+1; Data Byte
0
heinrichsweikamp
parents:
diff changeset
334 rcall I2C_OneByteRX ; Get one byte
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
335 movff SSP1BUF,compass_DZ+0; Data Byte
0
heinrichsweikamp
parents:
diff changeset
336 rcall I2C_OneByteRX ; Get one byte
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
337 movff SSP1BUF,compass_DX+1; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
338 bsf SSP1CON2, RCEN ; Enable recieve mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
339 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
340 movff SSP1BUF,compass_DX+0; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
341 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
342 rcall WaitMSSP
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
343 btfss flip_screen ; 180° rotation ?
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
344 return ; No, done.
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
345 ; Yes, flip X
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
346 banksel compass_DX
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
347 comf compass_DX+1 ; 16bit sign change.
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
348 negf compass_DX+0
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
349 btfsc STATUS,C ; Carry to propagate ?
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
350 incf compass_DX+1,F ; YES: do it.
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
351 banksel common
0
heinrichsweikamp
parents:
diff changeset
352 return
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
353
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
354 I2C_RX_compass1: ; New compass
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
355 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
356 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
357 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
358 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
359 movlw b'10001000' ; 0x08 with auto-increment (MSB=1)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
360 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
361 bsf SSP1CON2,RSEN ; Repeated start condition (!)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
362 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
363 movlw 0x3D ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
364 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
365
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
366 rcall I2C_OneByteRX ; Get one byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
367 movff SSP1BUF,lo ; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
368 rcall I2C_OneByteRX ; Get one byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
369 movff SSP1BUF,hi ; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
370 rcall I2C_TwoBytesRX_div8_2
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
371 movff lo,compass_DX+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
372 movff hi,compass_DX+1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
373 btfss flip_screen ; 180° rotation ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
374 bra I2C_RX_compass1_1 ; Yes
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
375 ; Yes, flip X
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
376 banksel compass_DX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
377 comf compass_DX+1 ; 16bit sign change.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
378 negf compass_DX+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
379 btfsc STATUS,C ; Carry to propagate ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
380 incf compass_DX+1,F ; YES: do it.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
381 banksel common
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
382 I2C_RX_compass1_1:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
383 rcall I2C_OneByteRX ; Get one byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
384 movff SSP1BUF,lo ; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
385 rcall I2C_OneByteRX ; Get one byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
386 movff SSP1BUF,hi ; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
387 rcall I2C_TwoBytesRX_div8_2
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
388 movff lo,compass_DY+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
389 movff hi,compass_DY+1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
390 btfss flip_screen ; 180° rotation ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
391 bra I2C_RX_compass1_2 ; Yes
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
392 ; Yes, flip Y
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
393 banksel compass_DY
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
394 comf compass_DY+1 ; 16bit sign change.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
395 negf compass_DY+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
396 btfsc STATUS,C ; Carry to propagate ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
397 incf compass_DY+1,F ; YES: do it.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
398 I2C_RX_compass1_2:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
399 banksel common
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
400 rcall I2C_OneByteRX ; Get one byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
401 movff SSP1BUF,lo ; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
402 bsf SSP1CON2, RCEN ; Enable recieve mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
403 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
404 movff SSP1BUF,hi ; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
405 rcall I2C_TwoBytesRX_div8_2
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
406 movff lo,compass_DZ+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
407 movff hi,compass_DZ+1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
408 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
409 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
410 return
0
heinrichsweikamp
parents:
diff changeset
411
heinrichsweikamp
parents:
diff changeset
412 global I2C_init_compass
heinrichsweikamp
parents:
diff changeset
413 I2C_init_compass:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
414 bsf compass_enabled
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
415 bsf compass_type ; set flag
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
416 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
417 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
418 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
419 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
420 movlw 0x0F
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
421 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
422 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
423 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
424 bcf PIR1,SSPIF
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
425 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
426 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
427 movlw 0x3D ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
428 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
429 rcall I2C_OneByteRX ; Get one byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
430 movlw 0x49 ; 0x49 = Compass1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
431 cpfseq SSP1BUF
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
432 bcf compass_type ; clear flag
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
433 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
434 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
435
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
436 btfsc compass_type ; compass1?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
437 bra I2C_init_compass1 ; yes
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
438 ; init compass0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
439 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
440 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
441 movlw 0x3C ; address
0
heinrichsweikamp
parents:
diff changeset
442 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
443 movlw 0x00
0
heinrichsweikamp
parents:
diff changeset
444 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
445 ; movlw b'01101001' ; ConfigA: 3Hz, 8 Samples averaged, Test Mode (Positive Bias)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
446 movlw b'01101000' ; ConfigA: 3Hz, 8 Samples averaged
0
heinrichsweikamp
parents:
diff changeset
447 rcall I2C_TX
20
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
448 bra I2C_init_compass_common
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
449
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
450 global I2C_init_compass_fast
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
451 I2C_init_compass_fast:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
452 btfsc compass_type ; compass1?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
453 bra I2C_init_compass_fast1 ; yes
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
454 ;I2C_init_compass_fast0:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
455 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
456 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
457 movlw 0x3C ; address
20
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
458 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
459 movlw 0x00
20
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
460 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
461 movlw b'00111000' ; ConfigA: 75Hz, 2 Samples averaged
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
462 ; movlw b'00111001' ; ConfigA: 75Hz, 2 Samples averaged, Test Mode (Positive Bias)
20
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
463 rcall I2C_TX
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
464 I2C_init_compass_common:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
465 movff opt_compass_gain,i2c_temp ; 0-7 (230LSB/Gauss to 1370LSB/Gauss)
18
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
466 swapf i2c_temp,F
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
467 comf i2c_temp,F
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
468 bcf STATUS,C
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
469 rlcf i2c_temp
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
470 movf i2c_temp,W
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
471 clrf i2c_temp
0
heinrichsweikamp
parents:
diff changeset
472 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
473 movlw b'00000000' ; Continous Mode
0
heinrichsweikamp
parents:
diff changeset
474 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
475 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
476 rcall WaitMSSP
0
heinrichsweikamp
parents:
diff changeset
477 return
heinrichsweikamp
parents:
diff changeset
478
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
479 I2C_init_compass1:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
480 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
481 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
482 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
483 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
484 movlw 0x9F ; 1F with auto-increment (MSB=1)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
485 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
486 movlw b'00000000' ; CTRL0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
487 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
488 movlw b'00101111' ; CTRL1 (6,25Hz, BDU=0, x,y,z = ON)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
489 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
490 movlw b'11000000' ; CTRL2 (50Hz, +/-2g,
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
491 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
492 movlw b'00000000' ; CTRL3
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
493 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
494 movlw b'00000000' ; CTRL4
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
495 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
496 movlw b'01100100' ; CTRL5 HIGH res, 6,25Hz
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
497 rcall I2C_TX
429
4b93354b7738 hardware_flag handling
heinrichsweikamp
parents: 427
diff changeset
498 init_compass1_common:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
499 ;movlw b'01100000' ; CTRL6 Full scale (+/-12 Gauss -> 2730LSB/Gauss)
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
500 ;movlw b'00000000' ; CTRL6 (+/-2 Gauss)
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
501 movlw b'00100000' ; CTRL6 (+/-4 Gauss)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
502 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
503 movlw b'00000000' ; CTRL7 Continuous Mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
504 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
505 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
506 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
507 return
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
508
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
509 I2C_init_compass_fast1:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
510 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
511 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
512 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
513 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
514 movlw 0x9F ; 1F with auto-increment (MSB=1)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
515 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
516 movlw b'00000000' ; CTRL0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
517 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
518 movlw b'01101111' ; CTRL1 (100Hz, BDU=0, x,y,z = ON)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
519 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
520 movlw b'11000000' ; CTRL2 (50Hz, +/-2g,
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
521 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
522 movlw b'00000000' ; CTRL3
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
523 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
524 movlw b'00000000' ; CTRL4
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
525 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
526 movlw b'01110100' ; CTRL5 HIGH res, 100Hz
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
527 rcall I2C_TX
429
4b93354b7738 hardware_flag handling
heinrichsweikamp
parents: 427
diff changeset
528 bra init_compass1_common
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
529
0
heinrichsweikamp
parents:
diff changeset
530 global I2C_sleep_compass
heinrichsweikamp
parents:
diff changeset
531 I2C_sleep_compass:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
532 bcf compass_enabled
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
533
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
534 btfsc compass_type ; compass1?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
535 bra I2C_sleep_compass1 ; yes
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
536 ;I2C_sleep_compass0:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
537 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
538 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
539 movlw 0x3C ; address
0
heinrichsweikamp
parents:
diff changeset
540 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
541 movlw 0x00
0
heinrichsweikamp
parents:
diff changeset
542 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
543 movlw b'01101000' ; ConfigA
0
heinrichsweikamp
parents:
diff changeset
544 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
545 movlw b'00100000' ; ConfigB
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
546 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
547 movlw b'00000010' ; Idle Mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
548 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
549 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
550 rcall WaitMSSP
0
heinrichsweikamp
parents:
diff changeset
551 return
heinrichsweikamp
parents:
diff changeset
552
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
553 I2C_sleep_compass1:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
554 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
555 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
556 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
557 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
558 movlw 0x20 ; CTRL_REG1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
559 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
560 movlw b'00000000' ; data for CTRL_REG1: acceleration sensor Power-down mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
561 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
562 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
563 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
564 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
565 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
566 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
567 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
568 movlw 0x26 ; CTRL_REG7
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
569 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
570 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor Power-down mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
571 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
572 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
573 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
574 return
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
575
0
heinrichsweikamp
parents:
diff changeset
576
heinrichsweikamp
parents:
diff changeset
577 global I2C_init_accelerometer
heinrichsweikamp
parents:
diff changeset
578 I2C_init_accelerometer:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
579 btfsc compass_type ; compass1?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
580 return ; yes, ignore
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
581
0
heinrichsweikamp
parents:
diff changeset
582 rcall I2C_sleep_accelerometer ; Regs can only be changed in St.By mode
heinrichsweikamp
parents:
diff changeset
583
heinrichsweikamp
parents:
diff changeset
584 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
585 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
586 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
587 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
588 movlw 0x0E ; XYZ_DATA_CFG
heinrichsweikamp
parents:
diff changeset
589 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
590 movlw b'00000000' ; High pass Filter=0 , +/- 2g range
heinrichsweikamp
parents:
diff changeset
591 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
592 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
593 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
594
heinrichsweikamp
parents:
diff changeset
595
heinrichsweikamp
parents:
diff changeset
596 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
597 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
598 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
599 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
600 movlw 0x2A ; CTRL_REG1
heinrichsweikamp
parents:
diff changeset
601 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
602 ; movlw b'00110000' ; CTRL_REG1: 160ms data rate, St.By Mode
heinrichsweikamp
parents:
diff changeset
603 movlw b'00110100' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode
heinrichsweikamp
parents:
diff changeset
604 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
605 movlw b'00000010' ; CTRL_REG2: High Res in Active mode
heinrichsweikamp
parents:
diff changeset
606 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
607 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
608 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
609
heinrichsweikamp
parents:
diff changeset
610 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
611 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
612 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
613 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
614 movlw 0x2A ; CTRL_REG1
heinrichsweikamp
parents:
diff changeset
615 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
616 ; movlw b'00110001' ; CTRL_REG1: 160ms data rate, Active Mode
heinrichsweikamp
parents:
diff changeset
617 movlw b'00110101' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode, Active Mode
heinrichsweikamp
parents:
diff changeset
618 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
619 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
620 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
621
heinrichsweikamp
parents:
diff changeset
622 return
heinrichsweikamp
parents:
diff changeset
623
heinrichsweikamp
parents:
diff changeset
624 global I2C_sleep_accelerometer
heinrichsweikamp
parents:
diff changeset
625 I2C_sleep_accelerometer:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
626 btfsc compass_type ; compass1?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
627 return ; yes, ignore
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
628
0
heinrichsweikamp
parents:
diff changeset
629 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
630 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
631 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
632 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
633 movlw 0x2A ; CTRL_REG1
heinrichsweikamp
parents:
diff changeset
634 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
635 movlw b'00000000' ; St. By Mode
heinrichsweikamp
parents:
diff changeset
636 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
637 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
638 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
639 return
heinrichsweikamp
parents:
diff changeset
640
113
heinrichsweikamp
parents: 20
diff changeset
641 global lt2942_init
heinrichsweikamp
parents: 20
diff changeset
642 lt2942_init: ; Setup Control register B
heinrichsweikamp
parents: 20
diff changeset
643 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
644 movlw 0x01 ; Point to control reg B
heinrichsweikamp
parents: 20
diff changeset
645 call I2C_TX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
646 movlw b'11111000' ; Automatic conversion every two seconds
heinrichsweikamp
parents: 20
diff changeset
647 movff WREG, SSP1BUF ; Data Byte
heinrichsweikamp
parents: 20
diff changeset
648 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
649 rcall I2C_WaitforACK
heinrichsweikamp
parents: 20
diff changeset
650 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 20
diff changeset
651 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
652 return
heinrichsweikamp
parents: 20
diff changeset
653
heinrichsweikamp
parents: 20
diff changeset
654 global lt2942_get_status
heinrichsweikamp
parents: 20
diff changeset
655 lt2942_get_status: ; Read status register
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
656 bcf battery_gauge_available ; Clear flag
113
heinrichsweikamp
parents: 20
diff changeset
657 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
658 movlw 0x00 ; Point to Status reg
heinrichsweikamp
parents: 20
diff changeset
659 call I2C_TX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
660 call I2C_RX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
661 movff SSP1BUF,WREG
heinrichsweikamp
parents: 20
diff changeset
662 btfss WREG,7 ; 2942 found?
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
663 bsf battery_gauge_available ; Yes, set flag
113
heinrichsweikamp
parents: 20
diff changeset
664 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 20
diff changeset
665 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
666 return
heinrichsweikamp
parents: 20
diff changeset
667
heinrichsweikamp
parents: 20
diff changeset
668
heinrichsweikamp
parents: 20
diff changeset
669 global lt2942_get_voltage
heinrichsweikamp
parents: 20
diff changeset
670 lt2942_get_voltage: ; Read battery voltage registers
heinrichsweikamp
parents: 20
diff changeset
671 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
672 movlw 0x08 ; Point to voltage registers
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
673 rcall I2C_TX_GAUGE
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
674 rcall I2C_RX_GAUGE
113
heinrichsweikamp
parents: 20
diff changeset
675 bsf SSP1CON2,ACKEN ; Master acknowlegde
heinrichsweikamp
parents: 20
diff changeset
676 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
677 movff SSP1BUF,xA+1
heinrichsweikamp
parents: 20
diff changeset
678 bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents: 20
diff changeset
679 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
680 movff SSP1BUF,xA+0
heinrichsweikamp
parents: 20
diff changeset
681 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 20
diff changeset
682 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
683
heinrichsweikamp
parents: 20
diff changeset
684 ; banksel common
heinrichsweikamp
parents: 20
diff changeset
685 ; xA:2 loaded with raw values
heinrichsweikamp
parents: 20
diff changeset
686 movlw LOW .6000
heinrichsweikamp
parents: 20
diff changeset
687 movwf xB+0
heinrichsweikamp
parents: 20
diff changeset
688 movlw HIGH .6000
heinrichsweikamp
parents: 20
diff changeset
689 movwf xB+1
heinrichsweikamp
parents: 20
diff changeset
690 call mult16x16 ;xA*xB=xC
heinrichsweikamp
parents: 20
diff changeset
691
heinrichsweikamp
parents: 20
diff changeset
692 ; devide xC (32bit)/65535 for result in mV (16bit)
heinrichsweikamp
parents: 20
diff changeset
693 movlw .16
heinrichsweikamp
parents: 20
diff changeset
694 movwf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
695 lt2942_get_voltage2:
heinrichsweikamp
parents: 20
diff changeset
696 bcf STATUS,C
heinrichsweikamp
parents: 20
diff changeset
697 rrcf xC+3,F
heinrichsweikamp
parents: 20
diff changeset
698 rrcf xC+2,F
heinrichsweikamp
parents: 20
diff changeset
699 rrcf xC+1,F
heinrichsweikamp
parents: 20
diff changeset
700 rrcf xC+0,F
heinrichsweikamp
parents: 20
diff changeset
701 decfsz i2c_temp,F
heinrichsweikamp
parents: 20
diff changeset
702 bra lt2942_get_voltage2
heinrichsweikamp
parents: 20
diff changeset
703
heinrichsweikamp
parents: 20
diff changeset
704 ; Update battery voltage in mV
heinrichsweikamp
parents: 20
diff changeset
705 movff xC+1,batt_voltage+1
heinrichsweikamp
parents: 20
diff changeset
706 movff xC+0,batt_voltage+0
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
707
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
708 tstfsz batt_voltage+1 ; <256mV?
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
709 return ; No, done.
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
710
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
711 rcall lt2942_init
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
712 return
113
heinrichsweikamp
parents: 20
diff changeset
713
heinrichsweikamp
parents: 20
diff changeset
714 ; global lt2942_get_temperature
heinrichsweikamp
parents: 20
diff changeset
715 ;lt2942_get_temperature: ; Read temperature registers
heinrichsweikamp
parents: 20
diff changeset
716 ; clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
717 ; movlw 0x0C ; Point to temperature registers
heinrichsweikamp
parents: 20
diff changeset
718 ; call I2C_TX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
719 ; call I2C_RX
heinrichsweikamp
parents: 20
diff changeset
720 ; bsf SSP1CON2,ACKEN ; Master acknowlegde
heinrichsweikamp
parents: 20
diff changeset
721 ; rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
722 ; movff SSP1BUF,xA+1
heinrichsweikamp
parents: 20
diff changeset
723 ; bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents: 20
diff changeset
724 ; rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
725 ; movff SSP1BUF,xA+0
heinrichsweikamp
parents: 20
diff changeset
726 ; bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 20
diff changeset
727 ; rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
728 ;
heinrichsweikamp
parents: 20
diff changeset
729 ;; banksel common
heinrichsweikamp
parents: 20
diff changeset
730 ; ; xA:2 loaded with raw values
heinrichsweikamp
parents: 20
diff changeset
731 ; movlw LOW .6000
heinrichsweikamp
parents: 20
diff changeset
732 ; movwf xB+0
heinrichsweikamp
parents: 20
diff changeset
733 ; movlw HIGH .6000
heinrichsweikamp
parents: 20
diff changeset
734 ; movwf xB+1
heinrichsweikamp
parents: 20
diff changeset
735 ; call mult16x16 ;xA*xB=xC
heinrichsweikamp
parents: 20
diff changeset
736 ;
heinrichsweikamp
parents: 20
diff changeset
737 ; ; devide xC (32bit)/65535 for result in 0.1K (16bit)
heinrichsweikamp
parents: 20
diff changeset
738 ; movlw .16
heinrichsweikamp
parents: 20
diff changeset
739 ; movwf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
740 ;lt2942_get_temperature2:
heinrichsweikamp
parents: 20
diff changeset
741 ; bcf STATUS,C
heinrichsweikamp
parents: 20
diff changeset
742 ; rrcf xC+3,F
heinrichsweikamp
parents: 20
diff changeset
743 ; rrcf xC+2,F
heinrichsweikamp
parents: 20
diff changeset
744 ; rrcf xC+1,F
heinrichsweikamp
parents: 20
diff changeset
745 ; rrcf xC+0,F
heinrichsweikamp
parents: 20
diff changeset
746 ; decfsz i2c_temp,F
heinrichsweikamp
parents: 20
diff changeset
747 ; bra lt2942_get_temperature2
heinrichsweikamp
parents: 20
diff changeset
748 ;
heinrichsweikamp
parents: 20
diff changeset
749 ; movff xC+1,sub_a+1
heinrichsweikamp
parents: 20
diff changeset
750 ; movff xC+0,sub_a+0
heinrichsweikamp
parents: 20
diff changeset
751 ; movlw LOW .2731 ; Kelvin to Celcius offset
heinrichsweikamp
parents: 20
diff changeset
752 ; movwf sub_b+0
heinrichsweikamp
parents: 20
diff changeset
753 ; movlw HIGH .2731 ; Kelvin to Celcius offset
heinrichsweikamp
parents: 20
diff changeset
754 ; movwf sub_b+1
heinrichsweikamp
parents: 20
diff changeset
755 ; call subU16 ; sub_c = sub_a - sub_b (with UNSIGNED values)
heinrichsweikamp
parents: 20
diff changeset
756 ;
heinrichsweikamp
parents: 20
diff changeset
757 ; ; Update batttery_temperature in 0.1°C
heinrichsweikamp
parents: 20
diff changeset
758 ; movff sub_c+1,battery_temperature+1
heinrichsweikamp
parents: 20
diff changeset
759 ; movff sub_c+0,battery_temperature+0
heinrichsweikamp
parents: 20
diff changeset
760 ; return
heinrichsweikamp
parents: 20
diff changeset
761
heinrichsweikamp
parents: 20
diff changeset
762 global lt2942_get_accumulated_charge
heinrichsweikamp
parents: 20
diff changeset
763 lt2942_get_accumulated_charge: ; Read accumulated charge and compute percent
heinrichsweikamp
parents: 20
diff changeset
764 clrf i2c_temp
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
765 movlw 0x00 ; Point to status register
113
heinrichsweikamp
parents: 20
diff changeset
766 call I2C_TX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
767 call I2C_RX_GAUGE
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
768 bsf SSP1CON2,ACKEN ; Master acknowlegde
113
heinrichsweikamp
parents: 20
diff changeset
769 rcall WaitMSSP
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
770 movff SSP1BUF,gauge_status_byte
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
771
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
772 bsf SSP1CON2, RCEN ; Enable recieve mode
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
773 rcall WaitMSSP ; Dummy read (Control byte)
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
774
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
775 bsf SSP1CON2, RCEN ; Enable recieve mode
113
heinrichsweikamp
parents: 20
diff changeset
776 rcall WaitMSSP
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
777 movff SSP1BUF,sub_a+1
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
778
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
779 bsf SSP1CON2, RCEN ; Enable recieve mode
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
780 rcall WaitMSSP
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
781 movff SSP1BUF,sub_a+0
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
782 bsf SSP1CON2,PEN ; Stop condition
113
heinrichsweikamp
parents: 20
diff changeset
783 rcall WaitMSSP
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
784
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
785 movff gauge_status_byte,sub_b+0 ; copy into bank common
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
786 btfsc sub_b+0,0 ; =1: UVLO Event
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
787 rcall lt2942_init
113
heinrichsweikamp
parents: 20
diff changeset
788
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
789 movff sub_a+1,battery_acumulated_charge+1 ; Save raw value
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
790 movff sub_a+0,battery_acumulated_charge+0 ; Save raw value
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
791
113
heinrichsweikamp
parents: 20
diff changeset
792 ; Compute batt_percent
211
heinrichsweikamp
parents: 200
diff changeset
793 ; (charge-battery_offset)/365
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
794 movff battery_offset+0,sub_b+0
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
795 movff battery_offset+1,sub_b+1
113
heinrichsweikamp
parents: 20
diff changeset
796 call subU16 ; sub_c = sub_a - sub_b (with signed values)
heinrichsweikamp
parents: 20
diff changeset
797
heinrichsweikamp
parents: 20
diff changeset
798 clrf batt_percent ; Set to zero
heinrichsweikamp
parents: 20
diff changeset
799 btfsc neg_flag ; result negative?
449
heinrichsweikamp
parents: 448
diff changeset
800 bra lt2942_set_to_zero_percent ; Yes, keep LT2942 at zero percent and return
113
heinrichsweikamp
parents: 20
diff changeset
801
heinrichsweikamp
parents: 20
diff changeset
802 ; > Zero, set batt_percent properly
heinrichsweikamp
parents: 20
diff changeset
803 movff sub_c+0,xA+0
heinrichsweikamp
parents: 20
diff changeset
804 movff sub_c+1,xA+1
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
805 movff battery_capacity+0,xB+0
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
806 movff battery_capacity+1,xB+1
113
heinrichsweikamp
parents: 20
diff changeset
807 call div16x16 ;xA/xB=xC with xA+0 as remainder, uses divB as temp variable
heinrichsweikamp
parents: 20
diff changeset
808 movff xC+0,batt_percent
heinrichsweikamp
parents: 20
diff changeset
809 return
heinrichsweikamp
parents: 20
diff changeset
810
449
heinrichsweikamp
parents: 448
diff changeset
811 lt2942_set_to_zero_percent:
heinrichsweikamp
parents: 448
diff changeset
812 clrf i2c_temp
heinrichsweikamp
parents: 448
diff changeset
813 movlw 0x02 ; Point to accumulated charge registers
heinrichsweikamp
parents: 448
diff changeset
814 rcall I2C_TX_GAUGE
heinrichsweikamp
parents: 448
diff changeset
815 movff battery_offset+1,SSP1BUF
heinrichsweikamp
parents: 448
diff changeset
816 rcall WaitMSSP
heinrichsweikamp
parents: 448
diff changeset
817 rcall I2C_WaitforACK
heinrichsweikamp
parents: 448
diff changeset
818 movff battery_offset+0,SSP1BUF
heinrichsweikamp
parents: 448
diff changeset
819 rcall WaitMSSP
heinrichsweikamp
parents: 448
diff changeset
820 rcall I2C_WaitforACK
heinrichsweikamp
parents: 448
diff changeset
821 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 448
diff changeset
822 bra WaitMSSP; (and return)
heinrichsweikamp
parents: 448
diff changeset
823
113
heinrichsweikamp
parents: 20
diff changeset
824 global lt2942_charge_done
heinrichsweikamp
parents: 20
diff changeset
825 lt2942_charge_done: ; Reset accumulating registers to 0xFFFF
heinrichsweikamp
parents: 20
diff changeset
826 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
827 movlw 0x02 ; Point to accumulated charge registers
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
828 rcall I2C_TX_GAUGE
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
829 setf SSP1BUF ; Data Byte
113
heinrichsweikamp
parents: 20
diff changeset
830 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
831 rcall I2C_WaitforACK
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
832 setf SSP1BUF ; Data Byte
113
heinrichsweikamp
parents: 20
diff changeset
833 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
834 rcall I2C_WaitforACK
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
835 bsf SSP1CON2,PEN ; Stop condition
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
836 bra WaitMSSP; (and return)
113
heinrichsweikamp
parents: 20
diff changeset
837
heinrichsweikamp
parents: 20
diff changeset
838 I2C_TX_GAUGE: ; Sends a byte to the LT2942 Gauge IC
heinrichsweikamp
parents: 20
diff changeset
839 movwf i2c_temp+1 ; Data byte
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
840 bsf SSP1CON2,SEN ; Start condition
113
heinrichsweikamp
parents: 20
diff changeset
841 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
842 movlw b'11001000' ; Address byte + Write bit
heinrichsweikamp
parents: 20
diff changeset
843 movwf SSP1BUF ; control byte
heinrichsweikamp
parents: 20
diff changeset
844 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
845 rcall I2C_WaitforACK
heinrichsweikamp
parents: 20
diff changeset
846 movff i2c_temp+1, SSP1BUF ; Data Byte
heinrichsweikamp
parents: 20
diff changeset
847 rcall WaitMSSP
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
848 bra I2C_WaitforACK ; (and return)
113
heinrichsweikamp
parents: 20
diff changeset
849
heinrichsweikamp
parents: 20
diff changeset
850 I2C_RX_GAUGE:
heinrichsweikamp
parents: 20
diff changeset
851 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents: 20
diff changeset
852 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
853 movlw b'11001001' ; Address byte + Read bit
heinrichsweikamp
parents: 20
diff changeset
854 movwf SSP1BUF ; control byte
heinrichsweikamp
parents: 20
diff changeset
855 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
856 rcall I2C_WaitforACK
heinrichsweikamp
parents: 20
diff changeset
857 bsf SSP1CON2, RCEN ; Enable recieve mode
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
858 bra WaitMSSP; (and return)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
859
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
860 END