annotate src/i2c.asm @ 632:0347acdf6d8e

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