annotate src/i2c.asm @ 564:54346c651b6a

bring digital S8 HUD back to life...
author heinrichsweikamp
date Mon, 05 Feb 2018 17:59:55 +0100
parents b7eb98dbd800
children 0ba88db66492
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 ;
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
5 ; I2C Interface
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
6 ;
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
7 ; Compass0
0
heinrichsweikamp
parents:
diff changeset
8 ; HMC5883L's read address (8-Bit): 0x3D
heinrichsweikamp
parents:
diff changeset
9 ; HMC5883L's write address (8-Bit): 0x3C
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 ;
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
13 ; Compass1
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
14 ; LSM303D's read address (8-Bit): 0x3D
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
15 ; LSM303D's write address (8-Bit): 0x3C
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
16 ;
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
17 ; Compass2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
18 ; LSM303AGR's Compass read address (8-Bit): 0x3D
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
19 ; LSM303AGR's Compass write address (8-Bit): 0x3C
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
20 ; LSM303AGR's Acceleration read address (8-Bit): 0x33
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
21 ; LSM303AGR's Acceleration write address (8-Bit): 0x32
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
22 ;
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
23 ; RX Circuity
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
24 ; RX Circuity read address (8-Bit): 0x51
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
25 ; RX Circuity write address (8-Bit): 0x50
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
26 ;
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
27 ;
0
heinrichsweikamp
parents:
diff changeset
28 ; Copyright (c) 2012, JD Gascuel, HeinrichsWeikamp, all right reserved.
heinrichsweikamp
parents:
diff changeset
29 ;=============================================================================
heinrichsweikamp
parents:
diff changeset
30 ; HISTORY
heinrichsweikamp
parents:
diff changeset
31 ; 2012-08-22 : [mH] Creation
heinrichsweikamp
parents:
diff changeset
32
heinrichsweikamp
parents:
diff changeset
33
275
653a3ab08062 rename into hwOS
heinrichsweikamp
parents: 235
diff changeset
34 #include "hwos.inc" ; Mandatory header
0
heinrichsweikamp
parents:
diff changeset
35 #include "wait.inc"
113
heinrichsweikamp
parents: 20
diff changeset
36 #include "math.inc"
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
37 #include "external_flash.inc"
113
heinrichsweikamp
parents: 20
diff changeset
38
0
heinrichsweikamp
parents:
diff changeset
39 i2c CODE
heinrichsweikamp
parents:
diff changeset
40
heinrichsweikamp
parents:
diff changeset
41 WaitMSSP:
heinrichsweikamp
parents:
diff changeset
42 decfsz i2c_temp,F ; check for timeout during I2C action
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
43 bra WaitMSSP2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
44 bra I2CFail ; timeout occured
0
heinrichsweikamp
parents:
diff changeset
45 WaitMSSP2:
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
46 btfss PIR1,SSP1IF
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
47 bra WaitMSSP
0
heinrichsweikamp
parents:
diff changeset
48 clrf i2c_temp
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
49 bcf PIR1,SSP1IF
0
heinrichsweikamp
parents:
diff changeset
50 return
heinrichsweikamp
parents:
diff changeset
51
heinrichsweikamp
parents:
diff changeset
52 I2C_WaitforACK:
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
53 btfss SSP1CON2,ACKSTAT ; checks for ACK bit from slave
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
54 return
0
heinrichsweikamp
parents:
diff changeset
55 I2CFail:
heinrichsweikamp
parents:
diff changeset
56 rcall I2CReset ; I2C Reset
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
57 bcf PIR1,SSP1IF
0
heinrichsweikamp
parents:
diff changeset
58 clrf i2c_temp
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
59 bsf i2c_error_flag ; set error flag
0
heinrichsweikamp
parents:
diff changeset
60 return
heinrichsweikamp
parents:
diff changeset
61
heinrichsweikamp
parents:
diff changeset
62 I2CReset: ; Something went wrong (Slave holds SDA low?)
heinrichsweikamp
parents:
diff changeset
63 clrf SSP1CON1 ; wake-up slave and reset entire module
heinrichsweikamp
parents:
diff changeset
64 clrf SSP1CON2
heinrichsweikamp
parents:
diff changeset
65 clrf SSP1STAT
heinrichsweikamp
parents:
diff changeset
66 bcf TRISC,3 ; SCL OUTPUT
heinrichsweikamp
parents:
diff changeset
67 bsf TRISC,4 ; SDA Input
heinrichsweikamp
parents:
diff changeset
68 bcf PORTC,3
heinrichsweikamp
parents:
diff changeset
69 movlw d'9'
heinrichsweikamp
parents:
diff changeset
70 movwf i2c_temp ; clock-out 9 clock cycles manually
heinrichsweikamp
parents:
diff changeset
71 I2CReset_1:
heinrichsweikamp
parents:
diff changeset
72 bsf PORTC,3 ; SCL=1
heinrichsweikamp
parents:
diff changeset
73 nop
heinrichsweikamp
parents:
diff changeset
74 nop
heinrichsweikamp
parents:
diff changeset
75 nop
heinrichsweikamp
parents:
diff changeset
76 nop
heinrichsweikamp
parents:
diff changeset
77 btfsc PORTC,4 ; SDA=1?
heinrichsweikamp
parents:
diff changeset
78 bra I2CReset_2 ; =1, SDA has been released from slave
heinrichsweikamp
parents:
diff changeset
79 bcf PORTC,3 ; SCL=0
heinrichsweikamp
parents:
diff changeset
80 nop
heinrichsweikamp
parents:
diff changeset
81 nop
heinrichsweikamp
parents:
diff changeset
82 bcf PORTC,3
heinrichsweikamp
parents:
diff changeset
83 nop
heinrichsweikamp
parents:
diff changeset
84 nop
heinrichsweikamp
parents:
diff changeset
85 decfsz i2c_temp,F
heinrichsweikamp
parents:
diff changeset
86 bra I2CReset_1 ; check for nine clock cycles
heinrichsweikamp
parents:
diff changeset
87 I2CReset_2:
heinrichsweikamp
parents:
diff changeset
88 bsf TRISC,3 ; SCL Input
heinrichsweikamp
parents:
diff changeset
89 clrf SSP1CON1 ; setup I²C Mode
heinrichsweikamp
parents:
diff changeset
90 WAITMS d'10' ; Reset-Timeout for I2C devices
heinrichsweikamp
parents:
diff changeset
91 movlw b'00000000' ; with slew rate control
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
92 movwf SSP1STAT
0
heinrichsweikamp
parents:
diff changeset
93 movlw b'00101000'
heinrichsweikamp
parents:
diff changeset
94 movwf SSP1CON1
heinrichsweikamp
parents:
diff changeset
95 movlw b'00000000'
heinrichsweikamp
parents:
diff changeset
96 movwf SSP1CON2
heinrichsweikamp
parents:
diff changeset
97 movlw 0x27
heinrichsweikamp
parents:
diff changeset
98 movwf SSP1ADD
heinrichsweikamp
parents:
diff changeset
99 return
heinrichsweikamp
parents:
diff changeset
100
heinrichsweikamp
parents:
diff changeset
101 I2C_TX:
heinrichsweikamp
parents:
diff changeset
102 movwf SSP1BUF
heinrichsweikamp
parents:
diff changeset
103 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
104 bra I2C_WaitforACK ; Returns...
heinrichsweikamp
parents:
diff changeset
105
heinrichsweikamp
parents:
diff changeset
106 I2C_TwoBytesRX_div16: ; Get two bytes and devide lo:hi/16 (signed)
heinrichsweikamp
parents:
diff changeset
107 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
108 movff SSP1BUF,hi ; Data Byte
heinrichsweikamp
parents:
diff changeset
109 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
110 movff SSP1BUF,lo ; Data Byte
heinrichsweikamp
parents:
diff changeset
111 I2C_TwoBytesRX_div16_2: ; devide lo:hi/16 (signed) only
heinrichsweikamp
parents:
diff changeset
112 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
113 btfsc hi,7 ; Copy sign bit to carry
heinrichsweikamp
parents:
diff changeset
114 bsf STATUS,C
heinrichsweikamp
parents:
diff changeset
115 rrcf hi ; /2
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
116 rrcf lo
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
117 I2C_TwoBytesRX_div8_2: ; devide lo:hi/8 (signed) only
0
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 ; /4
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
122 rrcf lo
0
heinrichsweikamp
parents:
diff changeset
123 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
124 btfsc hi,7 ; Copy sign bit to carry
heinrichsweikamp
parents:
diff changeset
125 bsf STATUS,C
heinrichsweikamp
parents:
diff changeset
126 rrcf hi ; /8
heinrichsweikamp
parents:
diff changeset
127 rrcf lo
heinrichsweikamp
parents:
diff changeset
128 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
129 btfsc hi,7 ; Copy sign bit to carry
heinrichsweikamp
parents:
diff changeset
130 bsf STATUS,C
heinrichsweikamp
parents:
diff changeset
131 rrcf hi ; /16
heinrichsweikamp
parents:
diff changeset
132 rrcf lo
heinrichsweikamp
parents:
diff changeset
133 return
heinrichsweikamp
parents:
diff changeset
134
heinrichsweikamp
parents:
diff changeset
135 global I2C_RX_accelerometer
heinrichsweikamp
parents:
diff changeset
136 I2C_RX_accelerometer:
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
137 btfsc compass_type2 ; compass2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
138 bra I2C_RX_accelerometer_compass2 ; yes
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
139 btfsc compass_type ; compass1?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
140 bra I2C_RX_accelerometer_compass1 ; yes
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
141 I2C_RX_accelerometer_compass0:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
142 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
143 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
144 movlw 0x38 ; address
0
heinrichsweikamp
parents:
diff changeset
145 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
146 movlw 0x00
0
heinrichsweikamp
parents:
diff changeset
147 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
148 bsf SSP1CON2,RSEN ; Repeated start condition (!)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
149 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
150 movlw 0x39 ; address
0
heinrichsweikamp
parents:
diff changeset
151 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
152
158
683321c09cfa nicer boot into surfacemode
heinrichsweikamp
parents: 113
diff changeset
153 rcall I2C_OneByteRX ; Get Status Byte
683321c09cfa nicer boot into surfacemode
heinrichsweikamp
parents: 113
diff changeset
154 movf SSP1BUF,W
683321c09cfa nicer boot into surfacemode
heinrichsweikamp
parents: 113
diff changeset
155
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
156 ; Non-flipped screen:
0
heinrichsweikamp
parents:
diff changeset
157 ; Chip orientation on the PCB requires
heinrichsweikamp
parents:
diff changeset
158 ; Original = Corrected
heinrichsweikamp
parents:
diff changeset
159 ; x = -x
heinrichsweikamp
parents:
diff changeset
160 ; y = -y
heinrichsweikamp
parents:
diff changeset
161 ; z = -z
heinrichsweikamp
parents:
diff changeset
162
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
163 ; Flipped screen:
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
164 ; Chip orientation on the PCB requires
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
165 ; Original = Corrected
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
166 ; x = x
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
167 ; y = y
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
168 ; z = -z
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
169
0
heinrichsweikamp
parents:
diff changeset
170 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
171 btfsc flip_screen ; 180° rotation ?
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
172 bra I2C_RX_accelerometer2 ; Yes
0
heinrichsweikamp
parents:
diff changeset
173 comf hi ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
174 negf lo
heinrichsweikamp
parents:
diff changeset
175 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
176 incf hi,F ; YES: do it.
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
177 I2C_RX_accelerometer2:
0
heinrichsweikamp
parents:
diff changeset
178 movff lo,accel_DX+0
heinrichsweikamp
parents:
diff changeset
179 movff hi,accel_DX+1 ; Copy result
heinrichsweikamp
parents:
diff changeset
180
heinrichsweikamp
parents:
diff changeset
181 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
182 btfsc flip_screen ; 180° rotation ?
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
183 bra I2C_RX_accelerometer3 ; Yes
0
heinrichsweikamp
parents:
diff changeset
184 comf hi ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
185 negf lo
heinrichsweikamp
parents:
diff changeset
186 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
187 incf hi,F ; YES: do it.
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
188 I2C_RX_accelerometer3:
0
heinrichsweikamp
parents:
diff changeset
189 movff lo,accel_DY+0
heinrichsweikamp
parents:
diff changeset
190 movff hi,accel_DY+1 ; Copy result
heinrichsweikamp
parents:
diff changeset
191
heinrichsweikamp
parents:
diff changeset
192 rcall I2C_OneByteRX ; Get one byte
heinrichsweikamp
parents:
diff changeset
193 movff SSP1BUF,hi ; Data Byte
heinrichsweikamp
parents:
diff changeset
194 bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents:
diff changeset
195 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
196 ; According to datasheet there should be no Master Acknowlegde for the last Byte (accel_DZ+0)...
heinrichsweikamp
parents:
diff changeset
197 movff SSP1BUF,lo ; Data Byte
heinrichsweikamp
parents:
diff changeset
198
heinrichsweikamp
parents:
diff changeset
199 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only
heinrichsweikamp
parents:
diff changeset
200 comf hi ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
201 negf lo
heinrichsweikamp
parents:
diff changeset
202 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
203 incf hi,F ; YES: do it.
heinrichsweikamp
parents:
diff changeset
204 movff lo,accel_DZ+0
heinrichsweikamp
parents:
diff changeset
205 movff hi,accel_DZ+1 ; Copy result
heinrichsweikamp
parents:
diff changeset
206
heinrichsweikamp
parents:
diff changeset
207 bsf SSP1CON2,PEN ; Stop condition
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
208 bra WaitMSSP ; (And return)
0
heinrichsweikamp
parents:
diff changeset
209
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
210 I2C_RX_accelerometer_compass1:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
211 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
212 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
213 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
214 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
215 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
216 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
217 bsf SSP1CON2,RSEN ; Repeated start condition (!)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
218 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
219 movlw 0x3D ; address
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
220 I2C_RX_accelerometer_compass1_xx: ; compass2 continues here...
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
221 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
222 ; Non-flipped screen:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
223 ; Chip orientation on the PCB requires
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
224 ; Original = Corrected
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
225 ; x = -x (Compass 1)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
226 ; x = x (Compass 2)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
227 ; y = -y
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
228 ; z = -z
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
229
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
230 ; Flipped screen:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
231 ; Chip orientation on the PCB requires
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
232 ; Original = Corrected
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
233 ; x = x (Compass 1)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
234 ; x = -x (Compass 2)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
235 ; y = y
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
236 ; z = -z
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
237
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
238 ; Dump the accelerator data
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
239 rcall I2C_OneByteRX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
240 movff SSP1BUF,lo ;accel_DX+0
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,hi ;accel_DX+1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
243 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
244 btfss compass_type2 ; compass 2?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
245 bra I2C_RX_accelerometer1_c1 ; No, compass 1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
246 ; compass 2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
247 btfss flip_screen ; 180° rotation ?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
248 bra I2C_RX_accelerometer2_c1 ; No, continue with normal compass1 routines for Y and Z
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
249 ; flipped compass 2, negate x
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
250 comf hi ; 16bit sign change.
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
251 negf lo
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
252 btfsc STATUS,C ; Carry to propagate ?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
253 incf hi,F ; YES: do it.
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
254 bra I2C_RX_accelerometer2_c1 ; continue with normal compass1 routines for Y and Z
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
255
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
256 I2C_RX_accelerometer1_c1:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
257 btfsc flip_screen ; 180° rotation ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
258 bra I2C_RX_accelerometer2_c1 ; Yes
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
259 ; non-flipped compass 1, negate x
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
260 comf hi ; 16bit sign change.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
261 negf lo
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
262 btfsc STATUS,C ; Carry to propagate ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
263 incf hi,F ; YES: do it.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
264 I2C_RX_accelerometer2_c1:
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
265 ; flipped compass 1, non-flipped compass 2
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
266 movff lo,accel_DX+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
267 movff hi,accel_DX+1 ; Copy result
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
268 rcall I2C_OneByteRX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
269 movff SSP1BUF,lo ;accel_DY+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
270 rcall I2C_OneByteRX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
271 movff SSP1BUF,hi ;accel_DY+1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
272
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
273 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
274 btfsc flip_screen ; 180° rotation ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
275 bra I2C_RX_accelerometer3_c1 ; Yes
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
276 comf hi ; 16bit sign change.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
277 negf lo
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
278 btfsc STATUS,C ; Carry to propagate ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
279 incf hi,F ; YES: do it.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
280 I2C_RX_accelerometer3_c1:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
281 movff lo,accel_DY+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
282 movff hi,accel_DY+1 ; Copy result
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
283
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
284 rcall I2C_OneByteRX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
285 movff SSP1BUF,lo ;accel_DZ+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
286 bsf SSP1CON2, RCEN ; Enable recieve mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
287 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
288 ; 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
289 movff SSP1BUF,hi ;accel_DZ+1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
290 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
291 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
292 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
293 comf hi ; 16bit sign change for Z
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
294 negf lo
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
295 btfsc STATUS,C ; Carry to propagate ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
296 incf hi,F ; YES: do it.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
297 movff lo,accel_DZ+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
298 movff hi,accel_DZ+1 ; Copy result
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
299 return
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
300
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
301 I2C_RX_accelerometer_compass2:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
302 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
303 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
304 movlw 0x32 ; address
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
305 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
306 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
307 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
308 bsf SSP1CON2,RSEN ; Repeated start condition (!)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
309 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
310 movlw 0x33 ; address
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
311 bra I2C_RX_accelerometer_compass1_xx
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
312
0
heinrichsweikamp
parents:
diff changeset
313 I2C_OneByteRX:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
314 bsf SSP1CON2, RCEN ; Enable recieve mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
315 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
316 bsf SSP1CON2,ACKEN ; Master acknowlegde
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
317 bra WaitMSSP ; And return!
0
heinrichsweikamp
parents:
diff changeset
318
heinrichsweikamp
parents:
diff changeset
319 global I2C_RX_compass
heinrichsweikamp
parents:
diff changeset
320 I2C_RX_compass:
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
321 btfsc compass_type2 ; compass2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
322 bra I2C_RX_compass2 ; yes
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
323 btfsc compass_type ; compass1?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
324 bra I2C_RX_compass1 ; yes
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
325 I2C_RX_compass0:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
326 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
327 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
328 movlw 0x3C ; address
0
heinrichsweikamp
parents:
diff changeset
329 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
330 movlw 0x03
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
331 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
332 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
333 rcall WaitMSSP
0
heinrichsweikamp
parents:
diff changeset
334
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
335 bcf PIR1,SSP1IF
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
336 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
337 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
338 movlw 0x3D ; address
0
heinrichsweikamp
parents:
diff changeset
339 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
340
heinrichsweikamp
parents:
diff changeset
341 ; Compass IC sends data in following order:
heinrichsweikamp
parents:
diff changeset
342 ; x MSB
heinrichsweikamp
parents:
diff changeset
343 ; x LSB
heinrichsweikamp
parents:
diff changeset
344 ; z MSB
heinrichsweikamp
parents:
diff changeset
345 ; z LSB
heinrichsweikamp
parents:
diff changeset
346 ; y MSB
heinrichsweikamp
parents:
diff changeset
347 ; y LSB
heinrichsweikamp
parents:
diff changeset
348
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
349 ; Non-flipped screen
0
heinrichsweikamp
parents:
diff changeset
350 ; Chip orientation on the PCB requires
heinrichsweikamp
parents:
diff changeset
351 ; Original = Corrected
heinrichsweikamp
parents:
diff changeset
352 ; x = -y
heinrichsweikamp
parents:
diff changeset
353 ; z = z
heinrichsweikamp
parents:
diff changeset
354 ; y = x
heinrichsweikamp
parents:
diff changeset
355
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
356 ; Flipped screen
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
357 ; Chip orientation on the PCB requires
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
358 ; Original = Corrected
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
359 ; x = y
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
360 ; z = z
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
361 ; y = -x
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
362
0
heinrichsweikamp
parents:
diff changeset
363 rcall I2C_OneByteRX ; Get one byte
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
364 movff SSP1BUF,compass_DY+1; Data Byte
0
heinrichsweikamp
parents:
diff changeset
365 rcall I2C_OneByteRX ; Get one byte
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
366 movff SSP1BUF,compass_DY+0; Data Byte
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
367 btfsc flip_screen ; 180° rotation ?
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
368 bra I2C_RX_compass0_2 ; Yes
0
heinrichsweikamp
parents:
diff changeset
369 banksel compass_DY
heinrichsweikamp
parents:
diff changeset
370 comf compass_DY+1 ; 16bit sign change.
heinrichsweikamp
parents:
diff changeset
371 negf compass_DY+0
heinrichsweikamp
parents:
diff changeset
372 btfsc STATUS,C ; Carry to propagate ?
heinrichsweikamp
parents:
diff changeset
373 incf compass_DY+1,F ; YES: do it.
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
374 I2C_RX_compass0_2:
0
heinrichsweikamp
parents:
diff changeset
375 banksel common
heinrichsweikamp
parents:
diff changeset
376 rcall I2C_OneByteRX ; Get one byte
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
377 movff SSP1BUF,compass_DZ+1; Data Byte
0
heinrichsweikamp
parents:
diff changeset
378 rcall I2C_OneByteRX ; Get one byte
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
379 movff SSP1BUF,compass_DZ+0; Data Byte
0
heinrichsweikamp
parents:
diff changeset
380 rcall I2C_OneByteRX ; Get one byte
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
381 movff SSP1BUF,compass_DX+1; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
382 bsf SSP1CON2, RCEN ; Enable recieve mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
383 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
384 movff SSP1BUF,compass_DX+0; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
385 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
386 rcall WaitMSSP
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
387 btfss flip_screen ; 180° rotation ?
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
388 return ; No, done.
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
389 ; Yes, flip X
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
390 banksel compass_DX
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
391 comf compass_DX+1 ; 16bit sign change.
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
392 negf compass_DX+0
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
393 btfsc STATUS,C ; Carry to propagate ?
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
394 incf compass_DX+1,F ; YES: do it.
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
395 banksel common
0
heinrichsweikamp
parents:
diff changeset
396 return
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
397
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
398 I2C_RX_compass1: ; New compass
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
399 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
400 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
401 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
402 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
403 movlw b'10001000' ; 0x08 with auto-increment (MSB=1)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
404 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
405 bsf SSP1CON2,RSEN ; Repeated start condition (!)
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 movlw 0x3D ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
408 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
409 ;rcall WaitMSSP ; Needed? (mH)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
410 rcall I2C_OneByteRX ; Get one byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
411 movff SSP1BUF,lo ; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
412 rcall I2C_OneByteRX ; Get one byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
413 movff SSP1BUF,hi ; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
414 rcall I2C_TwoBytesRX_div8_2
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
415 movff lo,compass_DX+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
416 movff hi,compass_DX+1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
417 btfss flip_screen ; 180° rotation ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
418 bra I2C_RX_compass1_1 ; Yes
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
419 ; Yes, flip X
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
420 banksel compass_DX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
421 comf compass_DX+1 ; 16bit sign change.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
422 negf compass_DX+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
423 btfsc STATUS,C ; Carry to propagate ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
424 incf compass_DX+1,F ; YES: do it.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
425 banksel common
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
426 I2C_RX_compass1_1:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
427 rcall I2C_OneByteRX ; Get one byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
428 movff SSP1BUF,lo ; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
429 rcall I2C_OneByteRX ; Get one byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
430 movff SSP1BUF,hi ; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
431 rcall I2C_TwoBytesRX_div8_2
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
432 movff lo,compass_DY+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
433 movff hi,compass_DY+1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
434 btfss flip_screen ; 180° rotation ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
435 bra I2C_RX_compass1_2 ; Yes
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
436 ; Yes, flip Y
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
437 banksel compass_DY
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
438 comf compass_DY+1 ; 16bit sign change.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
439 negf compass_DY+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
440 btfsc STATUS,C ; Carry to propagate ?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
441 incf compass_DY+1,F ; YES: do it.
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
442 I2C_RX_compass1_2:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
443 banksel common
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
444 rcall I2C_OneByteRX ; Get one byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
445 movff SSP1BUF,lo ; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
446 bsf SSP1CON2, RCEN ; Enable recieve mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
447 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
448 movff SSP1BUF,hi ; Data Byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
449 rcall I2C_TwoBytesRX_div8_2
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
450 movff lo,compass_DZ+0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
451 movff hi,compass_DZ+1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
452 bsf SSP1CON2,PEN ; Stop condition
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
453 bra WaitMSSP ;(And return)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
454
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
455 I2C_RX_compass2: ; newest compass
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
456 bsf SSP1CON2,SEN ; Start condition
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
457 rcall WaitMSSP
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
458 movlw 0x3C ; address
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
459 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
460 movlw 0xE8 ; 0x68 with auto-increment (MSB=1)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
461 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
462 bsf SSP1CON2,RSEN ; Repeated start condition (!)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
463 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
464 movlw 0x3D ; address
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
465 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
466 ; rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
467 rcall I2C_OneByteRX ; Get one byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
468 movff SSP1BUF,lo ; Data Byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
469 rcall I2C_OneByteRX ; Get one byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
470 movff SSP1BUF,hi ; Data Byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
471 ; rcall I2C_TwoBytesRX_div8_2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
472 btfsc flip_screen ; 180° rotation ?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
473 bra I2C_RX_compass2_1 ; Yes, do nothing with X
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
474 ; No, flip X
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
475 comf hi ; 16bit sign change.
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
476 negf lo
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
477 btfsc STATUS,C ; Carry to propagate ?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
478 incf hi,F ; YES: do it.
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
479 I2C_RX_compass2_1:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
480 movff lo,compass_DX+0
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
481 movff hi,compass_DX+1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
482 rcall I2C_OneByteRX ; Get one byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
483 movff SSP1BUF,lo ; Data Byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
484 rcall I2C_OneByteRX ; Get one byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
485 movff SSP1BUF,hi ; Data Byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
486 ; rcall I2C_TwoBytesRX_div8_2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
487 btfss flip_screen ; 180° rotation ?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
488 bra I2C_RX_compass2_2 ; No, do nothing with Y
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
489 ; Yes, flip Y
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
490 comf hi ; 16bit sign change.
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
491 negf lo
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
492 btfsc STATUS,C ; Carry to propagate ?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
493 incf hi,F ; YES: do it.
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
494 I2C_RX_compass2_2:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
495 movff lo,compass_DY+0
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
496 movff hi,compass_DY+1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
497 rcall I2C_OneByteRX ; Get one byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
498 movff SSP1BUF,lo ; Data Byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
499 rcall I2C_OneByteRX ; Get one byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
500 movff SSP1BUF,hi ; Data Byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
501 ;rcall I2C_TwoBytesRX_div8_2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
502 movff lo,compass_DZ+0
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
503 movff hi,compass_DZ+1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
504 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
505 bra WaitMSSP ;(And return)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
506
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
507
0
heinrichsweikamp
parents:
diff changeset
508 global I2C_init_compass
heinrichsweikamp
parents:
diff changeset
509 I2C_init_compass:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
510 bsf compass_enabled
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
511 bcf compass_type2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
512 ; probe compass 2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
513 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
514 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
515 movlw 0x32 ; Address byte + Write bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
516 movwf SSP1BUF ; control byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
517 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
518 btfss SSP1CON2,ACKSTAT ; ACK?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
519 bsf compass_type2 ; ACK send. compass2 present
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
520 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
521 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
522
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
523 btfsc compass_type2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
524 bra I2C_init_compass2 ; Compass2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
525 ; Check for compass0 or compass1...
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
526 bsf compass_type ; set flag
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
527 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
528 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
529 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
530 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
531 movlw 0x0F
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
532 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
533 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
534 rcall WaitMSSP
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
535 bcf PIR1,SSP1IF
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
536 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
537 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
538 movlw 0x3D ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
539 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
540 rcall I2C_OneByteRX ; Get one byte
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
541 movlw 0x49 ; 0x49 = Compass1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
542 cpfseq SSP1BUF
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
543 bcf compass_type ; clear flag
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
544 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
545 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
546
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
547 btfsc compass_type ; compass1?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
548 bra I2C_init_compass1 ; yes
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
549 ; init compass0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
550 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
551 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
552 movlw 0x3C ; address
0
heinrichsweikamp
parents:
diff changeset
553 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
554 movlw 0x00
0
heinrichsweikamp
parents:
diff changeset
555 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
556 ; movlw b'01101001' ; ConfigA: 3Hz, 8 Samples averaged, Test Mode (Positive Bias)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
557 movlw b'01101000' ; ConfigA: 3Hz, 8 Samples averaged
0
heinrichsweikamp
parents:
diff changeset
558 rcall I2C_TX
20
9b7dd3103545 minor cleanup
heinrichsweikamp
parents: 18
diff changeset
559 I2C_init_compass_common:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
560 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
561 swapf i2c_temp,F
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
562 comf i2c_temp,F
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
563 bcf STATUS,C
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
564 rlcf i2c_temp
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
565 movf i2c_temp,W
4e3f133dfbf4 add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents: 0
diff changeset
566 clrf i2c_temp
0
heinrichsweikamp
parents:
diff changeset
567 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
568 movlw b'00000000' ; Continous Mode
0
heinrichsweikamp
parents:
diff changeset
569 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
570 bsf SSP1CON2,PEN ; Stop condition
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
571 bra WaitMSSP ; (And return)
0
heinrichsweikamp
parents:
diff changeset
572
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
573 I2C_init_compass1:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
574 bsf SSP1CON2,SEN ; Start 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 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
577 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
578 movlw 0x9F ; 1F with auto-increment (MSB=1)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
579 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
580 movlw b'00000000' ; CTRL0
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
581 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
582 movlw b'00101111' ; CTRL1 (6,25Hz, BDU=0, x,y,z = ON)
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
583 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
584 movlw b'11000000' ; CTRL2 (50Hz, +/-2g,
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
585 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
586 movlw b'00000000' ; CTRL3
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
587 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
588 movlw b'00000000' ; CTRL4
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
589 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
590 movlw b'01100100' ; CTRL5 HIGH res, 6,25Hz
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
591 rcall I2C_TX
429
4b93354b7738 hardware_flag handling
heinrichsweikamp
parents: 427
diff changeset
592 init_compass1_common:
498
e8351dd7bb90 compass1 full scale gain test
heinrichsweikamp
parents: 467
diff changeset
593 movlw b'01100000' ; CTRL6 Full scale (+/-12 Gauss -> 2730LSB/Gauss)
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
594 ;movlw b'00000000' ; CTRL6 (+/-2 Gauss)
498
e8351dd7bb90 compass1 full scale gain test
heinrichsweikamp
parents: 467
diff changeset
595 ;movlw b'00100000' ; CTRL6 (+/-4 Gauss)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
596 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
597 movlw b'00000000' ; CTRL7 Continuous Mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
598 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
599 bsf SSP1CON2,PEN ; Stop condition
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
600 bra WaitMSSP ; (And return)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
601
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
602 I2C_init_compass2:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
603 bsf SSP1CON2,SEN ; Start condition
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
604 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
605 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
606 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
607 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
608 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
609 movlw b'00000000' ; CFG_REG_A_M (10Hz, Continuous)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
610 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
611 movlw b'00000000' ; CFG_REG_B_M (Low-Pass Filter off)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
612 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
613 movlw b'00000000' ; CFG_REG_C_M BDU=0
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
614 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
615 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
616 bra WaitMSSP ;(And return)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
617
0
heinrichsweikamp
parents:
diff changeset
618 global I2C_sleep_compass
heinrichsweikamp
parents:
diff changeset
619 I2C_sleep_compass:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
620 bcf compass_enabled
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
621 btfsc compass_type2 ; compass2?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
622 bra I2C_sleep_compass2 ; yes
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
623 btfsc compass_type ; compass1?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
624 bra I2C_sleep_compass1 ; yes
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
625 I2C_sleep_compass0:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
626 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
627 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
628 movlw 0x3C ; address
0
heinrichsweikamp
parents:
diff changeset
629 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
630 movlw 0x00
0
heinrichsweikamp
parents:
diff changeset
631 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
632 movlw b'01101000' ; ConfigA
0
heinrichsweikamp
parents:
diff changeset
633 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
634 movlw b'00100000' ; ConfigB
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
635 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
636 movlw b'00000010' ; Idle Mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
637 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
638 bsf SSP1CON2,PEN ; Stop condition
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
639 bra WaitMSSP ; (And return)
0
heinrichsweikamp
parents:
diff changeset
640
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
641 I2C_sleep_compass1:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
642 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
643 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
644 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
645 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
646 movlw 0x20 ; CTRL_REG1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
647 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
648 movlw b'00000000' ; data for CTRL_REG1: acceleration sensor Power-down mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
649 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
650 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
651 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
652 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
653 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
654 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
655 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
656 movlw 0x26 ; CTRL_REG7
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
657 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
658 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor Power-down mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
659 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
660 bsf SSP1CON2,PEN ; Stop condition
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
661 bra WaitMSSP ;(And return)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
662
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
663
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
664 I2C_sleep_compass2:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
665 ; magnetic
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
666 bsf SSP1CON2,SEN ; Start condition
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
667 rcall WaitMSSP
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
668 movlw 0x3C ; address
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
669 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
670 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
671 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
672 movlw b'00000010' ; CFG_REG_A_M (Idle mode)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
673 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
674 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
675 bra WaitMSSP ; (And return)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
676
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
677 I2C_sleep_accelerometer2:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
678 ; accelerometer
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
679 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
680 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
681 movlw 0x32 ; address
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
682 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
683 movlw 0x9F ; 1F with auto-increment (MSB=1)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
684 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
685 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
686 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
687 movlw b'00000000' ; CTRL_REG1_A (All off)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
688 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
689 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
690 bra WaitMSSP ; (And return)
0
heinrichsweikamp
parents:
diff changeset
691
heinrichsweikamp
parents:
diff changeset
692 global I2C_init_accelerometer
heinrichsweikamp
parents:
diff changeset
693 I2C_init_accelerometer:
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
694 btfsc compass_type2 ; compass2?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
695 bra I2C_init_accelerometer2 ; Yes.
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
696
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
697 btfsc compass_type ; compass1?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
698 return ; yes, ignore
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
699
0
heinrichsweikamp
parents:
diff changeset
700 rcall I2C_sleep_accelerometer ; Regs can only be changed in St.By mode
heinrichsweikamp
parents:
diff changeset
701
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
702 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
703 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
704 movlw 0x38 ; address
0
heinrichsweikamp
parents:
diff changeset
705 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
706 movlw 0x0E ; XYZ_DATA_CFG
0
heinrichsweikamp
parents:
diff changeset
707 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
708 movlw b'00000000' ; High pass Filter=0 , +/- 2g range
0
heinrichsweikamp
parents:
diff changeset
709 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
710 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
711 rcall WaitMSSP
0
heinrichsweikamp
parents:
diff changeset
712
heinrichsweikamp
parents:
diff changeset
713
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
714 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
715 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
716 movlw 0x38 ; address
0
heinrichsweikamp
parents:
diff changeset
717 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
718 movlw 0x2A ; CTRL_REG1
0
heinrichsweikamp
parents:
diff changeset
719 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
720 ; movlw b'00110000' ; CTRL_REG1: 160ms data rate, St.By Mode
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
721 movlw b'00110100' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode
0
heinrichsweikamp
parents:
diff changeset
722 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
723 movlw b'00000010' ; CTRL_REG2: High Res in Active mode
0
heinrichsweikamp
parents:
diff changeset
724 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
725 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
726 rcall WaitMSSP
0
heinrichsweikamp
parents:
diff changeset
727
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
728 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
729 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
730 movlw 0x38 ; address
0
heinrichsweikamp
parents:
diff changeset
731 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
732 movlw 0x2A ; CTRL_REG1
0
heinrichsweikamp
parents:
diff changeset
733 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
734 ; movlw b'00110001' ; CTRL_REG1: 160ms data rate, Active Mode
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
735 movlw b'00110101' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode, Active Mode
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
736 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
737 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
738 bra WaitMSSP ; (And return)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
739
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
740 I2C_init_accelerometer2:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
741 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
742 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
743 movlw 0x32 ; address
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
744 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
745 movlw 0x9F ; 1F with auto-increment (MSB=1)
0
heinrichsweikamp
parents:
diff changeset
746 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
747 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
748 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
749 movlw b'01010111' ; CTRL_REG1_A (100Hz, x,y,z = ON)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
750 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
751 movlw b'00000000' ; CTRL_REG2_A
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
752 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
753 movlw b'00000000' ; CTRL_REG3_A
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
754 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
755 movlw b'00000000' ; CTRL_REG4_A (BDU=0, +/-2g,
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
756 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
757 movlw b'00000000' ; CTRL_REG5_A
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
758 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
759 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
760 bra WaitMSSP ; (And return)
0
heinrichsweikamp
parents:
diff changeset
761
heinrichsweikamp
parents:
diff changeset
762 global I2C_sleep_accelerometer
heinrichsweikamp
parents:
diff changeset
763 I2C_sleep_accelerometer:
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
764 btfsc compass_type2 ; Compass2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
765 bra I2C_sleep_accelerometer2 ; Yes
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
766 btfsc compass_type ; compass1?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
767 return ; yes, ignore
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
768
0
heinrichsweikamp
parents:
diff changeset
769 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
770 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
771 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
772 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
773 movlw 0x2A ; CTRL_REG1
heinrichsweikamp
parents:
diff changeset
774 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
775 movlw b'00000000' ; St. By Mode
heinrichsweikamp
parents:
diff changeset
776 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
777 bsf SSP1CON2,PEN ; Stop condition
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
778 bra WaitMSSP ; (And return)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
779
556
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
780 lt2942_init_again:
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
781 clrf i2c_temp
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
782 movlw 0x02 ; Point to accumulated charge registers
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
783 rcall I2C_TX_GAUGE
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
784 movff battery_acumulated_charge+1,SSP1BUF ; Data Byte
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
785 rcall WaitMSSP
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
786 rcall I2C_WaitforACK
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
787 movff battery_acumulated_charge+0,SSP1BUF ; Data Byte
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
788 rcall WaitMSSP
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
789 rcall I2C_WaitforACK
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
790 bsf SSP1CON2,PEN ; Stop condition
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
791 rcall WaitMSSP
556
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
792 movff battery_acumulated_charge+1,sub_a+1
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
793 movff battery_acumulated_charge+0,sub_a+0
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
794 ; and init again...
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
795
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
796 global lt2942_init
113
heinrichsweikamp
parents: 20
diff changeset
797 lt2942_init: ; Setup Control register B
heinrichsweikamp
parents: 20
diff changeset
798 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
799 movlw 0x01 ; Point to control reg B
467
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
800 rcall I2C_TX_GAUGE
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
801 movlw b'11111000' ; Automatic conversion every two seconds
113
heinrichsweikamp
parents: 20
diff changeset
802 movff WREG, SSP1BUF ; Data Byte
heinrichsweikamp
parents: 20
diff changeset
803 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
804 rcall I2C_WaitforACK
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
805 bsf SSP1CON2,PEN ; Stop condition
467
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
806 bra WaitMSSP ; (And return)
113
heinrichsweikamp
parents: 20
diff changeset
807
heinrichsweikamp
parents: 20
diff changeset
808 global lt2942_get_status
heinrichsweikamp
parents: 20
diff changeset
809 lt2942_get_status: ; Read status register
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
810 bcf battery_gauge_available ; Clear flag
113
heinrichsweikamp
parents: 20
diff changeset
811 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
812 movlw 0x00 ; Point to Status reg
467
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
813 rcall I2C_TX_GAUGE
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
814 rcall I2C_RX_GAUGE
113
heinrichsweikamp
parents: 20
diff changeset
815 movff SSP1BUF,WREG
467
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
816 btfss WREG,7 ; 2942 found?
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
817 bsf battery_gauge_available ; Yes, set flag
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
818 bsf SSP1CON2,PEN ; Stop condition
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
819 bra WaitMSSP ; (And return)
113
heinrichsweikamp
parents: 20
diff changeset
820
heinrichsweikamp
parents: 20
diff changeset
821
heinrichsweikamp
parents: 20
diff changeset
822 global lt2942_get_voltage
heinrichsweikamp
parents: 20
diff changeset
823 lt2942_get_voltage: ; Read battery voltage registers
heinrichsweikamp
parents: 20
diff changeset
824 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
825 movlw 0x08 ; Point to voltage registers
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
826 rcall I2C_TX_GAUGE
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
827 rcall I2C_RX_GAUGE
113
heinrichsweikamp
parents: 20
diff changeset
828 bsf SSP1CON2,ACKEN ; Master acknowlegde
heinrichsweikamp
parents: 20
diff changeset
829 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
830 movff SSP1BUF,xA+1
heinrichsweikamp
parents: 20
diff changeset
831 bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents: 20
diff changeset
832 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
833 movff SSP1BUF,xA+0
heinrichsweikamp
parents: 20
diff changeset
834 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 20
diff changeset
835 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
836
heinrichsweikamp
parents: 20
diff changeset
837 ; banksel common
heinrichsweikamp
parents: 20
diff changeset
838 ; xA:2 loaded with raw values
heinrichsweikamp
parents: 20
diff changeset
839 movlw LOW .6000
heinrichsweikamp
parents: 20
diff changeset
840 movwf xB+0
heinrichsweikamp
parents: 20
diff changeset
841 movlw HIGH .6000
heinrichsweikamp
parents: 20
diff changeset
842 movwf xB+1
heinrichsweikamp
parents: 20
diff changeset
843 call mult16x16 ;xA*xB=xC
heinrichsweikamp
parents: 20
diff changeset
844
heinrichsweikamp
parents: 20
diff changeset
845 ; devide xC (32bit)/65535 for result in mV (16bit)
heinrichsweikamp
parents: 20
diff changeset
846 movlw .16
heinrichsweikamp
parents: 20
diff changeset
847 movwf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
848 lt2942_get_voltage2:
heinrichsweikamp
parents: 20
diff changeset
849 bcf STATUS,C
heinrichsweikamp
parents: 20
diff changeset
850 rrcf xC+3,F
heinrichsweikamp
parents: 20
diff changeset
851 rrcf xC+2,F
heinrichsweikamp
parents: 20
diff changeset
852 rrcf xC+1,F
heinrichsweikamp
parents: 20
diff changeset
853 rrcf xC+0,F
heinrichsweikamp
parents: 20
diff changeset
854 decfsz i2c_temp,F
heinrichsweikamp
parents: 20
diff changeset
855 bra lt2942_get_voltage2
heinrichsweikamp
parents: 20
diff changeset
856
heinrichsweikamp
parents: 20
diff changeset
857 ; Update battery voltage in mV
heinrichsweikamp
parents: 20
diff changeset
858 movff xC+1,batt_voltage+1
heinrichsweikamp
parents: 20
diff changeset
859 movff xC+0,batt_voltage+0
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
860
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
861 tstfsz batt_voltage+1 ; <256mV?
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
862 return ; No, done.
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
863
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
864 bra lt2942_init ;(and return)
113
heinrichsweikamp
parents: 20
diff changeset
865
heinrichsweikamp
parents: 20
diff changeset
866 ; global lt2942_get_temperature
heinrichsweikamp
parents: 20
diff changeset
867 ;lt2942_get_temperature: ; Read temperature registers
heinrichsweikamp
parents: 20
diff changeset
868 ; clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
869 ; movlw 0x0C ; Point to temperature registers
heinrichsweikamp
parents: 20
diff changeset
870 ; call I2C_TX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
871 ; call I2C_RX
heinrichsweikamp
parents: 20
diff changeset
872 ; bsf SSP1CON2,ACKEN ; Master acknowlegde
heinrichsweikamp
parents: 20
diff changeset
873 ; rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
874 ; movff SSP1BUF,xA+1
heinrichsweikamp
parents: 20
diff changeset
875 ; bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents: 20
diff changeset
876 ; rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
877 ; movff SSP1BUF,xA+0
heinrichsweikamp
parents: 20
diff changeset
878 ; bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 20
diff changeset
879 ; rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
880 ;
heinrichsweikamp
parents: 20
diff changeset
881 ;; banksel common
heinrichsweikamp
parents: 20
diff changeset
882 ; ; xA:2 loaded with raw values
heinrichsweikamp
parents: 20
diff changeset
883 ; movlw LOW .6000
heinrichsweikamp
parents: 20
diff changeset
884 ; movwf xB+0
heinrichsweikamp
parents: 20
diff changeset
885 ; movlw HIGH .6000
heinrichsweikamp
parents: 20
diff changeset
886 ; movwf xB+1
heinrichsweikamp
parents: 20
diff changeset
887 ; call mult16x16 ;xA*xB=xC
heinrichsweikamp
parents: 20
diff changeset
888 ;
heinrichsweikamp
parents: 20
diff changeset
889 ; ; devide xC (32bit)/65535 for result in 0.1K (16bit)
heinrichsweikamp
parents: 20
diff changeset
890 ; movlw .16
heinrichsweikamp
parents: 20
diff changeset
891 ; movwf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
892 ;lt2942_get_temperature2:
heinrichsweikamp
parents: 20
diff changeset
893 ; bcf STATUS,C
heinrichsweikamp
parents: 20
diff changeset
894 ; rrcf xC+3,F
heinrichsweikamp
parents: 20
diff changeset
895 ; rrcf xC+2,F
heinrichsweikamp
parents: 20
diff changeset
896 ; rrcf xC+1,F
heinrichsweikamp
parents: 20
diff changeset
897 ; rrcf xC+0,F
heinrichsweikamp
parents: 20
diff changeset
898 ; decfsz i2c_temp,F
heinrichsweikamp
parents: 20
diff changeset
899 ; bra lt2942_get_temperature2
heinrichsweikamp
parents: 20
diff changeset
900 ;
heinrichsweikamp
parents: 20
diff changeset
901 ; movff xC+1,sub_a+1
heinrichsweikamp
parents: 20
diff changeset
902 ; movff xC+0,sub_a+0
heinrichsweikamp
parents: 20
diff changeset
903 ; movlw LOW .2731 ; Kelvin to Celcius offset
heinrichsweikamp
parents: 20
diff changeset
904 ; movwf sub_b+0
heinrichsweikamp
parents: 20
diff changeset
905 ; movlw HIGH .2731 ; Kelvin to Celcius offset
heinrichsweikamp
parents: 20
diff changeset
906 ; movwf sub_b+1
heinrichsweikamp
parents: 20
diff changeset
907 ; call subU16 ; sub_c = sub_a - sub_b (with UNSIGNED values)
heinrichsweikamp
parents: 20
diff changeset
908 ;
heinrichsweikamp
parents: 20
diff changeset
909 ; ; Update batttery_temperature in 0.1°C
heinrichsweikamp
parents: 20
diff changeset
910 ; movff sub_c+1,battery_temperature+1
heinrichsweikamp
parents: 20
diff changeset
911 ; movff sub_c+0,battery_temperature+0
heinrichsweikamp
parents: 20
diff changeset
912 ; return
heinrichsweikamp
parents: 20
diff changeset
913
heinrichsweikamp
parents: 20
diff changeset
914 global lt2942_get_accumulated_charge
heinrichsweikamp
parents: 20
diff changeset
915 lt2942_get_accumulated_charge: ; Read accumulated charge and compute percent
heinrichsweikamp
parents: 20
diff changeset
916 clrf i2c_temp
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
917 movlw 0x00 ; Point to status register
467
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
918 rcall I2C_TX_GAUGE
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
919 rcall I2C_RX_GAUGE
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
920 bsf SSP1CON2,ACKEN ; Master acknowlegde
113
heinrichsweikamp
parents: 20
diff changeset
921 rcall WaitMSSP
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
922 movff SSP1BUF,gauge_status_byte
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
923
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
924 bsf SSP1CON2, RCEN ; Enable recieve mode
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
925 rcall WaitMSSP ; Dummy read (Control byte)
467
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
926 movf SSP1BUF,W
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
927 bsf SSP1CON2,ACKEN ; Master acknowlegde
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
928 rcall WaitMSSP
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
929
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
930 bsf SSP1CON2, RCEN ; Enable recieve mode
113
heinrichsweikamp
parents: 20
diff changeset
931 rcall WaitMSSP
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
932 movff SSP1BUF,sub_a+1
467
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
933 bsf SSP1CON2,ACKEN ; Master acknowlegde
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
934 rcall WaitMSSP
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
935
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
936 bsf SSP1CON2, RCEN ; Enable recieve mode
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
937 rcall WaitMSSP
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
938 movff SSP1BUF,sub_a+0
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
939 bsf SSP1CON2,PEN ; Stop condition
113
heinrichsweikamp
parents: 20
diff changeset
940 rcall WaitMSSP
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
941
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
942 movff gauge_status_byte,sub_b+0 ; copy into bank common
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
943 btfsc sub_b+0,0 ; =1: UVLO Event
556
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
944 rcall lt2942_init_again
113
heinrichsweikamp
parents: 20
diff changeset
945
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
946 movff sub_a+1,battery_acumulated_charge+1 ; Save raw value
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
947 movff sub_a+0,battery_acumulated_charge+0 ; Save raw value
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
948
113
heinrichsweikamp
parents: 20
diff changeset
949 ; Compute batt_percent
211
heinrichsweikamp
parents: 200
diff changeset
950 ; (charge-battery_offset)/365
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
951 movff battery_offset+0,sub_b+0
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
952 movff battery_offset+1,sub_b+1
113
heinrichsweikamp
parents: 20
diff changeset
953 call subU16 ; sub_c = sub_a - sub_b (with signed values)
heinrichsweikamp
parents: 20
diff changeset
954
heinrichsweikamp
parents: 20
diff changeset
955 clrf batt_percent ; Set to zero
heinrichsweikamp
parents: 20
diff changeset
956 btfsc neg_flag ; result negative?
449
heinrichsweikamp
parents: 448
diff changeset
957 bra lt2942_set_to_zero_percent ; Yes, keep LT2942 at zero percent and return
113
heinrichsweikamp
parents: 20
diff changeset
958
heinrichsweikamp
parents: 20
diff changeset
959 ; > Zero, set batt_percent properly
heinrichsweikamp
parents: 20
diff changeset
960 movff sub_c+0,xA+0
heinrichsweikamp
parents: 20
diff changeset
961 movff sub_c+1,xA+1
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
962 movff battery_capacity+0,xB+0
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
963 movff battery_capacity+1,xB+1
113
heinrichsweikamp
parents: 20
diff changeset
964 call div16x16 ;xA/xB=xC with xA+0 as remainder, uses divB as temp variable
heinrichsweikamp
parents: 20
diff changeset
965 movff xC+0,batt_percent
heinrichsweikamp
parents: 20
diff changeset
966 return
heinrichsweikamp
parents: 20
diff changeset
967
449
heinrichsweikamp
parents: 448
diff changeset
968 lt2942_set_to_zero_percent:
heinrichsweikamp
parents: 448
diff changeset
969 clrf i2c_temp
heinrichsweikamp
parents: 448
diff changeset
970 movlw 0x02 ; Point to accumulated charge registers
heinrichsweikamp
parents: 448
diff changeset
971 rcall I2C_TX_GAUGE
heinrichsweikamp
parents: 448
diff changeset
972 movff battery_offset+1,SSP1BUF
heinrichsweikamp
parents: 448
diff changeset
973 rcall WaitMSSP
heinrichsweikamp
parents: 448
diff changeset
974 rcall I2C_WaitforACK
heinrichsweikamp
parents: 448
diff changeset
975 movff battery_offset+0,SSP1BUF
heinrichsweikamp
parents: 448
diff changeset
976 rcall WaitMSSP
heinrichsweikamp
parents: 448
diff changeset
977 rcall I2C_WaitforACK
heinrichsweikamp
parents: 448
diff changeset
978 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 448
diff changeset
979 bra WaitMSSP; (and return)
heinrichsweikamp
parents: 448
diff changeset
980
113
heinrichsweikamp
parents: 20
diff changeset
981 global lt2942_charge_done
heinrichsweikamp
parents: 20
diff changeset
982 lt2942_charge_done: ; Reset accumulating registers to 0xFFFF
heinrichsweikamp
parents: 20
diff changeset
983 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
984 movlw 0x02 ; Point to accumulated charge registers
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
985 rcall I2C_TX_GAUGE
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
986 setf SSP1BUF ; Data Byte
113
heinrichsweikamp
parents: 20
diff changeset
987 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
988 rcall I2C_WaitforACK
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
989 setf SSP1BUF ; Data Byte
113
heinrichsweikamp
parents: 20
diff changeset
990 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
991 rcall I2C_WaitforACK
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
992 bsf SSP1CON2,PEN ; Stop condition
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
993 bra WaitMSSP; (and return)
113
heinrichsweikamp
parents: 20
diff changeset
994
heinrichsweikamp
parents: 20
diff changeset
995 I2C_TX_GAUGE: ; Sends a byte to the LT2942 Gauge IC
heinrichsweikamp
parents: 20
diff changeset
996 movwf i2c_temp+1 ; Data byte
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
997 bsf SSP1CON2,SEN ; Start condition
113
heinrichsweikamp
parents: 20
diff changeset
998 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
999 movlw b'11001000' ; Address byte + Write bit
heinrichsweikamp
parents: 20
diff changeset
1000 movwf SSP1BUF ; control byte
heinrichsweikamp
parents: 20
diff changeset
1001 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
1002 rcall I2C_WaitforACK
heinrichsweikamp
parents: 20
diff changeset
1003 movff i2c_temp+1, SSP1BUF ; Data Byte
heinrichsweikamp
parents: 20
diff changeset
1004 rcall WaitMSSP
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
1005 bra I2C_WaitforACK ; (and return)
113
heinrichsweikamp
parents: 20
diff changeset
1006
heinrichsweikamp
parents: 20
diff changeset
1007 I2C_RX_GAUGE:
heinrichsweikamp
parents: 20
diff changeset
1008 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents: 20
diff changeset
1009 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
1010 movlw b'11001001' ; Address byte + Read bit
heinrichsweikamp
parents: 20
diff changeset
1011 movwf SSP1BUF ; control byte
heinrichsweikamp
parents: 20
diff changeset
1012 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
1013 rcall I2C_WaitforACK
467
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
1014 bsf SSP1CON2, RCEN ; Enable recieve mode
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
1015 bra WaitMSSP; (and return)
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1016
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1017
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1018 global I2C_probe_OSTC_rx
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1019 I2C_probe_OSTC_rx:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1020 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1021 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1022 movlw 0x50 ; Address byte + Write bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1023 movwf SSP1BUF ; control byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1024 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1025 btfss SSP1CON2,ACKSTAT ; ACK?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1026 bsf ostc_rx_present ; ACK send. OSTC_RX present!
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1027 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1028 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1029 btfss ostc_rx_present ; Do we have the RX?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1030 return ; No, Done.
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1031 WAITMS .1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1032 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1033 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1034 movlw 0x50 ; Address byte + Write bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1035 movwf SSP1BUF ; control byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1036 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1037 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1038 movlw 0x1B
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1039 movwf SSP1BUF ; Data Byte (Get firmware)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1040 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1041 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1042 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1043 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1044 WAITMS .1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1045 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1046 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1047 movlw 0x51 ; Address byte + Read bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1048 movwf SSP1BUF ; control byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1049 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1050 bsf SSP1CON2, RCEN ; Enable recieve mode
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1051 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1052 movff SSP1BUF,rx_firmware+0
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1053 bsf SSP1CON2,ACKEN ; Master acknowlegde
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1054 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1055
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1056 ; last byte in read from RX circuity always with a NACK!
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1057 bsf SSP1CON2, RCEN ; Enable recieve mode
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1058 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1059 movff SSP1BUF,rx_firmware+1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1060 bsf SSP1CON2,ACKDT
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1061 bsf SSP1CON2,ACKEN ; Master NOT acknowlegde
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1062 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1063 bcf SSP1CON2,ACKDT ; Reset ACKDT flag
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1064 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1065 bra WaitMSSP ;(and return)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1066
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1067
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1068 global I2C_get_tankdata
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1069 I2C_get_tankdata:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1070 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1071 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1072 movlw 0x50 ; Address byte + Write bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1073 movwf SSP1BUF ; control byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1074 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1075 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1076 movlw 0x1E ; Read buffer2 (48 Bytes)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1077 movwf SSP1BUF ; Data Byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1078 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1079 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1080 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1081 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1082 WAITMS .1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1083
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1084 ; read 48 bytes
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1085 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1086 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1087 movlw 0x51 ; Address byte + read bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1088 movwf SSP1BUF ; control byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1089 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1090 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1091 movlw .47 ; 47 with ACK + 1 w/o ACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1092 movwf temp1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1093 lfsr FSR2,rx_buffer+0
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1094 I2C_get_tankdata_loop_read:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1095 bsf SSP1CON2, RCEN ; Enable recieve mode
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1096 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1097 movff SSP1BUF,POSTINC2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1098 bcf SSP1CON2,ACKDT
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1099 bsf SSP1CON2,ACKEN ; Master acknowlegde
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1100 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1101 decfsz temp1,F
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1102 bra I2C_get_tankdata_loop_read
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1103
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1104 ; 1 w/o ACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1105 bsf SSP1CON2, RCEN ; Enable recieve mode
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1106 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1107 movff SSP1BUF,POSTINC2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1108 bsf SSP1CON2,ACKDT
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1109 bsf SSP1CON2,ACKEN ; Master NOT acknowlegde
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1110 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1111 bcf SSP1CON2,ACKDT ; Reset ACKDT flag
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1112
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1113 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1114 bra WaitMSSP ;(and return)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1115
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1116
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1117 global I2C_update_OSTC_rx
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1118 I2C_update_OSTC_rx: ; 992*64byte master loop
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1119 bcf i2c_error_flag ; clear error flag
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1120 ; write 64 bytes
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1121 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1122 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1123 movlw 0x50 ; Address byte + Write bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1124 movwf SSP1BUF ; control byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1125 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1126 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1127 lfsr FSR2,buffer ; send buffer for verify
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1128 movlw .64
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1129 movwf temp1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1130 I2C_update_OSTC_loop: ; 64byte flash page loop
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1131 movff up,POSTINC2 ; store for verify
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1132 movff up,SSP1BUF
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1133 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1134 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1135 call ext_flash_read_block ; Read one byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1136 movwf up ; prepare for transmit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1137 decfsz temp1,F
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1138 bra I2C_update_OSTC_loop
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1139 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1140 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1141 WAITMS .1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1142
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1143 ; read 64 bytes
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1144 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1145 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1146 movlw 0x51 ; Address byte + read bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1147 movwf SSP1BUF ; control byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1148 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1149 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1150 lfsr FSR2,buffer ; send buffer for verify
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1151 movlw .63 ; 63 with ACK + 1 w/o ACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1152 movwf temp1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1153 I2C_update_OSTC_loop_read:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1154 bsf SSP1CON2, RCEN ; Enable recieve mode
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1155 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1156 movf SSP1BUF,W
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1157 cpfseq POSTINC2 ; compare readback with original
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1158 bsf i2c_error_flag ; Not equal, set flag
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1159 bcf SSP1CON2,ACKDT
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1160 bsf SSP1CON2,ACKEN ; Master acknowlegde
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1161 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1162 decfsz temp1,F
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1163 bra I2C_update_OSTC_loop_read
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1164
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1165 ; 1 w/o ACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1166 bsf SSP1CON2, RCEN ; Enable recieve mode
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1167 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1168 movf SSP1BUF,W
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1169 cpfseq POSTINC2 ; compare readback with original
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1170 bsf i2c_error_flag ; Not equal, set flag
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1171 bsf SSP1CON2,ACKDT
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1172 bsf SSP1CON2,ACKEN ; Master NOT acknowlegde
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1173 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1174 bcf SSP1CON2,ACKDT ; Reset ACKDT flag
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1175
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1176 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1177 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1178 WAITMS .1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1179
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1180 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1181 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1182 movlw 0x50 ; Address byte + Write bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1183 movwf SSP1BUF ; control byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1184 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1185 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1186 movlw 0x1F ; Write command!
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1187 movwf SSP1BUF ; Data Byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1188 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1189 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1190 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1191 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1192 WAITMS .5 ; Required waiting time
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1193
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1194 btfss i2c_error_flag
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1195 retlw .0 ; All ok
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1196 retlw .255 ; an error occured
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
1197 END