annotate src/i2c.asm @ 656:8af5aefbcdaf default tip

Update to 3.31 beta
author heinrichsweikamp
date Thu, 27 Nov 2025 18:32:58 +0100
parents c7b7b8a358cd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
heinrichsweikamp
parents:
diff changeset
1 ;=============================================================================
heinrichsweikamp
parents:
diff changeset
2 ;
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
3 ; File i2c.asm * combined next generation V3.09.4b
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 ;
654
75e90cd0c2c3 hwOS sport 10.77 release
heinrichsweikamp
parents: 650
diff changeset
7 ; Copyright (c) 2012, JD Gascuel, heinrichs weikamp gmbh, all right reserved.
0
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
624
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
28 ;
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
29 ; Compass3
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
30 ; --------
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
31 ; LSM303C's Compass read address (8-Bit): 0x3D
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
32 ; LSM303C's Compass write address (8-Bit): 0x3C
624
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
33 ; LSM303C's Acceleration read address (8-Bit): 0x3B
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
34 ; LSM303C's Acceleration write address (8-Bit): 0x3A
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
35 ;
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
36 ; RX Circuity
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
37 ; -----------
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
38 ; RX Circuity read address (8-Bit): 0x51
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
39 ; RX Circuity write address (8-Bit): 0x50
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
40 ;
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
41 ; Battery gauge
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
42 ; -------------
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
43 ; LTC2942 read address (8-Bit): 0xC9
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
44 ; LTC2942 write address (8-Bit): 0xC8
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
45 ;
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
46 ; Alternative Battery gauge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
47 ; -------------
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
48 ; LTC2959 read address (8-Bit): 0xC7
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
49 ; LTC2959 write address (8-Bit): 0xC6
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
50 ;
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
51 ; Alternative pressure sensor
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
52 ; -----------
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
53 ; MS5837 read address (8-Bit): 0xED
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
54 ; MS5837 write address (8-Bit): 0xEC
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
55 ;
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
56 ;
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
57
0
heinrichsweikamp
parents:
diff changeset
58 ; HISTORY
heinrichsweikamp
parents:
diff changeset
59 ; 2012-08-22 : [mH] Creation
565
0ba88db66492 CHANGE: Limit button sensitivity to 80%
heinrichsweikamp
parents: 564
diff changeset
60 ; 2018-02-18 : [mH] Sync with hwOS Sport release
0
heinrichsweikamp
parents:
diff changeset
61
heinrichsweikamp
parents:
diff changeset
62
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
63 #include "hwos.inc" ; Mandatory header
0
heinrichsweikamp
parents:
diff changeset
64 #include "wait.inc"
113
heinrichsweikamp
parents: 20
diff changeset
65 #include "math.inc"
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
66 #include "eeprom_rs232.inc"
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
67
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
68
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
69 ;=============================================================================
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
70 i2c CODE
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
71 ;=============================================================================
0
heinrichsweikamp
parents:
diff changeset
72
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
73
0
heinrichsweikamp
parents:
diff changeset
74
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
75 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
76 ; Helper Function - get two Bytes and divide hi:lo/16 (signed)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
77 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
78 I2C_TwoBytesRX_div16:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
79 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
80 movff SSP1BUF,hi ; copy data byte to hi
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
81 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
82 movff SSP1BUF,lo ; copy data byte to lo
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
83 I2C_TwoBytesRX_div16_2: ; divide hi:lo/16 (signed) only
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
84 bcf STATUS,C
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
85 btfsc hi,7 ; copy sign bit to carry
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
86 bsf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
87 rrcf hi ; /2
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
88 rrcf lo
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
89 ;bra I2C_TwoBytesRX_div8 ; continue dividing hi:lo/8 (signed)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
90
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
91
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
92 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
93 ; Helper Function - divide hi:lo/8 (signed)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
94 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
95 I2C_TwoBytesRX_div8:
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
96 bcf STATUS,C
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
97 btfsc hi,7 ; copy sign bit to carry
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
98 bsf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
99 rrcf hi ; /4
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
100 rrcf lo
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
101 bcf STATUS,C
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
102 btfsc hi,7 ; copy sign bit to carry
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
103 bsf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
104 rrcf hi ; /8
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
105 rrcf lo
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
106 bcf STATUS,C
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
107 btfsc hi,7 ; copy sign bit to carry
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
108 bsf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
109 rrcf hi ; /16
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
110 rrcf lo
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
111 return ; done
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
112
0
heinrichsweikamp
parents:
diff changeset
113
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
114 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
115 ; Read Accelerometer
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
116 ;
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
117 global I2C_RX_accelerometer
0
heinrichsweikamp
parents:
diff changeset
118 I2C_RX_accelerometer:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
119 btfsc compass_type3 ; compass3 ?
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
120 bra I2C_RX_accelerometer_compass3 ; YES
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
121 btfsc compass_type2 ; compass2 ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
122 bra I2C_RX_accelerometer_compass2 ; YES
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
123 btfsc compass_type1 ; compass1 ?
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
124 bra I2C_RX_accelerometer_compass1 ; YES
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
125 ;bra I2C_RX_accelerometer_compass0 ; NO - compass0 then
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
126
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
127 ;I2C_RX_accelerometer_compass0:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
128 rcall I2C_SEN ; start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
129 movlw 0x38 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
130 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
131 movlw 0x00 ; ??
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
132 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
133 rcall I2C_RSEN ; repeated start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
134 movlw 0x39 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
135 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
136 rcall I2C_OneByteRX ; get status byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
137 movf SSP1BUF,W ; copy status byte to WREG
158
683321c09cfa nicer boot into surfacemode
heinrichsweikamp
parents: 113
diff changeset
138
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
139 ; Non-flipped screen:
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
140 ; Chip orientation on the PCB requires
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
141 ; Original = corrected
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
142 ; x = -x
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
143 ; y = -y
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
144 ; z = -z
0
heinrichsweikamp
parents:
diff changeset
145
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
146 ; Flipped screen:
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
147 ; Chip orientation on the PCB requires
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
148 ; Original = corrected
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
149 ; x = x
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
150 ; y = y
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
151 ; z = -z
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
152
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
153 rcall I2C_TwoBytesRX_div16 ; get two bytes and divide /16 (signed)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
154 btfsc flip_screen ; 180° rotation?
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
155 bra I2C_RX_accelerometer2 ; YES
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
156 comf hi ; NO - 16 bit sign change
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
157 negf lo ; - ....
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
158 btfsc STATUS,C ; - carry to propagate?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
159 incf hi,F ; - YES - do it
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
160 I2C_RX_accelerometer2:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
161 MOVII mpr,accel_DX ; copy result to accel_DX
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
162
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
163 rcall I2C_TwoBytesRX_div16 ; get two bytes and divide /16 (signed)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
164 btfsc flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
165 bra I2C_RX_accelerometer3 ; YES
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
166 comf hi ; NO - 16 bit sign change
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
167 negf lo ; - ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
168 btfsc STATUS,C ; - carry to propagate?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
169 incf hi,F ; YES - do it
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
170 I2C_RX_accelerometer3:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
171 MOVII mpr,accel_DY ; copy result to accel_DY
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
172
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
173 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
174 movff SSP1BUF,hi ; copy data byte to hi
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
175 rcall I2C_OneByteRX_NACK ; receive last byte with not acknowledge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
176 movff SSP1BUF,lo ; copy data byte to lo
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
177 rcall I2C_TwoBytesRX_div16_2 ; divide hi:lo/16 (signed)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
178 comf hi ; 16 bit sign change
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
179 negf lo ; ...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
180 btfsc STATUS,C ; carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
181 incf hi,F ; YES - do it
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
182 MOVII mpr,accel_DZ ; copy result to accel_DZ
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
183 bra I2C_PEN ; stop condition
0
heinrichsweikamp
parents:
diff changeset
184
heinrichsweikamp
parents:
diff changeset
185
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
186 I2C_RX_accelerometer_compass1:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
187 rcall I2C_SEN ; start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
188 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
189 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
190 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
191 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
192 rcall I2C_RSEN ; repeated start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
193 movlw 0x3D ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
194
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
195 I2C_RX_accelerometer_common: ; common part for compass 1,2 and 3
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
196 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
197
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
198 ; Non-flipped screen:
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
199 ; Chip orientation on the PCB requires
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
200 ; Original = Corrected
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
201 ; x = -x (Compass 1)
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
202 ; x = x (Compass 2)
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
203 ; y = -y
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
204 ; z = -z
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
205
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
206 ; Flipped screen:
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
207 ; Chip orientation on the PCB requires
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
208 ; Original = Corrected
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
209 ; x = x (Compass 1)
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
210 ; x = -x (Compass 2)
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
211 ; y = y
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
212 ; z = -z
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
213
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
214 ; Dump the accelerator data
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
215 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
216 movff SSP1BUF,lo ; accel_DX+0
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
217 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
218 movff SSP1BUF,hi ; accel_DX+1
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
219 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
220 btfss compass_type2 ; compass 2?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
221 bra I2C_RX_accelerometer1_c1 ; NO - compass 1
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
222 ; compass 2
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
223 btfss flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
224 bra I2C_RX_accelerometer2_c1 ; NO - continue with normal compass1 routines for Y and Z
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
225 ; flipped compass 2, negate x
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
226 comf hi ; YES - 16 bit sign change
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
227 negf lo ; - ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
228 btfsc STATUS,C ; - carry to propagate?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
229 incf hi,F ; YES - do it
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
230 bra I2C_RX_accelerometer2_c1 ; - continue with normal compass1 routines for Y and Z
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
231
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
232 I2C_RX_accelerometer1_c1:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
233 btfsc flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
234 bra I2C_RX_accelerometer2_c1 ; YES
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
235 ; non-flipped compass 1, negate x
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
236 comf hi ; NO - 16 bit sign change
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
237 negf lo ; - ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
238 btfsc STATUS,C ; - carry to propagate?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
239 incf hi,F ; YES - do it
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
240 I2C_RX_accelerometer2_c1:
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
241 ; flipped compass 1, non-flipped compass 2
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
242 MOVII mpr,accel_DX ; copy result
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
243 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
244 movff SSP1BUF,lo ; copy accel_DY+0 to lo
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
245 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
246 movff SSP1BUF,hi ; copy accel_DY+1 to hi
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
247 rcall I2C_TwoBytesRX_div16_2 ; divide hi:lo/16 (signed) only
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
248 btfsc flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
249 bra I2C_RX_accelerometer3_c1 ; YES
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
250 comf hi ; NO - 16 bit sign change
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
251 negf lo ; - ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
252 btfsc STATUS,C ; - carry to propagate?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
253 incf hi,F ; YES - do it
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
254 I2C_RX_accelerometer3_c1:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
255 MOVII mpr,accel_DY ; copy result
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
256 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
257 movff SSP1BUF,lo ; accel_DZ+0
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
258 rcall I2C_OneByteRX_NACK ; receive last byte with not acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
259 movff SSP1BUF,hi ; accel_DZ+1
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
260 rcall I2C_PEN ; stop condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
261 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
262 comf hi ; 16 bit sign change for Z
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
263 negf lo ; ...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
264 btfsc STATUS,C ; carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
265 incf hi,F ; YES - do it
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
266 MOVII mpr,accel_DZ ; copy result
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
267 return ; done
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
268
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
269
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
270 I2C_RX_accelerometer_compass2:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
271 rcall I2C_SEN ; start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
272 movlw 0x32 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
273 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
274 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
275 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
276 rcall I2C_RSEN ; repeated start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
277 movlw 0x33 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
278 bra I2C_RX_accelerometer_common ; continue with common part
624
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
279
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
280 I2C_RX_accelerometer_compass3:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
281 rcall I2C_SEN ; start condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
282 movlw 0x3A ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
283 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
284 movlw 0x28 ; 0x28 (OUT_X_L_A)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
285 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
286 rcall I2C_RSEN ; repeated start condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
287 movlw 0x3B ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
288 bra I2C_RX_accelerometer_common ; continue with common part
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
289
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
290
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
291
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
292 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
293 ; Read Compass
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
294 ;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
295 IFDEF _compass
0
heinrichsweikamp
parents:
diff changeset
296
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
297 global I2C_RX_compass
0
heinrichsweikamp
parents:
diff changeset
298 I2C_RX_compass:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
299 btfsc compass_type3 ; compass 3 ?
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
300 bra I2C_RX_compass3 ; YES
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
301 btfsc compass_type2 ; compass 2 ?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
302 bra I2C_RX_compass2 ; YES
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
303 btfsc compass_type1 ; compass 1 ?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
304 bra I2C_RX_compass1 ; YES
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
305 ;bra I2C_RX_compass0 ; NO - compass 0 then
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
306
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
307 ;I2C_RX_compass0:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
308 rcall I2C_SEN ; start condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
309 movlw 0x3C ; address + write bit
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
310 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
311 movlw 0x03 ; Point to Data Output X MSB Register (0x03)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
312 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
313 rcall I2C_PEN ; stop condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
314 rcall I2C_SEN ; start condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
315 movlw 0x3D ; address + read bit
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
316 rcall I2C_TX ; send byte
0
heinrichsweikamp
parents:
diff changeset
317
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
318 ; Compass IC sends data in following order:
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
319 ; x MSB
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
320 ; x LSB
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
321 ; z MSB
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
322 ; z LSB
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
323 ; y MSB
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
324 ; y LSB
0
heinrichsweikamp
parents:
diff changeset
325
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
326 ; Non-flipped screen
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
327 ; Chip orientation on the PCB requires
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
328 ; Original = Corrected
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
329 ; x = -y
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
330 ; z = z
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
331 ; y = x
0
heinrichsweikamp
parents:
diff changeset
332
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
333 ; Flipped screen
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
334 ; Chip orientation on the PCB requires
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
335 ; Original = Corrected
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
336 ; x = y
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
337 ; z = z
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
338 ; y = -x
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
339
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
340 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
341 movff SSP1BUF,compass_DY+1 ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
342 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
343 movff SSP1BUF,compass_DY+0 ; data byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
344 btfsc flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
345 bra I2C_RX_compass0_2 ; NO
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
346 banksel compass_DY ; YES - flip Y
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
347 comf compass_DY+1 ; - 16 bit sign change
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
348 negf compass_DY+0
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
349 btfsc STATUS,C ; - carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
350 incf compass_DY+1,F ; YES - do it
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
351 banksel common
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
352 I2C_RX_compass0_2:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
353 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
354 movff SSP1BUF,compass_DZ+1 ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
355 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
356 movff SSP1BUF,compass_DZ+0 ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
357 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
358 movff SSP1BUF,compass_DX+1 ; data byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
359 rcall I2C_RCEN ; enable receive mode
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
360 movff SSP1BUF,compass_DX+0 ; data byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
361 rcall I2C_PEN ; stop condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
362 btfss flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
363 return ; NO - done
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
364 banksel compass_DX ; YES - flip X
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
365 comf compass_DX+1 ; - 16 bit sign change
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
366 negf compass_DX+0
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
367 btfsc STATUS,C ; - carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
368 incf compass_DX+1,F ; YES - do it
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
369 banksel common ; back to bank common
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
370 return ; done
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
371
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
372
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
373 I2C_RX_compass1: ; compass type 1
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
374 rcall I2C_SEN ; start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
375 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
376 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
377 movlw b'10001000' ; 0x08 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
378 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
379 rcall I2C_RSEN ; repeated start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
380 movlw 0x3D ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
381 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
382 ;rcall WaitMSSP ; TODO needed? (mH)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
383 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
384 movff SSP1BUF,lo ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
385 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
386 movff SSP1BUF,hi ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
387 rcall I2C_TwoBytesRX_div8 ; divide hi,lo by 8 (signed)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
388 MOVII mpr,compass_DX ; copy result
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
389 btfss flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
390 bra I2C_RX_compass1_1 ; NO
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
391 banksel compass_DX ; YES - flip X
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
392 comf compass_DX+1 ; - 16 bit sign change
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
393 negf compass_DX+0
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
394 btfsc STATUS,C ; - carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
395 incf compass_DX+1,F ; YES - do it
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
396 banksel common
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
397 I2C_RX_compass1_1:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
398 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
399 movff SSP1BUF,lo ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
400 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
401 movff SSP1BUF,hi ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
402 rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
403 MOVII mpr,compass_DY
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
404 btfss flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
405 bra I2C_RX_compass1_2 ; NO
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
406 banksel compass_DY ; YES - flip Y
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
407 comf compass_DY+1 ; - 16 bit sign change
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
408 negf compass_DY+0 ; - ...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
409 btfsc STATUS,C ; - carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
410 incf compass_DY+1,F ; YES - do it
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
411 banksel common
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
412 I2C_RX_compass1_2:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
413 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
414 movff SSP1BUF,lo ; data byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
415 rcall I2C_OneByteRX_NACK ; receive last byte with not acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
416 movff SSP1BUF,hi ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
417 rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
418 MOVII mpr,compass_DZ ; copy result
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
419 bra I2C_PEN ; stop condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
420
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
421
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
422
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
423 I2C_RX_compass2: ; compass type 2
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
424 rcall I2C_SEN ; start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
425 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
426 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
427 movlw 0xE8 ; 0x68 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
428 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
429 rcall I2C_RSEN ; repeated start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
430 movlw 0x3D ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
431 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
432 I2C_RX_compass2_xx: ; compass 3 joins in here
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
433 ; rcall WaitMSSP ; wait for TX to complete (not needed, as included in I2C_TX)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
434 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
435 movff SSP1BUF,lo ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
436 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
437 movff SSP1BUF,hi ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
438 ; rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
439 btfsc flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
440 bra I2C_RX_compass2_1 ; YES - do nothing with X
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
441 ; NO - flip X
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
442 comf hi ; - 16 bit sign change
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
443 negf lo ; - ...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
444 btfsc STATUS,C ; - carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
445 incf hi,F ; YES - do it
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
446 I2C_RX_compass2_1:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
447 MOVII mpr,compass_DX
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
448 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
449 movff SSP1BUF,lo ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
450 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
451 movff SSP1BUF,hi ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
452 ; rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
453 btfss flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
454 bra I2C_RX_compass2_2 ; NO - do nothing with Y
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
455 ; YES - flip Y
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
456 comf hi ; - 16 bit sign change
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
457 negf lo ; - ...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
458 btfsc STATUS,C ; - carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
459 incf hi,F ; YES - do it
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
460 I2C_RX_compass2_2:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
461 MOVII mpr,compass_DY
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
462 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
463 movff SSP1BUF,lo ; data byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
464 rcall I2C_OneByteRX_NACK ; receive last byte with not acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
465 movff SSP1BUF,hi ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
466 ; rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
467 MOVII mpr,compass_DZ ; copy result
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
468 bra I2C_PEN ; stop condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
469
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
470
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
471
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
472 I2C_RX_compass3: ; compass type 3
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
473 rcall I2C_SEN ; start condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
474 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
475 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
476 movlw 0xA8 ; 0x28 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
477 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
478 rcall I2C_RSEN ; repeated start condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
479 movlw 0x3D ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
480 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
481 bra I2C_RX_compass2_xx ; join with compass 2 code
624
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
482
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
483 ENDIF ; _compass
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
484
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
485
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
486 ;-----------------------------------------------------------------------------
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
487 ; Initialize Compass / Accelerometer Chip
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
488 ;
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
489 global I2C_init_compass
0
heinrichsweikamp
parents:
diff changeset
490 I2C_init_compass:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
491 bsf compass_present ; Set global compass flag
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
492 bsf compass_enabled ; flag compass will be enabled
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
493 bcf compass_type2 ; clear in preparation of chip detection
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
494 bcf compass_type3 ; ...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
495
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
496 ; probe for compass 3
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
497 rcall I2C_SEN ; start condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
498 movlw 0x3A ; address byte + write bit
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
499 movwf SSP1BUF ; control byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
500 rcall WaitMSSP ; wait for TX to complete
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
501 ; we need the ACKSTAT bit! Do not use I2C_TX routine here which might reset this bit!
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
502 btfss SSP1CON2,ACKSTAT ; ACK received?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
503 bsf compass_type3 ; YES - ACK was send, compass 3 found
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
504 rcall I2C_PEN ; stop condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
505
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
506 btfsc compass_type3 ; compass 3 found?
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
507 bra I2C_init_compass3 ; YES - initialize compass 3
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
508
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
509 ; probe for compass 2
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
510 rcall I2C_SEN ; start condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
511 movlw 0x32 ; address byte + write bit
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
512 movwf SSP1BUF ; control byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
513 rcall WaitMSSP ; wait for TX to complete
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
514 ; we need the ACKSTAT bit! Do not use I2C_TX routine here which might reset this bit!
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
515 btfss SSP1CON2,ACKSTAT ; ACK received?
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
516 bsf compass_type2 ; YES - ACK send, compass 2 found
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
517 rcall I2C_PEN ; stop condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
518
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
519 btfsc compass_type2 ; compass 2 found?
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
520 bra I2C_init_compass2 ; YES - initialize compass 2
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
521
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
522 ; probe for compass 1
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
523 bsf compass_type1 ; assume compass 1 by default
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
524 rcall I2C_SEN ; start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
525 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
526 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
527 movlw 0x0F ; ??
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
528 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
529 rcall I2C_PEN ; stop condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
530 rcall I2C_SEN ; start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
531 movlw 0x3D ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
532 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
533 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
534 movlw 0x49 ; 0x49 = compass 1
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
535 cpfseq SSP1BUF ; 0x49 received?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
536 bcf compass_type1 ; NO - clear flag for compass 1
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
537 rcall I2C_PEN ; stop condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
538
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
539 btfsc compass_type1 ; compass 1 found?
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
540 bra I2C_init_compass1 ; YES - initialize compass 1
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
541
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
542 ; probe for compass 0
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
543 bsf compass_type0 ; assume compass 0 by default
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
544 rcall I2C_SEN ; start condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
545 movlw 0x3C ; address + write bit
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
546 rcall I2C_TX ; send byte
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
547 movlw 0x0A ; Point to Identification Register A
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
548 rcall I2C_TX ; send byte
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
549 rcall I2C_PEN ; stop condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
550 rcall I2C_SEN ; start condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
551 movlw 0x3D ; address + read bit
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
552 rcall I2C_TX ; send byte
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
553 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
554 movlw b'01001000' ; Identification Register A must be "H"
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
555 cpfseq SSP1BUF ; Compare with received value
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
556 bcf compass_type0 ; Not equal -> Not compass 0
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
557 rcall I2C_PEN ; stop condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
558
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
559 btfsc compass_type0 ; compass 0 found?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
560 bra I2C_init_compass0 ; YES - initialize compass 0
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
561 ; ; no compass of any type found
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
562 bcf compass_present ; Delete global compass flag
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
563 return
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
564
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
565
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
566 I2C_init_compass0:
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
567 ; magnetic
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
568 rcall I2C_SEN ; start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
569 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
570 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
571 movlw 0x00 ; ??
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
572 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
573 movlw b'01101000' ; ConfigA: 3 Hz, 8 samples averaged
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
574 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
575 movff opt_compass_gain,i2c_temp1 ; 0-7 (230 LSB/Gauss to 1370 LSB/Gauss)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
576 swapf i2c_temp1,F ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
577 comf i2c_temp1,F ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
578 bcf STATUS,C ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
579 rlcf i2c_temp1 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
580 movf i2c_temp1,W ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
581 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
582 movlw b'00000000' ; select continuous mode
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
583 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
584 rcall I2C_PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
585
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
586 ; accelerometer
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
587 rcall I2C_sleep_accelerometer0 ; registers can only be changed in standby mode
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
588
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
589 rcall I2C_SEN ; start condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
590 movlw 0x38 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
591 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
592 movlw 0x0E ; XYZ_DATA_CFG
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
593 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
594 movlw b'00000000' ; high pass filter = 0, +/- 2 g range
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
595 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
596 rcall I2C_PEN ; stop condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
597 rcall I2C_SEN ; start condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
598 movlw 0x38 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
599 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
600 movlw 0x2A ; CTRL_REG1
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
601 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
602 ; movlw b'00110000' ; CTRL_REG1: 160 ms data rate, standby mode
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
603 movlw b'00110100' ; CTRL_REG1: 160 ms data rate, standby mode, reduced noise mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
604 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
605 movlw b'00000010' ; CTRL_REG2: high-res in active mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
606 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
607 rcall I2C_PEN ; stop condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
608
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
609 rcall I2C_SEN ; start condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
610 movlw 0x38 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
611 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
612 movlw 0x2A ; CTRL_REG1
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
613 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
614 ; movlw b'00110001' ; CTRL_REG1: 160 ms data rate, active mode
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
615 movlw b'00110101' ; CTRL_REG1: 160 ms data rate, standby mode, reduced noise mode, active Mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
616 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
617 bra I2C_PEN ; stop condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
618
0
heinrichsweikamp
parents:
diff changeset
619
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
620
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
621 I2C_init_compass1:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
622 rcall I2C_SEN ; start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
623 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
624 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
625 movlw 0x9F ; 1F with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
626 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
627 movlw b'00000000' ; CTRL0
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
628 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
629 movlw b'00101111' ; CTRL1 (6.25 Hz, BDU=0, x,y,z = ON)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
630 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
631 movlw b'11000000' ; CTRL2 (50 Hz, +/- 2g)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
632 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
633 movlw b'00000000' ; CTRL3
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
634 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
635 movlw b'00000000' ; CTRL4
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
636 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
637 movlw b'01100100' ; CTRL5 HIGH res, 6.25 Hz
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
638 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
639 movff opt_compass_gain,i2c_temp1 ; 0-7 (230 LSB/Gauss to 1370 LSB/Gauss)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
640 movlw b'01100000' ; CTRL6 Full scale (+/-12 Gauss -> 2730 LSB/Gauss)
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
641 dcfsnz i2c_temp1,F ; = 1?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
642 movlw b'01100000' ; YES - CTRL6 Full scale (+/-12 Gauss -> 2730 LSB/Gauss)
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
643 dcfsnz i2c_temp1,F ; = 2?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
644 movlw b'01000000' ; YES - CTRL6 (+/-8 Gauss)
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
645 dcfsnz i2c_temp1,F ; = 3?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
646 movlw b'01000000' ; YES - CTRL6 (+/-8 Gauss)
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
647 dcfsnz i2c_temp1,F ; = 4?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
648 movlw b'00100000' ; YES - CTRL6 (+/-4 Gauss)
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
649 dcfsnz i2c_temp1,F ; = 5?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
650 movlw b'00100000' ; YES - CTRL6 (+/-4 Gauss)
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
651 dcfsnz i2c_temp1,F ; = 6?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
652 movlw b'00000000' ; YES - CTRL6 (+/-2 Gauss)
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
653 dcfsnz i2c_temp1,F ; = 7?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
654 movlw b'00000000' ; YES - CTRL6 (+/-2 Gauss)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
655 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
656 movlw b'00000000' ; CTRL7 Continuous Mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
657 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
658 bra I2C_PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
659
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
660 ; accelerometer initializes along with magnetic sensor
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
661
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
662
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
663 I2C_init_compass2:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
664 ; magnetic
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
665 rcall I2C_SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
666 movlw 0x3C ; address
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
667 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
668 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
669 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
670 movlw b'10000000' ; CFG_REG_A_M (10Hz, Continuous) 0x60 0x00
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
671 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
672 movlw b'00000011' ; CFG_REG_B_M (low-pass filter enabled) 0x61 (set pulse is released every 63 ODR)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
673 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
674 movlw b'00010000' ; CFG_REG_C_M BDU=1 0x62 0x57
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
675 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
676 rcall I2C_PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
677
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
678 ; accelerometer
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
679 rcall I2C_SEN ; start condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
680 movlw 0x32 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
681 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
682 movlw 0x9F ; 1F with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
683 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
684 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
685 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
686 movlw b'00100111' ; CTRL_REG1_A (10Hz, x,y,z = ON)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
687 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
688 movlw b'00000000' ; CTRL_REG2_A
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
689 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
690 movlw b'00000000' ; CTRL_REG3_A
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
691 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
692 movlw b'00001000' ; CTRL_REG4_A (BDU=0, +/-2g, HR=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
693 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
694 ; movlw b'00000000' ; CTRL_REG5_A
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
695 ; rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
696 bra I2C_PEN ; stop condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
697
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
698
624
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
699 I2C_init_compass3:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
700 ; magnetic
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
701 rcall I2C_SEN ; start condition
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
702 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
703 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
704 movlw 0xA0 ; 0x20 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
705 rcall I2C_TX ; send byte
645
070528a88715 3.16 release
heinrichsweikamp
parents: 643
diff changeset
706 movlw b'01110000' ; CTRL_REG1_M (10Hz, X and Y in Ultra-high performance mode) 0x20
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
707 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
708 movlw b'01100000' ; CTRL_REG2_M (Full-scale: +/- 16gauss) 0x21
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
709 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
710 movlw b'01000000' ; CTRL_REG3_M (Continuous) 0x22
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
711 rcall I2C_TX ; send byte
645
070528a88715 3.16 release
heinrichsweikamp
parents: 643
diff changeset
712 movlw b'00001100' ; CTRL_REG4_M (Z in Ultra-high performance mode) 0x23
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
713 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
714 movlw b'00000000' ; CTRL_REG5_M 0x24
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
715 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
716 movlw b'00000000' ; CTRL_REG5_M 0x24
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
717 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
718 rcall I2C_PEN ; stop condition
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
719
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
720 ;accelerometer
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
721 rcall I2C_SEN ; start condition
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
722 movlw 0x3A ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
723 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
724 movlw 0x20
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
725 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
726 movlw b'10010111' ; CTRL_REG1_A (100Hz, x,y,z = ON, BDU=OFF) 0x20
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
727 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
728 movlw b'00000000' ; CTRL_REG2_A 0x21
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
729 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
730 movlw b'00000000' ; CTRL_REG3_A 0x22
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
731 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
732 movlw b'11001100' ; CTRL_REG4_A 0x23
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
733 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
734 bra I2C_PEN ; stop condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
735
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
736
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
737
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
738 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
739 ; Deactivate Compass / Accelerometer
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
740 ;
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
741 global I2C_sleep_compass
0
heinrichsweikamp
parents:
diff changeset
742 I2C_sleep_compass:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
743 btfss compass_enabled ; compass active?
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
744 return ; NO - done
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
745 bcf compass_enabled
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
746 btfsc compass_type3 ; compass 3 ?
624
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
747 bra I2C_sleep_compass3 ; YES
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
748 btfsc compass_type2 ; compass 2 ?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
749 bra I2C_sleep_compass2 ; YES
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
750 btfsc compass_type1 ; compass 1 ?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
751 bra I2C_sleep_compass1 ; YES
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
752 ;bra I2C_sleep_compass0 ; NO - must be compass 0 then
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
753
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
754
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
755 I2C_sleep_compass0:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
756 ; magnetic
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
757 rcall I2C_SEN ; start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
758 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
759 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
760 movlw 0x00 ; ??
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
761 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
762 movlw b'01101000' ; ConfigA
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
763 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
764 movlw b'00100000' ; ConfigB
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
765 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
766 movlw b'00000010' ; idle mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
767 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
768 rcall I2C_PEN ; stop condition
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
769
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
770 I2C_sleep_accelerometer0:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
771 ; accelerometer
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
772 rcall I2C_SEN ; start condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
773 movlw 0x38 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
774 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
775 movlw 0x2A ; CTRL_REG1
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
776 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
777 movlw b'00000000' ; standby mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
778 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
779 bra I2C_PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
780
0
heinrichsweikamp
parents:
diff changeset
781
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
782 I2C_sleep_compass1:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
783 rcall I2C_SEN ; start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
784 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
785 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
786 movlw 0x20 ; CTRL_REG1
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
787 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
788 movlw b'00000000' ; data for CTRL_REG1: acceleration sensor power-down mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
789 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
790 rcall I2C_PEN ; stop condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
791 rcall I2C_SEN ; start condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
792 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
793 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
794 movlw 0x26 ; CTRL_REG7
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
795 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
796 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor power-down mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
797 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
798 bra I2C_PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
799
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
800 ; accelerometer sleeps with magnetic sensor
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
801
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
802
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
803 I2C_sleep_compass2:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
804 ; magnetic
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
805 rcall I2C_SEN ; start condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
806 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
807 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
808 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
809 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
810 movlw b'00000011' ; CFG_REG_A_M 0x60 (idle mode))
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
811 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
812 movlw b'00000100' ; CFG_REG_B_M 0x61 (set pulse is released only at power-on after PD condition)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
813 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
814 movlw b'01010001' ; CFG_REG_C_M 0x62
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
815 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
816 movlw b'00000000' ; INT_CTRL_REG_M 0x63
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
817 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
818 rcall I2C_PEN ; stop condition
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
819
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
820 ; accelerometer
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
821 rcall I2C_SEN ; start condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
822 movlw 0x32 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
823 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
824 movlw 0x9F ; 1F with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
825 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
826 movlw b'00000000' ; TEMP_CFG_REG_A 0x1F (temp sensor off)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
827 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
828 movlw b'00000000' ; CTRL_REG1_A 0x20 (all off)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
829 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
830 bra I2C_PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
831
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
832
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
833 I2C_sleep_compass3:
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
834 ; magnetic
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
835 rcall I2C_SEN ; start condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
836 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
837 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
838 movlw 0xA2 ; 0x22 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
839 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
840 movlw b'01000010' ; CTRL_REG3_M (power-down) 0x22
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
841 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
842 rcall I2C_PEN ; stop condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
843
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
844 ; accelerometer
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
845 rcall I2C_SEN ; start condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
846 movlw 0x3A ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
847 rcall I2C_TX ; send byte
624
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
848 movlw 0x20
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
849 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
850 movlw b'00000000' ; CTRL_REG1_A (100Hz, x,y,z = OFF) 0x20
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
851 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
852 bra I2C_PEN ; stop condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
853
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
854
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
855 ;-----------------------------------------------------------------------------
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
856 ; Helper Function - receive 1 Byte with Not-Acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
857 ;
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
858 I2C_OneByteRX_NACK:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
859 rcall I2C_RCEN ; enable receive mode
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
860 bsf SSP1CON2,ACKDT ; set ACKDT flag
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
861 rcall I2C_ACKEN ; send master acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
862 bcf SSP1CON2,ACKDT ; reset ACKDT flag
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
863 return
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
864
0
heinrichsweikamp
parents:
diff changeset
865
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
866 ;-----------------------------------------------------------------------------
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
867 ; Helper Function - receive 1 Byte with Acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
868 ;
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
869 I2C_OneByteRX:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
870 rcall I2C_RCEN ; enable receive mode
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
871 bra I2C_ACKEN ; send master acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
872
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
873 ;-----------------------------------------------------------------------------
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
874 ; Helper Function - send 1 Byte, wait for end of transmission and check ackn
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
875 ; add WREG to checksum byte first
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
876 I2C_TX_CHKSUM:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
877 addwf sub_a+0,F ; add to checksum
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
878 ; bra I2C_TX
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
879 ;-----------------------------------------------------------------------------
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
880 ; Helper Function - send 1 Byte, wait for end of transmission and check ackn
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
881 ;
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
882 I2C_TX:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
883 movwf SSP1BUF ; put byte to be sent into TX buffer
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
884 btfss i2c_error_flag_lock ; vault in use already?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
885 movff WREG,i2c_error_vault+0 ; NO, Store address
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
886 rcall WaitMSSP ; wait for TX to complete
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
887 bra I2C_Check_ACK ; check for acknowledge by receiver and return
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
888
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
889
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
890 ;-----------------------------------------------------------------------------
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
891 ; Helper Function - I2C Start Condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
892 ;
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
893 I2C_SEN:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
894 bsf SSP1CON2,SEN ; start condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
895 bra WaitMSSP ; wait for TX to complete (And return)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
896
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
897 ;-----------------------------------------------------------------------------
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
898 ; Helper Function - I2C Repeated Start Condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
899 ;
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
900 I2C_RSEN:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
901 bsf SSP1CON2,RSEN ; repeated start condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
902 bra WaitMSSP ; wait for TX to complete (And return)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
903
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
904
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
905 ;-----------------------------------------------------------------------------
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
906 ; Helper Function - I2C Stop Condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
907 ;
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
908 I2C_PEN:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
909 bsf SSP1CON2,PEN ; stop condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
910 rcall WaitMSSP ; wait for TX to complete (And return)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
911 I2C_WAIT_100US:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
912 ; add ~100µs bus free time (min. 66µs recommended for LTC2959)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
913 ; Remark: not exact: 122µs +/- 30.5 µs + worst case ISR latency
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
914 setf TMR5H ; initialize timer 5, high byte first
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
915 movlw .255-.4 ; 4 x 31.5 µs = 126µs, min: 94,5µs
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
916 movwf TMR5L ; initialize timer 5, low byte thereafter
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
917 bcf PIR5,TMR5IF ; clear timer 5 overrun flag
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
918 I2C_PEN_Loop:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
919 btfss PIR5,TMR5IF ; did timer 5 overrun?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
920 bra I2C_PEN_Loop ; NO - repeat inner loop
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
921 return
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
922
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
923 ;-----------------------------------------------------------------------------
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
924 ; Helper Function - Master Acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
925 ;
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
926 I2C_ACKEN:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
927 bsf SSP1CON2,ACKEN ; master acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
928 bra WaitMSSP ; wait for TX to complete (And return)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
929
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
930 ;-----------------------------------------------------------------------------
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
931 ; Helper Function - Enable reeive mode
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
932 ;
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
933 I2C_RCEN:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
934 bsf SSP1CON2,RCEN ; enable receive mode
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
935 bra WaitMSSP ; wait for TX to complete (And return)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
936
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
937 ;-----------------------------------------------------------------------------
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
938 ; Helper Function - wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
939 ;
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
940 WaitMSSP:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
941 btfss i2c_error_flag_lock ; vault in use already?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
942 movff SSP1BUF,i2c_error_vault+1 ; NO, store value
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
943 btfss i2c_error_flag_lock ; vault in use already?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
944 movff SSP1CON2,i2c_error_vault+2 ; NO, store value
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
945 clrf i2c_temp1 ; wait for max 256 loops
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
946 WaitMSSP_loop:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
947 decfsz i2c_temp1,F ; decrement loop counter, timeout?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
948 bra WaitMSSP2 ; NO
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
949 bra I2CFail ; YES
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
950 WaitMSSP2:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
951 btfss PIR1,SSP1IF ; TX completed?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
952 bra WaitMSSP_loop ; NO - loop
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
953 bcf PIR1,SSP1IF ; YES - clear TX completion flag
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
954 return ; - done
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
955
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
956
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
957 ;-----------------------------------------------------------------------------
650
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
958 ; Helper Function - Master NOT acknowledge and Stop
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
959 ;
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
960 I2C_MasterNotAckStop:
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
961 bsf SSP1CON2,ACKDT ; set ACKDT flag
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
962 rcall I2C_ACKEN ; send master NOT acknowledge
650
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
963 bcf SSP1CON2,ACKDT ; reset ACKDT flag
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
964
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
965 bra I2C_PEN ; stop condition
650
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
966
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
967 ;-----------------------------------------------------------------------------
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
968 ; Helper Function - check for Acknowledge by Receiver
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
969 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
970 I2C_Check_ACK:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
971 btfss SSP1CON2,ACKSTAT ; ACK received from slave?
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
972 return ; YES - done
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
973 ;bra I2CFail ; NO - do some clean up
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
974
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
975
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
976 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
977 ; Helper Function - clean up I2C Interface after missing Acknowledge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
978 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
979 I2CFail:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
980 bsf active_reset_ostc_rx ; reset RX circuitry (which may be the cause for the hang up)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
981 rcall I2CReset ; reset I2C
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
982 bcf PIR1,SSP1IF ; clear TX completion flag
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
983 bsf i2c_error_flag ; set error flag
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
984 bsf i2c_error_flag_lock ; lock the error vault
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
985 bcf active_reset_ostc_rx ; release reset from RX circuitry
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
986 ; bcf i2c_busy_temperature
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
987 ; bcf i2c_busy_pressure
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
988 return ; done
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
989
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
990
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
991 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
992 ; Helper Function - Reset I2C Module
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
993 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
994 ; recover in case something went wrong, i.g. slave holds SDA low
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
995 ;
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
996 global I2CReset
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
997 I2CReset:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
998 clrf SSP1CON1 ; reset entire module
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
999 clrf SSP1CON2 ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1000 clrf SSP1STAT ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1001 bcf TRISC,3 ; SCL as OUTPUT
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1002 bsf TRISC,4 ; SDA as input
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1003 bcf PORTC,3 ; SCL = 0
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1004 movlw d'9' ; clock-out 9 clock cycles manually
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1005 movwf i2c_temp1 ; ...
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
1006 I2CReset_1:
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
1007 bsf PORTC,3 ; SCL = 1
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1008 nop ; pause for 4 CPU cycles
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1009 nop ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1010 nop ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1011 nop ; ...
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
1012 btfsc PORTC,4 ; SDA = 1 ?
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1013 bra I2CReset_2 ; YES - SDA has been released from slave
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
1014 bcf PORTC,3 ; NO - set SCL = 0
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1015 nop ; - pause for 2 CPU cycles
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1016 nop ; - ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1017 bcf PORTC,3 ; - SCL = 0
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1018 nop ; - pause for 2 CPU cycles
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1019 nop ; - ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1020 decfsz i2c_temp1,F ; - clock counter, all cycles done?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1021 bra I2CReset_1 ; NO - loop
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
1022 I2CReset_2:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1023 bsf TRISC,3 ; SCL as input
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1024 clrf SSP1CON1 ; setup I2C mode
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1025 rcall I2C_WAIT_100US ; ISR-Safe 100µs wait
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1026 movlw b'00000000' ; enable slew rate control
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1027 movwf SSP1STAT ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1028 movlw b'00101000' ; configure I2C module
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1029 movwf SSP1CON1 ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1030 movlw b'00000000' ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1031 movwf SSP1CON2 ; ...
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1032 movlw i2c_speed_value
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1033 movwf SSP1ADD ; ...
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1034 rcall I2C_WAIT_100US ; ISR-Safe 100µs wait
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1035 movlw .1
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1036 addwf i2c_error_counter+0
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1037 movlw .0
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1038 addwfc i2c_error_counter+1 ; +1 on the error counter
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1039 btfss press_sensor_type ; =1: pressure sensor MS5837, =0: Pressure sensor MS5541
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1040 return ; MS5541, Done.
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1041 ; reset the sensor
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1042 rcall I2C_SEN ; start condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1043 movlw 0xEC ; address byte + write bit
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1044 rcall I2C_TX ; send byte
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1045 movlw 0x1E
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1046 rcall I2C_TX ; send byte
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1047 rcall I2C_PEN ; stop condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1048 WAITMS .5 ; 2.8ms according to datasheet
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1049 return ; done
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1050
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1051
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1052 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1053 ; Helper Function - Initialize Gauge IC again after an UVLO Event
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1054 ;
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1055 battery_gauge_init_again:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1056 btfsc battery_gauge_type ; =1: Gauge IC LTC2959, =0: LT2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1057 return
655
c7b7b8a358cd hwOS tech 3.22 release
heinrichsweikamp
parents: 654
diff changeset
1058 rcall I2CReset
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1059 movlw 0x02 ; point to accumulated charge registers
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1060 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1061 movff battery_accumulated_charge+1,SSP1BUF ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1062 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1063 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1064 movff battery_accumulated_charge+0,SSP1BUF ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1065 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1066 rcall I2C_Check_ACK ; check for acknowledge by receiver
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1067 rcall I2C_PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1068 MOVII battery_accumulated_charge,sub_a ; copy result to sub_a
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1069 ;bra battery_gauge_init ; and initialize again...
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1070
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1071
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1072 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1073 ; Initialize Gauge IC
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1074 ;
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1075 global battery_gauge_init
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1076 battery_gauge_init:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1077 movlw 0x01 ; point to control reg B / ADC Control
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1078 rcall I2C_TX_GAUGE ; send byte to the gauge IC
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1079 movlw b'11111000' ; automatic conversion every two seconds (LT2942)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1080 btfsc battery_gauge_type ; =1: Gauge IC LTC2959, =0: LT2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1081 movlw b'00111000' ; ADC Mode to "Smart Sleep" (converts V, I, T every 52 seconds (tolerance < ±5%)) (LTC2959)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1082 rcall I2C_TX ; send byte
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1083 btfsc battery_gauge_type ; =1: Gauge IC LTC2959, =0: LT2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1084 movlw b'01010000' ; 20µV deadband
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1085 btfsc battery_gauge_type ; =1: Gauge IC LTC2959, =0: LT2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1086 rcall I2C_TX ; send one byte more for the LTC2959
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1087 bra I2C_PEN ; stop condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1088
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1089 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1090 ; Read Gauge IC - Status Register
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1091 ;
113
heinrichsweikamp
parents: 20
diff changeset
1092 global lt2942_get_status
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1093 lt2942_get_status:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1094 bcf battery_gauge_type ; =1: Gauge IC LTC2959, =0: LT2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1095 bcf battery_gauge_available ; clear flag on default
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1096
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1097 ; try LTC2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1098 rcall I2C_SEN ; start condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1099 movlw 0xC8 ; address byte + Write bit
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1100 movwf SSP1BUF ; put byte to be sent into TX buffer
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1101 rcall WaitMSSP ; wait for TX to complete
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1102 ; we need the ACKSTAT bit! Do not use I2C_TX routine here which might reset this bit!
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1103 btfss SSP1CON2,ACKSTAT ; ACK received from slave?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1104 bsf battery_gauge_available ; YES - set flag
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1105 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1106 btfsc battery_gauge_available ; found?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1107 return ; YES - done
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1108
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1109 ; try LTC2959
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1110 rcall I2C_SEN ; start condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1111 movlw 0xC6 ; address byte + Write bit
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1112 movwf SSP1BUF ; put byte to be sent into TX buffer
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1113 rcall WaitMSSP ; wait for TX to complete
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1114 ; we need the ACKSTAT bit! Do not use I2C_TX routine here which might reset this bit!
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1115 btfss SSP1CON2,ACKSTAT ; ACK received from slave?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1116 bsf battery_gauge_available ; YES - set flag
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1117 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1118 btfsc battery_gauge_available ; found?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1119 bsf battery_gauge_type ; =1: Gauge IC LTC2959, =0: LT2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1120 return ; NO, return
113
heinrichsweikamp
parents: 20
diff changeset
1121
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1122 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1123 ; Read Gauge IC - Voltage
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1124 ;
113
heinrichsweikamp
parents: 20
diff changeset
1125 global lt2942_get_voltage
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1126 lt2942_get_voltage:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1127
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1128 btfsc battery_gauge_type ; =1: Gauge IC LTC2959, =0: LT2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1129 bra LTC2959_get_voltage ; use new IC
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1130
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1131 movlw 0x08 ; point to voltage registers
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1132 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1133 rcall I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1134 rcall I2C_ACKEN ; send master acknowledge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1135 movff SSP1BUF,xA+1 ; copy received byte to xA+1
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1136 rcall I2C_RCEN ; enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1137 movff SSP1BUF,xA+0 ; copy received byte to xA+0
650
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
1138
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
1139 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1140
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1141 ; convert voltage from raw value to Volt
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1142 MOVLI .6000,xB ; load conversion multiplicand into xB
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1143 call mult16x16 ; xC = xA * xB -> multiply raw value in xA with conversion multiplicand
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1144 ; divide by 65536 instead of 65535, introducing an error of 65536/65535 = 0.002 %
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1145 movff xC+2,xC+0 ; divide by 65536 can easily be done by just taking the 3rd and 4th byte of the multiplication result
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1146 movff xC+3,xC+1 ; use xC+0 and xC+1 as temp
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
1147
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1148 movlw .4 ; battery voltage < 4*256mV (1.024)?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1149 cpfslt xC+1 ; < 1024 mV ?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1150 bra lt2942_get_voltage_check_high ; NO - Continue checking
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1151 bra battery_gauge_init_again ; YES - initialize gauge and return (and leave batt_voltage:2 untouched)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1152
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1153 lt2942_get_voltage_check_high:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1154 movlw .17 ; battery voltage >= 17*256mV (4.352V)?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1155 cpfslt xC+1 ; - ... ?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1156 bra battery_gauge_init_again ; YES - initialize gauge and return (and leave batt_voltage:2 untouched)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1157 ; NO - done
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1158 movff xC+0, batt_voltage+0
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1159 movff xC+1, batt_voltage+1
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1160 bra lt2942_get_accumulated_charge ; - read coulomb counter (And return)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1161
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1162 LTC2959_get_voltage:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1163 movlw 0x0F ; point to voltage registers
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1164 rcall I2C_TX_GAUGE ; send byte to the gauge IC
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1165 rcall I2C_RX_GAUGE ; receive byte from the Gauge IC
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1166 rcall I2C_ACKEN ; send master acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1167 movff SSP1BUF,xA+1 ; copy received byte to xA+1
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1168 rcall I2C_RCEN ; enable receive mode
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1169 movff SSP1BUF,xA+0 ; copy received byte to xA+0
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1170
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1171 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1172
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1173 ; convert voltage from raw value to mV
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1174 MOVLI .62600,xB ; load conversion multiplicand into xB
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1175 call mult16x16 ; xC = xA * xB -> multiply raw value in xA with conversion multiplicand
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1176 ; divide by 65536 instead of 65535, introducing an error of 65536/65535 = 0.002 %
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1177 ;movff xC+2,xC+0 ; divide by 65536 can easily be done by just taking the 3rd and 4th byte of the multiplication result
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1178 ;movff xC+3,xC+1 ; use xC+0 and xC+1 as temp
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1179
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1180 movff xC+2, batt_voltage+0
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1181 movff xC+3, batt_voltage+1
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1182 rcall lt2942_get_accumulated_charge ; - read coulomb counter (And return)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1183
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1184 return ; done
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1185
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
1186
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1187 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1188 ; Read Gauge IC - Temperature
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1189 ;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1190 global lt2942_get_temperature
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1191 lt2942_get_temperature: ; read battery temperature
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1192 btfsc battery_gauge_type ; =1: Gauge IC LTC2959, =0: LT2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1193 bra LTC2959_get_temperature
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1194
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1195 movlw 0x0C ; point to temperature register
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1196 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1197 rcall I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1198 rcall I2C_ACKEN ; send master acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1199 movff SSP1BUF,xA+1 ; store raw temperature, high byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1200 rcall I2C_RCEN ; enable receive mode
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1201 movff SSP1BUF,xA+0 ; store raw temperature, low byte
650
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
1202 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1203
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1204 ; convert temperature from raw value to Kelvin in 0.1K
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1205 MOVLI .6000,xB ; load conversion multiplicand into xB
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1206 call mult16x16 ; xC = xA * xB -> multiply raw value in xA with conversion multiplicand
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1207 ; divide by 65536 instead of 65535, introducing an error of 65536/65535 = 0.002 %
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1208 movff xC+2,battery_temperature+0 ; divide by 65536 can easily be done by just taking the 3rd and 4th byte of the multiplication result
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1209 movff xC+3,battery_temperature+1 ; ...
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1210
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1211 lt2942_get_temperature_common:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1212 ; check if battery is being charged right now
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1213 btfss cc_active ; in CC charging mode?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1214 return ; NO - not charging, done
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1215
640
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1216 ; ignore false readings (>125°C)
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1217 movlw LOW .3307
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1218 movwf sub_a+0
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1219 movlw HIGH .3307
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1220 movwf sub_a+1
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1221 MOVII battery_temperature, sub_b
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1222 call cmpU16 ; sub_a - sub_b (with UNSIGNED values)
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1223 btfsc neg_flag ; result negative?
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1224 return ; YES - temperature > 125°C, not possible here. Skip test.
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1225
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1226 ; check for over-temperature while charging
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1227 MOVLI max_battery_charge_temp,sub_a
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1228 MOVII battery_temperature, sub_b
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1229 call cmpU16 ; sub_a - sub_b (with UNSIGNED values)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1230 btfss neg_flag ; result negative?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1231 return ; NO - temperature <= threshold, ok, done
640
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1232 return
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1233 ; YES - too hot, disable charging circuitry
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1234 bsf charge_disable ; - set charging-inhibit signal
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1235 bcf charge_enable ; - activate charging-inhibit signal
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1236 bsf battery_overtemp ; - flag that the battery charging over-temperature protection has tripped
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1237 return ; - done
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1238
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1239 LTC2959_get_temperature:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1240 return
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1241
113
heinrichsweikamp
parents: 20
diff changeset
1242
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1243 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1244 ; Read Gauge IC - Read State of Charge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1245 ;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1246 lt2942_get_accumulated_charge: ; read accumulated charge and compute percent
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1247 btfsc battery_gauge_type ; =1: Gauge IC LTC2959, =0: LTC2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1248 bra LTC2959_get_accumulated_charge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1249
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1250 ; old LTC2942
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1251 movlw 0x00 ; point to status register
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1252 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1253 rcall I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1254 rcall I2C_ACKEN ; send master acknowledge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1255 movff SSP1BUF,gauge_status_byte ; copy received byte to gauge_status_byte
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
1256
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1257 rcall I2C_RCEN ; enable receive mode
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1258 movf SSP1BUF,W ; dump to WREG (Control)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1259 rcall I2C_ACKEN ; send master acknowledge
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1260
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1261 rcall I2C_RCEN ; enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1262 movff SSP1BUF,sub_a+1 ; copy received byte to sub_a+1
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1263 rcall I2C_ACKEN ; send master acknowledge
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1264
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1265 rcall I2C_RCEN ; enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1266 movff SSP1BUF,sub_a+0 ; copy received byte to sub_a+0
650
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
1267 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop
113
heinrichsweikamp
parents: 20
diff changeset
1268
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1269 lt2942_get_accumulated_charge2:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1270 btfsc gauge_status_byte,0 ; UVLO event ?
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1271 rcall battery_gauge_init_again ; YES - do an re-initialization
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1272 MOVII sub_a,battery_accumulated_charge ; save raw value
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1273 ; Compute batt_percent = (charge - battery_offset) / capacity_xxxxxx
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1274 MOVII battery_offset,sub_b ; get battery offset
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1275 call subU16 ; sub_c = sub_a - sub_b (with signed values)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1276 clrf batt_percent ; default batt_percent to zero
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1277 btfsc neg_flag ; result negative?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1278 bra lt2942_set_to_zero_percent ; YES - keep LT2942 at zero percent and return
113
heinrichsweikamp
parents: 20
diff changeset
1279
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1280 ; > zero, set batt_percent properly
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1281 MOVII sub_c,xA ; copy net charge to xA
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1282 MOVII battery_capacity,xB ; get battery capacity into xB
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
1283 call div16x16 ; xC = xA / xB with xA as remainder
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1284 movff xC+0,batt_percent ; result is battery percentage
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1285 movlw .100 ; max. value is 100 %
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1286 cpfslt batt_percent ; batt_percent < 100 % ?
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1287 movwf batt_percent ; NO - limit to 100 %
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1288 movlw battery_cycle_counter_unlock
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1289 cpfsgt batt_percent ; < threshold ?
654
75e90cd0c2c3 hwOS sport 10.77 release
heinrichsweikamp
parents: 650
diff changeset
1290 bcf lock_cycle_counter ; Yes, unlock cycle counter
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
1291 return ; done
113
heinrichsweikamp
parents: 20
diff changeset
1292
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1293 LTC2959_get_accumulated_charge:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1294 movlw 0x00 ; point to status register
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1295 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1296 rcall I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1297 rcall I2C_ACKEN ; send master acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1298 movff SSP1BUF,gauge_status_byte ; copy received byte to gauge_status_byte
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1299
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1300 rcall I2C_RCEN ; enable receive mode
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1301 movf SSP1BUF,W ; dump to WREG (ADC Control)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1302 rcall I2C_ACKEN ; send master acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1303
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1304 rcall I2C_RCEN ; enable receive mode
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1305 movf SSP1BUF,W ; dump to WREG (Coulumb Counter Control)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1306 rcall I2C_ACKEN ; send master acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1307
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1308 rcall I2C_RCEN ; enable receive mode
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1309 movff SSP1BUF,xC+3 ; copy received byte to xC+3
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1310 rcall I2C_ACKEN ; send master acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1311
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1312 rcall I2C_RCEN ; enable receive mode
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1313 movff SSP1BUF,xC+2 ; copy received byte to xC+2
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1314 rcall I2C_ACKEN ; send master acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1315
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1316 rcall I2C_RCEN ; enable receive mode
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1317 movff SSP1BUF,xC+1 ; copy received byte to xC+1
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1318 rcall I2C_ACKEN ; send master acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1319
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1320 rcall I2C_RCEN ; enable receive mode
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1321 movff SSP1BUF,xC+0 ; copy received byte to xC+0
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1322 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1323
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1324 MOVLI .159,xB ; 159,4745717 (0,085mAh/533nAh)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1325 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1326
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1327 ; clamp to 100% here
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1328 tstfsz xC+2 ; Devision result > 0xFFFF
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1329 setf xC+0 ; Yes, make sure xC:2 is 0xFFFF
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1330 tstfsz xC+2 ; Devision result > 0xFFFF
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1331 setf xC+1 ; Yes, make sure xC:2 is 0xFFFF
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1332
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1333 movff xC+1,sub_a+1
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1334 movff xC+0,sub_a+0 ; put result in sub_a:2
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1335 ;
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1336 ; movff sub_a+1,gp_debug+1
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1337 ; movff sub_a+0,gp_debug+0
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1338 ;
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1339 bra lt2942_get_accumulated_charge2 ; continue as with LT2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1340
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1341
449
heinrichsweikamp
parents: 448
diff changeset
1342 lt2942_set_to_zero_percent:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1343 btfsc battery_gauge_type ; =1: Gauge IC LTC2959, =0: LT2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1344 bra LTC2959_set_to_zero_percent
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1345
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1346 movlw 0x02 ; point to accumulated charge registers
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1347 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1348 movff battery_offset+1,SSP1BUF ; send battery offset, high byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1349 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1350 rcall I2C_Check_ACK ; check for acknowledge by receiver
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1351 movff battery_offset+0,SSP1BUF ; send battery offset, low byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1352 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1353 rcall I2C_Check_ACK ; check for acknowledge by receiver
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1354 bra I2C_PEN ; stop condition (and return)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1355
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1356 LTC2959_set_to_zero_percent:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1357 ; multiply battery_offset:2 with 159 and write result to LTC2959
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1358
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1359 MOVII battery_offset,xA
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1360 MOVLI .159,xB ; 159,4745717 (0,085mAh/533nAh)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1361 call mult16x16 ; xC:4 = xA:2 * xB:2
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1362
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1363 movlw 0x03 ; point to accumulated charge registers
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1364 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1365 movff xC+3,SSP1BUF ; send battery offset
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1366 rcall WaitMSSP ; wait for TX to complete
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1367 rcall I2C_Check_ACK ; check for acknowledge by receiver
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1368 movff xC+2,SSP1BUF ; send battery offset
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1369 rcall WaitMSSP ; wait for TX to complete
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1370 rcall I2C_Check_ACK ; check for acknowledge by receiver
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1371 movff xC+1,SSP1BUF ; send battery offset
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1372 rcall WaitMSSP ; wait for TX to complete
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1373 rcall I2C_Check_ACK ; check for acknowledge by receiver
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1374 movff xC+0,SSP1BUF ; send battery offset
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1375 rcall WaitMSSP ; wait for TX to complete
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1376 rcall I2C_Check_ACK ; check for acknowledge by receiver
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1377 bra I2C_PEN ; stop condition (and return)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1378
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1379
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1380 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1381 ; Read Gauge IC - Reset Accumulating Register to 0xFFFF
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1382 ;
113
heinrichsweikamp
parents: 20
diff changeset
1383 global lt2942_charge_done
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1384 lt2942_charge_done:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1385 btfsc battery_gauge_type ; =1: Gauge IC LTC2959, =0: LT2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1386 bra lt2959_charge_done
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1387
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1388 movlw 0x02 ; point to accumulated charge registers
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1389 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1390 setf SSP1BUF ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1391 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1392 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1393 setf SSP1BUF ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1394 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1395 rcall I2C_Check_ACK ; check for acknowledge by receiver
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1396 bra I2C_PEN ; stop condition (and return)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1397
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1398 lt2959_charge_done:
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1399 ; Reset Accumulating Register to 0x009EFF61 (0xFFFF * .159)
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1400 movlw 0x03 ; point to accumulated charge registers
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1401 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1402 movlw 0x00
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1403 movwf SSP1BUF ; data byte
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1404 rcall WaitMSSP ; wait for TX to complete
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1405 rcall I2C_Check_ACK ; check for acknowledge by receiver
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1406 movlw 0x9E
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1407 movwf SSP1BUF ; data byte
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1408 rcall WaitMSSP ; wait for TX to complete
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1409 rcall I2C_Check_ACK ; check for acknowledge by receiver
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1410 movlw 0xFF
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1411 movwf SSP1BUF ; data byte
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1412 rcall WaitMSSP ; wait for TX to complete
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1413 rcall I2C_Check_ACK ; check for acknowledge by receiver
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1414 movlw 0x61
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1415 movwf SSP1BUF ; data byte
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1416 rcall WaitMSSP ; wait for TX to complete
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1417 rcall I2C_Check_ACK ; check for acknowledge by receiver
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1418 bra I2C_PEN ; stop condition (and return)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1419
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1420
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1421 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1422 ; Helper Function - send 1 Byte to the LT2942 Gauge IC
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1423 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1424 I2C_TX_GAUGE:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1425 movwf i2c_temp2 ; save data byte to be sent
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1426 rcall I2C_SEN ; start condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1427 movlw 0xC8 ; address byte + Write bit LT2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1428 btfsc battery_gauge_type ; =1: Gauge IC LTC2959, =0: LT2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1429 movlw 0xC6 ; address byte + Write bit LTC2559
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1430 rcall I2C_TX ; send byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1431 movf i2c_temp2,W ; restore data byte to be sent
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1432 bra I2C_TX ; send byte and return
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1433
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1434
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1435 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1436 ; Helper Function - receive 1 Byte from the LT2942 Gauge IC
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1437 ;
113
heinrichsweikamp
parents: 20
diff changeset
1438 I2C_RX_GAUGE:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1439 rcall I2C_RSEN ; repeated start condition
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1440 movlw 0xC9 ; address byte + Read bit LT2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1441 btfsc battery_gauge_type ; =1: Gauge IC LTC2959, =0: LT2942
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1442 movlw 0xC7 ; address byte + Read bit LTC2959
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1443 rcall I2C_TX ; send byte
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1444 bra I2C_RCEN ; enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1445
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1446
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1447 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1448 ; Reset Hardware and Software Battery Gauge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1449 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1450 ; called from comm.asm and menu_tree.asm
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1451 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1452 global reset_battery_gauge_and_lt2942
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1453 reset_battery_gauge_and_lt2942:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1454 btfsc battery_gauge_available ; battery gauge chip available?
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1455 call lt2942_charge_done ; YES - reset meter to 0xFFFF
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1456 ;bra reset_battery_gauge ; continue resetting gauge registers
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1457
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1458
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1459 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1460 ; Reset Software Battery Gauge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1461 ;
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
1462 global reset_battery_gauge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1463 reset_battery_gauge:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1464 bsf block_battery_gauge ; suspend ISR from accessing the battery registers
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1465 movlw .100 ; set battery level to 100%
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1466 movwf batt_percent ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1467 banksel battery_gauge ; select bank ISR data
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1468 clrf battery_gauge+0 ; null the battery registers
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1469 clrf battery_gauge+1 ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1470 clrf battery_gauge+2 ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1471 clrf battery_gauge+3 ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1472 clrf battery_gauge+4 ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1473 clrf battery_gauge+5 ; ...
654
75e90cd0c2c3 hwOS sport 10.77 release
heinrichsweikamp
parents: 650
diff changeset
1474 banksel charge_cycles
75e90cd0c2c3 hwOS sport 10.77 release
heinrichsweikamp
parents: 650
diff changeset
1475 clrf charge_cycles+0 ; null the charge cycles
75e90cd0c2c3 hwOS sport 10.77 release
heinrichsweikamp
parents: 650
diff changeset
1476 clrf charge_cycles+1 ; ...
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1477 banksel common ; back to bank common
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1478 goto eeprom_battery_gauge_write ; update battery registers in EEPROM, unblock ISR and return
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
1479
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
1480
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1481
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
1482 IFDEF _rx_functions
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
1483
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1484 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1485 ; OSTC TR - probe if TR Module available
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1486 ;
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1487 global I2C_probe_OSTC_rx
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1488 I2C_probe_OSTC_rx:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1489 bcf ostc_rx_present ; default to no TR module available
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1490 clrf WREG ; bank-safe set to zero of ...
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1491 movff WREG,rx_firmware_cur_major ; ... current TR module firmware, major
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1492 movff WREG,rx_firmware_cur_minor ; ... current TR module firmware, minor
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1493 movlw .5 ; max number of tries for detecting a TR module
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1494 movwf hy ; initialize loop counter for tries
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
1495 I2C_probe_OSTC_rx_1:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1496 rcall I2C_SEN ; start condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1497 movlw 0x50 ; address byte + write bit
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1498 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1499 bsf ostc_rx_present ; YES - TR module detected
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1500 rcall I2C_PEN ; stop condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1501 btfss ostc_rx_present ; was a TR module detected?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1502 return ; NO - done
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1503
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1504 WAITMS .1 ; wait 1 ms
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1505 rcall I2C_SEN ; start condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1506 movlw 0x50 ; address byte + write bit
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1507 rcall I2C_TX ; send byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1508 movlw 0x1B ; command: get firmware
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1509 rcall I2C_TX ; send byte
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1510 rcall I2C_PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1511
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1512 WAITMS .1 ; wait 1 ms
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1513
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1514 rcall I2C_SEN ; start condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1515 movlw 0x51 ; address byte + Read bit
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1516 movwf SSP1BUF ; control byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1517 rcall WaitMSSP ; wait for TX to complete
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1518 rcall I2C_RCEN ; enable receive mode
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1519 movff SSP1BUF,rx_firmware_cur_major ; store as firmware version, major
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1520 rcall I2C_ACKEN ; send master acknowledge
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1521
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1522 ; last byte in read from RX circuity always with a NACK!
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1523 rcall I2C_RCEN ; enable receive mode
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1524 movff SSP1BUF,rx_firmware_cur_minor ; store as firmware version, minor
650
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
1525 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1526
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1527 ; wait for TR module becoming ready
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1528 movff rx_firmware_cur_minor,i2c_temp1 ; copy minor firmware version to bank common
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1529 movlw .147 ; code for not ready, minor
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1530 cpfseq i2c_temp1 ; equal?
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1531 return ; NO - TR module ready, done
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1532 movff rx_firmware_cur_major,i2c_temp1 ; YES - copy major firmware version to bank common
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1533 movlw .27 ; - code for not ready, major
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1534 cpfseq i2c_temp1 ; - equal?
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1535 return ; NO - TR module ready, done
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1536 bsf active_reset_ostc_rx ; YES - apply reset to TR module
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1537 WAITMS .5 ; - wait 5 ms
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1538 bcf active_reset_ostc_rx ; - release reset
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1539 WAITMS .250 ; - wait for 250 ms
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1540 WAITMS .250 ; - wait another 250 ms
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1541 decfsz hy,F ; - decrement counter for number of tries, became zero?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1542 bra I2C_probe_OSTC_rx_1 ; - NO - try again
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1543 bcf ostc_rx_present ; - YES - something is wrong, flag TR module as not available
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1544 return ; - done
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1545
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1546
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1547 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1548 ; OSTC TR - get Tank Data
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1549 ;
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1550 global I2C_get_tankdata
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1551 I2C_get_tankdata:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1552 rcall I2C_SEN ; start condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1553 movlw 0x50 ; address byte + write bit
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1554 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1555 movlw 0x1E ; read buffer2 (48 bytes)
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1556 rcall I2C_TX ; send byte
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1557 rcall I2C_PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1558
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1559 WAITMS .1 ; wait 1 ms
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1560
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1561 ; read 48 bytes
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1562 rcall I2C_SEN ; start condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1563 movlw 0x51 ; address byte + read bit
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1564 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1565 movlw .47 ; 47 with ACK + 1 w/o ACK
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1566 movwf i2c_temp2 ; initialize loop counter
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1567 lfsr FSR2,rx_buffer ; point to start of rx data buffer
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1568 I2C_get_tankdata_loop_read:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1569 rcall I2C_RCEN ; enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1570 movff SSP1BUF,POSTINC2 ; copy received byte to the rx buffer
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1571 bcf SSP1CON2,ACKDT ; reset ACKDT flag
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1572 rcall I2C_ACKEN ; send master acknowledge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1573 decfsz i2c_temp2,F ; decrement loop counter, done?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1574 bra I2C_get_tankdata_loop_read ; NO - loop
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1575 ; read last byte without ACK
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1576 rcall I2C_RCEN ; enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1577 movff SSP1BUF,POSTINC2 ; copy received byte to the rx buffer
650
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
1578 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1579 return ; done
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1580
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1581
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1582
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1583 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1584 ; OSTC TR - Firmware Update
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1585 ;
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1586 IFDEF _rx_update
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1587
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1588 global I2C_update_OSTC_rx
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1589 I2C_update_OSTC_rx: ; transfer 64 byte to RX co-processor
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1590 ; setup for write
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1591 bcf i2c_error_flag ; clear error flag
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1592 lfsr FSR2,buffer ; initialize pointer to send buffer used for verify
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1593 movlw .64 ; initialize loop counter: 64 byte with ACK
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1594 movwf i2c_temp2 ; ...
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1595 ; address write
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1596 rcall I2C_SEN ; start condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1597 movlw 0x50 ; address byte + write bit
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1598 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1599 ; write 64 bytes
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1600 I2C_update_OSTC_loop:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1601 TBLRD*+ ; read a byte from program memory
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1602 movff TABLAT,POSTINC2 ; copy to send buffer
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1603 movf TABLAT,W ; copy to W
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1604 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1605 decfsz i2c_temp2,F ;decrement loop counter, became zero?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1606 bra I2C_update_OSTC_loop ; NO - loop
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1607 rcall I2C_PEN ; stop condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1608 WAITMS .1 ; - wait another 1 ms
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1609 ; setup for read-back
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1610 lfsr FSR2,buffer ; reset pointer to begin of send buffer
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1611 movlw .63 ; initialize loop counter: 63 byte with ACK + 1 w/o ACK
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1612 movwf i2c_temp2 ; ...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1613 ; address read-back
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1614 rcall I2C_SEN ; start condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1615 movlw 0x51 ; address byte + read bit
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1616 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1617 ; read-back 64 bytes
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1618 I2C_update_OSTC_loop_read:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1619 rcall I2C_RCEN ; enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1620 movf SSP1BUF,W ; copy received byte to WREG
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1621 cpfseq POSTINC2 ; compare read-back byte with sent byte, equal?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1622 bsf i2c_error_flag ; NO - not equal, set error flag
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1623 bcf SSP1CON2,ACKDT ; reset ACKDT flag
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1624 rcall I2C_ACKEN ; send master acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1625 decfsz i2c_temp2,F ; decrement loop counter, became zero?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1626 bra I2C_update_OSTC_loop_read ; NO - loop
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1627 ; 1 w/o ACK
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1628 bsf SSP1CON2, RCEN ; YES - enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1629 rcall WaitMSSP ; - wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1630 movf SSP1BUF,W ; - get 64th byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1631 cpfseq POSTINC2 ; - compare read-back byte with sent byte, equal?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1632 bsf i2c_error_flag ; NO - not equal, set error flag
650
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
1633 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1634 WAITMS .1
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1635 ; address commit
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1636 rcall I2C_SEN ; start condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1637 movlw 0x50 ; address byte + write bit
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1638 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1639 movlw 0x1F ; write command
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1640 rcall I2C_TX ; send byte
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1641 rcall I2C_PEN ; stop condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1642 WAITMS .5 ; required waiting time
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1643 ; error check
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1644 btfss i2c_error_flag ; did an error occur?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1645 retlw .0 ; NO - data transfered successfully
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1646 retlw .255 ; YES - error in data transfer occurred
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1647
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1648 ENDIF ; _rx_update
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1649
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1650 ENDIF ; _rx_functions
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1651
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1652 ;-----------------------------------------------------------------------------
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1653 ; Probe for MS5837 sensor
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1654 ;
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1655 global I2C_probe_pressure_sensor ; Do not call from ISR!
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1656 I2C_probe_pressure_sensor: ; Probe the type of sensor, set/clear press_sensor_type
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1657 bcf press_sensor_type ; MS5541 as default
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1658 rcall I2C_SEN ; start condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1659 movlw 0xEC ; address byte + write bit
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1660 movwf SSP1BUF ; control byte
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1661 rcall WaitMSSP ; wait for TX to complete
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1662 ; we need the ACKSTAT bit! Do not use I2C_TX routine here which might reset this bit!
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1663 btfss SSP1CON2,ACKSTAT ; ACK received?
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1664 bsf press_sensor_type ; MS5837 sensor found
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1665 bra I2C_PEN ; stop condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1666
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1667 ;--------------------------------------------------------------------
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1668 ; Helper Function - get the calibration parameter from # WREG address
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1669 ; Do not call from ISR!
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1670 I2C_get_calib_parameter:
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1671 movwf lo ; store address
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1672 rcall I2C_SEN ; start condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1673 movlw 0xEC ; address byte + write bit
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1674 rcall I2C_TX ; send byte
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1675 movf lo,W ; Point to calibration register
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1676 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1677 rcall I2C_PEN ; stop condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1678
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1679 rcall I2C_SEN ; start condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1680 movlw 0xED ; address byte + read bit
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1681 rcall I2C_TX ; send byte
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1682 rcall I2C_RCEN ; enable receive mode
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1683 movff SSP1BUF,dMSB ; High byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1684 rcall I2C_ACKEN ; send master acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1685
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1686 rcall I2C_OneByteRX_NACK ; receive last byte with not acknowledge
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1687 movff SSP1BUF,dLSB ; Low byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1688 bra I2C_PEN ; stop condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1689
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1690
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1691 global I2C_get_calib_MS5837 ; Do not call from ISR!
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1692 I2C_get_calib_MS5837:
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1693 banksel common
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1694 ; first, send a reset
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1695 rcall I2C_SEN ; start condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1696 movlw 0xEC ; address byte + write bit
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1697 rcall I2C_TX ; send byte
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1698 movlw 0x1E
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1699 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1700 rcall I2C_PEN ; stop condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1701 WAITMS .5 ; 2.8ms according to datasheet
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1702
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1703 movlw 0xA2 ; Point to C1
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1704 rcall I2C_get_calib_parameter ; returns calibration value in lo and hi
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1705 movff dLSB,C1+0 ; store calib
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1706 movff dMSB,C1+1 ; store calib
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1707
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1708 movlw 0xA4 ; Point to C2
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1709 rcall I2C_get_calib_parameter ; returns calibration value in lo and hi
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1710 movff dLSB,C2+0 ; store calib
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1711 movff dMSB,C2+1 ; store calib
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1712
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1713 movlw 0xA6 ; Point to C3
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1714 rcall I2C_get_calib_parameter ; returns calibration value in lo and hi
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1715 movff dLSB,C3+0 ; store calib
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1716 movff dMSB,C3+1 ; store calib
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1717
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1718 movlw 0xA8 ; Point to C4
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1719 rcall I2C_get_calib_parameter ; returns calibration value in lo and hi
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1720 movff dLSB,C4+0 ; store calib
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1721 movff dMSB,C4+1 ; store calib
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1722
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1723 movlw 0xAA ; Point to C5
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1724 rcall I2C_get_calib_parameter ; returns calibration value in lo and hi
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1725 movff dLSB,C5+0 ; store calib
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1726 movff dMSB,C5+1 ; store calib
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1727
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1728 movlw 0xAC ; Point to C6
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1729 rcall I2C_get_calib_parameter ; returns calibration value in lo and hi
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1730 movff dLSB,C6+0 ; store calib
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1731 movff dMSB,C6+1 ; store calib
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1732
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1733 return
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1734
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1735 global I2C_get_press_val_MS5837
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1736 I2C_get_press_val_MS5837:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1737 btfsc i2c_reinit_sensor2 ; Sensor ok?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1738 rcall I2CReset ; Try a I2C reset first
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1739 btfsc i2c_reinit_sensor2 ; Sensor ok?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1740 bra I2C_get_press_val_MS5837_skip ; We did a reset, restart conversion first
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1741
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1742 rcall I2C_SEN ; start condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1743 movlw 0xEC ; address byte + write bit
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1744 rcall I2C_TX ; send byte
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1745 movlw 0x00 ; command byte (0x00, read ADC)
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1746 rcall I2C_TX ; send byte
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1747
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1748 rcall I2C_RSEN ; repeated start condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1749 movlw 0xED ; address byte + read bit
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1750 rcall I2C_TX ; send byte
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1751
650
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
1752 bsf i2c_busy_pressure ; reading new pressure
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1753 rcall I2C_RCEN ; enable receive mode
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1754 movff SSP1BUF,D1_buffer+2 ; Upper byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1755 rcall I2C_ACKEN ; send master acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1756 rcall I2C_RCEN ; enable receive mode
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1757 movff SSP1BUF,D1_buffer+1 ; high byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1758 rcall I2C_ACKEN ; send master acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1759 rcall I2C_RCEN ; enable receive mode
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1760 movff SSP1BUF,D1_buffer+0 ; Low byte
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1761
650
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
1762 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
1763 bcf i2c_busy_pressure ; reading new pressure done.
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1764
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1765 I2C_get_press_val_MS5837_skip:
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1766 ; Start temperature measurement
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1767 rcall I2C_SEN ; start condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1768 movlw 0xEC ; address byte + write bit
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1769 rcall I2C_TX ; send byte
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1770 movlw 0x58 ; OSR=4096, type=D2
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1771 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1772 rcall I2C_PEN ; stop condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1773 bcf ms5837_state ; =0: result of temperature will be in the ADC
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1774 bcf i2c_reinit_sensor2 ; Clear error flag
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1775 return
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1776
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1777 global I2C_get_temp_val_MS5837
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1778 I2C_get_temp_val_MS5837:
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1779 btfsc i2c_reinit_sensor2 ; Sensor ok?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1780 rcall I2CReset ; Try a I2C reset first
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1781 btfsc i2c_reinit_sensor2 ; Sensor ok?
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1782 bra I2C_get_temp_val_MS5837_skip ; We did a reset, restart conversion first
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1783
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1784 rcall I2C_SEN ; start condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1785 movlw 0xEC ; address byte + write bit
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1786 rcall I2C_TX ; send byte
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1787 movlw 0x00 ; command byte (0x00, read ADC)
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1788 rcall I2C_TX ; send byte
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1789
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1790 rcall I2C_RSEN ; repeated start condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1791 movlw 0xED ; address byte + read bit
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1792 rcall I2C_TX ; send byte
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1793
650
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
1794 bsf i2c_busy_temperature ; reading new temperature
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1795 rcall I2C_RCEN ; enable receive mode
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1796 movff SSP1BUF,D2_buffer+2 ; Upper byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1797 rcall I2C_ACKEN ; send master acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1798 rcall I2C_RCEN ; enable receive mode
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1799 movff SSP1BUF,D2_buffer+1 ; high byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1800 rcall I2C_ACKEN ; send master acknowledge
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1801 rcall I2C_RCEN ; enable receive mode
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1802 movff SSP1BUF,D2_buffer+0 ; Low byte
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1803
650
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
1804 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop
bc214815deb2 3.19/10.75 release
heinrichsweikamp
parents: 649
diff changeset
1805 bcf i2c_busy_temperature ; reading new temperature done.
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1806
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1807 I2C_get_temp_val_MS5837_skip:
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1808 ; Start pressure measurement
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1809 rcall I2C_SEN ; start condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1810 movlw 0xEC ; address byte + write bit
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1811 rcall I2C_TX ; send byte
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1812 movlw 0x48 ; OSR=4096, type=D1
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1813 rcall I2C_TX ; send byte
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1814 rcall I2C_PEN ; stop condition
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1815 bsf ms5837_state ; =0: result of pressure will be in the ADC
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1816 bcf i2c_reinit_sensor2 ; Clear error flag
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1817 return
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1818
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1819
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1820 ;-----------------------------------------------------------------------------
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1821 ; I2C Bus error checker
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1822 ;
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1823 global check_i2c_error
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1824 extern TFT_message_i2c_error
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1825 check_i2c_error:
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1826 btfss i2c_error_flag
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1827 return
656
8af5aefbcdaf Update to 3.31 beta
heinrichsweikamp
parents: 655
diff changeset
1828 bcf i2c_error_flag_lock ; arm error vault again
643
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1829 incf message_counter,F ; increase message counter
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1830 goto TFT_message_i2c_error ; show message for battery low (battery percent) and return
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1831
7d8a4c60ec1a 3.15 release
heinrichsweikamp
parents: 640
diff changeset
1832 ;-----------------------------------------------------------------------------
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1833 END