' {$STAMP BS2} ' {$PBASIC 2.5} ' ' Experiments 3-Phase Wind Turbines ' 3-Phase Wind Experiments ' Version 1.0, Program Listing 1.0 ' File: Parallax_Wind_3Phase_Exp.bs2 ' Copyright: LearnOnLine, Inc. ' Author: J. Gavlik ' Last Updated: 06-30-2009 ' '------------------------------------------------------------------ ' Declarations '------------------------------------------------------------------ ' ' General VARs ' tempWord VAR Word flag VAR Bit phase1 VAR Word 'A/D Channel 1 phase2 VAR Word 'A/D Channel 2 phase3 VAR Word 'A/D Channel 3 rectDC VAR Word 'A/D Channel 0 phase1prev VAR Word phase2prev VAR Word phase3prev VAR Word rectDCprev VAR Word loLimit CON 50 hiLimit CON 100 prevReading VAR Word ckSum VAR Byte delay VAR Byte 'ms of delay between samples ' ' 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 '------------------------------------------------------------------ ' 3-Phase Wind Experiment Algorithm '------------------------------------------------------------------ ' Sync up on Phase 1 by waiting for zero volts then waiting for the voltage to ' begin to peak again. ' delay xx milliseconds to before sampling the three phases and rectified DC voltage ' take samples of all three phases and the rectified DC voltage ' covert the raw count to millivolts ' compute the checksum and transmit the data to the computer ' increment the delay 1 millisecond for the next sample ' repeat '------------------------------------------------------------------ ' Main Loop '------------------------------------------------------------------ Wind_3Phase_Exp: GOSUB Wait_For_Phase1_Zero_Volts GOSUB Wait_For_Phase1_New_Peak PAUSE delay GOSUB Take_Samples GOSUB Transmit_Data_To_PC delay = delay + 1 GOTO Wind_3Phase_Exp '------------------------------------------------------------------ ' Wait_For_Phase1_Zero_Volts ' Loop until phase1 voltage is below a minimum a2d count ' indicating that it is at the bottom of its voltage cycle '------------------------------------------------------------------ Wait_For_Phase1_Zero_Volts: a2dIdShiftOut = a2dId1 GOSUB A2D IF (a2dResult > loLimit) THEN Wait_For_Phase1_Zero_Volts RETURN '------------------------------------------------------------------ ' Wait_For_Phase1_New_Peak ' Loop until phase1 voltage is above a minimum a2d count ' indicating that it is just beginning to rise again '------------------------------------------------------------------ Wait_For_Phase1_New_Peak: a2dIdshiftOut = a2dId1 GOSUB A2D IF (a2dResult < hiLimit) THEN Wait_For_Phase1_New_Peak RETURN '------------------------------------------------------------------ ' Take Samples ' Acquire a2d readings for all three phase voltages plus ' rectified voltage and average the reading with the last sample '------------------------------------------------------------------ Take_Samples: a2dIdShiftOut = a2dId1 'acquire new phase and rectDC samples GOSUB A2d phase1 = a2dResult a2dIdShiftOut = a2dId2 GOSUB A2d phase2 = a2dResult a2dIdShiftOut = a2dId3 GOSUB A2d phase3 = a2dResult a2dIdShiftOut = a2dId0 GOSUB A2d rectDC = a2dResult tempWord = phase1 'average new samples with last samples to smooth plot phase1 = (phase1 + phase1prev)/2 phase1prev = tempWord tempWord = phase2 phase2 = (phase2 + phase2prev)/2 phase2prev = tempWord tempWord = phase3 phase3 = (phase3 + phase3prev)/2 phase3prev = tempWord tempWord = rectDC rectDC = (rectDC + rectDCprev)/2 rectDCprev = tempWord RETURN '------------------------------------------------------------------ ' Transmit Data To PC ' 1. The raw a2d readings are converted to millivolts by multiplying each count by 1.22 mv/count ' using the Multiply Middle Operator */ where the fraction of 0.22 is converted to ' a hex number that represents 0.22 x 256 = 56.32 = $38. The integer 1 forms the other ' part of the total hex number $0138. ' ' 2. The checksum (cksum) is computed as a one-byte value ' ' 3. The entire data stream is transmitted to the computer using the Debug instruction @ 9600 baud ' '------------------------------------------------------------------ Transmit_Data_To_PC: phase1 = phase1 */ $0138 phase2 = phase2 */ $0138 phase3 = phase3 */ $0138 rectDC = rectDC */ $0138 cksum = phase1.HIGHBYTE + phase1.LOWBYTE cksum = phase2.HIGHBYTE + phase2.LOWBYTE + cksum cksum = phase3.HIGHBYTE + phase3.LOWBYTE + cksum cksum = rectDC.HIGHBYTE + rectDC.LOWBYTE + cksum DEBUG phase1.HIGHBYTE, phase1.LOWBYTE, phase2.HIGHBYTE, phase2.LOWBYTE, phase3.HIGHBYTE, phase3.LOWBYTE, rectDC.HIGHBYTE, rectDC.LOWBYTE DEBUG 0, 0, 0, 0, 0, 0, cksum 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