annotate src/i2c.asm @ 429:4b93354b7738

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