'---------------------------------------------------------------------------------------- ' ' Solar Experiments PICAXE Code ' ' file: picaxe_solar.bas ' ' created: 6-30-2009 ' updated: 6-30-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 ' oneOhmDrop ADC12 pin 21 ' serin pin 6 ' serout pin 7 ' reset pin 1 ' gnd pin 8 & 19 ' +5v pin 20 '---------------------------------------------------------------------------------------- ' ' 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 '---------------------------------------------------------------------------------------- ' ' Main Routine ' '---------------------------------------------------------------------------------------- Solar_Exp: gosub Get_Average_Voltage 'get average solar panel voltage and 'voltage drop across 1 ohm sense resistor 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 ' R = resistance in ohms GOSUB Plot_It 'transmit the value to the computer GOTO Solar_Exp 'tepeat '---------------------------------------------------------------------------------------- ' ' Sub Routines ' '---------------------------------------------------------------------------------------- Get_Average_Voltage: aveVolts = 0 'clear averages aveDrop = 0 for i = 0 to 63 'take 64 samples of solar panel voltage and readadc10 11,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. ' Plot_It: cksum = b3 + b2 + b5 + b4 serout A.4,N9600_8,(b3,b2,b5,b4,cksum) return '---------------------------------------------------------------------------------------- end