annotate src/i2c.asm @ 623:c40025d8e750

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