annotate src/i2c.asm @ 571:1a7957306d49

French updates
author heinrichsweikamp
date Sun, 11 Feb 2018 12:20:59 +0100
parents 0ba88db66492
children b8f45b57302d
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:
498
e8351dd7bb90 compass1 full scale gain test
heinrichsweikamp
parents: 467
diff changeset
594 movlw b'01100000' ; CTRL6 Full scale (+/-12 Gauss -> 2730LSB/Gauss)
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
595 ;movlw b'00000000' ; CTRL6 (+/-2 Gauss)
498
e8351dd7bb90 compass1 full scale gain test
heinrichsweikamp
parents: 467
diff changeset
596 ;movlw b'00100000' ; CTRL6 (+/-4 Gauss)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
597 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
598 movlw b'00000000' ; CTRL7 Continuous Mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
599 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
600 bsf SSP1CON2,PEN ; Stop condition
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
601 bra WaitMSSP ; (And return)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
602
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
603 I2C_init_compass2:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
604 bsf SSP1CON2,SEN ; Start condition
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
605 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
606 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
607 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
608 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
609 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
610 movlw b'00000000' ; CFG_REG_A_M (10Hz, Continuous)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
611 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
612 movlw b'00000000' ; CFG_REG_B_M (Low-Pass Filter off)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
613 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
614 movlw b'00000000' ; CFG_REG_C_M BDU=0
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
615 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
616 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
617 bra WaitMSSP ;(And return)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
618
0
heinrichsweikamp
parents:
diff changeset
619 global I2C_sleep_compass
heinrichsweikamp
parents:
diff changeset
620 I2C_sleep_compass:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
621 bcf compass_enabled
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
622 btfsc compass_type2 ; compass2?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
623 bra I2C_sleep_compass2 ; yes
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
624 btfsc compass_type ; compass1?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
625 bra I2C_sleep_compass1 ; yes
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
626 I2C_sleep_compass0:
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
627 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
628 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
629 movlw 0x3C ; address
0
heinrichsweikamp
parents:
diff changeset
630 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
631 movlw 0x00
0
heinrichsweikamp
parents:
diff changeset
632 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
633 movlw b'01101000' ; ConfigA
0
heinrichsweikamp
parents:
diff changeset
634 rcall I2C_TX
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
635 movlw b'00100000' ; ConfigB
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
636 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
637 movlw b'00000010' ; Idle Mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
638 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
639 bsf SSP1CON2,PEN ; Stop condition
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
640 bra WaitMSSP ; (And return)
0
heinrichsweikamp
parents:
diff changeset
641
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
642 I2C_sleep_compass1:
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
643 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
644 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
645 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
646 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
647 movlw 0x20 ; CTRL_REG1
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
648 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
649 movlw b'00000000' ; data for CTRL_REG1: acceleration sensor Power-down mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
650 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
651 bsf SSP1CON2,PEN ; Stop condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
652 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
653 bsf SSP1CON2,SEN ; Start condition
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
654 rcall WaitMSSP
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
655 movlw 0x3C ; address
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
656 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
657 movlw 0x26 ; CTRL_REG7
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
658 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
659 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor Power-down mode
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
660 rcall I2C_TX
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
661 bsf SSP1CON2,PEN ; Stop condition
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
662 bra WaitMSSP ;(And return)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
663
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
664
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
665 I2C_sleep_compass2:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
666 ; magnetic
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
667 bsf SSP1CON2,SEN ; Start condition
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
668 rcall WaitMSSP
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
669 movlw 0x3C ; address
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
670 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
671 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
672 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
673 movlw b'00000010' ; CFG_REG_A_M (Idle mode)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
674 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
675 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
676 bra WaitMSSP ; (And return)
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
677
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
678 I2C_sleep_accelerometer2:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
679 ; accelerometer
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
680 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
681 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
682 movlw 0x32 ; 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 0x9F ; 1F 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'00000000' ; TEMP_CFG_REG_A (Temp sensor off)
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 movlw b'00000000' ; CTRL_REG1_A (All off)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
689 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
690 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
691 bra WaitMSSP ; (And return)
0
heinrichsweikamp
parents:
diff changeset
692
heinrichsweikamp
parents:
diff changeset
693 global I2C_init_accelerometer
heinrichsweikamp
parents:
diff changeset
694 I2C_init_accelerometer:
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
695 btfsc compass_type2 ; compass2?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
696 bra I2C_init_accelerometer2 ; Yes.
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
697
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
698 btfsc compass_type ; compass1?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
699 return ; yes, ignore
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
700
0
heinrichsweikamp
parents:
diff changeset
701 rcall I2C_sleep_accelerometer ; Regs can only be changed in St.By mode
heinrichsweikamp
parents:
diff changeset
702
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
703 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
704 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
705 movlw 0x38 ; address
0
heinrichsweikamp
parents:
diff changeset
706 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
707 movlw 0x0E ; XYZ_DATA_CFG
0
heinrichsweikamp
parents:
diff changeset
708 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
709 movlw b'00000000' ; High pass Filter=0 , +/- 2g range
0
heinrichsweikamp
parents:
diff changeset
710 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
711 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
712 rcall WaitMSSP
0
heinrichsweikamp
parents:
diff changeset
713
heinrichsweikamp
parents:
diff changeset
714
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
715 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
716 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
717 movlw 0x38 ; address
0
heinrichsweikamp
parents:
diff changeset
718 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
719 movlw 0x2A ; CTRL_REG1
0
heinrichsweikamp
parents:
diff changeset
720 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
721 ; movlw b'00110000' ; CTRL_REG1: 160ms data rate, St.By Mode
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
722 movlw b'00110100' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode
0
heinrichsweikamp
parents:
diff changeset
723 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
724 movlw b'00000010' ; CTRL_REG2: High Res in Active mode
0
heinrichsweikamp
parents:
diff changeset
725 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
726 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
727 rcall WaitMSSP
0
heinrichsweikamp
parents:
diff changeset
728
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
729 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
730 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
731 movlw 0x38 ; address
0
heinrichsweikamp
parents:
diff changeset
732 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
733 movlw 0x2A ; CTRL_REG1
0
heinrichsweikamp
parents:
diff changeset
734 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
735 ; movlw b'00110001' ; CTRL_REG1: 160ms data rate, Active Mode
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
736 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
737 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
738 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
739 bra WaitMSSP ; (And return)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
740
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
741 I2C_init_accelerometer2:
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 0x32 ; address
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
745 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
746 movlw 0x9F ; 1F with auto-increment (MSB=1)
0
heinrichsweikamp
parents:
diff changeset
747 rcall I2C_TX
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
748 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
749 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
750 movlw b'01010111' ; CTRL_REG1_A (100Hz, x,y,z = ON)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
751 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
752 movlw b'00000000' ; CTRL_REG2_A
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
753 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
754 movlw b'00000000' ; CTRL_REG3_A
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
755 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
756 movlw b'00000000' ; CTRL_REG4_A (BDU=0, +/-2g,
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
757 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
758 movlw b'00000000' ; CTRL_REG5_A
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
759 rcall I2C_TX
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
760 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
761 bra WaitMSSP ; (And return)
0
heinrichsweikamp
parents:
diff changeset
762
heinrichsweikamp
parents:
diff changeset
763 global I2C_sleep_accelerometer
heinrichsweikamp
parents:
diff changeset
764 I2C_sleep_accelerometer:
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
765 btfsc compass_type2 ; Compass2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
766 bra I2C_sleep_accelerometer2 ; Yes
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
767 btfsc compass_type ; compass1?
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
768 return ; yes, ignore
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
769
0
heinrichsweikamp
parents:
diff changeset
770 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents:
diff changeset
771 rcall WaitMSSP
heinrichsweikamp
parents:
diff changeset
772 movlw 0x38 ; address
heinrichsweikamp
parents:
diff changeset
773 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
774 movlw 0x2A ; CTRL_REG1
heinrichsweikamp
parents:
diff changeset
775 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
776 movlw b'00000000' ; St. By Mode
heinrichsweikamp
parents:
diff changeset
777 rcall I2C_TX
heinrichsweikamp
parents:
diff changeset
778 bsf SSP1CON2,PEN ; Stop condition
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
779 bra WaitMSSP ; (And return)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
780
556
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
781 lt2942_init_again:
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
782 clrf i2c_temp
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
783 movlw 0x02 ; Point to accumulated charge registers
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
784 rcall I2C_TX_GAUGE
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
785 movff battery_acumulated_charge+1,SSP1BUF ; Data Byte
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
786 rcall WaitMSSP
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
787 rcall I2C_WaitforACK
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
788 movff battery_acumulated_charge+0,SSP1BUF ; Data Byte
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
789 rcall WaitMSSP
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
790 rcall I2C_WaitforACK
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
791 bsf SSP1CON2,PEN ; Stop condition
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
792 rcall WaitMSSP
556
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
793 movff battery_acumulated_charge+1,sub_a+1
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
794 movff battery_acumulated_charge+0,sub_a+0
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
795 ; and init again...
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
796
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
797 global lt2942_init
113
heinrichsweikamp
parents: 20
diff changeset
798 lt2942_init: ; Setup Control register B
heinrichsweikamp
parents: 20
diff changeset
799 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
800 movlw 0x01 ; Point to control reg B
467
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
801 rcall I2C_TX_GAUGE
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
802 movlw b'11111000' ; Automatic conversion every two seconds
113
heinrichsweikamp
parents: 20
diff changeset
803 movff WREG, SSP1BUF ; Data Byte
heinrichsweikamp
parents: 20
diff changeset
804 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
805 rcall I2C_WaitforACK
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
806 bsf SSP1CON2,PEN ; Stop condition
467
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
807 bra WaitMSSP ; (And return)
113
heinrichsweikamp
parents: 20
diff changeset
808
heinrichsweikamp
parents: 20
diff changeset
809 global lt2942_get_status
heinrichsweikamp
parents: 20
diff changeset
810 lt2942_get_status: ; Read status register
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
811 bcf battery_gauge_available ; Clear flag
113
heinrichsweikamp
parents: 20
diff changeset
812 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
813 movlw 0x00 ; Point to Status reg
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 rcall I2C_RX_GAUGE
113
heinrichsweikamp
parents: 20
diff changeset
816 movff SSP1BUF,WREG
467
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
817 btfss WREG,7 ; 2942 found?
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
818 bsf battery_gauge_available ; Yes, set flag
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
819 bsf SSP1CON2,PEN ; Stop condition
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
heinrichsweikamp
parents: 20
diff changeset
823 global lt2942_get_voltage
heinrichsweikamp
parents: 20
diff changeset
824 lt2942_get_voltage: ; Read battery voltage registers
heinrichsweikamp
parents: 20
diff changeset
825 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
826 movlw 0x08 ; Point to voltage registers
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
827 rcall I2C_TX_GAUGE
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
828 rcall I2C_RX_GAUGE
113
heinrichsweikamp
parents: 20
diff changeset
829 bsf SSP1CON2,ACKEN ; Master acknowlegde
heinrichsweikamp
parents: 20
diff changeset
830 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
831 movff SSP1BUF,xA+1
heinrichsweikamp
parents: 20
diff changeset
832 bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents: 20
diff changeset
833 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
834 movff SSP1BUF,xA+0
heinrichsweikamp
parents: 20
diff changeset
835 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 20
diff changeset
836 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
837
heinrichsweikamp
parents: 20
diff changeset
838 ; banksel common
heinrichsweikamp
parents: 20
diff changeset
839 ; xA:2 loaded with raw values
heinrichsweikamp
parents: 20
diff changeset
840 movlw LOW .6000
heinrichsweikamp
parents: 20
diff changeset
841 movwf xB+0
heinrichsweikamp
parents: 20
diff changeset
842 movlw HIGH .6000
heinrichsweikamp
parents: 20
diff changeset
843 movwf xB+1
heinrichsweikamp
parents: 20
diff changeset
844 call mult16x16 ;xA*xB=xC
heinrichsweikamp
parents: 20
diff changeset
845
heinrichsweikamp
parents: 20
diff changeset
846 ; devide xC (32bit)/65535 for result in mV (16bit)
heinrichsweikamp
parents: 20
diff changeset
847 movlw .16
heinrichsweikamp
parents: 20
diff changeset
848 movwf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
849 lt2942_get_voltage2:
heinrichsweikamp
parents: 20
diff changeset
850 bcf STATUS,C
heinrichsweikamp
parents: 20
diff changeset
851 rrcf xC+3,F
heinrichsweikamp
parents: 20
diff changeset
852 rrcf xC+2,F
heinrichsweikamp
parents: 20
diff changeset
853 rrcf xC+1,F
heinrichsweikamp
parents: 20
diff changeset
854 rrcf xC+0,F
heinrichsweikamp
parents: 20
diff changeset
855 decfsz i2c_temp,F
heinrichsweikamp
parents: 20
diff changeset
856 bra lt2942_get_voltage2
heinrichsweikamp
parents: 20
diff changeset
857
heinrichsweikamp
parents: 20
diff changeset
858 ; Update battery voltage in mV
heinrichsweikamp
parents: 20
diff changeset
859 movff xC+1,batt_voltage+1
heinrichsweikamp
parents: 20
diff changeset
860 movff xC+0,batt_voltage+0
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
861
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
862 tstfsz batt_voltage+1 ; <256mV?
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
863 return ; No, done.
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
864
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
865 bra lt2942_init ;(and return)
113
heinrichsweikamp
parents: 20
diff changeset
866
heinrichsweikamp
parents: 20
diff changeset
867 ; global lt2942_get_temperature
heinrichsweikamp
parents: 20
diff changeset
868 ;lt2942_get_temperature: ; Read temperature registers
heinrichsweikamp
parents: 20
diff changeset
869 ; clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
870 ; movlw 0x0C ; Point to temperature registers
heinrichsweikamp
parents: 20
diff changeset
871 ; call I2C_TX_GAUGE
heinrichsweikamp
parents: 20
diff changeset
872 ; call I2C_RX
heinrichsweikamp
parents: 20
diff changeset
873 ; bsf SSP1CON2,ACKEN ; Master acknowlegde
heinrichsweikamp
parents: 20
diff changeset
874 ; rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
875 ; movff SSP1BUF,xA+1
heinrichsweikamp
parents: 20
diff changeset
876 ; bsf SSP1CON2, RCEN ; Enable recieve mode
heinrichsweikamp
parents: 20
diff changeset
877 ; rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
878 ; movff SSP1BUF,xA+0
heinrichsweikamp
parents: 20
diff changeset
879 ; bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 20
diff changeset
880 ; rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
881 ;
heinrichsweikamp
parents: 20
diff changeset
882 ;; banksel common
heinrichsweikamp
parents: 20
diff changeset
883 ; ; xA:2 loaded with raw values
heinrichsweikamp
parents: 20
diff changeset
884 ; movlw LOW .6000
heinrichsweikamp
parents: 20
diff changeset
885 ; movwf xB+0
heinrichsweikamp
parents: 20
diff changeset
886 ; movlw HIGH .6000
heinrichsweikamp
parents: 20
diff changeset
887 ; movwf xB+1
heinrichsweikamp
parents: 20
diff changeset
888 ; call mult16x16 ;xA*xB=xC
heinrichsweikamp
parents: 20
diff changeset
889 ;
heinrichsweikamp
parents: 20
diff changeset
890 ; ; devide xC (32bit)/65535 for result in 0.1K (16bit)
heinrichsweikamp
parents: 20
diff changeset
891 ; movlw .16
heinrichsweikamp
parents: 20
diff changeset
892 ; movwf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
893 ;lt2942_get_temperature2:
heinrichsweikamp
parents: 20
diff changeset
894 ; bcf STATUS,C
heinrichsweikamp
parents: 20
diff changeset
895 ; rrcf xC+3,F
heinrichsweikamp
parents: 20
diff changeset
896 ; rrcf xC+2,F
heinrichsweikamp
parents: 20
diff changeset
897 ; rrcf xC+1,F
heinrichsweikamp
parents: 20
diff changeset
898 ; rrcf xC+0,F
heinrichsweikamp
parents: 20
diff changeset
899 ; decfsz i2c_temp,F
heinrichsweikamp
parents: 20
diff changeset
900 ; bra lt2942_get_temperature2
heinrichsweikamp
parents: 20
diff changeset
901 ;
heinrichsweikamp
parents: 20
diff changeset
902 ; movff xC+1,sub_a+1
heinrichsweikamp
parents: 20
diff changeset
903 ; movff xC+0,sub_a+0
heinrichsweikamp
parents: 20
diff changeset
904 ; movlw LOW .2731 ; Kelvin to Celcius offset
heinrichsweikamp
parents: 20
diff changeset
905 ; movwf sub_b+0
heinrichsweikamp
parents: 20
diff changeset
906 ; movlw HIGH .2731 ; Kelvin to Celcius offset
heinrichsweikamp
parents: 20
diff changeset
907 ; movwf sub_b+1
heinrichsweikamp
parents: 20
diff changeset
908 ; call subU16 ; sub_c = sub_a - sub_b (with UNSIGNED values)
heinrichsweikamp
parents: 20
diff changeset
909 ;
heinrichsweikamp
parents: 20
diff changeset
910 ; ; Update batttery_temperature in 0.1°C
heinrichsweikamp
parents: 20
diff changeset
911 ; movff sub_c+1,battery_temperature+1
heinrichsweikamp
parents: 20
diff changeset
912 ; movff sub_c+0,battery_temperature+0
heinrichsweikamp
parents: 20
diff changeset
913 ; return
heinrichsweikamp
parents: 20
diff changeset
914
heinrichsweikamp
parents: 20
diff changeset
915 global lt2942_get_accumulated_charge
heinrichsweikamp
parents: 20
diff changeset
916 lt2942_get_accumulated_charge: ; Read accumulated charge and compute percent
heinrichsweikamp
parents: 20
diff changeset
917 clrf i2c_temp
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
918 movlw 0x00 ; Point to status register
467
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
919 rcall I2C_TX_GAUGE
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
920 rcall I2C_RX_GAUGE
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
921 bsf SSP1CON2,ACKEN ; Master acknowlegde
113
heinrichsweikamp
parents: 20
diff changeset
922 rcall WaitMSSP
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
923 movff SSP1BUF,gauge_status_byte
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
924
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
925 bsf SSP1CON2, RCEN ; Enable recieve mode
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
926 rcall WaitMSSP ; Dummy read (Control byte)
467
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
927 movf SSP1BUF,W
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
928 bsf SSP1CON2,ACKEN ; Master acknowlegde
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
929 rcall WaitMSSP
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
930
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
931 bsf SSP1CON2, RCEN ; Enable recieve mode
113
heinrichsweikamp
parents: 20
diff changeset
932 rcall WaitMSSP
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
933 movff SSP1BUF,sub_a+1
467
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
934 bsf SSP1CON2,ACKEN ; Master acknowlegde
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
935 rcall WaitMSSP
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
936
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
937 bsf SSP1CON2, RCEN ; Enable recieve mode
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
938 rcall WaitMSSP
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
939 movff SSP1BUF,sub_a+0
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
940 bsf SSP1CON2,PEN ; Stop condition
113
heinrichsweikamp
parents: 20
diff changeset
941 rcall WaitMSSP
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
942
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
943 movff gauge_status_byte,sub_b+0 ; copy into bank common
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
944 btfsc sub_b+0,0 ; =1: UVLO Event
556
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
945 rcall lt2942_init_again
113
heinrichsweikamp
parents: 20
diff changeset
946
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
947 movff sub_a+1,battery_acumulated_charge+1 ; Save raw value
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
948 movff sub_a+0,battery_acumulated_charge+0 ; Save raw value
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
949
113
heinrichsweikamp
parents: 20
diff changeset
950 ; Compute batt_percent
211
heinrichsweikamp
parents: 200
diff changeset
951 ; (charge-battery_offset)/365
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
952 movff battery_offset+0,sub_b+0
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
953 movff battery_offset+1,sub_b+1
113
heinrichsweikamp
parents: 20
diff changeset
954 call subU16 ; sub_c = sub_a - sub_b (with signed values)
heinrichsweikamp
parents: 20
diff changeset
955
heinrichsweikamp
parents: 20
diff changeset
956 clrf batt_percent ; Set to zero
heinrichsweikamp
parents: 20
diff changeset
957 btfsc neg_flag ; result negative?
449
heinrichsweikamp
parents: 448
diff changeset
958 bra lt2942_set_to_zero_percent ; Yes, keep LT2942 at zero percent and return
113
heinrichsweikamp
parents: 20
diff changeset
959
heinrichsweikamp
parents: 20
diff changeset
960 ; > Zero, set batt_percent properly
heinrichsweikamp
parents: 20
diff changeset
961 movff sub_c+0,xA+0
heinrichsweikamp
parents: 20
diff changeset
962 movff sub_c+1,xA+1
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
963 movff battery_capacity+0,xB+0
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
964 movff battery_capacity+1,xB+1
113
heinrichsweikamp
parents: 20
diff changeset
965 call div16x16 ;xA/xB=xC with xA+0 as remainder, uses divB as temp variable
heinrichsweikamp
parents: 20
diff changeset
966 movff xC+0,batt_percent
heinrichsweikamp
parents: 20
diff changeset
967 return
heinrichsweikamp
parents: 20
diff changeset
968
449
heinrichsweikamp
parents: 448
diff changeset
969 lt2942_set_to_zero_percent:
heinrichsweikamp
parents: 448
diff changeset
970 clrf i2c_temp
heinrichsweikamp
parents: 448
diff changeset
971 movlw 0x02 ; Point to accumulated charge registers
heinrichsweikamp
parents: 448
diff changeset
972 rcall I2C_TX_GAUGE
heinrichsweikamp
parents: 448
diff changeset
973 movff battery_offset+1,SSP1BUF
heinrichsweikamp
parents: 448
diff changeset
974 rcall WaitMSSP
heinrichsweikamp
parents: 448
diff changeset
975 rcall I2C_WaitforACK
heinrichsweikamp
parents: 448
diff changeset
976 movff battery_offset+0,SSP1BUF
heinrichsweikamp
parents: 448
diff changeset
977 rcall WaitMSSP
heinrichsweikamp
parents: 448
diff changeset
978 rcall I2C_WaitforACK
heinrichsweikamp
parents: 448
diff changeset
979 bsf SSP1CON2,PEN ; Stop condition
heinrichsweikamp
parents: 448
diff changeset
980 bra WaitMSSP; (and return)
heinrichsweikamp
parents: 448
diff changeset
981
113
heinrichsweikamp
parents: 20
diff changeset
982 global lt2942_charge_done
heinrichsweikamp
parents: 20
diff changeset
983 lt2942_charge_done: ; Reset accumulating registers to 0xFFFF
heinrichsweikamp
parents: 20
diff changeset
984 clrf i2c_temp
heinrichsweikamp
parents: 20
diff changeset
985 movlw 0x02 ; Point to accumulated charge registers
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
986 rcall I2C_TX_GAUGE
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
987 setf SSP1BUF ; Data Byte
113
heinrichsweikamp
parents: 20
diff changeset
988 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
989 rcall I2C_WaitforACK
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
990 setf SSP1BUF ; Data Byte
113
heinrichsweikamp
parents: 20
diff changeset
991 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
992 rcall I2C_WaitforACK
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
993 bsf SSP1CON2,PEN ; Stop condition
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
994 bra WaitMSSP; (and return)
113
heinrichsweikamp
parents: 20
diff changeset
995
heinrichsweikamp
parents: 20
diff changeset
996 I2C_TX_GAUGE: ; Sends a byte to the LT2942 Gauge IC
heinrichsweikamp
parents: 20
diff changeset
997 movwf i2c_temp+1 ; Data byte
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
998 bsf SSP1CON2,SEN ; Start condition
113
heinrichsweikamp
parents: 20
diff changeset
999 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
1000 movlw b'11001000' ; Address byte + Write bit
heinrichsweikamp
parents: 20
diff changeset
1001 movwf SSP1BUF ; control byte
heinrichsweikamp
parents: 20
diff changeset
1002 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
1003 rcall I2C_WaitforACK
heinrichsweikamp
parents: 20
diff changeset
1004 movff i2c_temp+1, SSP1BUF ; Data Byte
heinrichsweikamp
parents: 20
diff changeset
1005 rcall WaitMSSP
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
1006 bra I2C_WaitforACK ; (and return)
113
heinrichsweikamp
parents: 20
diff changeset
1007
heinrichsweikamp
parents: 20
diff changeset
1008 I2C_RX_GAUGE:
heinrichsweikamp
parents: 20
diff changeset
1009 bsf SSP1CON2,SEN ; Start condition
heinrichsweikamp
parents: 20
diff changeset
1010 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
1011 movlw b'11001001' ; Address byte + Read bit
heinrichsweikamp
parents: 20
diff changeset
1012 movwf SSP1BUF ; control byte
heinrichsweikamp
parents: 20
diff changeset
1013 rcall WaitMSSP
heinrichsweikamp
parents: 20
diff changeset
1014 rcall I2C_WaitforACK
467
6ca155fc1509 battery gauge handling
heinrichsweikamp
parents: 466
diff changeset
1015 bsf SSP1CON2, RCEN ; Enable recieve mode
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
1016 bra WaitMSSP; (and return)
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1017
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1018
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1019 global I2C_probe_OSTC_rx
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1020 I2C_probe_OSTC_rx:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1021 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1022 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1023 movlw 0x50 ; Address byte + Write bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1024 movwf SSP1BUF ; control byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1025 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1026 btfss SSP1CON2,ACKSTAT ; ACK?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1027 bsf ostc_rx_present ; ACK send. OSTC_RX present!
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1028 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1029 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1030 btfss ostc_rx_present ; Do we have the RX?
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1031 return ; No, Done.
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1032 WAITMS .1
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 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1039 movlw 0x1B
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1040 movwf SSP1BUF ; Data Byte (Get firmware)
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 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1043 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1044 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1045 WAITMS .1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1046 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1047 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1048 movlw 0x51 ; Address byte + Read bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1049 movwf SSP1BUF ; control byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1050 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1051 bsf SSP1CON2, RCEN ; Enable recieve mode
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1052 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1053 movff SSP1BUF,rx_firmware+0
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1054 bsf SSP1CON2,ACKEN ; Master acknowlegde
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1055 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1056
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1057 ; last byte in read from RX circuity always with a NACK!
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1058 bsf SSP1CON2, RCEN ; Enable recieve mode
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 movff SSP1BUF,rx_firmware+1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1061 bsf SSP1CON2,ACKDT
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1062 bsf SSP1CON2,ACKEN ; Master NOT acknowlegde
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1063 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1064 bcf SSP1CON2,ACKDT ; Reset ACKDT flag
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1065 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1066 bra WaitMSSP ;(and return)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1067
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1068
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1069 global I2C_get_tankdata
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1070 I2C_get_tankdata:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1071 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1072 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1073 movlw 0x50 ; Address byte + Write bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1074 movwf SSP1BUF ; control byte
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 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1077 movlw 0x1E ; Read buffer2 (48 Bytes)
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1078 movwf SSP1BUF ; Data Byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1079 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1080 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1081 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1082 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1083 WAITMS .1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1084
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1085 ; read 48 bytes
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1086 bsf SSP1CON2,SEN ; Start condition
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 movlw 0x51 ; Address byte + read bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1089 movwf SSP1BUF ; control byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1090 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1091 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1092 movlw .47 ; 47 with ACK + 1 w/o ACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1093 movwf temp1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1094 lfsr FSR2,rx_buffer+0
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1095 I2C_get_tankdata_loop_read:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1096 bsf SSP1CON2, RCEN ; Enable recieve mode
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1097 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1098 movff SSP1BUF,POSTINC2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1099 bcf SSP1CON2,ACKDT
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1100 bsf SSP1CON2,ACKEN ; Master acknowlegde
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1101 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1102 decfsz temp1,F
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1103 bra I2C_get_tankdata_loop_read
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1104
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1105 ; 1 w/o ACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1106 bsf SSP1CON2, RCEN ; Enable recieve mode
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1107 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1108 movff SSP1BUF,POSTINC2
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1109 bsf SSP1CON2,ACKDT
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1110 bsf SSP1CON2,ACKEN ; Master NOT acknowlegde
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1111 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1112 bcf SSP1CON2,ACKDT ; Reset ACKDT flag
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1113
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1114 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1115 bra WaitMSSP ;(and return)
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
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1118 global I2C_update_OSTC_rx
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1119 I2C_update_OSTC_rx: ; 992*64byte master loop
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1120 bcf i2c_error_flag ; clear error flag
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1121 ; write 64 bytes
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1122 bsf SSP1CON2,SEN ; Start condition
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 movlw 0x50 ; Address byte + Write bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1125 movwf SSP1BUF ; control byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1126 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1127 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1128 lfsr FSR2,buffer ; send buffer for verify
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1129 movlw .64
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1130 movwf temp1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1131 I2C_update_OSTC_loop: ; 64byte flash page loop
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1132 movff up,POSTINC2 ; store for verify
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1133 movff up,SSP1BUF
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1134 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1135 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1136 call ext_flash_read_block ; Read one byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1137 movwf up ; prepare for transmit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1138 decfsz temp1,F
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1139 bra I2C_update_OSTC_loop
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1140 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1141 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1142 WAITMS .1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1143
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1144 ; read 64 bytes
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1145 bsf SSP1CON2,SEN ; Start condition
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 movlw 0x51 ; Address byte + read bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1148 movwf SSP1BUF ; control byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1149 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1150 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1151 lfsr FSR2,buffer ; send buffer for verify
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1152 movlw .63 ; 63 with ACK + 1 w/o ACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1153 movwf temp1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1154 I2C_update_OSTC_loop_read:
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1155 bsf SSP1CON2, RCEN ; Enable recieve mode
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1156 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1157 movf SSP1BUF,W
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1158 cpfseq POSTINC2 ; compare readback with original
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1159 bsf i2c_error_flag ; Not equal, set flag
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1160 bcf SSP1CON2,ACKDT
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1161 bsf SSP1CON2,ACKEN ; Master acknowlegde
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1162 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1163 decfsz temp1,F
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1164 bra I2C_update_OSTC_loop_read
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1165
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1166 ; 1 w/o ACK
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 bsf SSP1CON2,ACKDT
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1173 bsf SSP1CON2,ACKEN ; Master NOT 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 bcf SSP1CON2,ACKDT ; Reset ACKDT flag
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1176
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1177 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1178 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1179 WAITMS .1
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1180
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1181 bsf SSP1CON2,SEN ; Start condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1182 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1183 movlw 0x50 ; Address byte + Write bit
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1184 movwf SSP1BUF ; control byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1185 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1186 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1187 movlw 0x1F ; Write command!
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1188 movwf SSP1BUF ; Data Byte
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1189 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1190 rcall I2C_WaitforACK
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1191 bsf SSP1CON2,PEN ; Stop condition
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1192 rcall WaitMSSP
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1193 WAITMS .5 ; Required waiting time
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1194
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1195 btfss i2c_error_flag
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1196 retlw .0 ; All ok
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1197 retlw .255 ; an error occured
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 429
diff changeset
1198 END