' {$STAMP BS2} ' {$PBASIC 2.5} ' ' Experiments with Fuel Cells ' Version 1.0, Program Listing 1.0 ' File: Parallax_Fuel_Cell_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 average 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 '------------------------------------------------------------------ ' Fuel Cell Experiment Algorithm '------------------------------------------------------------------ ' ' Sample and average the input voltage from the a/d converter ' Convert the value to millivolts ' Sample and average the voltage across the 1 ohm sense resistor ' Convert the value to millivolts ' Test to see if the input voltage > voltage drop across the 1 ohm resistor ' if so, ingore readings and loop back to top for more ' if not, continue ' Convert the 1 ohm voltage drop to current in milliamps ' Transmit the voltage and current values to the computer ' Repeat ' '------------------------------------------------------------------ Fuel_Cell_Exp: GOSUB Get_Voltage GOSUB Get_One_Ohm_Drop IF (voltage < oneOhmDrop) THEN GOTO Fuel_Cell_Exp ENDIF current = (voltage - oneOhmDrop) / 1 '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 Fuel_Cell_Exp ' Repeat '------------------------------------------------------------------ ' Get Input Voltage '------------------------------------------------------------------ Get_Voltage: a2dIdShiftOut = a2dId0 ' Set the a/d converter to ch0 GOSUB Average_A2D ' Sample the raw 12-bit input voltage from the a/d cnverter voltage = a2dresult ' Set voltage variable to result voltage = voltage */$0138' Convert the 12-bit value to millivolts (multiply by 1.22mv / count) RETURN '------------------------------------------------------------------ ' Get Voltage Drop Across 1 ohm Sense Resistor '------------------------------------------------------------------ Get_One_Ohm_Drop: a2dIdShiftOut = a2dId5 ' Set the a/d converter to ch5 GOSUB Average_A2D ' Sample the raw 12-bit voltage across the 1 ohm sense resistor oneOhmDrop= a2dresult */ $0138 'Convert the 12-bit value to millivolts (multiply by 1.22mv / count) RETURN '------------------------------------------------------------------ ' Average A2D Readings ' Enter with a2dIdShiftOut as the channel to convert ' Return with averaged 12-bit result in a2dResult '------------------------------------------------------------------ Average_A2D: average = 0 FOR i = 0 TO 15 'take 16 a2d readings and average the result GOSUB A2D average = average + a2dresult NEXT a2dresult = average / 16 Average_A2D_End: 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 A2D_End: RETURN 'Return to calling routine '------------------------------------------------------------------ ' 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 Plot_It_End: RETURN