comparison code_part1/OSTC_code_asm_part1/i2c_eeprom.asm @ 245:dee88c962653

Some clean up and minor I2C speed improvements
author heinrichsweikamp
date Thu, 24 Mar 2011 21:51:36 +0100
parents 055977afc2f9
children 7250ca7c8d24
comparison
equal deleted inserted replaced
244:976260083540 245:dee88c962653
218 bcf first_FD 218 bcf first_FD
219 return ; return 219 return ; return
220 220
221 221
222 I2CREAD: 222 I2CREAD:
223 bsf SSPCON2, PEN ; Stop 223 rcall I2CREAD_COMMON
224 rcall WaitMSSP
225 bsf SSPCON2,SEN ; Start condition
226 rcall WaitMSSP
227 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ
228 movwf SSPBUF ; control byte
229 rcall WaitMSSP
230 btfsc SSPCON2,ACKSTAT
231 bra I2CREAD ; EEPROM NOT acknowledged, retry!
232 movff eeprom_address+1,SSPBUF ; High Address byte
233 rcall WaitMSSP
234 rcall I2C_WaitforACK
235 movff eeprom_address+0,SSPBUF ; Low Address byte
236 rcall WaitMSSP
237 rcall I2C_WaitforACK
238
239 bsf SSPCON2,RSEN ; Start condition
240 rcall WaitMSSP
241 movlw b'10100111' ; Bit0=0: WRITE, Bit0=1: READ
242 movwf SSPBUF ; control byte
243 rcall WaitMSSP
244 rcall I2C_WaitforACK
245
246 bsf SSPCON2, RCEN ; Enable recieve mode
247 rcall WaitMSSP
248 movf SSPBUF,W ; copy read byte into WREG
249 bsf SSPCON2, PEN ; Stop 224 bsf SSPCON2, PEN ; Stop
250 rcall WaitMSSP 225 rcall WaitMSSP
251 return 226 return
252 227
253 I2CREAD2: ; same as I2CREAD but with automatic address increase 228 I2CREAD2: ; same as I2CREAD but with automatic address increase
254 rcall I2CREAD 229 rcall I2CREAD
230 I2CREAD2_2:
255 movlw d'1' 231 movlw d'1'
256 addwf eeprom_address+0,F 232 addwf eeprom_address+0,F
257 movlw d'0' 233 movlw d'0'
258 addwfc eeprom_address+1,F 234 addwfc eeprom_address+1,F
259 bcf eeprom_overflow 235 bcf eeprom_overflow
262 238
263 clrf eeprom_address+0 ; Yes, clear address 239 clrf eeprom_address+0 ; Yes, clear address
264 clrf eeprom_address+1 240 clrf eeprom_address+1
265 bsf eeprom_overflow ; and set overflow bit 241 bsf eeprom_overflow ; and set overflow bit
266 return 242 return
267 243
244 I2CREAD3: ; block read start with automatic address increase
245 rcall I2CREAD_COMMON
246 ; no Stop condition here
247 bra I2CREAD2_2
248
249 I2CREAD4: ; block read with automatic address increase
250 bsf SSPCON2,ACKEN ; Master acknowlegde
251 rcall WaitMSSP
252 bsf SSPCON2, RCEN ; Enable recieve mode
253 rcall WaitMSSP
254 movf SSPBUF,W ; copy read byte into WREG
255 ; no Stop condition here
256 bra I2CREAD2_2
257
258
259 I2CREAD_COMMON:
260 bsf SSPCON2, PEN ; Stop
261 rcall WaitMSSP
262 bsf SSPCON2,SEN ; Start condition
263 rcall WaitMSSP
264 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ
265 movwf SSPBUF ; control byte
266 rcall WaitMSSP
267 btfsc SSPCON2,ACKSTAT
268 bra I2CREAD ; EEPROM NOT acknowledged, retry!
269 movff eeprom_address+1,SSPBUF ; High Address byte
270 rcall WaitMSSP
271 rcall I2C_WaitforACK
272 movff eeprom_address+0,SSPBUF ; Low Address byte
273 rcall WaitMSSP
274 rcall I2C_WaitforACK
275
276 bsf SSPCON2,RSEN ; Start condition
277 rcall WaitMSSP
278 movlw b'10100111' ; Bit0=0: WRITE, Bit0=1: READ
279 movwf SSPBUF ; control byte
280 rcall WaitMSSP
281 rcall I2C_WaitforACK
282
283 bsf SSPCON2, RCEN ; Enable recieve mode
284 rcall WaitMSSP
285 movf SSPBUF,W ; copy read byte into WREG
286 return
287
288
268 I2CWRITE: 289 I2CWRITE:
269 movwf ext_ee_temp1 ; Data byte 290 movwf ext_ee_temp1 ; Data byte
270 bsf SSPCON2,SEN ; Start condition 291 bsf SSPCON2,SEN ; Start condition
271 rcall WaitMSSP 292 rcall WaitMSSP
272 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ 293 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ
282 movff ext_ee_temp1, SSPBUF ; Data Byte 303 movff ext_ee_temp1, SSPBUF ; Data Byte
283 rcall WaitMSSP 304 rcall WaitMSSP
284 rcall I2C_WaitforACK 305 rcall I2C_WaitforACK
285 bsf SSPCON2,PEN ; Stop condition 306 bsf SSPCON2,PEN ; Stop condition
286 rcall WaitMSSP 307 rcall WaitMSSP
287 WAITMS d'6' ; Write delay 308 WAITMS d'5' ; Write delay
288 return 309 return
289 310
290 I2C_WaitforACK: 311 I2C_WaitforACK:
291 btfsc SSPCON2,ACKSTAT ; checks for ACK bit from slave 312 btfsc SSPCON2,ACKSTAT ; checks for ACK bit from slave
292 rcall I2CFail 313 rcall I2CFail