' {$STAMP BS2} ' {$PBASIC 2.5} ' ' Title: Experiments with Solar Panels ' File: Parallax_Solar_Basic_Exp.bs2 ' Copyright: LearnOnLine, Inc. ' Author: J. Gavlik ' Last Updated: 06-30-2009 '------------------------------------------------------------------ ' Declarations '------------------------------------------------------------------ ' ' General VARs ' tempByte VAR Byte tempWord VAR Word i VAR Byte voltage VAR Word oneOhmDrop VAR Word current VAR Word ckSum VAR Byte ' ' VARs for MCP3208 8 channel, 12 bit multiplexed A/D Converter ' a2dClk CON 0 'clock (P0) a2dDout CON 1 'data out (P1) a2dDin CON 2 'data in (P2) a2dCs CON 3 'chip select (P3) a2dId0 CON %1000 'A/D Channel 0 ID a2dId1 CON %1001 'A/D Channel 1 ID a2dId2 CON %1010 'A/D Channel 2 ID a2DId3 CON %1011 'A/D Channel 3 ID a2dId4 CON %1100 'A/D Channel 4 ID a2dId5 CON %1101 'A/D Channel 5 ID a2dId6 CON %1110 'A/D Channel 6 ID a2DId7 CON %1111 'A/D Channel 7 ID a2dIdShiftOut VAR Nib 'A/D Channel ID to shift out a2dResult VAR Word '16-bit result of A/D conversion '------------------------------------------------------------------ ' Solar Experiment Algorithm '------------------------------------------------------------------ ' ' Sample the solar panel input voltage from the a/d converter ' Convert the value to millivolts ' Transmit the voltage and current values to the computer ' Delay 300 ms to allow the computer to process and display the data ' Repeat ' '------------------------------------------------------------------ Solar_Exp_Basic: GOSUB Get_Solar_Panel_Voltage ' acquire solar panel voltage current = 0 ' set to zero since doing no measurements GOSUB Plot_It ' transmit the value to the computer PAUSE 300 ' allow computer to process data GOTO Solar_Exp_Basic ' repeat '------------------------------------------------------------------ ' Get Solar Panel Voltage '------------------------------------------------------------------ Get_Solar_Panel_Voltage: a2dIdShiftOut = a2dId0 ' Set the a/d converter to ch0 GOSUB A2D ' Get A/D count voltage = a2dresult */$0138 ' Convert 12-bit count to millivolts (multiply by 1.22mv / count) RETURN '------------------------------------------------------------------ ' Plot Acquired Data '------------------------------------------------------------------ Plot_It: ckSum = (voltage.HIGHBYTE + voltage.LOWBYTE + current.HIGHBYTE + current.LOWBYTE) 'compute checksum DEBUG voltage.HIGHBYTE, voltage.LOWBYTE, current.HIGHBYTE, current.LOWBYTE ,ckSum 'Transmit data to computer RETURN '------------------------------------------------------------------ ' A/D Converter Routine ' Enter with a2dIdShiftOut as the channel to convert ' Return with 12-bit result in a2dResult - right shifted '------------------------------------------------------------------ A2D: 'Initialize signals HIGH a2dCs 'Disable A/D chip select HIGH a2dDin 'Initial state of data in LOW a2dClk 'Initial state of clock a2dResult = 0 'Clear the 10-bit result A2D_Start_Conversion: 'Start the conversion process LOW a2dCs 'Enable A/D chip select A2D_Shift_Out_Channel_ID: 'Shift out the Channel ID value PULSOUT a2dClk,10 'Send first clock with Din high SHIFTOUT a2dDin,a2dClk,MSBFIRST,[a2dIdShiftOut\4] A2D_Shift_In_Result: 'Shift in the result PULSOUT a2dClk,10 'clock out null bit PULSOUT a2dClk,10 'clock out null bit SHIFTIN a2dDout,a2dClk,MSBPRE,[a2dResult\12] A2D_End_Conversion: HIGH a2dCs 'Disable A/D chip select RETURN '------------------------------------------------------------------ ' Program End '------------------------------------------------------------------ END