comparison src/i2c.asm @ 466:7f4c3b2e6bb7

battery gauge handling
author heinrichsweikamp
date Sat, 19 Nov 2016 18:37:47 +0100
parents 850c1f3c4824
children 6ca155fc1509
comparison
equal deleted inserted replaced
465:025a0ce21f8b 466:7f4c3b2e6bb7
702 bra lt2942_get_voltage2 702 bra lt2942_get_voltage2
703 703
704 ; Update battery voltage in mV 704 ; Update battery voltage in mV
705 movff xC+1,batt_voltage+1 705 movff xC+1,batt_voltage+1
706 movff xC+0,batt_voltage+0 706 movff xC+0,batt_voltage+0
707 return 707
708 tstfsz batt_voltage+1 ; <256mV?
709 return ; No, done.
710
711 rcall lt2942_init
712 return
708 713
709 ; global lt2942_get_temperature 714 ; global lt2942_get_temperature
710 ;lt2942_get_temperature: ; Read temperature registers 715 ;lt2942_get_temperature: ; Read temperature registers
711 ; clrf i2c_temp 716 ; clrf i2c_temp
712 ; movlw 0x0C ; Point to temperature registers 717 ; movlw 0x0C ; Point to temperature registers
755 ; return 760 ; return
756 761
757 global lt2942_get_accumulated_charge 762 global lt2942_get_accumulated_charge
758 lt2942_get_accumulated_charge: ; Read accumulated charge and compute percent 763 lt2942_get_accumulated_charge: ; Read accumulated charge and compute percent
759 clrf i2c_temp 764 clrf i2c_temp
760 movlw 0x02 ; Point to accumulated charge registers 765 movlw 0x00 ; Point to status register
761 call I2C_TX_GAUGE 766 call I2C_TX_GAUGE
762 call I2C_RX_GAUGE 767 call I2C_RX_GAUGE
763 bsf SSP1CON2,ACKEN ; Master acknowlegde 768 bsf SSP1CON2,ACKEN ; Master acknowlegde
764 rcall WaitMSSP 769 rcall WaitMSSP
765 movff SSP1BUF,sub_a+1 ; battery_acumulated_charge+1 770 movff SSP1BUF,gauge_status_byte
766 bsf SSP1CON2, RCEN ; Enable recieve mode 771
767 rcall WaitMSSP 772 bsf SSP1CON2, RCEN ; Enable recieve mode
768 movff SSP1BUF,sub_a+0 ; battery_acumulated_charge+0 773 rcall WaitMSSP ; Dummy read (Control byte)
769 bsf SSP1CON2,PEN ; Stop condition 774
770 rcall WaitMSSP 775 bsf SSP1CON2, RCEN ; Enable recieve mode
771 776 rcall WaitMSSP
777 movff SSP1BUF,sub_a+1
778
779 bsf SSP1CON2, RCEN ; Enable recieve mode
780 rcall WaitMSSP
781 movff SSP1BUF,sub_a+0
782 bsf SSP1CON2,PEN ; Stop condition
783 rcall WaitMSSP
784
785 movff gauge_status_byte,sub_b+0 ; copy into bank common
786 btfsc sub_b+0,0 ; =1: UVLO Event
787 rcall lt2942_init
788
789 movff sub_a+1,battery_acumulated_charge+1 ; Save raw value
790 movff sub_a+0,battery_acumulated_charge+0 ; Save raw value
791
772 ; Compute batt_percent 792 ; Compute batt_percent
773 ; (charge-battery_offset)/365 793 ; (charge-battery_offset)/365
774 movff battery_offset+0,sub_b+0 794 movff battery_offset+0,sub_b+0
775 movff battery_offset+1,sub_b+1 795 movff battery_offset+1,sub_b+1
776 call subU16 ; sub_c = sub_a - sub_b (with signed values) 796 call subU16 ; sub_c = sub_a - sub_b (with signed values)