'---------------------------------------------------------------------------------------- ' ' Solar Experiments PICAXE Code ' ' file: picaxe_solar_auto_charger.bas ' ' created: 8-16-2009 ' updated: 8-16-2009 ' ' owner: LearnOnLine, Inc. ' author: John Gavlik ' ' created for the PicAxe 28X2 microprocessor uisng internal 8Mhz oscillator ' '---------------------------------------------------------------------------------------- '---------------------------------------------------------------------------------------- ' ' A/D Setup - Use all A/D channels ' '---------------------------------------------------------------------------------------- let adcsetup = %0000000011111111 'use ADC0,1,2,3,8,9,10,11,12 '---------------------------------------------------------------------------------------- ' ' Defines ' '---------------------------------------------------------------------------------------- #com4 'change com port number to match your system #picaxe 28X2 'don't change this!!! '---------------------------------------------------------------------------------------- ' ' Important I/O Pins for PIC 28X2 Microprocessor ' '---------------------------------------------------------------------------------------- ' voltage ADC11 pin 25 'Solar panel voltage (not used) ' oneOhmDrop ADC12 pin 21 'bottom of 1 ohm sense resistor ' ADC9 pin 24 'top of 1 ohm sense resistor ' ' serin pin 6 ' serout pin 7 ' reset pin 1 ' gnd pin 8 & 19 ' +5v pin 20 ' ' Q1 pin 25 (B.5) 'charge ON-OFF transistor switch ' chargeLed pin 28 (B.7) 'charging LED '---------------------------------------------------------------------------------------- ' ' Variables ' '---------------------------------------------------------------------------------------- symbol i = b0 symbol cksum = b1 'for data transmission to PC symbol voltage = w1 'input voltage symbol current = w2 'computed current symbol oneOhmDrop = w3 'voltage drop across 1 ohm sense resistor symbol aveVolts = w4 'average of solar panel voltage symbol aveDrop = w5 'average of voltage drop '---------------------------------------------------------------------------------------- ' ' On Demand Charging Variables ' '---------------------------------------------------------------------------------------- symbol fullChargeVolts = 1200 '1.20 volts (1200 millivolts) symbol fullDischargeVolts = 750 '0.75 volts (750 millivolts) symbol minEnergy = 7350 '7350 mAm (milli-amp-minutes) symbol measuredEnergy = w6 'accumulated current values every minute symbol charging = b14 '0 = not charging, 1 = charging symbol energyLoopCtr = w8 'energy loop counter symbol Q1 = B.5 symbol chargeLED = B.7 '------------------------------------------------------------------ ' ' On Demand Battery Charge Algorithm ' '------------------------------------------------------------------ ' ' Test_Battery_Voltage: 'stay in this loop as long as the battery is above min voltage ' Disable charging (Q1 = LOW) and extinguish LED ' Get voltage reading ' if voltage readng is >= fullChargeVolts, loop back ' else, goto Charge_Battery ' ' Charge_Battery: 'stay in this loop until a minimum charge is delivered into the battery ' Enable charging (Q1 = HIGH) and illuminate LED ' acquire a minimum battery charge ' when minimum battery charge is acquired, branch to Verify_Battery_Charged ' ' Verify_Battery_Charged: 'stay in this loop until the battery is at full voltage ' if voltage reading is >= fullChargeVolts, goto Test_Battery_Voltage ' else, loop back to Verify_Battery_Charged - charging is still taking place ' ' NOTE1: Current, power or resistance values are NOT displayed when the battery ' is NOT being charged. Only voltage is displayed. ' ' NOTE2: The displayed voltage is NOT that of the solar panel. Rather it is that of the battery. ' Test_Battery_Voltage: LOW Q1 'disable charging LOW ChargeLed 'extinguish charging LED charging = 0 'disable current reading GOSUB Solar_Exp 'get averaged voltage and current IF oneOhmDrop < fullDischargeVolts THEN 'test battery voltage for below minimum measuredEnergy = 0 'if so, clear accumulated energy value GOTO Charge_Battery 'branch to begin charging ELSE GOTO Test_Battery_Voltage 'else, keep looping ENDIF Charge_Battery: HIGH Q1 'enable charging HIGH ChargeLed 'illuminate charging LED charging = 1 'enable current reading FOR energyLoopCtr = 1 TO 300 'loop for 1 minute and output data to PC GOSUB Solar_Exp 'get averaged voltage and current NEXT 'each gosub to Solar_Exp takes 0.2 sec x 300 = 60 seconds measuredEnergy = measuredEnergy + current 'add latest current reading to measuredEnergy var IF measuredEnergy >= minEnergy THEN 'test for accumulated ampMinutes GOTO Verify_Battery_Charged 'branch if minimum energy level is met ELSE GOTO Charge_Battery 'loop back ENDIF Verify_Battery_Charged: TOGGLE ChargeLed GOSUB Solar_Exp 'get averaged voltage and current IF oneOhmDrop >= fullChargeVolts THEN 'test against fully charged voltage GOTO Test_Battery_Voltage 'if there, then disable charging ELSE GOTO Verify_Battery_Charged 'else, maintain charging state ENDIF '---------------------------------------------------------------------------------------- ' ' Sub Routines ' '---------------------------------------------------------------------------------------- Solar_Exp: gosub Get_Average_Voltage 'get average solar panel voltage and 'voltage drop across 1 ohm sense resistor IF charging = 0 THEN 'set current = 0 if charging since we can't measure it current = 0 'and skip the test below and go directly to the Plot_It subroutine GOTO Solar_Exp_Plot_It ENDIF IF voltage < oneOhmDrop THEN 'test for illegal condition GOTO Solar_Exp ENDIF current = voltage - oneOhmDrop 'compute the voltage drop across the 1 ohm sense resistor 'which is automatically now in milliamps by the following: ' ' I = E / R where ' I = current in milliamps ' E = voltage in millivolts Solar_Exp_Plot_It: ' R = resistance in ohms GOSUB Plot_It 'transmit the value to the computer return '---------------------------------------------------------------------------------------- Get_Average_Voltage: aveVolts = 0 'clear averages aveDrop = 0 for i = 0 to 63 'take 64 samples of solar panel voltage and readadc10 9,voltage '1 ohm sense resistor voltage drop to reduce ripple aveVolts = aveVolts + voltage readadc10 12,oneOhmDrop aveDrop = aveDrop + oneOhmDrop next aveVolts = aveVolts / 64 'get raw solar panel voltage count voltage = aveVolts */$04E1 'convert to millivolts aveDrop = aveDrop / 64 'get raw 1 ohm resistor voltage drop count oneOhmDrop = aveDrop */$04E1 'convert to millivolts return '---------------------------------------------------------------------------------------- ' ' Note: Keep the bytes in the order shown as the PC software depends on having them transmitted ' exactly in this sequence. ' ' Transmit oneOhmDrop followed by current followed by the checksum ' Plot_It: cksum = b7 + b6 + b5 + b4 serout A.4,N9600_8,(b7,b6,b5,b4,cksum) return '---------------------------------------------------------------------------------------- end