annotate src/i2c.asm @ 628:cd58f7fc86db

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