My code is below and turns on/off a led/load on output 0 depending on the pic supply voltage. It was designed using a picaxe chip which works well but I want to migrate to pbp and non picaxe chips.
The adc part does not seem to be working I suspect my config/defines. Can someone have a look for me please.
Code:
'************************* 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 376 out of 2048 Words *********************
'*******************************************************************************
@ DEVICE PIC12F683,MCLR_OFF
@ DEVICE PIC12F683,BOD_OFF
@ DEVICE PIC12F683,PROTECT_OFF
@ DEVICE PIC12F683,WDT_ON
Define OSC 4 'set processor speed to 4 Mhz
INTCON = %10001000 'internal oscillator
OSCCON = %01100111 'sets internal osc to 4 Mhz and stable
CMCON0 = 7 'Comparators off
GPIO = %00000000 'outputs low
TRISIO = %00001010 'Set Pins GPIO.1 & GPIO.3 as inputs
ANSEL = %00110010 'AN1 Analog Input
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
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS
'------------------------------ General configuration --------------------------
include "bs1defs.bas" 'allows the use basic stamp variables and "modedefs.bas" for n2400 command
'*******************************************************************************
'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
'----------------- Variables 8bit --------------------------------------------
VData var CellV.byte0 'VData is low byte of Word Var CellV
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)
'*******************************************************************************
Start: 'Main program start
high SlaveBusOut 'Turn off interrupt signal for next Slave (SlaveBusOut)
high MasterBus 'Turn off MasterBus Optocoupler (Reqd due to sink driven)
low Load 'Turn off by-pass resistor
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 '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 > 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
if slavebusIn = 0 then Main 'If no interrupt signal goto Main
if CellV >DHigh or CellV < DLow then 'If V >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
serout MasterBus,T9600,[VData] 'Send VData (b0) to Master at Baud9600
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)
goto Main 'Goto start of Main loop
My schematic is here if it helps.
www.solarvan.co.uk/bms/DigitalSlave080908.jpg
Bookmarks