'**************************************************************** '* Name : UNTITLED.BAS * '* Author : [select VIEW...EDITOR OPTIONS] * '* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] * '* : All Rights Reserved * '* Date : 12/18/2011 * '* Version : 1.0 * '* Notes : * '* : * '**************************************************************** '--- Li-Ion Battery Management System Command Pulse Slave (Digital) Module ----- '------- Picbasic Pro Compiler version PIC12F683 - 070211 - V09 Beta ---------- '------ This code is for serial command slaves only & requires Master board V2 - '------ & Master software V1.72 onwards. Please report any errors. ------------- '------------------------------------------------------------------------------- '**************************** 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. '************************** Slave PIC12F683 Pinouts **************************** ' Top ' _____ '(+ Cell Supply) +Ve -1| ^ |8- -Ve (- Cell Supply) '(Slave Alive Out) GP5 -2| 6 |7- GP0 (Bypass Load Out) '(Slave Data Bus Out) GP4 -3| 8 |6- GP1 (RefAdc In 1.235V) '(Slave Data Bus In) GP3 -4| 3 |5- GP2 (Master Data Bus Out) ' ----- '************************ Slave Module Specification *************************** 'Supply/Cell Voltage 2.00 - 5.00V DC (Recommended Pic limits) 'Cell Voltages <2.00V are not recommended due to Pic and software limitations 'Average Supply Current at 3.35v <1ma 'Voltage Ref LM 385 1.235V accuracy 1% 'Supply/Cell Voltage sensing maximum accuracy +/- 20mv 'Maximum balancing resistor value 100R (3.65V / 100R = 36.5ma) 'Minimum balancing resistor value 10R (3.65V / 10R = 365ma) 'Serial Bus data rate 9600 baud 'Maximum cell Capacity 650ah (65,000) (Limit of 16bit Word Master Variable) 'Maximum 95 Slaves per PBP Master Module 'CPU Speed 8mhz with internal resonator (Lower speed could possibly be used for power saving) 'Permitted Working Cell Voltage Range 1.90 - 4.45V (2.55v) (Can be adjusted up/down) 'RefVADC 63200 but can be adjusted to compensate for variations 'Note Slave Opto's are Sink driven therefore (Pic Output Low = Opto On, High = Opto Off) '***************************** Slave Commands ********************************** ' Command 01 = Send Cell Voltage on Master Bus ' Command 02 = Turn Off Loads ' Command 03 = Turn On Loads as Reqd ' Command 04 = Increase Load CutIn Voltage by 50mv ' Command 05 = Decrease Load CutIn Voltage by 50mv ' Command 06 = Set Slave Load CutIn Voltage Default ' Command 07 = Turn On Slave Load for 0.5 seconds (Flash Led) ' Command 08 = Report Slave Software Version Number ' Command 09 = Increase FailLow Cut Off Voltage by 50mv ' Command 10 = Decrease FailLow Cut Off Voltage by 50mv ' Command 11 = Increase FailHigh Cut in Voltage by 50mv ' Command 12 = Decrease FailHigh Cut in Voltage by 50mv ' Command 13 = Set FailHigh/Low Voltages to Default '******************************************************************************* '********************** Program Size 635 out of 2048 Words ********************* '******************************************************************************* '------------------------------ General configuration -------------------------- 'Below used with MPASM Assembler #config __config _FCMEN_ON & _IESO_ON & _BOD_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_OFF & _WDT_OFF & _INTOSCIO #ENDCONFIG 'Below used with PM Assembler '@ DEVICE PIC12F683,MCLR_OFF '@ DEVICE PIC12F683,BOD_OFF '@ DEVICE PIC12F683,PROTECT_OFF '@ DEVICE PIC12F683,WDT_OFF 'osccon = %01000000 'Osc set to 1 MHz 'osccon = %01010000 'Osc set to 2 MHz 'osccon = %01100000 'Osc set to 4 MHz (Default) 'osccon = %01110000 'Osc set to 8 MHz Define OSC 8 'Set PicBasic Pro processor speed to 8 Mhz (Must match oscillator value) INTCON = %10001000 'Internal oscillator OSCCON = %01110101 'Sets internal osc to 8 Mhz (Default) and stable CMCON0 = %00000111 'Comparators off TRISIO = %00001010 'Set Pins GPIO.1 & GPIO.3 as inputs ANSEL = %01010010 'AN1 Analog Input Clock Source fosc/16 (101) ADCON0 = %10000101 'Turns A/D on, A/D reads pin 6(AN1), right justify and Vref=Vdd 'Define ADCIN parameters Define ADC_BITS 10 'Set number of bits in result (10) Define ADC_CLOCK 3 'Set clock source (3=rc) Define ADC_SAMPLEUS 50 'Set sampling time in microseconds (50us) include "modedefs.bas" 'Allows the use of the serin/serout command '******************************************************************************* 'Variables Constants and I/O definitions '------------------------ Variables 16bit -------------------------------------- CellV var Word 'Cell Voltage (16bit value) RefADC var Word 'Raw Adc input data variable range 0-1023 10bit CutInV var Word 'Balancing load/bypass cut in Voltage CellS var Word 'Stored Cell Voltage (16bit value) when Command 1 active FailLow var Word 'Fail Safe Minimum Load voltage Cut off (Default 3.35V) FailHigh var Word 'Fail Safe Maximum Load voltage Cut in (Default 3.85V) '------------------- Variables 8bit -------------------------------------------- VData var CellV.LowByte 'VData is low byte of Word Var CellV CountB var byte 'General 0-255 byte ADC loop counter variable Command var byte 'Command 0-255 '------------------- Variables 1bit -------------------------------------------- '--------------------- Constants ----------------------------------------------- RefVADC con 63200 'Fixed Ref Voltage Calibration LM385 1.235v * 1023 * 100 / 2 = 63200 DLow con 175 'Cell correction value 175 (1.75V) subtracted from CellVoltage DHigh con 430 'Cell correction value 430 (4.30V) (If Cell V>430 or <175 then error) CutInVD con 365 'Balancing load/bypass default cut in Voltage (Must match value set in Master)(isaac buspack) FailLowDef con 300 'Default Fail Safe Minimum Load voltage Cut off (3.35V)(Isaac) FailHighDef con 370 'Default Fail Safe Maximum Load voltage Cut in (3.85V)(Isaac) '------------------ Pins used for I/O and designations ------------------------- '---------------------- Digital high/low Inputs/Outputs ------------------------ Load var GPIO.0 'Load/bypass resistor & led indicator on Output 0 (pin 7) MasterBus var GPIO.2 'Master Data Bus Serial Output Baud 9600 on Output 2 (pin 5) SlaveBusIn var GPIO.3 'Slave Data Bus Serial Input Baud 9600 on Input 3 (pin 4) SlaveBusOut var GPIO.4 'Slave Data Bus Output Pulses on Output 4 (pin 3) SlaveAlive var GPIO.5 'Slave Alive Toggle Test Ouput (Toggles high/low each time through main loop) '---------------------------- Analogue ADC Inputs ------------------------------ RefInput var GPIO.1 'LM385 1.235V RefAdc on Input 1 (pin 6) '************************* Simulation and Formula Notes ************************ 'Use a generic adc input of 650 to simulate a cell voltage of 1.92V 'Use a generic adc input of 400 to simulate a cell voltage of 3.16V 'Use a generic adc input of 300 to simulate a cell voltage of 4.20V 'The forumla for what the cell voltage should be for a given Adc reading is '(63200 / Adc Reading) * 2 Example (63200 / 400) * 2 = 3.16V 'The forumla for what the Adc reading should be for a given cell voltage is '(63200 / Voltage) * 2 Example (63200 / 316) * 2 = 400 '******************************************************************************* Start: 'Main program start high SlaveBusOut 'Turn off interrupt signal for next Slave Optocoupler (Note sink driven) high MasterBus 'Turn off MasterBus Optocoupler (Note sink driven) low Load 'Turn off by-pass load resistor (Note source driven) CutInV = CutInVD 'Set default Balancing load/bypass cut in Voltage FailLow = FailLowDef 'Set default Fail Safe Cut off voltage (3.35V) FailHigh = FailHighDef 'Set default Fail Safe Cut in voltage (3.85V) '******************************************************************************* Main: 'Main program loop label Toggle SlaveAlive 'Slave Alive Toggle Test Ouput (Toggles high/low each time through main loop) CellV = 0 'Set CellV word variable to 0 (Zero) for CountB = 1 to 10 '10x ADC Oversampling loop counter ADCON0.1 = 1 'Start ADC conversion while ADCON0.1 = 1 'Wait for ADC DONE wend RefADC.highbyte = ADRESH 'Move HIGH byte of result to RefADC.highbyte RefADC.lowbyte = ADRESL 'Move LOW byte of result to RefADC.lowbyte CellV = CellV + RefADC 'Add latest ADC reading to running total next CountB 'Repeat loop until 10 ADC readings obtained CellV = CellV / 10 'Calculate average ADC reading for last 10 readings CellV = RefVADC / CellV 'Calculate Cell Voltage from the average ADC LM385 VRef 1.235v reading CellV = CellV * 2 'Calculate Cell Voltage from the average ADC LM385 VRef 1.235v reading if CellV < FailLow then 'FailLow voltage check if CellV < 3.35v then load is turned off. low Load 'Turn off bypass resistor and bypass led endif if CellV > FailHigh then 'FailHigh voltage check if CellV > 3.85v then load is turned on. high Load 'Turn on bypass resistor and bypass led endif SERIN2 SlaveBusIn, 16468, 10000, TimeOut, [Command] 'Receive Command Byte on Slave Data Bus Timeout 10s ' 9600** Inverted No Parity 16468 Idle Low '******************************************************************************* Incomming: 'Incomming Command if Command = 1 then 'If command = 1 then send voltage data on Master Bus CellS = CellV 'Store CellV in CellS for use with Command 3 if CellV >DHigh or CellV 4.30V or V <1.75V set out of range VData = 0 'Set VData = 0 Cell error out of Voltage range condition else CellV = CellV - DLow 'Convert Word (CellV) data into Byte (VData) for output endif endif if Command = 2 then 'If command = 2 then Load off VData = Command 'set VData to Command low Load 'Turn off bypass resistor and bypass led endif if Command = 3 then 'If command = 3 then Load on as reqd if CellS > CutInV then 'If stored cell voltage CellS > CutInV then high Load 'Turn on bypass resistor and bypass led endif VData = Command 'set VData to Command endif if Command = 4 then 'If command = 4 then increase Load CutInV by 10mv CutInV = CutInV + 5 'Increase Load Cut in voltage by 50mv VData = Command 'set VData to Command endif if Command = 5 then 'If command = 5 then decrease Load CutInV by 10mv CutInV = CutInV - 5 'Decrease Load Cut in voltage by 50mv VData = Command 'set VData to Command endif if Command = 6 then 'If command = 6 then set Slave CutIn defaults CutInV = CutInVD 'Set default Balancing load/bypass cut in Voltage VData = Command 'set VData to Command endif if Command = 7 then 'If command = 7 then load Resistor On high Load 'Turn on bypass resistor and bypass led pause 500 'Pause for 0.5 second low Load 'Turn off bypass resistor and bypass led VData = Command 'set VData to Command endif if Command = 8 then 'If command = 8 then report software version number VData = 09 'Set VData to 09 (Software Version Number) endif if Command = 9 then 'If command = 9 then increase Fail Safe Low Cut Off FailLow = FailLow + 5 'Increase FailLow Cut Off by 50mv VData = Command 'set VData to Command endif if Command = 10 then 'If command = 10 then decrease Fail Safe Low Cut Off FailLow = FailLow - 5 'Decrease FailLow Cut Off by 50mv VData = Command 'set VData to Command endif if Command = 11 then 'If command = 11 then increase Fail Safe High Cut Off FailHigh = FailHigh + 5 'Increase FailHigh Cut Off by 50mv VData = Command 'set VData to Command endif if Command = 12 then 'If command = 12 then decrease Fail Safe High Cut Off FailHigh = FailHigh - 5 'Decrease FailHigh Cut Off by 50mv VData = Command 'set VData to Command endif if Command = 13 then 'If command = 13 then set Default Fail Safe High/Low Voltages FailLow = FailLowDef 'set Default Fail Safe Cut Off Voltage FailHigh = FailHighDef 'set Default Fail Safe High Cut Off Voltage VData = Command 'set VData to Command endif Serout MasterBus, T9600, [VData] 'Send VData to Master at Baud9600 (MasterBus) serout SlaveBusOut, T9600, [Command] 'Send Slave command at Baud9600 (SlaveBus) to next slave goto Main 'Goto main loop '******************************************************************************* TimeOut: 'Jumps here if Serial input timesout no data for >5s low Load 'Turn off bypass resistor and bypass led goto Main 'Goto main loop '*******************************************************************************