Here is generic Basic Stamp code, capable of detecting, charging and reporting capacity of LiIon, NiCd and NiMH cells. Some proprietary algorithms hace been removed. Designed for any 3.6V pack, the algorithms are upwardly compatible to larger voltage battery systems. Steve Halla '{$STAMP BS2} '{$PBASIC 2.5} '{$PORT COM1} '========================================================================= ' Technology: VIR2003 CHEMISTRY INDEPENDENT BATTERY MANAGEMENT ' Firmware: VIR2003-BSII Board ' Version: VIR2003-BSII-V2.0 (for Parallax Basic Stamp II) ' Created by: VIR Technologies, Inc., 11/05/03 ' Revised by: VIR Technologies, Inc., 11/02/05 '========================================================================= ' Copyright 2005 by VIR Technologies, Inc. '========================================================================= ' ' Abbreviations ' CCCV = Constant Current/Constant V ' CC = Constant Current ' SS = Soft Start ' V = Voltage ' FC = Fast Charge ' MC = Maintenance Charge ' BAT = BAT ' MSEC = Milliseconds ' LEDpinrd CON 8 ' Red LED for polling & Ni confirmation LEDpinyl CON 9 ' Yellow LED for Li, failing Ni, or CCCV charge LEDpingr CON 10 ' Green LED for charge pulse ON CHGpin CON 11 ' CHG pin LOW to turn ON charge circuit RESETpin CON 22 ' RESET pin; 0 = active reset CCpin CON 12 ' CCpin LOW to enable CC charge ADcs CON 15 ' Chip select; 0 = active ADclock CON 14 ' Clock to ADC; out on rising, in on falling edge. ADdata CON 13 ' Data I/O pin number. config VAR Nib ' Configuration bits for ADC. AD VAR Word ' Variable to hold 12-bit AD result. DlychgRd CON 35 ' ADC read delay time during CHG pulse in MSEC DlyrstRd CON 7 ' ADC read delay time during rest pulse in msec ISSPeriod CON 200 ' Initial softstart pulse length in MSEC ChgPeriod CON 1050 ' Complete charge cycle length in MSEC ChgPlsAdd CON 16 ' Wait time added to previous SS charge pulse Voffset CON 1450 ' V offset due to layout, noise or design requirements mVolts VAR Word ' Millivolts imVolts VAR Word ' Initial mVolts reading, BAT at rest dmVolts VAR Word ' PROPRIETARY calculated variable rmVolts VAR Byte ' PROPRIETARY calculated variable rstmVolts VAR Word ' ADC read of mVolts during rest pulse chgmVolts VAR Word ' ADC read of mVolts during CHG pulse chgpulse VAR Word ' Charge pulse period, increases during SS to 100% pmVolts VAR Word ' Peak mVolts point batsoe VAR Word ' PROPRIETARY calculated variable, State of Energy of the battery startB VAR config.BIT0 ' Start bit for comm with ADC. sglDif VAR config.BIT1 ' Single-ended or differential mode. oddSign VAR config.BIT2 ' Channel selection. msbf VAR config.BIT3 ' Output 0s after data xfer complete. '========================================================================= ' Beginning of Program '========================================================================= Initalize: LOW RESETpin ' Clear variables PAUSE 1000 ' Pause to allow BASIC STAMP to reset HIGH LEDpinrd ' Turn ON red LED as startup test PAUSE 50 ' Hold red LED ON HIGH LEDpinyl ' Turn ON yellow LED as startup test PAUSE 50 ' Hold yellow LED ON HIGH LEDpingr ' Turn ON red LED as startup test PAUSE 50 ' Hold red LED ON LOW CCpin ' Turn ON charge circuit HIGH CHGpin ' Disable CC mode, and activate CCCV mode chgpulse = ISSPeriod ' Set chgpulse period, default is 200 ms GOSUB Read1298 ' Reading initial milliV reading from BAT imVolts = AD + Voffset ' Write V reading plus V offset to variable IF imVolts < 3000 THEN Polling ' This value should be 66% of nominal pack V pmVolts = imVolts ' Set peak millivolts to initial millivolt reading DEBUG CLS ' Clear screen for new DEBUG data DEBUG "imVolts,ADC,chgmVolts,rstmVolts,dmVolts,rmVolts,pmVolts,chgpulse,SOE", CR Main: LOW LEDpinrd ' Turn OFF red LED FOr startup LOW LEDpinyl ' Turn OFF yellow LED for startup LOW CCpin ' Engage CC Charge LOW CHGpin ' Turn ON Charge Circuit HIGH LEDpingr ' Turn ON green LED to indicate charge pulse PAUSE DlychgRd ' Pause to let charge circuit stabilize GOSUB Read1298 ' Read BAT V under charge chgmvolts = AD + Voffset ' Store charge V plus V offset to variable chgpulse = chgpulse + ChgPlsAdd ' Increase charge pulse width by designated length IF chgmVolts > 4850 THEN Polling ' Poll for BAT is open circuit V is too high HIGH CHGpin ' Turn OFF charge circuit HIGH CCpin ' Disable CC mode LOW LEDpingr ' Turn OFF green LED to indicate charge pulse OFF PAUSE DlyrstRd ' Pause to let charge circuit stabilize GOSUB Read1298 rstmVolts = AD + Voffset GOSUB Display ' Send variable data and calculations to screen PAUSE 1016 - chgpulse ' Set SoftStart duration to approximately 90 seconds ' dmVolts = PROPRIETARY algorithm ' rmVolts = PROPRIETARY algorithm ' Here is where patent pending code detects the type of battery chemistry ' IF (dmVolts....PROPRIETARY ALGORITHM) THEN LiIon ' CC/CV charge for Lithium Ion Lead Acid batteries,and check capacity ' IF (PROPRIETARY ALGORITHM) THEN FastCharge ' CC charge for Ni batteries, and check capacity ' IF (PROPRIETARY ALGORITHM) THEN Alkaline ' Battery identified as primary batteries, no charge, check capacity GOTO Main ' do it again END '========================================================================================= ' ADC Subroutine '========================================================================================= ' The Stamp first sends the config bits to the 1298, then clocks in the ' conversion data. Shiftout sends data bits to pin DIO and clock ' the CLK pin. Sending the least-significant bit first, it shifts out ' the four bits of the variable config. Then Shiftin changes DIO to ' input and clocks in the data bits--most-significant bit first, post ' clock (valid after clock pulse). It shifts in 12 bits to the ' variable AD. Read1298: LOW ADcs ' Activate the ADC. config = config | %1101 ' Set all bits except oddSign. SHIFTOUT ADdata,ADclock,LSBFIRST,[config\4] ' Send config bits. SHIFTIN ADdata,ADclock,MSBPOST,[AD\12] ' Get DATa bits. HIGH ADcs ' Deactivate the ADC. RETURN ' Return to program. ' ======================================================================================= ' CAPACITY Subroutine ' ======================================================================================= Capacity: ' Copyrighted code is located here to be used by the patent pending source code. ' To review the patent-pending source code listed above, you will need to purchase a license to ' receive a serial number and unlock the file. SERIAL NUMBERS are site specific, and allow you ' to use and integrate the copyright protected source code into your projects or products. The ' license does NOT allow you to re-sell the source code, without written permission from ' VIR Technologies, Inc. RETURN NiCapacity: ' Copyrighted code is located here to be used by the patent pending source code. ' To review the patent-pending source code listed above, you will need to purchase a license to ' receive a serial number and unlock the file. SERIAL NUMBERS are site specific, and allow you ' to use and integrate the copyright protected source code into your projects or products. The ' license does NOT allow you to re-sell the source code, without written permission from ' VIR Technologies, Inc. RETURN LiCapacity: ' Copyrighted code is located here to be used by the patent pending source code. ' To review the patent-pending source code listed above, you will need to purchase a license to ' receive a serial number and unlock the file. SERIAL NUMBERS are site specific, and allow you ' to use and integrate the copyright protected source code into your projects or products. The ' license does NOT allow you to re-sell the source code, without written permission from ' VIR Technologies, Inc. RETURN Empty: batsoe = 0 ' Set State-Of-Energy to 0% RETURN Display: ' Send appropriate data to screen through DEBUG DEBUG DEC imVolts DIG 3, ".", DEC3 imVolts, "," DEBUG DEC AD + Voffset, "," DEBUG DEC chgmVolts DIG 3, ".", DEC3 chgmVolts, "," DEBUG DEC rstmVolts DIG 3, ".", DEC3 rstmVolts, "," DEBUG DEC dmVolts DIG 3, ".", DEC3 dmVolts, "," DEBUG DEC rmVolts DIG 3, ".", DEC3 rmVolts, "," DEBUG DEC pmVolts DIG 3, ".", DEC3 pmVolts, "," DEBUG DEC3 chgpulse, "," DEBUG DEC batsoe DIG 3, DEC batsoe DIG 2, DEC batsoe DIG 1, ".", DEC1 batsoe, "%", CR RETURN Polling: LOW RESETpin ' Reset variables LOW LEDpinrd ' Turn OFF red LED LOW LEDpinyl ' Turn OFF yellow LED LOW LEDpingr ' Turn OFF green LED DEBUG CLS ' Clear screen for DEBUG data DEBUG "POLLING FOR BAT", CR ' Send indication that polling is active HIGH CHGpin ' Turn OFF charge circuit as a reset command HIGH CCpin ' Disable CC mode, and engage CCCV mode LOW CHGpin ' Turn ON charge circuit, to startup in CCCV mode HIGH LEDpinrd ' Begin polling by turning red LED ON PAUSE 500 ' Red LED ON for 500 MSEC GOSUB Read1298 ' Goto read BAT V under charge chgmVolts = AD + Voffset ' Store BAT V under charge, plus offset, to variable IF chgmVolts > 4200 THEN Initalize ' If BAT V under charge is above low level, initialize HIGH CHGpin ' Turn OFF charge circuit LOW LEDpinrd ' Turn OFF red LED to indicate end of polling pulse PAUSE 500 ' Pause for BAT V to settle DEBUG CLS ' Clear screen for new DEBUG data GOSUB Read1298 ' Goto read BAT V at rest rstmVolts = AD + Voffset ' Store BAT V at rest, plus offset, to variable IF rstmVolts < 4150 THEN Initalize ' If BAT volage at rest is below high level, initialize GOTO Polling ' do it again END FastCharge: ' Industry standard fast charge for Ni batteries LOW LEDpinrd ' Turn OFF red LED LOW LEDpinyl ' Turn OFF yellow LED LOW CHGpin ' Turn ON charge circuit LOW CCPin ' Enable CC mode HIGH LEDpingr ' Turn ON red LED PAUSE DlychgRd ' Pause to let charge circuit stabilize GOSUB Read1298 ' Goto read BAT V under charge chgmvolts = AD + Voffset ' Store BAT V under charge, plus offset, to variable PAUSE 1000 ' Pause to maintain charge pulse HIGH CHGpin ' Turn OFF charge circuit LOW LEDpingr ' Turn ON green LED HIGH LEDpinrd ' Turn ON red LED PAUSE DlyrstRd ' Pause to let charge circuit stabilize GOSUB Read1298 ' Goto read BAT V at rest rstmvolts = AD + Voffset ' Store BAT V at rest, plus offset, to variable LOW LEDpinrd ' Turn OFF red LED IF rstmVolts <= pmVolts THEN Peak ' Compare BAT V at rest to peak V pmVolts = rstmVolts ' Write higher rest V to peak V Peak: ' Industry standard termination for Ni batteries IF rstmVolts <= pmVolts - 10 THEN Maintenance ' If peak V has been achieved, goto maintenance GOSUB Capacity ' If not, run capacity subroutine PAUSE DlyrstRd ' Pause a default rest time, NOT CRITICAL GOSUB Display ' Send variable data and calculations to screen GOTO FastCharge ' Continue with fast charge LiIon: ' Industry standard fast charge/termination for Li batteries LOW LEDpinrd ' Turn OFF red LED HIGH LEDpinyl ' Turn ON yellow LED, confirms Li BAT HIGH LEDpingr ' Turn ON green LED, to indicate beginning of FC pulse HIGH CCpin ' Enable CCCV charge mode LOW CHGpin ' Turn ON charge circuit, should not exceed 500 MSEC PAUSE 500 ' Pause to extend charge pulse GOSUB Read1298 ' Read BAT under charge V chgmvolts = AD + Voffset ' Write BAT V under FC, plus offset, to variable PAUSE 1500 ' Continue charge pulse, duration should not exceed 1500 MSEC HIGH CHGpin ' Turn OFF charge circuit LOW LEDpingr ' Turn OFF green LED, to indicate end of charge pulse HIGH LEDpinrd ' Turn ON red LED, to indicate beginning of rest period PAUSE DlyrstRd ' Pause to allow charge circuit to stabilize prior to ADC read LOW LEDpinrd GOSUB Read1298 ' Read BAT V under rest rstmvolts = AD + Voffset ' Write BAT V at rest, plus offset, to variable LOW LEDpinrd ' Turn OFF red LED to indicate end of rest period IF rstmVolts < 3700 THEN Polling ' If BAT V at rest is below low level, poll for BAT GOSUB Capacity ' If BAT V at rest is in range, run capacity subroutine PAUSE 300 ' Pause to complete rest period GOSUB Display ' Send variable data and calculations to screen GOTO LiIon ' Continue with LiIon FC Maintenance: ' Industry standard maintenance charge mode LOW LEDpinrd ' Turn OFF red LED LOW LEDpinyl ' Turn OFF yellow LED LOW LEDpingr ' Turn OFF green LED LOW CHGpin ' Turn ON charge circuit LOW CCPin ' Enable CC charge PAUSE 100 ' Pause to begin MC pulse HIGH LEDpingr ' Turn ON green LED PAUSE 100 ' Pause to initiate flash pattern for MC mode LOW LEDpingr ' Turn OFF green LED PAUSE 100 ' Pause to terminate flash pattern for MC mode HIGH LEDpingr ' Turn ON green LED GOSUB Read1298 ' Read BAT V during MC pulse chgmvolts = AD + Voffset ' Write BAT V under MC, plus offset, to variable HIGH CHGpin ' Turn OFF charge circuit HIGH CCpin ' Disable CC mode PAUSE 2500 ' Continue MC with 2.5 sec rest period GOSUB Read1298 ' Read BAT V at rest period in MC rstmvolts = AD + Voffset ' Write BAT V at rest during MC IF chgmVolts > 5000 THEN Polling ' If MC BAT V under charge if over high level, poll for BAT GOSUB Capacity ' If BAT V at rest is in range, run capacity subroutine PAUSE 300 ' Continue rest period under MC GOSUB Display ' Send variable data and calculations to screen GOTO Maintenance ' Continue with MC -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist