annotate src/i2c.asm @ 588:bf0c76e9b01b

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