#REM Li-Ion Battery Management System Master Module. By Peter Perkins Picaxe 28X1 Firmware A3+ PIC16F886 - 020809 - www.150mpg.co.uk - V1.06 Beta **************************** General Information ****************************** The BMS modules carry no warranty or guarantee of any kind! They are used at your own risk, and I make no claims as to their suitability for a particular function. Prospective users must evaluate the system before using it, and no liability will be entertained by myself in any shape or form whatsoever. The modules and software have been produced at low cost for the benefit of the EV & electronic community. The software is available free via the internet. Users may modify or adapt the system as they see fit. If you are not fully competent to work on potentially lethal battery systems and high voltages, then do not experiment with or use this system. Be aware that vehicle modifications can lead to invalidated insurance and warranty issues. You the end user remain fully liable for any modifications made to your vehicle. ************************** Master Picaxe 28X1 Pinout ************************** Top _____ (Pic Reset) Reset -01| ^ |28- Output 7 (Drive Inhibit Out) (Current Sensor Adc In) Adc 0 -02| |27- Output 6 (Charger Relay Out) (Buttons Adc In) Adc 1 -03| |26- Output 5 (Video Display Out) (Analog Temp1 Adc In) Adc 2 -04| |25- Output 4 (Controller Cutback Out) (Analog Temp2 Adc In) Adc 3 -05| P |24- Output 3 (WatchDog Led Out) (Program In) Rxd -06| 2 |23- Output 2 (Slave Data Bus Out) (Program Out) Txd -07| 8 |22- Output 1 (Charger Cutback Out) (- Supply) Gnd -08| X |21- Output 0 (Audible Alarm Out) (Resonator In) Osc 1 -09| 1 |20- +Ve (+5V Supply) (Resonator In) Osc 2 -10| |19- Gnd (- Supply) (I2C Temp Sensor In) Input 0 -11| A |18- Input 7 (Master Data Bus In) (I2C Temp Sensor In) Input 1 -12| 4 |17- Input 6 (Interlocks In) (I2C Temp Sensor In) Input 2 -13| |16- Output c5 (Charging Led 2 Out) (VSS Speed Sensor In) Input 3 -14| |15- Output c4 (Dash Led 1 Out) ----- ************************ Other IC's On Master Pcb ***************************** *********************** Watchdog Picaxe 08M Pinouts *************************** Top _____ (+5V Supply) +Ve -1| ^ |8- Gnd (- Supply) (Program In) Rxd -2| 0 |7- Txd (Program Out) (Watchdog Led Out) Output 4 -3| 8 |6- Output 1 (Audible Alarm Out) (Pulse Count In) Input 3 -4| M |5- Output 2 (Master Reset Out) ----- ****************** SV2000 Serial to Composite Video IC ************************ Top _____ (Sync Out) Syn -1| ^ |8- +Ve (+5V Supply) (Video Out) Vid -2| V |7- Nc (No Connection) (Serial Data In) Rxd 4 -3| I |6- Inv (Serial Data Invert Mode) (- Supply) Gnd 3 -4| D |5- Txd (No Connection) ----- *********** 16x9 Character Serial 8,N,1 9600 baud display layout ************** 0> > 1> > 2> > 3> > 4> > 5> > 6> > 7> > 8> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 15 ************************ Master Module Specification ************************** Board Supply Voltage 8.00-30.00V DC or as limited by 5.00V 78L05 regulator CPU Supply Voltage 5.00V CPU Speed 8mhz with internal resonator (Pic limit 20mhz with external res) Average Board Supply Current at 12.00v <100ma Maximum Serial Bus data rate 4800 baud (Picaxe 08M limit at 8mhz) Maximum Cell Capacity 65ah (65335) (Limit of 16bit Word Variable) Maximum Pack Voltage 650v (65335) (Limit of 16bit Word Variable) Maximum Charge/Discharge rate 100A (Allegro Current Sensor limit) Maximum 128 Slave Modules per Master Module (Pic Scratchpad Ram limit) Battery Pack Temp Sensor Range (-55 to +127C) Composite Video Display data rate 9600 baud (SV2000 Serial to Video Chip) Composite RCA Video Monitor Output 1V 75ohm PAL/NTSC Charger relay output is board supply voltage max 30v at 500ma (Note! not 5v) Opto Isolated Charger/Controller Cutback outputs max 50ma 50v The display is limited to 16x9 characters in a 0-15 & 0-8 matrix. The bottom row is reserved for error messages! ******************************************************************************* ********************* Program Size 1489 out of 4096 Bytes ********************* ******************************************************************************* #ENDREM #picaxe 28x1 ;Set Picaxe type 28X1 for compiler/editor `Variables Constants and I/O definitions `Note Global variables must be preserved throughout the program `Note Local variables may be used at will within subroutines `Variables 16bit (Word) symbol AlarmsWarnings= w0 ;w0 Global (b0,b1) = Alarms Word (b0,b1 bytes) (16 bits = 2 x 8bits) symbol CellVoltage = w1 ;w1 Local (b2,b3) = Cell Voltage 0-1023 10bit (0-5v) symbol PackVoltage = w2 ;w2 Local (b4,b5) = Calculated pack voltage (Voltage of all Cells added together) symbol Soc = w3 ;w3 Global (b6,b7) = Calculated pack capacity (Soc State of Charge) resolution 10ma symbol BatCurrent = w4 ;w4 Local (b8,b9) = Current Sensor ADC value 0-1023 10bit Approx 200ma resolution `symbol = w5 ;w5 Global (b10,b11) = Spare symbol Charge = w6 ;w6 Global (b12,b13) = Accumulated Charge current for last minute symbol Discharge = w7 ;w7 Global (b14,b15) = Accumulated Discharge current for last minute symbol PackTemp12 = w8 ;w8 Local (b16,b17) = Battery Block Temperature raw 12 bit sensor data from I2C chip symbol Distance = w9 ;w9 Global (b18,b19) = Distance (feet) travelled accumulator (5280ft = 1 mile) symbol WhMile = w10 ;w10 Global (b20,b21) = Watt Hours per mile (Power Consumed) symbol CountESoc = w11 ;w11 Global (b22,b23) = Word var split into two bytes (CountE & Soc Counter) symbol CountW = w12 ;w12 Local (b24,b25) = General 0-65335 16bit Word Counter/Local Variable (Note CountC & D) symbol CountX = w13 ;w13 Local (b26,b27) = General 0-65335 16bit Word Counter/Local Variable (Note CountA & B) `Variables 8bit (Byte) symbol AlarmBits = b0 ;b0 Global Alarm Bits {8 bits} (To clear all Alarm bits set to 0) symbol WarnBits = b1 ;b1 Global Warning Bits {8 bits} (To clear all Warning bits set to 0) symbol VoltageData = b2 ;b2 Local Voltage data byte (8bit value) (Received from Slave via serial link) symbol SocCounter = b22 ;b22 Global SocCounter increments each time current measured to calc avg 1 min current symbol CountE = b23 ;b23 Local General 0-255 8bit Byte Counter or General/Local Variable (Note CountESoc) symbol CountD = b24 ;b24 Local General 0-255 8bit Byte Counter or General/Local Variable (Note CountW) symbol CountC = b25 ;b25 Local General 0-255 8bit Byte Counter or General/Local Variable (Note CountW) symbol CountB = b26 ;b26 Local General 0-255 8bit Byte Counter or General/Local Variable (Note CountX) symbol CountA = b27 ;b27 Local General 0-255 8bit Byte Counter or General/Local Variable (Note CountX) `Note the 16 Bit use Byte variables (b0) & (b1) which in turn use Word (w0) `Note to clear all sixteen Bits in one go set (AlarmsWarnings or w0 to Zero) `Alarm Bits x8 1bit (b0) `Note to clear all eight Alarm Bits below in one go set (Alarmbits or b0 to Zero) symbol Alarm1 = bit0 ;bit0 = Alarm Bit (Cell over AbsMax V) symbol Alarm2 = bit1 ;bit1 = Alarm Bit (Cell under AbsMin V) symbol Alarm3 = bit2 ;bit2 = Alarm Bit (Cell data serial transfer timeout error) symbol Alarm4 = bit3 ;bit3 = Alarm Bit (Battery Pack over AbsMax Temp) symbol Alarm5 = bit4 ;bit4 = symbol Alarm6 = bit5 ;bit5 = symbol Alarm7 = bit6 ;bit6 = symbol Alarm8 = bit7 ;bit7 = Alarm Test Condition (Used to simulate Alarms) `Warning Bits x 8 1bit (b1) `Note to clear all eight Warning Bits below in one go set (WarnBits or b1 to Zero) symbol Warn1 = bit8 ;bit8 = symbol Warn2 = bit9 ;bit9 = symbol Warn3 = bit10 ;bit10 = symbol Warn4 = bit11 ;bit11 = symbol Warn5 = bit12 ;bit12 = symbol Warn6 = bit13 ;bit13 = symbol Warn7 = bit14 ;bit14 = symbol Warn8 = bit15 ;bit15 = `*** Special Compiler Variable Notes *** `Timer = Internal timer variable and set to 1 second ticks (t1s_8) `ptr = Scratchpad Ram Variable data pointer (Scratchpad is 128 bytes) `@ptrinc = Scratchpad Ram pointer with automatic increment after execution. `Constants symbol Cells = 50 ;Number of cells in the battery pack (50) (Max is 128 cells) symbol MaxPackVoltage = 18000 ;Maximum pack voltage = 180v (18,000 as 16 bit value) Res 10mv (Max 650v) symbol MinPackVoltage = 12000 ;Minimum pack voltage = 120v (12,000 as 16 bit value) Res 10mv symbol AbsMaxCellVoltage = 380 ;Absolute Maximum permitted cell voltage = (3.80V) (Alarm & Shutdown point) symbol AbsMinCellVoltage = 230 ;Absolute Minimum permitted cell voltage = (2.30V) (Alarm & Shutdown point) symbol MaxCellVoltage = 375 ;Normal Maximum permitted cell voltage = (3.75V) (Charger/Regen cutback point) symbol MinCellVoltage = 240 ;Normal Minimum permitted cell voltage = (2.40V) (Controller/Assist cutback point) symbol CutInV = 360 ;Balancing load/bypass cut in Voltage (This must match value set in Slaves) symbol CutOutV = 355 ;Balancing load/bypass cut out Voltage (This must match value set in Slaves) symbol AbsMaxPackTemp = 45 ;Absolute Maximum permitted pack temperature = (45C) (Alarm & Shutdown point) symbol InitialSoc = 40000 ;Sets initial Soc/Capacity to this value after software download symbol SocMax = 40000 ;Maximum cell capacity = 40ah (40,000 as 16 bit value) (Max 65A) symbol SocMin = 4000 ;Minimum cell capacity = 4ah (4000 as 16 bit value) 10% of SocMax symbol OdoMeter = 0 ;Initial OdoMeter setting, set eprom saved Odo to this (Used if program updated) symbol Delay = 10 ;Interrupt and data delay (Pause 10 = 5ms at 8mhz) symbol DiscardLow = 175 ;Cell correction value 175 or 1.75V added to CellVoltage to recreate correct V symbol TimeOut = 100 ;Serial Data Receive Timeout value 100ms symbol PulsePerMile = 455 ;Pulses from speed sensor per mile (4550) / 10 to fit into integer maths `BaudRate constants for 8mhz symbol Baud1200 = N600 ;Baud rate 1200 at 8mhz (T=True Idle High)(N=Inverted Idle Low) symbol Baud2400 = N1200 ;Baud rate 2400 at 8mhz (T=True Idle High)(N=Inverted Idle Low) symbol Baud4800 = N2400 ;Baud rate 4800 at 8mhz (T=True Idle High)(N=Inverted Idle Low) symbol Baud9600 = T4800 ;Baud rate 9600 at 8mhz (Note Baud9600 is reserved for the Video output chip) `Pins used for I/O and designations `*** Digital high/low Outputs on port b - Outputs 0-7 *** symbol Alarm = 0 ;Audible Alarm warning on Output 0 symbol Charger = 1 ;Charger Cutback output on Output 1 (Opto conducts when cell V > 3.75V) symbol SlaveBus = 2 ;Slave Data Bus Output Baud4800 on Output 2 symbol WatchDogLed = 3 ;Watchdog flashing Green Led on Output 3 (Flashes every other program loop) symbol Controller = 4 ;Controller Cutback output on Output 4 (Opto conducts when cell V < 2.40V) symbol Video = 5 ;Dashboard Video display Baud9600 on Output 5 symbol ChargerOnOff = 6 ;Charger Relay On/Off control on Output 6 (5.00v Max 500ma) symbol DriveInhibit = 7 ;Drive inhibit Opto on Output 7 `*** Extra Digital high/low Outputs on port c 4-5 *** symbol Led1 = 4 ;Dashboard Led 1 on Output portc 4 symbol Led2 = 5 ;Dashboard Led 2 on Output portc 5 `*** Digital high/low Inputs on port c - Inputs 0-3 & 6-7 *** symbol DigitalTemp1 = 0 ;I2C DS18B20 Battery Pack Temp Sensor on Input 0 (-55 to +125C) symbol DigitalTemp2 = 1 ;I2C DS18B20 Battery Pack Temp Sensor on Input 1 (-55 to +125C) symbol DigitalTemp3 = 2 ;I2C DS18B20 Battery Pack Temp Sensor on Input 2 (-55 to +125C) (Note can be used for multiple sensors) symbol SpeedSensor = 3 ;Speed Sensor pulse sensor input on Input 3 (Measures length of a single pulse) symbol Interlocks = pin6 ;Interlocks and additional safety/security switch inputs on Input 6 symbol MasterBus = 7 ;Master Data Bus Input on Input 7 `*** Analogue ADC Inputs *** symbol CurrentSensor = 0 ;Battery Current Sensor 0-5V ADC on Input 0 (+100 to -100A) 2.5v = 0 Amps symbol ButtonsADC = 1 ;Menu Buttons ADC input 0-5V ADC on Input 1 (0-5v = 1 of 6 values) (0,1,2,3,4,5) symbol AnalogTemp1 = 2 ;Analog Temp1 Sensor LM335 Input (-55 to +150C) symbol AnalogTemp2 = 3 ;Analog Temp2 Sensor LM335 Input (-55 to +150C) ;************************************* Memory Allocations for 28X1 ******************************************* ;************************************************************************************************************* ;*********************** 128 Bytes Scratchpad Ram Used by Serial data input routine ************************** ;****************** 95 Bytes Spare Ram [80 to 126] ($50 to $7E) & [192 to 239] ($C0 to $EF) ****************** ;*************************************** 255 Bytes Eeprom [0 to 255] ***************************************** ;************************************************************************************************************* ;*********************** Info stored in RAM from [192 to 239] & EEPROM from [0 to 19] ************************ `This information is stored in the ram/eeprom by the various subroutines `(Peek & Poke used to access RAM) `(Read & Write used for EEprom) `(Get & Put used to access Scratchpad RAM) `*** Ram addresses symbol ErrCell = 223 ;Ram address to store Byte data [Error Cell] Cell with error (Dec 223) symbol AlarmRep= 224 ;Ram address to store Byte data [Alarm Repeat check] stops screen re-drawing (Dec 224) `Spare RAM Addresses 225-237 Inclusive symbol ChgFlag = 238 ;Ram Byte ChgFlag indicates charging in progress (0=Not Charging) (1=Charging) symbol AlarmSt = 239 ;Ram Byte to store Alarm byte data b0 (Dec 239) `*** EEprom 0-19 addresses symbol SocStore = 0 ;EEprom address to store Word data [SOC] (Dec 0 + 1) Updated each minute symbol DistStore = 2 ;EEprom address to store Word data [Distance] (Dec 2 + 3) symbol OdoStore = 4 ;EEprom address to store Word data [Odo] (Dec 4 + 5) Updated each mile symbol WhStore = 6 ;EEprom address to store Word data [WhMile] (Dec 6 + 7) Updated each minute ;******************************************* Main Eeprom ***************************************************** `*** Eeprom Data Storage 0-255 bytes (This does not affect program memory) Numbers 0-9 stored in 3W x 3H matrix *** `EEPROM 0,(First 20 & last 6 bytes reserved for other program functions) EEPROM 250,(1,2,4,3,3,0) ;250-255 bytes Store 6 digit security code data (235441) in eeprom ;************************************************************************************************************* Start: ;Initialise Program. Start Timer, Load Variables setfreq m8 ;Setfreq CPU Freq to 8mhz high Video ;Set Video Output high to enable T9600 Serial Comms read SocStore, WORD Soc ;Load last saved Soc reading from eeprom storage if Soc = 0 then ;Test if stored Soc is 0, if it is set SOC to InitialSoc Soc = InitialSoc ;Set SOC/Capacity (State of charge) to InitialSoc write SocStore, WORD Soc ;Write SOC reading to eeprom for storage when Master Off endif read OdoStore, WORD CountW ;Load last saved Odometer (Mile) reading from eeprom storage if CountW = 0 then ;Test if stored Odo reading is 0, if it is set Odo to OdoMeter CountW = OdoMeter ;Set Initial Odo reading to the value in constant OdoMeter write OdoStore, WORD CountW ;Write Odometer reading to eeprom for storage when Master Off endif read DistStore, WORD Distance ;Load last saved Distance (Feet) reading from eeprom storage read WhStore, WORD WhMile ;Load last saved WhMile reading from eeprom storage serout Video,Baud9600,(27,46,27,67," BMS Master V06 By Peter Perkinswww.150mpg.co.uk") ;Video Display serout Video,Baud9600,(10,13," ** ",#Cells," Cells **") ;Video Display Number Of Cells pause 5000 ;Pause for 2.5 seconds at 8mhz serout Video,Baud9600,(27,67) ;Clear Screen gosub CalcSoc ;Gosub CalcSoc routine to display initial Soc settimer t1s_8 ;Set internal (timer) variable to 1 second ticks at 8mhz ;************************************************************************************************************* ;************************************************************************************************************* MainLoop: ;Main program loop toggle WatchDogLed ;Keeps watchdog happy, flashes every other time through loop gosub CheckVoltage ;Gosub CheckVoltage routine to collect/display Cell V data gosub CheckCurrent ;Gosub CheckCurrent routine to accumulate/display charge/discharge data gosub CheckTemp ;Gosub CheckTemp routine to get battery pack temperature gosub CheckSpeed ;Gosub CheckSpeed routine to calculate/display speed/distance if timer >59 then ;if SocTimer > 59 (1 Minute has elapsed) gosub CalcSoc ;Gosub CalcSoc routine to calculate/display running Soc at 1 min intervals timer = 0 ;Reset timer variable to 0 (Start another 1 minute timing loop) endif gosub MenuButtons ;Gosub MenuButtons gets button data (0,1,2,3,4,5=no button) in CountE on CountE gosub StartChg, DisplayB, SetSoc, SetAlarm, SetAlarm ;Gosub depending on button pressed if AlarmBits > 0 then DisplayAlarms ;If AlarmBits > 0 goto Alarms Display low Alarm ;Deactivate audible alarm poke AlarmRep, 0 ;Set AlarmRep byte flag to 0 indicates no Alarm set goto mainloop ;Goto main program loop ;************************************************************************************************************* ;************************************************************************************************************* CheckVoltage: ;Check Voltage subroutine, receive data to calculate pack voltage ptr = 0 ;Reset Scratchpad pointer to 0 (Start of 128 byte Scratchpad Ram) CountC = 0 ;Reset CountC to 0 (Used to accumulate highest Cell V) CountD = 255 ;Reset CountD to 255 (Used to accumulate lowest Cell V) high SlaveBus ;Turn on Message waiting signal (SlaveBus) pause Delay ;Hold Message waiting signal high until Slave has time to respond low SlaveBus ;Turn off Message waiting signal (SlaveBus) for CountB = 1 to Cells ;Start for/next loop to store cell data in 128 byte Scratchpad Ram serin [TimeOut,DataError],MasterBus,Baud4800,@ptrinc ;Rxd Data on Master Bus into Scratchpad Ram and inc pointer next CountB ;Increment for/next loop and move to next Cell PackVoltage = 0 ;Reset PackVoltage Total to zero = 0V ptr = 0 ;Reset Scratchpad pointer to 0 (Start of 128 byte Scratchpad Ram) for CountB = 1 to Cells ;Start for/next loop to calculate Pack voltage and check cell data/voltage CellVoltage = 0 ;Clear CellVoltage Variable VoltageData = @ptrinc ;Load VoltageData (b2) with value from Scratchpad Ram and inc data pointer if CountC < VoltageData then ;If VoltageData > CountC then (If Current Cell > Highest Cell) CountC = VoltageData ;CountC = VoltageData (Store Current Cell V in CountC) endif if CountD > VoltageData then ;If VoltageData < CountD then (If Current Cell < Lowest Cell) CountD = VoltageData ;CountD = VoltageData (Store Current Cell V in CountD) endif CellVoltage = CellVoltage + DiscardLow ;CellVoltage (w1) = CellVoltage (w1) (1-255 0.01-2.55V) + (175 1.75V) if CellVoltage > AbsMaxCellVoltage then ;If cell V > AbsMaxCellVoltage then set Alarm Bit Alarm1 = 1 ;Set Alarm1 Bit to 1 (Indicates Cell over AbsMax Voltage condition) poke ErrCell, CountB ;Store Error Cell number in ErrCell Ram Byte endif if CellVoltage < AbsMinCellVoltage then ;If cell V < AbsMinCellVoltage then set Alarm Bit Alarm2 = 1 ;Set Alarm2 Bit to 1 (Indicates Cell under AbsMin Voltage condition) poke ErrCell, CountB ;Store Error Cell number in ErrCell Ram Byte endif PackVoltage = PackVoltage + CellVoltage ;Add Cell voltage to accumulated Pack voltage next CountB ;Increment for/next loop and move to next cell `Display Pack Voltage PackVoltage = PackVoltage / 100 ;Get first part of decimal value (Before decimal point) serout Video,Baud9600,(27,83,0,0,"Pack Volts ",#PackVoltage," ") ;Video Display `Display Highest Cell Voltage CountX = CountC + DiscardLow ;Highest Cell V Correction factor if CountX > MaxCellVoltage then ;If CountX > MaxCellVoltage then activate Charger & Controller Optos high Charger ;Activate Charger pull down opto high Controller ;Activate Controller pull down opto high portc led1 ;Activate dash Over-V Led1 else low Charger ;De-activate Charger pull down opto low Controller ;De-activate Controller pull down opto low portc led1 ;De-Activate dash Over-V Led1 endif w2 = CountX / 100 ;Get first part of decimal value (Before decimal point) w4 = CountX // 100 ;Get second part of decimal value (After decimal point) serout Video,Baud9600,(27,83,0,1,"High Cell ",#w2,".",#w4," ") ;Video Display `Display Balancing Message if CountX > CutInV then ;If CountX (Highest Cell V) > Load Cut In V then system is Balancing serout Video,Baud9600,(27,83,0,7," * Balancing! *") ;Video Display else serout Video,Baud9600,(27,83,0,7," ") ;Video Display endif `Display Lowest Cell Voltage CountX = CountD + DiscardLow ;Lowest Cell V Correction factor if CountX < MinCellVoltage then ;If CountX < MinCellVoltage then activate Controller Opto high Controller ;Activate Controller pull down opto high portc led2 ;Activate dash Under-V Led2 else low Controller ;De-activate Controller pull down opto low portc led2 ;De-Activate dash Under-V Led2 endif w2 = CountX / 100 ;Get first part of decimal value (Before decimal point) w4 = CountX // 100 ;Get second part of decimal value (After decimal point) serout Video,Baud9600,(27,83,0,2,"Low Cell ",#w2,".",#w4," ") ;Video Display Alarm3 = 0 ;Set Alarm3 Bit to 0 (Indicates No Cell Data Timeout Error) return ;Return to main program loop DataError: ;Serial Slave Data Rxd TimeOut, If no Data within (TimeOut = 100ms) poke ErrCell, CountB ;Store Error Cell number in ErrCell Ram Byte Alarm3 = 1 ;Set Alarm3 Bit to 1 (Indicates Cell Data Timeout Error) return ;Return to main program loop ;************************************************************************************************************** CheckTemp: ;Check Battery Pack temperature routine (LM335 Sensors) Once per minute readadc10 AnalogTemp1, CountW ;Read Analog Temp Sensor 1 (0-1023 10bit) readadc10 AnalogTemp2, CountX ;Read Analog Temp Sensor 2 (0-1023 10bit) CountD = CountW - 558 / 2 ;Calculate Temp in C CountB = CountX - 558 / 2 ;Calculate Temp in C CountA = 43 ;Set CountA to 43 (+) temperature is positive if CountD > AbsMaxPackTemp or CountB > AbsMaxPackTemp then ;If Temp > AbsMaxPackTemp set Alarm Bit Alarm4 = 1 ;Set Alarm4 Bit to 1 (Indicates Temp over AbsMax Temp condition) endif `Display Temperatures serout Video,Baud9600,(27,83,0,3,"C A",CountA,#CountD," ","B",CountA,#CountB," ") ;Display Temps return ;Return to main program loop ;************************************************************************************************************** CheckCurrent: ;Accumulate (Current in Amps) charge/discharge data CountW = 0 ;Reset Current oversampling accumulator to 0 (Zero) for CountA = 1 to 10 ;Start x10 Current oversampling loop readadc10 CurrentSensor, BatCurrent ;Read present charge/discharge current (0-1023 10bit)(-100A to +100A 0-5V) CountW = CountW + BatCurrent ;Add Current to accumulated Current Next CountA ;Repeat loop until 10 samples obtained BatCurrent = CountW / 10 ;Divide CountW by 10 to get average current from last 10 samples if BatCurrent <510 then ;If BatCurrent is <510 means system is Charging BatCurrent = 512 - BatCurrent ;Subtract sensor offset to get a positive number (0-512 = 0-100A-) BatCurrent = BatCurrent * 10 / 25 ;Convert sensor charge rate to charge rate in Amps x 10 Charge = Charge + BatCurrent ;Add Latest sensor Current reading to running 1 minute Charge total CountE = 43 ;Set ascii Character 43 (+) to be displayed if charging goto ExitCurrent endif if BatCurrent >514 then ;If BatCurrent is >514 means system is Discharging BatCurrent = BatCurrent - 512 ;Subtract sensor offset to get a positive number (0-512 = 0-100A+) BatCurrent = BatCurrent * 10 / 25 ;Convert sensor charge rate to charge rate in Amps x 10 Discharge = Discharge + BatCurrent ;Add Latest sensor Current reading to running 1 minute Discharge total CountE = 45 ;Set ascii Character 45 (-) to be displayed if discharging goto ExitCurrent endif BatCurrent = 0 ;If BatCurrent = 510 to 514 inc then set BatCurrent to 0 (Zero) CountE = 32 ;Set ascii Character 32 (Space) to be displayed if no charge/discharge ExitCurrent: inc SocCounter ;SocCounter = SocCounter + 1 `Display Battery Current serout Video,Baud9600,(27,83,0,4,"Bat Amps ",CountE,#BatCurrent," ") ;Video Display return ;Return to main program loop ;************************************************************************************************************** CalcSoc: ;Use 1 min accumulated sensor current data to calculate Soc if Charge > 0 then ;If no Charge in last minute jump over Charge calculations Charge = Charge / SocCounter ;Calculate average sensor charge rate for last minute Charge = Charge * 10 ;Calculate Amount of power/current added in last minute Soc = Soc + Charge ;Add amount of power generated in last minute to (Soc) PackCapacity Charge = 0 ;Reset Charge Counter to 0 endif if Discharge > 0 then ;If no Discharge in last minute jump over Discharge calculations Discharge = Discharge / SocCounter ;Calculate average sensor discharge rate for last minute Discharge = Discharge * 10 ;Calculate Amount of power/current used in last minute Soc = Soc - Discharge ;Subtract amount of power used in last minute from (Soc) PackCapacity Discharge = 0 ;Reset Discharge Counter to 0 endif write SocStore, WORD Soc ;Write Soc reading to eeprom for storage when Master Off write DistStore, WORD Distance ;Write Distance reading to eeprom for storage when Master Off write WhStore, WORD WhMile ;Write WhMile reading to eeprom for storage when Master Off SocCounter = 0 ;Reset SocCounter to 0 CountW = Soc / 1000 ;Get first part of decimal value (Before decimal point) CountX = Soc // 1000 ;Get second part of decimal value (After decimal point) CountX = CountX / 10 ;Discard last digit of result (After decimal point) `Display Battery State of Charge SOC serout Video,Baud9600,(27,83,0,5,"Cap Ah ",#CountW,".",#CountX," ") ;Video Display return ;Return to main program loop ;************************************************************************************************************** #REM There are 4550 wheel sensor pulses per mile traveled for the Honda Insight ***** Using the Count command we are reading pulses per 1/4 second ***** s = speed in MPH x = pulses per second s = (x*360)/455 Should give pulses per hour, divided by pulses per mile, to give you a mile per hour figure so for 60mph reading would be reading about 76 pulses per second Speed Mph = (X*360)/455 The maximum pulses per second the 16 bit speed maths can stand is 180 which equates to a speed of 142 mph If you exceed 142mph the speedo will be incorrect/overflow (read low) during that second Now we need Distance in feet travelled in that second Distance ft per second = (X*528)/455 Distance ft per second at 60mph ( 88 = 5280 / 60) 88ft per second = 60mph To keep a running total of feet travelled Distance = Distance + (X*528)/455 When Distance = 5280ft (1 mile) increment odometer and start again The maximum pulses per second the 16 bit distance maths can stand is 124 which equates to a speed of 98mph If you exceed 98mph the distance will be incorrect/overflow (read low) during that second ***** Using the Pulsin command to measure the length of a single pulse instead of the Count command ******* We could try this: 7908 / (pulsin / 10) This will return the following: 1.2067 mph = 1.21 1.875mph = 1.88 3.75mph = 3.75 7.50 = 7.50 15.00 = 15.00 30.00 = 30.00 60.00 = 60.00 120.00= 120.00 We can get remainder using modulo division and then create an integer value from 12 (1.2mph) to 1200 (120.0mph) by appropriately scaling the two results. We still need to do the overflow check, which means that the speedo won't read below about 1.2mph. #ENDREM CheckSpeed: ` Pulsin command measures the length of a pulse. In no pulse occurs in the timeout period, the result will be 0 pulsin SpeedSensor,0,CountW ;Measure length of a VSS pulse in 5us (8mhz) units (Timeout 0.32768s) if CountW = 0 then ;If CountW = 0 means pulsin has timed out and vehicle is moving at < 1.5mph CountX = 0 ;Set CountX to 0 goto CheckDistance ;Jump over calculations as vehicle not moving endif CountW = CountW / 10 ;Divide CountW by 10 to enable results to fit into 16 bit Integer Maths CountW = 7908 / CountW ;Returns speed in mph accurate to 1 mph CheckDistance: `Display Speed serout Video,Baud9600,(27,83,0,6,"Mph ",#CountW," ") ;Video Display CountW = CountW * 146 ;Multiply CountW x 146 to work out feet/second (1mph = 1.46ft per second) CountW = CountW / 100 ;Divide CountW by 100 to get result back after integer maths scaling above Distance = Distance + CountW ;Add distance travelled in last second to running total read OdoStore, WORD CountW ;Load last saved Odometer (Mile) reading from eeprom storage If Distance >= 5280 then ;5280ft per mile / Tyre circumference 5.8ft = 910 revs per mile inc CountW ;Increment Odometer by 1 mile write OdoStore, WORD CountW ;Write current Odometer reading to eeprom for storage when Master Off Distance = Distance - 5280 ;Subtract (5280ft 1 mile) from Distance and start accumulating again endif `Display Distance serout Video,Baud9600,(27,83,7,6,"Odo ",#CountW," ") ;Video Display return ;Return to main program loop ;************************************************************************************************************** DisplayAlarms: ;Alarms turns on led/audible alarms and turns off charger etc ;Action taken depends on Alarm Bits (8 different Alarms max!) peek AlarmRep, CountB ;Retrieve CountB (AlarmRep flag byte from ram store) Alarm on previous loop if CountB = 0 then ;If CountB = 0 then no Alarm from previous loop so clear and reverse screen serout Video,Baud9600,(27,33,27,67," * BMS Alarms * ",10,13) ;Display Negative, Clear Screen & Display Message else serout Video,Baud9600,(27,83,0,2) ;Set cursor display position endif poke AlarmRep, 1 ;Set AlarmRep flag byte to 1 indicate Alarm set peek ErrCell, CountB ;Retrieve CountB (CountB = Cell Number) from ErrCell RAM Byte high Alarm ;Activate audible alarm high Charger ;Activate Charger pull down opto high Controller ;Activate Controller pull down opto low ChargerOnOff ;Turn off Charger main relay poke ChgFlag, 0 ;Set ChgFlag to 0 indicates Charging Off If Alarm1 = 1 then ;bit0 = Alarm Bit 1 (Cell over AbsMax V) serout Video,Baud9600,("Cell ",#CountB," >AMaxV ",10,13) ;Video Display endif If Alarm2 = 1 then ;bit1 = Alarm Bit 2 (Cell under AbsMin V) serout Video,Baud9600,("Cell ",#CountB," AbsMaxTemp ") ;Video Display endif If Alarm8 = 1 then ;bit7 = Alarm Bit 8 (Alarm test condition set) serout Video,Baud9600,(" Alarm Test! ") ;Video Display endif AlarmBits = 0 ;Reset AlarmBits to 0 (Clears any Alarm Flags) pause 6000 ;Pause for 3 seconds at 8mhz goto MainLoop ;Goto main program loop ;************************************************************************************************************** DisplayB: ;Display Extended Cell BMS Data pause 250 ;pause for 125ms at 8mhz ptr = 0 ;Reset Scratchpad pointer to 0 (Start of 128 byte Scratchpad Ram) CountC = 0 ;Reset CountC to 0 (Used to accumulate highest Cell V) CountD = 255 ;Reset CountD to 255 (Used to accumulate lowest Cell V) serout Video,Baud9600,(27,67) ;Video Display Clear Screen Command Sequence for CountA = 1 to Cells ;Start for/next loop to read cell data from 128 byte Scratchpad Ram CellVoltage = 0 ;Clear CellVoltage Variable VoltageData = @ptrinc ;Load VoltageData (b2) with value from Scratchpad Ram and inc data pointer CellVoltage = CellVoltage + DiscardLow ;CellVoltage (w1) = CellVoltage (w1) (1-255 0.01-2.55V) + (175 1.75V) w2 = CellVoltage / 100 ;Get first part of decimal value (Before decimal point) w4 = CellVoltage // 100 ;Get second part of decimal value (After decimal point) serout Video,Baud9600,("Cell ",#CountA," ",#w2,".",#w4,"V ",10,13) ;Video Display pause 250 ;Pause for 125ms Loop1: gosub MenuButtons ;MenuButtons routine gets current button data (0,1,2,3,4,5) value in CountE If CountE <> 1 then Loop1 ;If Button 2 is not pressed goto Loop1 next CountA ;Increment for/next loop and move to next cell Pause 1000 ;Pause for 0.5 seconds serout Video,Baud9600,(10,13,"Press 5 to Exit ") ;Video Display Loop2: gosub MenuButtons ;MenuButtons routine gets current button data (0,1,2,3,4,5) value in CountE If CountE <> 4 then Loop2 ;If Button 5 (Menu) is not pressed goto Loop2 Pause 1000 ;Pause for 0.5 seconds serout Video,Baud9600,(27,67) ;Clear Main Video Display return ;Return to main program loop ;************************************************************************************************************** StartChg: ;Start Charging Cycle Routine If AlarmBits >0 then ;If any Alarms are set then charge start is aborted serout Video,Baud9600,(27,83,0,8," Charge Error! ") ;Video Display high Alarm ;Activate audible alarm Pause 2000 ;Pause for 1 second at 8mhz low Alarm ;Deactivate audible alarm return ;Return to main loop, charging aborted due to relevant error Bit set endif peek ChgFlag, CountE If CountE = 0 then poke ChgFlag, 1 ;Set ChgFlag = 1 low Charger ;De-activate Charger pull down opto low Controller ;De-activate Controller pull down opto high ChargerOnOff ;Turn on Charger main relay `Display Charging Message serout Video,Baud9600,(27,83,0,8," ** Charging **") ;Video Display else poke ChgFlag, 0 ;Set ChgFlag = 0 low ChargerOnOff ;Turn off Charger main relay `Clear Charging Message serout Video,Baud9600,(27,83,0,8," ") ;Video Display endif return ;Return to main program loop ;************************************************************************************************************** MenuButtons: ;Decodes button Inputs and returns 6 values (0,1,2,3,4,5) readadc ButtonsADC,CountE ;Read ButtonsADC input and get value into Local Byte Variable CountE CountE = CountE + 6 / 42 - 1 ;Convert ADC data to button number. return ;Return to main program ;************************************************************************************************************** SetAlarm: ;Toggle test Alarm conditon if Alarm8 = 0 then Alarm8 = 1 else Alarm8 = 0 endif return ;************************************************************************************************************** SetSoc: ;Set Soc to Initial Soc Soc = InitialSoc ;Set Soc to Initial Soc write SocStore, WORD Soc ;Write Soc reading to eeprom for storage when Master Off return ;Return to main program ;************************************************************************************************************** #REM *** Odd Notes By measuring voltage as a function of current, you can work out the resistance of the battery. R = (V_0 - V_I)/I where V0 is the voltage with 0 current and VI is the terminal voltage when delivering current I to a load. #ENDREM