0
|
1 ;=============================================================================
|
|
2 ;
|
|
3 ; File eeprom_rs232.asm
|
|
4 ;
|
|
5 ; Internal EEPROM, RS232
|
|
6 ;
|
|
7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved.
|
|
8 ;=============================================================================
|
|
9 ; HISTORY
|
|
10 ; 2011-08-06 : [mH] moving from OSTC code
|
|
11
|
|
12 #include "ostc3.inc"
|
|
13 #include "start.inc"
|
|
14 #include "tft.inc"
|
|
15 #include "wait.inc"
|
|
16 #include "strings.inc"
|
|
17 #include "convert.inc"
|
113
|
18 #include "adc_lightsensor.inc"
|
|
19 #include "math.inc"
|
0
|
20
|
|
21 ;=============================================================================
|
|
22 eeprom code 0xF00000+0x10
|
|
23 ; Skip SERIAL number. Should not be overwritten.
|
|
24 global eeprom_serial_save, eeprom_opt_backup
|
|
25 eeprom_serial_save res 2
|
|
26 eeprom_opt_backup res 0x3E
|
|
27
|
|
28 ;=============================================================================
|
|
29 basic CODE
|
|
30
|
|
31 global write_int_eeprom_1
|
|
32 write_int_eeprom_1:
|
|
33 movwf EEADR
|
|
34 bra write_eeprom ; writes and "returns" after write
|
|
35
|
|
36 global read_int_eeprom_1
|
|
37 read_int_eeprom_1:
|
|
38 movwf EEADR
|
|
39 bra read_eeprom ; reads and "returns" after write
|
|
40
|
|
41 ;=============================================================================
|
|
42 ; reads from internal eeprom
|
|
43 ; Input: EEADRH:EEADR = EEPROM address.
|
|
44 ; Output: EEDATA.
|
|
45 ; Trashed: NONE.
|
|
46 global read_eeprom
|
|
47 read_eeprom:
|
|
48 bcf EECON1,EEPGD
|
|
49 bcf EECON1,CFGS
|
|
50 bsf EECON1,RD
|
|
51 return
|
|
52
|
|
53 ;=============================================================================
|
|
54 ; writes into internal eeprom
|
|
55 ; Input: EEADRH:EEADR = EEPROM address.
|
|
56 ; EEDATA = byte to write.
|
|
57 ; Trashed: WREG.
|
|
58 global write_eeprom
|
|
59 write_eeprom:
|
|
60 bcf EECON1,EEPGD
|
|
61 bcf EECON1,CFGS
|
|
62 bsf EECON1,WREN
|
|
63
|
|
64 bcf INTCON,GIE ; even the RTC will be delayed for the next 5 instructions...
|
|
65 movlw 0x55
|
|
66 movwf EECON2
|
|
67 movlw 0xAA
|
|
68 movwf EECON2
|
|
69 bsf EECON1,WR
|
|
70 bsf INTCON,GIE ; ...but the flag for the ISR routines were still set, so they will interrupt now!
|
|
71
|
|
72 write_eep2:
|
|
73 btfsc EECON1,WR
|
|
74 bra write_eep2 ; wait about 4ms...
|
|
75 bcf EECON1,WREN
|
|
76 return
|
|
77
|
|
78 global disable_ir
|
|
79 disable_ir:
|
|
80 banksel TXSTA2
|
|
81 clrf TXSTA2
|
|
82 clrf RCSTA2
|
|
83 banksel common
|
|
84 bcf ir_power ; IR off
|
113
|
85 bcf mcp_power ; Power-down intrumentation amp
|
|
86 bsf s8_npower ; Power-down S8 HUD
|
0
|
87 return
|
|
88
|
|
89 global enable_ir
|
|
90 enable_ir:
|
|
91 ;init serial port2 (TRISG2)
|
113
|
92 btfsc c3_hardware
|
|
93 bra enable_s8 ; Start S8
|
|
94
|
|
95 banksel BAUDCON2
|
|
96 movlw b'00100000' ; BRG16=0 ; inverted for IR
|
|
97 movwf BAUDCON2
|
0
|
98 banksel TXSTA2
|
|
99 movlw b'00100000' ; BRGH=0, SYNC=0
|
|
100 movwf TXSTA2
|
|
101 movlw .102 ; SPBRGH:SPBRG = .102 : 2403 BAUD @ 16MHz
|
|
102 movwf SPBRG2
|
|
103 clrf SPBRGH2
|
|
104 movlw b'10010000'
|
|
105 movwf RCSTA2
|
|
106 banksel common
|
113
|
107 bsf ir_power ; Power-up IR
|
0
|
108 btfss ir_power
|
|
109 bra $-6
|
|
110 return
|
|
111
|
113
|
112 enable_s8:
|
|
113 ; Check for Digital/Analog
|
|
114 bsf s8_npower ; Power-down S8 HUD
|
|
115 WAITMS d'1' ; Very short delay
|
|
116 bsf mcp_power ; Power-up intrumentation amp
|
|
117 btfss mcp_power
|
|
118 bra $-6
|
|
119 banksel TXSTA2
|
|
120 clrf TXSTA2
|
|
121 clrf RCSTA2
|
|
122 banksel common
|
|
123
|
|
124 ; It may be digital, check for voltage when isolator is powered
|
|
125 bcf s8_npower ; Power S8 HUD
|
|
126 WAITMS d'1' ; Very short delay
|
|
127
|
|
128 btfsc PORTG,2 ; RX2=1?
|
|
129 bra enable_s8_2 ; Yes, digital
|
|
130 WAITMS d'30'
|
|
131 btfsc PORTG,2 ; RX2=1?
|
|
132 bra enable_s8_2 ; Yes, digital
|
|
133
|
|
134 ; Not found, set to analog (fail-safe)
|
|
135
|
|
136 enable_s8_analog:
|
|
137 ; S8 Analog
|
|
138 bsf s8_npower ; Power-down S8 HUD
|
|
139 bcf s8_digital ; Clear flag
|
|
140 return
|
|
141
|
|
142 enable_s8_2: ; S8 Digital
|
|
143 banksel BAUDCON2
|
|
144 movlw b'00000000' ; BRG16=0 ; normal for S8
|
|
145 movwf BAUDCON2
|
|
146 banksel TXSTA2
|
|
147 movlw b'00100000' ; BRGH=0, SYNC=0
|
|
148 movwf TXSTA2
|
|
149 movlw .25 ; SPBRGH:SPBRG = .25 : 9615 BAUD @ 16MHz
|
|
150 movwf SPBRG2
|
|
151 clrf SPBRGH2
|
|
152 movlw b'10010000'
|
|
153 movwf RCSTA2
|
|
154 banksel common
|
|
155 bcf s8_npower ; Power S8 HUD
|
|
156 bsf s8_digital ; Set flag
|
|
157 return
|
|
158
|
0
|
159 ;=============================================================================
|
|
160 global enable_rs232
|
|
161 enable_rs232:
|
|
162 bcf TRISC,6 ; Output
|
|
163 bsf TRISC,7 ; Input
|
|
164 call speed_normal ; 16MHz
|
|
165 enable_rs232_2:
|
|
166 movlw T2CON_NORMAL
|
|
167 cpfseq T2CON
|
|
168 bra enable_rs232_2 ; Wait until speed is normal
|
|
169 ;init serial port1 (TRISC6/7)
|
|
170 clrf RCSTA1
|
|
171 clrf TXSTA1
|
|
172 movlw b'00001000' ; BRG16=1
|
|
173 movwf BAUDCON1
|
|
174 movlw b'00100100' ; BRGH=1, SYNC=0
|
|
175 movwf TXSTA1
|
|
176 movlw .34 ; SPBRGH:SPBRG = .34 : 114285 BAUD @ 16MHz (+0,79% Error to 115200 BAUD)
|
|
177 movwf SPBRG1
|
|
178 clrf SPBRGH1
|
|
179 movlw b'10010000'
|
|
180 movwf RCSTA1
|
|
181 return
|
|
182
|
|
183 global disable_rs232
|
|
184 disable_rs232:
|
|
185 clrf RCSTA1
|
|
186 clrf TXSTA1 ; UART disable
|
|
187 bsf TRISC,6 ; Input
|
|
188 bsf TRISC,7 ; Input
|
|
189 return
|
|
190
|
|
191 global rs232_wait_tx
|
|
192 rs232_wait_tx:
|
|
193 btfsc TXSTA1,TRMT ; Transmit Shift Register empty?
|
|
194 return ; Yes, return!
|
|
195
|
113
|
196 btfss TXSTA1,TRMT ; RS232 Busy?
|
0
|
197 bra rs232_wait_tx ; yes, wait...
|
|
198 return ; Done.
|
|
199
|
113
|
200 global rs232_wait_tx2
|
|
201 rs232_wait_tx2:
|
|
202 banksel TXSTA2
|
|
203 btfsc TXSTA2,TRMT ; Transmit Shift Register empty?
|
|
204 bra rs232_wait_tx2_2 ; Yes, return!
|
|
205
|
|
206 btfss TXSTA2,TRMT ; RS232 Busy?
|
|
207 bra rs232_wait_tx2 ; yes, wait...
|
|
208 rs232_wait_tx2_2:
|
|
209 banksel common
|
|
210 return ; Done.
|
|
211
|
0
|
212 global rs232_get_byte
|
|
213 rs232_get_byte:
|
|
214 bcf PIR1,RCIF ; clear flag
|
|
215 bcf rs232_recieve_overflow ; clear flag
|
|
216 clrf uart1_temp
|
|
217 clrf uart2_temp
|
|
218 rs232_get_byte2:
|
|
219 btfsc PIR1,RCIF ; data arrived?
|
|
220 return
|
|
221 ; bra rs232_get_byte3
|
|
222
|
|
223 decfsz uart2_temp,F
|
|
224 bra rs232_get_byte2
|
|
225 decfsz uart1_temp,F
|
|
226 bra rs232_get_byte2
|
|
227 ; timeout occoured (about 20ms)
|
|
228 bsf rs232_recieve_overflow ; set flag
|
|
229 ;rs232_get_byte3:
|
|
230 bcf RCSTA1,CREN ; Clear receiver status
|
|
231 bsf RCSTA1,CREN
|
|
232 return ; and return anyway
|
|
233
|
|
234 END |