annotate src/i2c.asm @ 620:cd986267a5ca

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