'------ Li-Ion Battery Management System Simple Slave (Digital) Module -------- '--------------------- PIC12F683 - 111108 - V19 Beta -------------------------- '------------------------------------------------------------------------------ '----------------------------- General Notes ----------------------------------- ' If I want to utilize the in circuit serial programming (ICSP) of the 12F683 it ' will require changes to the schematic as the pins used to program the picaxe ' are different than the pins used on the PIC. ' I will probably need to change the Q1 transistor and the R2 resistor to allow ' a larger bypass current. ' I might want to incorporate a timer such that if the slave has not received a ' request from the master in a certain time limit, put the slave to sleep. ' If I wanted all of the same features it would be better to use pin 5 for Slave ' Data Bus In (interupt). ICSP requires more connections and will be omitted. '************************* Slave PIC12F683 Pinouts **************************** ' Top ' _____ '(+ Cell Supply) +Ve -1| ^ |8- -Ve (- Cell Supply) '(Program In) Rxd -2| 6 |7- Output 0 (Bypass Load Out) '(Slave Data Bus Out) Output 4 -3| 8 |6- Input 1 (RefAdc In 1.235V) '(Slave Data Bus In) Input 3 -4| 3 |5- Output 2 (Master Data Bus Out) ' ----- '************************ Slave Module Specification *************************** 'Supply/Cell Voltage 1.75 - 5.00V DC (Pic limits) 'Average Supply Current at 3.35v <1ma 'Voltage Ref LM 385 1.235V accuracy 1% 'Supply/Cell Voltage sensing maximum accuracy +/- 20mv 'Maximum balancing/bypass current with 15R resistor 333ma at 5.00V 'Maximum serial Bus data rate 4800 baud (Picaxe 08M limit at 8mhz) 'Maximum cell Capacity 650ah (65,000) (Limit of 16bit Word Master Variable) 'Maximum 128 Slave Modules per Master Module (Master Pic Scratchpad Ram limit) 'CPU Speed 31khz - 8mhz with internal resonator (Lower speed used for power saving) 'Permitted Working Cell Voltage Range 1.75 - 4.30V 'RefVADC should be 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) '********************************************************************************** '********************** Program Size 95 out of 256 Bytes ********************** '******************************************************************************* 'Variables Constants and I/O definitions '----------------- Variables 16bit -------------------------------------- CellV var Word 'Corrected Cell voltage (16bit value) RefADC var Word 'Raw Adc input data variable range 0-1023 10bit CellA var Word 'Cell average voltage last 10 measurements PreV var Word 'Previous cell voltage, preserves Cell V from last loop '----------------- Variables 8bit -------------------------------------------- 'VData var CellV.lowbyte 'Voltage data byte (Sent to Master via link) (lower half of CellV; might should be upper half) 'VData var CellV.byte0 VData VAR BYTE CountB var byte 'General 0-255 byte ADC loop counter variable '------------------- Constants ----------------------------------------------- RefVADC con 63200 'Fixed Ref Voltage Calibration LM385 1.235v * 1023 * 100 / 2 = 63200 CutInV con 365 'Balancing load/bypass cut in Voltage (This must match value set in Master) CutOutV con 360 'Balancing load/bypass cut out Voltage (This must match value set in Master) Delay con 5 'Data delay time in milliseconds DLow con 175 'Cell correction value 175 or 1.75V subtracted from CellVoltage DHigh con 430 'Cell correction value 430 or 4.30V (If Cell V>430 or <175 then error) '-------------- Pins used for I/O and designations ------------------------- '---------------- Digital high/low Outputs ------------------------ Load var GPIO.0 '2W 15R Transistor driven load/bypass resistor & optional led indicator on Output 0 (pin 7) MasterBus var GPIO.2 'Master Data Bus Output Baud4800 on Output 2 (pin 5) SlaveBusIn var GPIO.3 'Slave Data Bus Input Baud4800 on Input 3 (pin 4) SlaveBusOut var GPIO.4 'Slave Data Bus Output Baud4800 on Output 4 & optional led indicator (pin 3) '---------------- Analogue ADC Inputs ------------------------------ RefInput var GPIO.1 'LM385 1.235V RefAdc on Input 1 (pin 6) '----------------- General configuration -------------------------- PCON = %1111100 'BOD set to off (allows chip to operate at lower voltage) Define ADC_BITS 10 'Set A to D to 10 bits Define ADC_CLOCK 3 'Set clock source (3=rc) Define ADC_SAMPLEUS 50 'Set sampling time in uS include "bs1defs.bas" 'allows the use basic stamp variables and "modedefs.bas" for n2400 command ADCON0 = %11010001 'turns A/D on, A/D reads pin 6(AN1), right justify and Vref=Vdd TRISIO = %00001010 'set pins 4 and 6 to input and all others to output OSCCON = %01100111 'sets internal osc to 4 Mhz and stable Define OSC 4 'set processor speed to 4 Mhz define CHAR_PACING 2000 'inserts a pause (2mS) between each character during serout INTCON = $80 'This disables possible interrupts '******************************************************************************* '******************************************************************************* Start: 'Main program start high SlaveBusOut 'Turn off interrupt signal for next Slave (SlaveBusOut) low Load 'Turn off by-pass resistor ON INTERRUPT GOTO senddata 'Tells interrupt where to go INTCON = %10001000 'Enable GPIO interrupts (input channels only) IOC = %00001000 'Enable GPIO.3 (pin 4) only Interrupt on change '******************************************************************************* '******************************************************************************* ' This section takes 10 readings of the voltage of the cell and then averages ' the value. It then compares this value to the preset values for turning ' the bypass resistor either on or off. It continuously runs this loop. If at ' any time an interupt is received on pin 4, the cell voltage value is sent ' to the master at 2400 baud. '------------------------------------------------------------------------------- Main: 'Main program loop label CellA = 0 'Set Cell Average word variable to 0 (Zero) for CountB = 1 to 10 '10x ADC Oversampling loop counter adcin RefInput, RefADC 'Measure cell voltage on RefInput pin and store in RefADC CellA = CellA + RefADC 'Add latest ADC reading to running total next CountB 'Repeat loop until 10 ADC readings obtained CellA = CellA / 10 'Calculate average ADC reading for last 10 readings CellV = RefVADC / CellA * 2 'Calculate Cell Voltage from the average ADC LM385 VRef 1.235v reading if CellV > CutInV then 'If Cell V > CutInV then high Load 'Turn on bypass resistor and bypass led endif if CellV < CutOutV then 'If Cell V < CutOutV then low Load 'Turn off bypass resistor and bypass led endif PreV = CellV 'Preserves CellV to prevent interrupt errors goto Main 'Goto start of Main loop '******************************************************************************* 'Note Slave Opto's are Sink driven therefore (Pic Output Low = Opto On, High = Opto Off) '******************************************************************************* DISABLE 'No interrupts past this point Senddata: CellV = PreV 'Restores CellV to prevent interrupt errors if CellV >DHigh or CellV 4.30V or V <1.75V set out of range (b0=0) 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 Loop1: if slavebusIn = 1 then Loop1 'Wait until interrupt signal (pin 4) is low before begining transmission VData=CellV.byte0 VData=85 Pause 5 low MasterBus 'Turn on MasterBus (pin 5) Optocoupler (Reqd due to sink driven) PAUSEUS 1100 serout MasterBus,T2400,[VData] 'Send VData (b0) to Master at Baud2400 high MasterBus 'Turn off MasterBus Optocoupler (Reqd due to sink driven) GOTO Loop1 low SlaveBusOut 'Turn on interrupt signal for next Slave (SlaveBusOut) Pause Delay 'Allow time for next Slave and Master to be ready high SlaveBusOut 'Turn off interrupt signal for next Slave (SlaveBusOut) INTCON.1=0 'Clear interrupt flag (not sure which to use) resume 'Goto main program loop