comparison src/i2c.asm @ 448:aadfe9f2edaf

work on new battery options
author heinrichsweikamp
date Tue, 30 Aug 2016 17:26:21 +0200
parents 4b93354b7738
children 850c1f3c4824
comparison
equal deleted inserted replaced
447:f2a1d535347f 448:aadfe9f2edaf
20 20
21 21
22 #include "hwos.inc" ; Mandatory header 22 #include "hwos.inc" ; Mandatory header
23 #include "wait.inc" 23 #include "wait.inc"
24 #include "math.inc" 24 #include "math.inc"
25
26 #DEFINE battery_offset .29065 ; 65536-(3,1Ah/0,085mAh)
27 #DEFINE battery_devider .365 ; 3,1Ah/0,085mAh/100 [%]
28 25
29 i2c CODE 26 i2c CODE
30 27
31 WaitMSSP: 28 WaitMSSP:
32 decfsz i2c_temp,F ; check for timeout during I2C action 29 decfsz i2c_temp,F ; check for timeout during I2C action
498 rcall I2C_TX 495 rcall I2C_TX
499 movlw b'01100100' ; CTRL5 HIGH res, 6,25Hz 496 movlw b'01100100' ; CTRL5 HIGH res, 6,25Hz
500 rcall I2C_TX 497 rcall I2C_TX
501 init_compass1_common: 498 init_compass1_common:
502 ;movlw b'01100000' ; CTRL6 Full scale (+/-12 Gauss -> 2730LSB/Gauss) 499 ;movlw b'01100000' ; CTRL6 Full scale (+/-12 Gauss -> 2730LSB/Gauss)
503 movlw b'00000000' ; CTRL6 (+/-2 Gauss) 500 ;movlw b'00000000' ; CTRL6 (+/-2 Gauss)
501 movlw b'00100000' ; CTRL6 (+/-4 Gauss)
504 rcall I2C_TX 502 rcall I2C_TX
505 movlw b'00000000' ; CTRL7 Continuous Mode 503 movlw b'00000000' ; CTRL7 Continuous Mode
506 rcall I2C_TX 504 rcall I2C_TX
507 bsf SSP1CON2,PEN ; Stop condition 505 bsf SSP1CON2,PEN ; Stop condition
508 rcall WaitMSSP 506 rcall WaitMSSP
653 rcall WaitMSSP 651 rcall WaitMSSP
654 return 652 return
655 653
656 global lt2942_get_status 654 global lt2942_get_status
657 lt2942_get_status: ; Read status register 655 lt2942_get_status: ; Read status register
658 bcf rechargeable ; Clear flag 656 bcf battery_gauge_available ; Clear flag
659 clrf i2c_temp 657 clrf i2c_temp
660 movlw 0x00 ; Point to Status reg 658 movlw 0x00 ; Point to Status reg
661 call I2C_TX_GAUGE 659 call I2C_TX_GAUGE
662 call I2C_RX_GAUGE 660 call I2C_RX_GAUGE
663 movff SSP1BUF,WREG 661 movff SSP1BUF,WREG
664 btfss WREG,7 ; 2942 found? 662 btfss WREG,7 ; 2942 found?
665 bsf rechargeable ; Yes, set flag 663 bsf battery_gauge_available ; Yes, set flag
666 bsf SSP1CON2,PEN ; Stop condition 664 bsf SSP1CON2,PEN ; Stop condition
667 rcall WaitMSSP 665 rcall WaitMSSP
668 return 666 return
669 667
670 668
671 global lt2942_get_voltage 669 global lt2942_get_voltage
672 lt2942_get_voltage: ; Read battery voltage registers 670 lt2942_get_voltage: ; Read battery voltage registers
673 clrf i2c_temp 671 clrf i2c_temp
674 movlw 0x08 ; Point to voltage registers 672 movlw 0x08 ; Point to voltage registers
675 call I2C_TX_GAUGE 673 rcall I2C_TX_GAUGE
676 call I2C_RX_GAUGE 674 rcall I2C_RX_GAUGE
677 bsf SSP1CON2,ACKEN ; Master acknowlegde 675 bsf SSP1CON2,ACKEN ; Master acknowlegde
678 rcall WaitMSSP 676 rcall WaitMSSP
679 movff SSP1BUF,xA+1 677 movff SSP1BUF,xA+1
680 bsf SSP1CON2, RCEN ; Enable recieve mode 678 bsf SSP1CON2, RCEN ; Enable recieve mode
681 rcall WaitMSSP 679 rcall WaitMSSP
771 bsf SSP1CON2,PEN ; Stop condition 769 bsf SSP1CON2,PEN ; Stop condition
772 rcall WaitMSSP 770 rcall WaitMSSP
773 771
774 ; Compute batt_percent 772 ; Compute batt_percent
775 ; (charge-battery_offset)/365 773 ; (charge-battery_offset)/365
776 movlw LOW battery_offset 774 movff battery_offset+0,sub_b+0
777 movwf sub_b+0 775 movff battery_offset+1,sub_b+1
778 movlw HIGH battery_offset
779 movwf sub_b+1
780 call subU16 ; sub_c = sub_a - sub_b (with signed values) 776 call subU16 ; sub_c = sub_a - sub_b (with signed values)
781 777
782 clrf batt_percent ; Set to zero 778 clrf batt_percent ; Set to zero
783 btfsc neg_flag ; result negative? 779 btfsc neg_flag ; result negative?
784 return ; Yes, done. 780 return ; Yes, done.
785 781
786 ; > Zero, set batt_percent properly 782 ; > Zero, set batt_percent properly
787 movff sub_c+0,xA+0 783 movff sub_c+0,xA+0
788 movff sub_c+1,xA+1 784 movff sub_c+1,xA+1
789 movlw LOW battery_devider 785 movff battery_capacity+0,xB+0
790 movwf xB+0 786 movff battery_capacity+1,xB+1
791 movlw HIGH battery_devider
792 movwf xB+1
793 call div16x16 ;xA/xB=xC with xA+0 as remainder, uses divB as temp variable 787 call div16x16 ;xA/xB=xC with xA+0 as remainder, uses divB as temp variable
794 movff xC+0,batt_percent 788 movff xC+0,batt_percent
795 return 789 return
796 790
797 global lt2942_charge_done 791 global lt2942_charge_done
798 lt2942_charge_done: ; Reset accumulating registers to 0xFFFF 792 lt2942_charge_done: ; Reset accumulating registers to 0xFFFF
799 clrf i2c_temp 793 clrf i2c_temp
800 movlw 0x02 ; Point to accumulated charge registers 794 movlw 0x02 ; Point to accumulated charge registers
801 call I2C_TX_GAUGE 795 rcall I2C_TX_GAUGE
802 movlw 0xFF 796 setf SSP1BUF ; Data Byte
803 movff WREG, SSP1BUF ; Data Byte
804 rcall WaitMSSP 797 rcall WaitMSSP
805 rcall I2C_WaitforACK 798 rcall I2C_WaitforACK
806 movlw 0xFF 799 setf SSP1BUF ; Data Byte
807 movff WREG, SSP1BUF ; Data Byte
808 rcall WaitMSSP 800 rcall WaitMSSP
809 rcall I2C_WaitforACK 801 rcall I2C_WaitforACK
810 bsf SSP1CON2,PEN ; Stop condition 802 bsf SSP1CON2,PEN ; Stop condition
811 rcall WaitMSSP 803 bra WaitMSSP; (and return)
812 return
813 804
814 I2C_TX_GAUGE: ; Sends a byte to the LT2942 Gauge IC 805 I2C_TX_GAUGE: ; Sends a byte to the LT2942 Gauge IC
815 movwf i2c_temp+1 ; Data byte 806 movwf i2c_temp+1 ; Data byte
816 bsf SSP1CON2,SEN ; Start condition 807 bsf SSP1CON2,SEN ; Start condition
817 rcall WaitMSSP 808 rcall WaitMSSP
818 movlw b'11001000' ; Address byte + Write bit 809 movlw b'11001000' ; Address byte + Write bit
819 movwf SSP1BUF ; control byte 810 movwf SSP1BUF ; control byte
820 rcall WaitMSSP 811 rcall WaitMSSP
821 rcall I2C_WaitforACK 812 rcall I2C_WaitforACK
822 movff i2c_temp+1, SSP1BUF ; Data Byte 813 movff i2c_temp+1, SSP1BUF ; Data Byte
823 rcall WaitMSSP 814 rcall WaitMSSP
824 rcall I2C_WaitforACK 815 bra I2C_WaitforACK ; (and return)
825 return
826 816
827 I2C_RX_GAUGE: 817 I2C_RX_GAUGE:
828 bsf SSP1CON2,SEN ; Start condition 818 bsf SSP1CON2,SEN ; Start condition
829 rcall WaitMSSP 819 rcall WaitMSSP
830 movlw b'11001001' ; Address byte + Read bit 820 movlw b'11001001' ; Address byte + Read bit
831 movwf SSP1BUF ; control byte 821 movwf SSP1BUF ; control byte
832 rcall WaitMSSP 822 rcall WaitMSSP
833 rcall I2C_WaitforACK 823 rcall I2C_WaitforACK
834 bsf SSP1CON2, RCEN ; Enable recieve mode 824 bsf SSP1CON2, RCEN ; Enable recieve mode
835 rcall WaitMSSP 825 bra WaitMSSP; (and return)
836 return 826
837 827 END
838
839 END