annotate src/i2c.asm @ 339:692903c50788

minor: fix calc_deko_divemode_sensor
author heinrichsweikamp
date Fri, 03 Jul 2015 15:08:15 +0200
parents 653a3ab08062
children ceb1b7329dce
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
275
653a3ab08062 rename into hwOS
heinrichsweikamp
parents: 235
diff changeset
19 #include "hwos.inc" ; Mandatory header
0
heinrichsweikamp
parents:
diff changeset
20 #include "wait.inc"
113
heinrichsweikamp
parents: 20
diff changeset
21 #include "math.inc"
heinrichsweikamp
parents: 20
diff changeset
22
200
dcd513840c6c cleanup
heinrichsweikamp
parents: 166
diff changeset
23 #DEFINE battery_offset .29065 ; 65536-(3,1Ah/0,085mAh)
dcd513840c6c cleanup
heinrichsweikamp
parents: 166
diff changeset
24 #DEFINE battery_devider .365 ; 3,1Ah/0,085mAh/100 [%]
0
heinrichsweikamp
parents:
diff changeset
25
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
heinrichsweikamp
parents:
diff changeset
103 rrcf lo
heinrichsweikamp
parents:
diff changeset
104 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
105 btfsc hi,7 ; Copy sign bit to carry
heinrichsweikamp
parents:
diff changeset
106 bsf STATUS,C
heinrichsweikamp
parents:
diff changeset
107 rrcf hi ; /4
heinrichsweikamp
parents:
diff changeset
108 rrcf lo
heinrichsweikamp
parents:
diff changeset
109 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
110 btfsc hi,7 ; Copy sign bit to carry
heinrichsweikamp
parents:
diff changeset
111 bsf STATUS,C
heinrichsweikamp
parents:
diff changeset
112 rrcf hi ; /8
heinrichsweikamp
parents:
diff changeset
113 rrcf lo
heinrichsweikamp
parents:
diff changeset
114 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
115 btfsc hi,7 ; Copy sign bit to carry
heinrichsweikamp
parents:
diff changeset
116 bsf STATUS,C
heinrichsweikamp
parents:
diff changeset
117 rrcf hi ; /16
heinrichsweikamp
parents:
diff changeset
118 rrcf lo
heinrichsweikamp
parents:
diff changeset
119 return
heinrichsweikamp
parents:
diff changeset
120
heinrichsweikamp
parents:
diff changeset
121 global I2C_RX_accelerometer
heinrichsweikamp
parents:
diff changeset
122 I2C_RX_accelerometer:
heinrichsweikamp
parents:
diff changeset
123 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
124 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
125 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
126 rcall I2C_TX
158
683321c09cfa nicer boot into surfacemode
heinrichsweikamp
parents: 113
diff changeset
127 movlw 0x00
0
heinrichsweikamp
parents:
diff changeset
128 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
129 bsf SSP1CON2,RSEN ; Repeated start condition (!)
heinrichsweikamp
parents:
diff changeset
130 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
131 movlw 0x39 ; address
heinrichsweikamp
parents:
diff changeset
132 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
133
158
683321c09cfa nicer boot into surfacemode
heinrichsweikamp
parents: 113
diff changeset
134 rcall I2C_OneByteRX ; Get Status Byte
683321c09cfa nicer boot into surfacemode
heinrichsweikamp
parents: 113
diff changeset
135 movf SSP1BUF,W
683321c09cfa nicer boot into surfacemode
heinrichsweikamp
parents: 113
diff changeset
136
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
137 ; Non-flipped screen:
0
heinrichsweikamp
parents:
diff changeset
138 ; Chip orientation on the PCB requires
heinrichsweikamp
parents:
diff changeset
139 ; Original = Corrected
heinrichsweikamp
parents:
diff changeset
140 ; x = -x
heinrichsweikamp
parents:
diff changeset
141 ; y = -y
heinrichsweikamp
parents:
diff changeset
142 ; z = -z
heinrichsweikamp
parents:
diff changeset
143
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
144 ; Flipped screen:
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
145 ; Chip orientation on the PCB requires
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
146 ; Original = Corrected
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
147 ; x = x
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
148 ; y = y
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
149 ; z = -z
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
150
0
heinrichsweikamp
parents:
diff changeset
151 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
152 btfsc flip_screen ; 180° rotation ?
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
153 bra I2C_RX_accelerometer2 ; Yes
0
heinrichsweikamp
parents:
diff changeset
154 comf hi ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
155 negf lo
heinrichsweikamp
parents:
diff changeset
156 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
157 incf hi,F ; YES: do it.
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
158 I2C_RX_accelerometer2:
0
heinrichsweikamp
parents:
diff changeset
159 movff lo,accel_DX+0
heinrichsweikamp
parents:
diff changeset
160 movff hi,accel_DX+1 ; Copy result
heinrichsweikamp
parents:
diff changeset
161
heinrichsweikamp
parents:
diff changeset
162 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
163 btfsc flip_screen ; 180° rotation ?
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
164 bra I2C_RX_accelerometer3 ; Yes
0
heinrichsweikamp
parents:
diff changeset
165 comf hi ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
166 negf lo
heinrichsweikamp
parents:
diff changeset
167 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
168 incf hi,F ; YES: do it.
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
169 I2C_RX_accelerometer3:
0
heinrichsweikamp
parents:
diff changeset
170 movff lo,accel_DY+0
heinrichsweikamp
parents:
diff changeset
171 movff hi,accel_DY+1 ; Copy result
heinrichsweikamp
parents:
diff changeset
172
heinrichsweikamp
parents:
diff changeset
173 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
174 movff SSP1BUF,hi ; Data Byte
heinrichsweikamp
parents:
diff changeset
175 bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents:
diff changeset
176 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
177 ; According to datasheet there should be no Master Acknowlegde for the last Byte (accel_DZ+0)...
heinrichsweikamp
parents:
diff changeset
178 movff SSP1BUF,lo ; Data Byte
heinrichsweikamp
parents:
diff changeset
179
heinrichsweikamp
parents:
diff changeset
180 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only
heinrichsweikamp
parents:
diff changeset
181 comf hi ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
182 negf lo
heinrichsweikamp
parents:
diff changeset
183 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
184 incf hi,F ; YES: do it.
heinrichsweikamp
parents:
diff changeset
185 movff lo,accel_DZ+0
heinrichsweikamp
parents:
diff changeset
186 movff hi,accel_DZ+1 ; Copy result
heinrichsweikamp
parents:
diff changeset
187
heinrichsweikamp
parents:
diff changeset
188 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
189 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
190 return
heinrichsweikamp
parents:
diff changeset
191
heinrichsweikamp
parents:
diff changeset
192 I2C_OneByteRX:
heinrichsweikamp
parents:
diff changeset
193 bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents:
diff changeset
194 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
195 bsf SSP1CON2,ACKEN ; Master acknowlegde
heinrichsweikamp
parents:
diff changeset
196 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
197 return
heinrichsweikamp
parents:
diff changeset
198
heinrichsweikamp
parents:
diff changeset
199 global I2C_RX_compass
heinrichsweikamp
parents:
diff changeset
200 I2C_RX_compass:
heinrichsweikamp
parents:
diff changeset
201 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
202 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
203 movlw 0x3C ; address
heinrichsweikamp
parents:
diff changeset
204 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
205 movlw 0x03
heinrichsweikamp
parents:
diff changeset
206 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
207 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
208 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
209
heinrichsweikamp
parents:
diff changeset
210 bcf PIR1,SSPIF
heinrichsweikamp
parents:
diff changeset
211 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
212 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
213 movlw 0x3D ; address
heinrichsweikamp
parents:
diff changeset
214 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
215
heinrichsweikamp
parents:
diff changeset
216 ; Compass IC sends data in following order:
heinrichsweikamp
parents:
diff changeset
217 ; x MSB
heinrichsweikamp
parents:
diff changeset
218 ; x LSB
heinrichsweikamp
parents:
diff changeset
219 ; z MSB
heinrichsweikamp
parents:
diff changeset
220 ; z LSB
heinrichsweikamp
parents:
diff changeset
221 ; y MSB
heinrichsweikamp
parents:
diff changeset
222 ; y LSB
heinrichsweikamp
parents:
diff changeset
223
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
224 ; Non-flipped screen
0
heinrichsweikamp
parents:
diff changeset
225 ; Chip orientation on the PCB requires
heinrichsweikamp
parents:
diff changeset
226 ; Original = Corrected
heinrichsweikamp
parents:
diff changeset
227 ; x = -y
heinrichsweikamp
parents:
diff changeset
228 ; z = z
heinrichsweikamp
parents:
diff changeset
229 ; y = x
heinrichsweikamp
parents:
diff changeset
230
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
231 ; Flipped screen
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
232 ; Chip orientation on the PCB requires
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
233 ; Original = Corrected
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
234 ; x = y
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
235 ; z = z
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
236 ; y = -x
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
237
0
heinrichsweikamp
parents:
diff changeset
238 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
239 movff SSP1BUF,compass_DY+1; Data Byte
heinrichsweikamp
parents:
diff changeset
240 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
241 movff SSP1BUF,compass_DY+0; Data Byte
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
242 btfsc flip_screen ; 180° rotation ?
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
243 bra I2C_RX_compass2 ; Yes
0
heinrichsweikamp
parents:
diff changeset
244 banksel compass_DY
heinrichsweikamp
parents:
diff changeset
245 comf compass_DY+1 ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
246 negf compass_DY+0
heinrichsweikamp
parents:
diff changeset
247 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
248 incf compass_DY+1,F ; YES: do it.
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
249 I2C_RX_compass2:
0
heinrichsweikamp
parents:
diff changeset
250 banksel common
heinrichsweikamp
parents:
diff changeset
251 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
252 movff SSP1BUF,compass_DZ+1; Data Byte
heinrichsweikamp
parents:
diff changeset
253 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
254 movff SSP1BUF,compass_DZ+0; Data Byte
heinrichsweikamp
parents:
diff changeset
255 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
256 movff SSP1BUF,compass_DX+1; Data Byte
heinrichsweikamp
parents:
diff changeset
257 bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents:
diff changeset
258 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
259 movff SSP1BUF,compass_DX+0; Data Byte
heinrichsweikamp
parents:
diff changeset
260 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
261 rcall WaitMSSP
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
262 btfss flip_screen ; 180° rotation ?
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
263 return ; No, done.
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
264 ; Yes, flip X
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
265 banksel compass_DX
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
266 comf compass_DX+1 ; 16bit sign change.
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
267 negf compass_DX+0
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
268 btfsc STATUS,C ; Carry to propagate ?
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
269 incf compass_DX+1,F ; YES: do it.
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
270 banksel common
0
heinrichsweikamp
parents:
diff changeset
271 return
heinrichsweikamp
parents:
diff changeset
272
heinrichsweikamp
parents:
diff changeset
273 global I2C_init_compass
heinrichsweikamp
parents:
diff changeset
274 I2C_init_compass:
heinrichsweikamp
parents:
diff changeset
275 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
276 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
277 movlw 0x3C ; address
heinrichsweikamp
parents:
diff changeset
278 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
279 movlw 0x00
heinrichsweikamp
parents:
diff changeset
280 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
281 ; movlw b'01101001' ; ConfigA: 3Hz, 8 Samples averaged, Test Mode (Positive Bias)
heinrichsweikamp
parents:
diff changeset
282 movlw b'01101000' ; ConfigA: 3Hz, 8 Samples averaged
heinrichsweikamp
parents:
diff changeset
283 rcall I2C_TX
20
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
284 bra I2C_init_compass_common
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
285
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
286 global I2C_init_compass_fast
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
287 I2C_init_compass_fast:
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
288 bsf SSP1CON2,SEN ; Start condition
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
289 rcall WaitMSSP
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
290 movlw 0x3C ; address
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
291 rcall I2C_TX
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
292 movlw 0x00
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
293 rcall I2C_TX
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
294 movlw b'00111000' ; ConfigA: 75Hz, 2 Samples averaged
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
295 ; movlw b'00111001' ; ConfigA: 75Hz, 2 Samples averaged, Test Mode (Positive Bias)
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
296 rcall I2C_TX
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
297 I2C_init_compass_common:
18
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
298 movff opt_compass_gain,i2c_temp ; 0-7 (230LSB/Gauss to 1370LSB/Gaus)
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
299 swapf i2c_temp,F
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
300 comf i2c_temp,F
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
301 bcf STATUS,C
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
302 rlcf i2c_temp
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
303 movf i2c_temp,W
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
304 clrf i2c_temp
0
heinrichsweikamp
parents:
diff changeset
305 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
306 movlw b'00000000' ; Continous Mode
heinrichsweikamp
parents:
diff changeset
307 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
308 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
309 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
310 bsf compass_enabled
heinrichsweikamp
parents:
diff changeset
311 return
heinrichsweikamp
parents:
diff changeset
312
heinrichsweikamp
parents:
diff changeset
313 global I2C_sleep_compass
heinrichsweikamp
parents:
diff changeset
314 I2C_sleep_compass:
heinrichsweikamp
parents:
diff changeset
315 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
316 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
317 movlw 0x3C ; address
heinrichsweikamp
parents:
diff changeset
318 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
319 movlw 0x00
heinrichsweikamp
parents:
diff changeset
320 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
321 movlw b'01101000' ; ConfigA
heinrichsweikamp
parents:
diff changeset
322 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
323 movlw b'00100000' ; ConfigB
heinrichsweikamp
parents:
diff changeset
324 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
325 movlw b'00000010' ; Idle Mode
heinrichsweikamp
parents:
diff changeset
326 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
327 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
328 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
329 bcf compass_enabled
heinrichsweikamp
parents:
diff changeset
330 return
heinrichsweikamp
parents:
diff changeset
331
heinrichsweikamp
parents:
diff changeset
332
heinrichsweikamp
parents:
diff changeset
333 global I2C_init_accelerometer
heinrichsweikamp
parents:
diff changeset
334 I2C_init_accelerometer:
heinrichsweikamp
parents:
diff changeset
335 rcall I2C_sleep_accelerometer ; Regs can only be changed in St.By mode
heinrichsweikamp
parents:
diff changeset
336
heinrichsweikamp
parents:
diff changeset
337 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
338 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
339 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
340 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
341 movlw 0x0E ; XYZ_DATA_CFG
heinrichsweikamp
parents:
diff changeset
342 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
343 movlw b'00000000' ; High pass Filter=0 , +/- 2g range
heinrichsweikamp
parents:
diff changeset
344 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
345 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
346 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
347
heinrichsweikamp
parents:
diff changeset
348
heinrichsweikamp
parents:
diff changeset
349 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
350 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
351 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
352 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
353 movlw 0x2A ; CTRL_REG1
heinrichsweikamp
parents:
diff changeset
354 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
355 ; movlw b'00110000' ; CTRL_REG1: 160ms data rate, St.By Mode
heinrichsweikamp
parents:
diff changeset
356 movlw b'00110100' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode
heinrichsweikamp
parents:
diff changeset
357 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
358 movlw b'00000010' ; CTRL_REG2: High Res in Active mode
heinrichsweikamp
parents:
diff changeset
359 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
360 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
361 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
362
heinrichsweikamp
parents:
diff changeset
363 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
364 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
365 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
366 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
367 movlw 0x2A ; CTRL_REG1
heinrichsweikamp
parents:
diff changeset
368 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
369 ; movlw b'00110001' ; CTRL_REG1: 160ms data rate, Active Mode
heinrichsweikamp
parents:
diff changeset
370 movlw b'00110101' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode, Active Mode
heinrichsweikamp
parents:
diff changeset
371 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
372 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
373 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
374
heinrichsweikamp
parents:
diff changeset
375 return
heinrichsweikamp
parents:
diff changeset
376
heinrichsweikamp
parents:
diff changeset
377 global I2C_sleep_accelerometer
heinrichsweikamp
parents:
diff changeset
378 I2C_sleep_accelerometer:
heinrichsweikamp
parents:
diff changeset
379 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
380 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
381 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
382 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
383 movlw 0x2A ; CTRL_REG1
heinrichsweikamp
parents:
diff changeset
384 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
385 movlw b'00000000' ; St. By Mode
heinrichsweikamp
parents:
diff changeset
386 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
387 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents:
diff changeset
388 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
389 return
heinrichsweikamp
parents:
diff changeset
390
113
heinrichsweikamp
parents: 20
diff changeset
391 global lt2942_init
heinrichsweikamp
parents: 20
diff changeset
392 lt2942_init: ; Setup Control register B
heinrichsweikamp
parents: 20
diff changeset
393 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
394 movlw 0x01 ; Point to control reg B
heinrichsweikamp
parents: 20
diff changeset
395 call I2C_TX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
396 movlw b'11111000' ; Automatic conversion every two seconds
heinrichsweikamp
parents: 20
diff changeset
397 movff WREG, SSP1BUF ; Data Byte
heinrichsweikamp
parents: 20
diff changeset
398 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
399 rcall I2C_WaitforACK
heinrichsweikamp
parents: 20
diff changeset
400 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 20
diff changeset
401 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
402 return
heinrichsweikamp
parents: 20
diff changeset
403
heinrichsweikamp
parents: 20
diff changeset
404 global lt2942_get_status
heinrichsweikamp
parents: 20
diff changeset
405 lt2942_get_status: ; Read status register
235
23311219dacc under construction: new hardware_flag to configure different hardware versions
heinrichsweikamp
parents: 211
diff changeset
406 bcf rechargeable ; Clear flag
113
heinrichsweikamp
parents: 20
diff changeset
407 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
408 movlw 0x00 ; Point to Status reg
heinrichsweikamp
parents: 20
diff changeset
409 call I2C_TX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
410 call I2C_RX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
411 movff SSP1BUF,WREG
heinrichsweikamp
parents: 20
diff changeset
412 btfss WREG,7 ; 2942 found?
235
23311219dacc under construction: new hardware_flag to configure different hardware versions
heinrichsweikamp
parents: 211
diff changeset
413 bsf rechargeable ; Yes, set flag
113
heinrichsweikamp
parents: 20
diff changeset
414 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 20
diff changeset
415 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
416 return
heinrichsweikamp
parents: 20
diff changeset
417
heinrichsweikamp
parents: 20
diff changeset
418
heinrichsweikamp
parents: 20
diff changeset
419 global lt2942_get_voltage
heinrichsweikamp
parents: 20
diff changeset
420 lt2942_get_voltage: ; Read battery voltage registers
heinrichsweikamp
parents: 20
diff changeset
421 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
422 movlw 0x08 ; Point to voltage registers
heinrichsweikamp
parents: 20
diff changeset
423 call I2C_TX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
424 call I2C_RX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
425 bsf SSP1CON2,ACKEN ; Master acknowlegde
heinrichsweikamp
parents: 20
diff changeset
426 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
427 movff SSP1BUF,xA+1
heinrichsweikamp
parents: 20
diff changeset
428 bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents: 20
diff changeset
429 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
430 movff SSP1BUF,xA+0
heinrichsweikamp
parents: 20
diff changeset
431 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 20
diff changeset
432 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
433
heinrichsweikamp
parents: 20
diff changeset
434 ; banksel common
heinrichsweikamp
parents: 20
diff changeset
435 ; xA:2 loaded with raw values
heinrichsweikamp
parents: 20
diff changeset
436 movlw LOW .6000
heinrichsweikamp
parents: 20
diff changeset
437 movwf xB+0
heinrichsweikamp
parents: 20
diff changeset
438 movlw HIGH .6000
heinrichsweikamp
parents: 20
diff changeset
439 movwf xB+1
heinrichsweikamp
parents: 20
diff changeset
440 call mult16x16 ;xA*xB=xC
heinrichsweikamp
parents: 20
diff changeset
441
heinrichsweikamp
parents: 20
diff changeset
442 ; devide xC (32bit)/65535 for result in mV (16bit)
heinrichsweikamp
parents: 20
diff changeset
443 movlw .16
heinrichsweikamp
parents: 20
diff changeset
444 movwf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
445 lt2942_get_voltage2:
heinrichsweikamp
parents: 20
diff changeset
446 bcf STATUS,C
heinrichsweikamp
parents: 20
diff changeset
447 rrcf xC+3,F
heinrichsweikamp
parents: 20
diff changeset
448 rrcf xC+2,F
heinrichsweikamp
parents: 20
diff changeset
449 rrcf xC+1,F
heinrichsweikamp
parents: 20
diff changeset
450 rrcf xC+0,F
heinrichsweikamp
parents: 20
diff changeset
451 decfsz i2c_temp,F
heinrichsweikamp
parents: 20
diff changeset
452 bra lt2942_get_voltage2
heinrichsweikamp
parents: 20
diff changeset
453
heinrichsweikamp
parents: 20
diff changeset
454 ; Update battery voltage in mV
heinrichsweikamp
parents: 20
diff changeset
455 movff xC+1,batt_voltage+1
heinrichsweikamp
parents: 20
diff changeset
456 movff xC+0,batt_voltage+0
heinrichsweikamp
parents: 20
diff changeset
457 return
heinrichsweikamp
parents: 20
diff changeset
458
heinrichsweikamp
parents: 20
diff changeset
459 ; global lt2942_get_temperature
heinrichsweikamp
parents: 20
diff changeset
460 ;lt2942_get_temperature: ; Read temperature registers
heinrichsweikamp
parents: 20
diff changeset
461 ; clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
462 ; movlw 0x0C ; Point to temperature registers
heinrichsweikamp
parents: 20
diff changeset
463 ; call I2C_TX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
464 ; call I2C_RX
heinrichsweikamp
parents: 20
diff changeset
465 ; bsf SSP1CON2,ACKEN ; Master acknowlegde
heinrichsweikamp
parents: 20
diff changeset
466 ; rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
467 ; movff SSP1BUF,xA+1
heinrichsweikamp
parents: 20
diff changeset
468 ; bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents: 20
diff changeset
469 ; rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
470 ; movff SSP1BUF,xA+0
heinrichsweikamp
parents: 20
diff changeset
471 ; bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 20
diff changeset
472 ; rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
473 ;
heinrichsweikamp
parents: 20
diff changeset
474 ;; banksel common
heinrichsweikamp
parents: 20
diff changeset
475 ; ; xA:2 loaded with raw values
heinrichsweikamp
parents: 20
diff changeset
476 ; movlw LOW .6000
heinrichsweikamp
parents: 20
diff changeset
477 ; movwf xB+0
heinrichsweikamp
parents: 20
diff changeset
478 ; movlw HIGH .6000
heinrichsweikamp
parents: 20
diff changeset
479 ; movwf xB+1
heinrichsweikamp
parents: 20
diff changeset
480 ; call mult16x16 ;xA*xB=xC
heinrichsweikamp
parents: 20
diff changeset
481 ;
heinrichsweikamp
parents: 20
diff changeset
482 ; ; devide xC (32bit)/65535 for result in 0.1K (16bit)
heinrichsweikamp
parents: 20
diff changeset
483 ; movlw .16
heinrichsweikamp
parents: 20
diff changeset
484 ; movwf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
485 ;lt2942_get_temperature2:
heinrichsweikamp
parents: 20
diff changeset
486 ; bcf STATUS,C
heinrichsweikamp
parents: 20
diff changeset
487 ; rrcf xC+3,F
heinrichsweikamp
parents: 20
diff changeset
488 ; rrcf xC+2,F
heinrichsweikamp
parents: 20
diff changeset
489 ; rrcf xC+1,F
heinrichsweikamp
parents: 20
diff changeset
490 ; rrcf xC+0,F
heinrichsweikamp
parents: 20
diff changeset
491 ; decfsz i2c_temp,F
heinrichsweikamp
parents: 20
diff changeset
492 ; bra lt2942_get_temperature2
heinrichsweikamp
parents: 20
diff changeset
493 ;
heinrichsweikamp
parents: 20
diff changeset
494 ; movff xC+1,sub_a+1
heinrichsweikamp
parents: 20
diff changeset
495 ; movff xC+0,sub_a+0
heinrichsweikamp
parents: 20
diff changeset
496 ; movlw LOW .2731 ; Kelvin to Celcius offset
heinrichsweikamp
parents: 20
diff changeset
497 ; movwf sub_b+0
heinrichsweikamp
parents: 20
diff changeset
498 ; movlw HIGH .2731 ; Kelvin to Celcius offset
heinrichsweikamp
parents: 20
diff changeset
499 ; movwf sub_b+1
heinrichsweikamp
parents: 20
diff changeset
500 ; call subU16 ; sub_c = sub_a - sub_b (with UNSIGNED values)
heinrichsweikamp
parents: 20
diff changeset
501 ;
heinrichsweikamp
parents: 20
diff changeset
502 ; ; Update batttery_temperature in 0.1°C
heinrichsweikamp
parents: 20
diff changeset
503 ; movff sub_c+1,battery_temperature+1
heinrichsweikamp
parents: 20
diff changeset
504 ; movff sub_c+0,battery_temperature+0
heinrichsweikamp
parents: 20
diff changeset
505 ; return
heinrichsweikamp
parents: 20
diff changeset
506
heinrichsweikamp
parents: 20
diff changeset
507 global lt2942_get_accumulated_charge
heinrichsweikamp
parents: 20
diff changeset
508 lt2942_get_accumulated_charge: ; Read accumulated charge and compute percent
heinrichsweikamp
parents: 20
diff changeset
509 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
510 movlw 0x02 ; Point to accumulated charge registers
heinrichsweikamp
parents: 20
diff changeset
511 call I2C_TX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
512 call I2C_RX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
513 bsf SSP1CON2,ACKEN ; Master acknowlegde
heinrichsweikamp
parents: 20
diff changeset
514 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
515 movff SSP1BUF,sub_a+1 ; battery_acumulated_charge+1
heinrichsweikamp
parents: 20
diff changeset
516 bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents: 20
diff changeset
517 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
518 movff SSP1BUF,sub_a+0 ; battery_acumulated_charge+0
heinrichsweikamp
parents: 20
diff changeset
519 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 20
diff changeset
520 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
521
heinrichsweikamp
parents: 20
diff changeset
522 ; Compute batt_percent
211
heinrichsweikamp
parents: 200
diff changeset
523 ; (charge-battery_offset)/365
113
heinrichsweikamp
parents: 20
diff changeset
524 movlw LOW battery_offset
heinrichsweikamp
parents: 20
diff changeset
525 movwf sub_b+0
heinrichsweikamp
parents: 20
diff changeset
526 movlw HIGH battery_offset
heinrichsweikamp
parents: 20
diff changeset
527 movwf sub_b+1
heinrichsweikamp
parents: 20
diff changeset
528 call subU16 ; sub_c = sub_a - sub_b (with signed values)
heinrichsweikamp
parents: 20
diff changeset
529
heinrichsweikamp
parents: 20
diff changeset
530 clrf batt_percent ; Set to zero
heinrichsweikamp
parents: 20
diff changeset
531 btfsc neg_flag ; result negative?
heinrichsweikamp
parents: 20
diff changeset
532 return ; Yes, done.
heinrichsweikamp
parents: 20
diff changeset
533
heinrichsweikamp
parents: 20
diff changeset
534 ; > Zero, set batt_percent properly
heinrichsweikamp
parents: 20
diff changeset
535 movff sub_c+0,xA+0
heinrichsweikamp
parents: 20
diff changeset
536 movff sub_c+1,xA+1
heinrichsweikamp
parents: 20
diff changeset
537 movlw LOW battery_devider
heinrichsweikamp
parents: 20
diff changeset
538 movwf xB+0
heinrichsweikamp
parents: 20
diff changeset
539 movlw HIGH battery_devider
heinrichsweikamp
parents: 20
diff changeset
540 movwf xB+1
heinrichsweikamp
parents: 20
diff changeset
541 call div16x16 ;xA/xB=xC with xA+0 as remainder, uses divB as temp variable
heinrichsweikamp
parents: 20
diff changeset
542 movff xC+0,batt_percent
heinrichsweikamp
parents: 20
diff changeset
543 return
heinrichsweikamp
parents: 20
diff changeset
544
heinrichsweikamp
parents: 20
diff changeset
545 global lt2942_charge_done
heinrichsweikamp
parents: 20
diff changeset
546 lt2942_charge_done: ; Reset accumulating registers to 0xFFFF
heinrichsweikamp
parents: 20
diff changeset
547 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
548 movlw 0x02 ; Point to accumulated charge registers
heinrichsweikamp
parents: 20
diff changeset
549 call I2C_TX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
550 movlw 0xFF
heinrichsweikamp
parents: 20
diff changeset
551 movff WREG, SSP1BUF ; Data Byte
heinrichsweikamp
parents: 20
diff changeset
552 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
553 rcall I2C_WaitforACK
heinrichsweikamp
parents: 20
diff changeset
554 movlw 0xFF
heinrichsweikamp
parents: 20
diff changeset
555 movff WREG, SSP1BUF ; Data Byte
heinrichsweikamp
parents: 20
diff changeset
556 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
557 rcall I2C_WaitforACK
heinrichsweikamp
parents: 20
diff changeset
558 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 20
diff changeset
559 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
560 return
heinrichsweikamp
parents: 20
diff changeset
561
heinrichsweikamp
parents: 20
diff changeset
562 I2C_TX_GAUGE: ; Sends a byte to the LT2942 Gauge IC
heinrichsweikamp
parents: 20
diff changeset
563 movwf i2c_temp+1 ; Data byte
heinrichsweikamp
parents: 20
diff changeset
564 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents: 20
diff changeset
565 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
566 movlw b'11001000' ; Address byte + Write bit
heinrichsweikamp
parents: 20
diff changeset
567 movwf SSP1BUF ; control byte
heinrichsweikamp
parents: 20
diff changeset
568 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
569 rcall I2C_WaitforACK
heinrichsweikamp
parents: 20
diff changeset
570 movff i2c_temp+1, SSP1BUF ; Data Byte
heinrichsweikamp
parents: 20
diff changeset
571 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
572 rcall I2C_WaitforACK
heinrichsweikamp
parents: 20
diff changeset
573 return
heinrichsweikamp
parents: 20
diff changeset
574
heinrichsweikamp
parents: 20
diff changeset
575 I2C_RX_GAUGE:
heinrichsweikamp
parents: 20
diff changeset
576 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents: 20
diff changeset
577 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
578 movlw b'11001001' ; Address byte + Read bit
heinrichsweikamp
parents: 20
diff changeset
579 movwf SSP1BUF ; control byte
heinrichsweikamp
parents: 20
diff changeset
580 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
581 rcall I2C_WaitforACK
heinrichsweikamp
parents: 20
diff changeset
582 bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents: 20
diff changeset
583 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
584 return
heinrichsweikamp
parents: 20
diff changeset
585
heinrichsweikamp
parents: 20
diff changeset
586
0
heinrichsweikamp
parents:
diff changeset
587
heinrichsweikamp
parents:
diff changeset
588 END