comparison src/i2c.asm @ 113:f3062a611eef

Merge
author heinrichsweikamp
date Mon, 23 Jun 2014 16:14:33 +0200
parents 9b7dd3103545
children 683321c09cfa
comparison
equal deleted inserted replaced
112:a24581f0b372 113:f3062a611eef
16 ; 2012-08-22 : [mH] Creation 16 ; 2012-08-22 : [mH] Creation
17 17
18 18
19 #include "ostc3.inc" ; Mandatory header 19 #include "ostc3.inc" ; Mandatory header
20 #include "wait.inc" 20 #include "wait.inc"
21 #include "math.inc"
22
23 #DEFINE battery_offset .27302 ; 65536-(3,25Ah/0,085mAh)
24 #DEFINE battery_devider .382 ; 3,25Ah/0,085mAh/100 [%]
21 25
22 i2c CODE 26 i2c CODE
23 27
24 WaitMSSP: 28 WaitMSSP:
25 decfsz i2c_temp,F ; check for timeout during I2C action 29 decfsz i2c_temp,F ; check for timeout during I2C action
346 rcall I2C_TX 350 rcall I2C_TX
347 bsf SSP1CON2,PEN ; Stop condition 351 bsf SSP1CON2,PEN ; Stop condition
348 rcall WaitMSSP 352 rcall WaitMSSP
349 return 353 return
350 354
355 global lt2942_init
356 lt2942_init: ; Setup Control register B
357 clrf i2c_temp
358 movlw 0x01 ; Point to control reg B
359 call I2C_TX_GAUGE
360 movlw b'11111000' ; Automatic conversion every two seconds
361 movff WREG, SSP1BUF ; Data Byte
362 rcall WaitMSSP
363 rcall I2C_WaitforACK
364 bsf SSP1CON2,PEN ; Stop condition
365 rcall WaitMSSP
366 return
367
368 global lt2942_get_status
369 lt2942_get_status: ; Read status register
370 bcf c3_hardware ; Clear flag
371 clrf i2c_temp
372 movlw 0x00 ; Point to Status reg
373 call I2C_TX_GAUGE
374 call I2C_RX_GAUGE
375 movff SSP1BUF,WREG
376 btfss WREG,7 ; 2942 found?
377 bsf c3_hardware ; Yes, set flag
378 bsf SSP1CON2,PEN ; Stop condition
379 rcall WaitMSSP
380 return
381
382
383 global lt2942_get_voltage
384 lt2942_get_voltage: ; Read battery voltage registers
385 clrf i2c_temp
386 movlw 0x08 ; Point to voltage registers
387 call I2C_TX_GAUGE
388 call I2C_RX_GAUGE
389 bsf SSP1CON2,ACKEN ; Master acknowlegde
390 rcall WaitMSSP
391 movff SSP1BUF,xA+1
392 bsf SSP1CON2, RCEN ; Enable recieve mode
393 rcall WaitMSSP
394 movff SSP1BUF,xA+0
395 bsf SSP1CON2,PEN ; Stop condition
396 rcall WaitMSSP
397
398 ; banksel common
399 ; xA:2 loaded with raw values
400 movlw LOW .6000
401 movwf xB+0
402 movlw HIGH .6000
403 movwf xB+1
404 call mult16x16 ;xA*xB=xC
405
406 ; devide xC (32bit)/65535 for result in mV (16bit)
407 movlw .16
408 movwf i2c_temp
409 lt2942_get_voltage2:
410 bcf STATUS,C
411 rrcf xC+3,F
412 rrcf xC+2,F
413 rrcf xC+1,F
414 rrcf xC+0,F
415 decfsz i2c_temp,F
416 bra lt2942_get_voltage2
417
418 ; Update battery voltage in mV
419 movff xC+1,batt_voltage+1
420 movff xC+0,batt_voltage+0
421 return
422
423 ; global lt2942_get_temperature
424 ;lt2942_get_temperature: ; Read temperature registers
425 ; clrf i2c_temp
426 ; movlw 0x0C ; Point to temperature registers
427 ; call I2C_TX_GAUGE
428 ; call I2C_RX
429 ; bsf SSP1CON2,ACKEN ; Master acknowlegde
430 ; rcall WaitMSSP
431 ; movff SSP1BUF,xA+1
432 ; bsf SSP1CON2, RCEN ; Enable recieve mode
433 ; rcall WaitMSSP
434 ; movff SSP1BUF,xA+0
435 ; bsf SSP1CON2,PEN ; Stop condition
436 ; rcall WaitMSSP
437 ;
438 ;; banksel common
439 ; ; xA:2 loaded with raw values
440 ; movlw LOW .6000
441 ; movwf xB+0
442 ; movlw HIGH .6000
443 ; movwf xB+1
444 ; call mult16x16 ;xA*xB=xC
445 ;
446 ; ; devide xC (32bit)/65535 for result in 0.1K (16bit)
447 ; movlw .16
448 ; movwf i2c_temp
449 ;lt2942_get_temperature2:
450 ; bcf STATUS,C
451 ; rrcf xC+3,F
452 ; rrcf xC+2,F
453 ; rrcf xC+1,F
454 ; rrcf xC+0,F
455 ; decfsz i2c_temp,F
456 ; bra lt2942_get_temperature2
457 ;
458 ; movff xC+1,sub_a+1
459 ; movff xC+0,sub_a+0
460 ; movlw LOW .2731 ; Kelvin to Celcius offset
461 ; movwf sub_b+0
462 ; movlw HIGH .2731 ; Kelvin to Celcius offset
463 ; movwf sub_b+1
464 ; call subU16 ; sub_c = sub_a - sub_b (with UNSIGNED values)
465 ;
466 ; ; Update batttery_temperature in 0.1°C
467 ; movff sub_c+1,battery_temperature+1
468 ; movff sub_c+0,battery_temperature+0
469 ; return
470
471 global lt2942_get_accumulated_charge
472 lt2942_get_accumulated_charge: ; Read accumulated charge and compute percent
473 clrf i2c_temp
474 movlw 0x02 ; Point to accumulated charge registers
475 call I2C_TX_GAUGE
476 call I2C_RX_GAUGE
477 bsf SSP1CON2,ACKEN ; Master acknowlegde
478 rcall WaitMSSP
479 movff SSP1BUF,sub_a+1 ; battery_acumulated_charge+1
480 bsf SSP1CON2, RCEN ; Enable recieve mode
481 rcall WaitMSSP
482 movff SSP1BUF,sub_a+0 ; battery_acumulated_charge+0
483 bsf SSP1CON2,PEN ; Stop condition
484 rcall WaitMSSP
485
486 ; Compute batt_percent
487 ; charge-battery_offset/382
488 movlw LOW battery_offset
489 movwf sub_b+0
490 movlw HIGH battery_offset
491 movwf sub_b+1
492 call subU16 ; sub_c = sub_a - sub_b (with signed values)
493
494 clrf batt_percent ; Set to zero
495 btfsc neg_flag ; result negative?
496 return ; Yes, done.
497
498 ; > Zero, set batt_percent properly
499 movff sub_c+0,xA+0
500 movff sub_c+1,xA+1
501 movlw LOW battery_devider
502 movwf xB+0
503 movlw HIGH battery_devider
504 movwf xB+1
505 call div16x16 ;xA/xB=xC with xA+0 as remainder, uses divB as temp variable
506 movff xC+0,batt_percent
507 return
508
509 global lt2942_charge_done
510 lt2942_charge_done: ; Reset accumulating registers to 0xFFFF
511 clrf i2c_temp
512 movlw 0x02 ; Point to accumulated charge registers
513 call I2C_TX_GAUGE
514 movlw 0xFF
515 movff WREG, SSP1BUF ; Data Byte
516 rcall WaitMSSP
517 rcall I2C_WaitforACK
518 movlw 0xFF
519 movff WREG, SSP1BUF ; Data Byte
520 rcall WaitMSSP
521 rcall I2C_WaitforACK
522 bsf SSP1CON2,PEN ; Stop condition
523 rcall WaitMSSP
524 return
525
526 I2C_TX_GAUGE: ; Sends a byte to the LT2942 Gauge IC
527 movwf i2c_temp+1 ; Data byte
528 bsf SSP1CON2,SEN ; Start condition
529 rcall WaitMSSP
530 movlw b'11001000' ; Address byte + Write bit
531 movwf SSP1BUF ; control byte
532 rcall WaitMSSP
533 rcall I2C_WaitforACK
534 movff i2c_temp+1, SSP1BUF ; Data Byte
535 rcall WaitMSSP
536 rcall I2C_WaitforACK
537 return
538
539 I2C_RX_GAUGE:
540 bsf SSP1CON2,SEN ; Start condition
541 rcall WaitMSSP
542 movlw b'11001001' ; Address byte + Read bit
543 movwf SSP1BUF ; control byte
544 rcall WaitMSSP
545 rcall I2C_WaitforACK
546 bsf SSP1CON2, RCEN ; Enable recieve mode
547 rcall WaitMSSP
548 return
549
550
351 551
352 END 552 END