annotate src/i2c.asm @ 627:bf5fee575701

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