How the AD Converter Routine Works

This routine references names (aliases) defined in the Declarations section. The first four pin directives give names to the BASIC Stamp I/O pins that identify which pin of the ADC0834 they are connected to. These aliases will be used in commands that enable communication between the two devices.

A2dChipSel A2dDataIn A2dClk A2dDataOut

PIN 0

PIN 1

PIN 2

PIN 3

The four con directives that follow are specially coded to let the ADC0834 know which channel, 0 through 3, that we want to convert. One of the first things the BASIC Stamp must do is send the appropriate MuxId to the ADC0834.

A2dMuxId0 A2dMuxId1 A2dMuxId2 A2dMuxId3

CON %1100

CON %1110

CON %1101

CON %1111

To accomplish this, the nibble sized variable a2dMuxid will be set to either A2dMuxid0, 1, 2, or 3, depending on which channel is to be converted. When the conversion is finished, the result will be stored in a2dResuit.

a2dMuxId VAR Nib a2dResult VAR Byte

The subroutine A2D begins by initializing the ADC0834 control signals. To start a conversion, the A2dChipSel (CS) pin must transition from high to low, so here, A2dChipSei is initially set high. A2dDatain and Ad2Cik are then set low, to initialize the Data In and Clock pins to be low. Finally, a2dResuit is initialized to 0.

A2D:

HIGH A2dChipSel LOW A2dDataIn LOW A2dClk a2dResult = 0

At the label A2D_Start_Conversion, the command LOW A2dChipSel sends a low signal to the ADC0834's CS pin, thus telling the ADC0834 to begin a conversion. The A2dChipSel pin needs to stay low for the duration of the conversion.

A2D_Start_Conversion: LOW A2dChipSel

Next, the BASIC Stamp module must tell the ADC0834 which channel to convert, done at the A2D_Shift_Out_Channel_ID label. The shiftout instruction sends the proper channel ID to the ADC0834.

A2D_Shift_Out_Channel_ID:

SHIFTOUT A2dDataIn,A2dClk,MSBFIRST,[a2dMuxId\4]

At the A2D_shift_in_Result label, the next command, pulsout A2dClk,10 will send a clock pulse that has the right shape, low-high-low.

A2D_Shift_In_Result: PULSOUT A2dClk,10

pulsout A2dClk,10 sends a clock pulse to the ADC0834's CLK input. This is the first clock pulse, and all it does is tell the ADC0834 to start converting on the next clock pulse. Because of this, we don't need to check for input from DO after this first clock pulse.

Since we set the clock low initially, pulsout sends the desired low-high-low signal. The duration of the high segment is twice the number specified in the pulsout command, in microseconds (^s). 1 ^s = 1/1,000,000 of a second. Therefore the duration of this high segment is 2 ^s x 10 = 20 ^s.

The following shiftin command is a powerful instruction that takes care of all the synchronous serial communication.

SHIFTIN A2dDataOut,A2dClk,MSBPRE,[a2dResult\8]

In effect, this command sends clock pulses to the ADC0834's CLK input and reads output bits from ADC0834's DO output. This command also loads each of the ADC0831's output bits into the variable a2dResult byte. The shiftin command is discussed in more detail in the BASIC Stamp Manual and the BASIC Stamp Editor's Help menu, but the general format for the command is:

SHIFTIN data_ pin, clock_pin, mode, [variable\bits]

In our case, the data pin is A2dDataOut, and the clock pin is A2dcik.

The mode in this case is msbpre, and it's one of four transmission modes that can be used in this command. It indicates that the ADC0834's output bits are ready before the clock pulse's negative edge, the transition from high to low. It also indicates that the bits are transmitted in descending order, starting with the MSB (most significant bit). [a2dResuit \8] means the data is shifted into the a2dResuit variable, and 8-bits are expected.

Finally, the command

HIGH A2dChipSel ends the data exchange with the ADC0834. The last label A2D_End is followed by the return command that sends the program back into the Main routine.

These additions to the larger program will remain in place for all of the experiments. Before going directly to Experiment 1, it would be prudent and proper to test the A/D converter to see if it is performing correctly. The A/D converter is used in each one of the five experiments, so validating its performance here and now would be wise. Then you can be confident of proper converted voltage readings when you do get to the experiments (which will be soon if you've done everything right up until now).

Solar Power

Solar Power

Start Saving On Your Electricity Bills Using The Power of the Sun And Other Natural Resources!

Get My Free Ebook


Post a comment