' LHBB_BenchTest.bs2 ' ' This is a Bench Test program, to control the Battery Balancer from ' a PC or serial terminal. Commands can test all functions: Turn any ' relay on/off, read voltages and currents and send them to the serial ' port, etc. A "Continuous Run" mode automatically cycles through all ' cell/battery modules, and reports their voltages and currents. ' ' Author: Jeff Shanab, Lee Hart ' Last Modified: 6/22/2014 ' ' Directives '{$STAMP BS2} '{$PBASIC 2.5} Delay CON 1000 ' time to read V/F converters (1 second) PackV CON 8 ' pin to read voltage V/F converter count PackC CON 9 ' pin to read current V/F converter count First CON 1 ' first relay board connected (1 to 10) Last CON 2 ' last relay board connected (1 to 10) ' LED numbers match with aux relay numbers but not Basic Stamp Ports. ' Here are some constants and a Mapping array. K1 CON 7 'port 7 controls Power board relay K1 (AC/DC Input) K2 CON 6 'port 6 controls Power board relay K2 (DC/DC Power) K3 CON 5 'port 5 controls Power board relay K3 (example: Battery Heater) K4 CON 4 'port 4 controls Power board relay K4 (example: Battery Fan) K5 CON 3 'port 3 controls Power board relay K5 (example: Charger High/Low) K6 CON 2 'port 2 controls Power board relay K6 (example: Charger On/Off) 'K7 is controlled by battery polarity, not by BASIC stamp K8 CON 15 'port 15 controls Power board relay K8 (DC/DC output) 'also used for U2 shift register clock ' Mapping Array. Maps port numbers to Power board relay numbers. auxMap VAR Nib(8) auxmap(1)=7 auxMap(2)=6 auxMap(3)=5 auxMap(4)=4 auxMap(5)=3 auxMap(6)=2 auxMap(7)=12 auxMap(8)=15 DCDC_Out VAR Bit TurnOn VAR Bit auxRelayPins VAR Nib startBoard VAR Nib endBoard VAR Nib boardndx VAR Nib commandVal VAR Byte expandOut VAR Byte ' immediates relay VAR Nib board VAR Nib power VAR Nib Voltage VAR Word Vscale VAR Word Voffset VAR Word Current VAR Word Cscale VAR Word Coffset VAR Word ' EEPROM data storage - calibration constants for Voltage and Current EE_Vscale DATA (2) EE_Voffset DATA (2) ' voltage = (raw counts x Vscale) + Voffset EE_Cscale DATA (2) EE_Coffset DATA (2) ' current = (raw counts x Cscale) + Coffset Main: PAUSE 1000 ' delay needed for USB serial connections DEBUG CR,TAB,"Battery Balancer Test Program - 6/22/14", CR,CR READ EE_Vscale, Word Vscale ' initialize saved values from EEPROM READ EE_Voffset, Word Voffset READ EE_Cscale, Word Cscale READ EE_Coffset, Word Coffset commandVal = "M" ' assume Measure Continuous command GOSUB Measure_Continuous ' scan Relay boards, and send their ' voltages and currents to serial port ' to the serial port. Press "H" to stop ' press "H" to exit to Help menu Menu: ' display the Help menu DEBUG "elp", TAB, "[A] A" GOSUB msg_Activate DEBUG CR, TAB, "[D] De-A" GOSUB msg_Activate DEBUG CR, TAB, "[R] activate Relay (1-8) on Board (", DEC First, "-", DEC Last, ") (0 for OFF)", CR, TAB, "[V] Voltage=(Voffset-Count)*Vscale/256", CR, TAB, TAB, "[1] Voffset=", SDEC Voffset, CR, TAB, TAB, "[2] Vscale=", SDEC Vscale, CR, TAB, "[C] Current=(Coffset-Count)*Cscale/256", CR, TAB, TAB, "[3] Coffset=", SDEC Coffset, CR, TAB, TAB, "[4] Cscale=", SDEC Cscale, CR, REP " "\12, REP "-"\17, CR, TAB, "[S] Step to next test", CR, TAB, "[T] Test continuously", CR, TAB, "[M] Measure continuously", CR, TAB, "[H] Help", CR, CR Again: DEBUG BKSP, CR, ":" ' display prompt SERIN 16, $4054, [commandVal] ' wait for command IF commandVal = "1" THEN GOSUB Set_Voltage_Offset IF commandVal = "2" THEN GOSUB Set_Voltage_Scale IF commandVal = "3" THEN GOSUB Set_Current_Offset IF commandVal = "4" THEN GOSUB Set_Current_Scale commandVal = commandVal & %01011111 ' convert to uppercase IF commandVal = "A" THEN GOSUB Activate_Relay IF commandVal = "D" THEN GOSUB Deactivate_Relay IF commandVal = "R" THEN GOSUB Activate_Relay_Board IF commandVal = "V" THEN GOSUB Measure_Voltage IF commandVal = "C" THEN GOSUB Measure_Current IF commandVal = "S" THEN GOSUB Step_Relay_Board IF commandVal = "T" THEN GOSUB Test_Continuous IF commandVal = "M" THEN GOSUB Measure_Continuous IF commandVal = "H" THEN GOTO Menu GOTO Again Activate_Relay: TurnOn = 1 ' turn on a selected power board relay GOTO Set_Power_Relay Deactivate_Relay: TurnOn = 0 ' turn off a selected power board relay DEBUG "e-a" Set_Power_Relay: ' set power board relay to match TurnOn flag GOSUB msg_Activate ' display message DEBUG ": K" ' display prompt DEBUGIN DEC1 power ' get relay DEBUG CR IF (power < 1 OR power > 8) THEN DEBUG BELL ' abort if out of range RETURN ENDIF IF power = 7 THEN ' can't control K7, so set TEST LED instead TurnOn = ~ TurnOn ' (TEST LED uses inverted logic; 0 = On) ENDIF IF TurnOn = 1 THEN HIGH auxMap(power) ELSE LOW auxMap(power) ENDIF RETURN Activate_Relay_Board: ' activate a selected relay and board DEBUG "elay: " DEBUGIN DEC1 relay IF (relay < 1 OR relay > 8) THEN DeActivate DEBUG ", Board: " DEBUGIN DEC2 board IF (board >= First AND board <= Last) THEN Activate DeActivate: relay = 0 board = 0 DEBUG " De-Activate" Activate: DEBUG CR GOSUB Set_Relay_Board RETURN Measure_Continuous: ' sequentially select each relay and board ' and display its voltage and current DEBUG "easure continuously", CR DO WHILE (commandVal = "M") DEBUG "S" ' finish the word "Step" GOSUB Step_Relay_Board ' select board/relay GOSUB Read_Voltage ' display voltage GOSUB Read_Current ' display current DEBUG CR GOSUB Abort ' loop until a key is typed LOOP RETURN Test_Continuous: ' sequentially test each relay and board DEBUG "est continuously", CR DO WHILE (commandVal = "T") DEBUG "S" ' finish the word "Step" GOSUB Step_Relay_Board DEBUG CR GOSUB Abort LOOP RETURN Step_Relay_Board: ' activate the next higher relay and board relay = relay + 1 IF relay > 8 THEN relay = 1 board = board + 1 ENDIF IF (board < First OR board > Last) THEN board = First ENDIF DEBUG "tep to Relay ", DEC relay, ", Board ", DEC board, " " GOSUB Set_Relay_Board RETURN Set_Relay_Board: DCDC_Out = OUT15 ' save state of K8 (DC/DC output) LOW 15 ' turn K8 off PAUSE 500 ' wait for current to fall to 0 HIGH 13 ' turn off relay boards PAUSE 500 ' wait for relay to drop out expandOut = (board-1)<<3 + (relay-1) + 128 ' \ \ \ \ \___\_set bit7=1 to enable outputs ' \ \ \________\_______put relay# in bits 0,1,2 ' \__________\___________shift board# into bits 3,4,5,6 SHIFTOUT 14, 15, LSBFIRST, [expandOut\8] LOW 13 HIGH 13 ' load shift reg to output LOW 13 ' enable outputs PAUSE 500 ' wait for relay to pull in OUT15 = DCDC_Out ' restore K8 (DC/DC output) RETURN Measure_Voltage: DEBUG "oltage", CR ' continuously display voltage DO WHILE (commandVal = "V") GOSUB Read_Voltage ' read and display voltage DEBUG CR GOSUB Abort ' test for a key to exit LOOP RETURN Read_Voltage: ' read and display voltage COUNT PackV, Delay, Voltage ' count for 1 second Voltage = (Voffset - Voltage) */ Vscale ' scale and offset DEBUG TAB, SDEC Voltage, "v" ' to convert to volts RETURN Set_Voltage_Offset: DEBUG " Set Voltage Offset to " DEBUGIN SDEC Voffset WRITE EE_Voffset, Word Voffset RETURN Set_Voltage_Scale: DEBUG " Set Voltage Scale to " DEBUGIN SDEC Vscale WRITE EE_Vscale, Word Vscale RETURN Measure_Current: DEBUG "urrent", CR ' continuously measure current DO WHILE (commandVal = "C") GOSUB Read_Current DEBUG CR GOSUB Abort LOOP RETURN Read_Current: ' read and display current COUNT PackC, Delay, Current ' count for 1 second Current = (Coffset - Current) */ Cscale ' scale and offset DEBUG TAB, SDEC Current, "a" ' to convert to amps RETURN Set_Current_Offset: DEBUG " Set Current Offset to " DEBUGIN DEC Coffset WRITE EE_Coffset, Word Coffset RETURN Set_Current_Scale: DEBUG " Set Current Scale to " DEBUGIN DEC Cscale WRITE EE_Cscale, Word Cscale RETURN Abort: ' wait 1 second for a key SERIN 16, $4054, 1000, No_Key, [commandVal] No_Key: RETURN ' return ' messages (to save programming space) msg_Activate: DEBUG "ctivate power board relay (1-8)" RETURN