annotate src/i2c.asm @ 575:bf34a2f784ab

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