'{$STAMP BS2SX} '' '' Programmer: Craig Stuart Sapp '' Creation Date: Tue Sep 26 11:06:07 PDT 2000 '' Last Modified: Wed Apr 23 21:26:23 PDT 2003 '' Filename: adflex.bsx '' Based On: midi1270.bsx '' Syntax: Basic Stamp IIsx '' '' Description: This program reads in samples from the '' Maxim 1270 (a 12-bit, 8 channels A/D chip). '' The sampled value is then sent out serially via '' MIDI protocol, using the aftertouch command '' with the key parameter holding 7-bits (MSB) of the '' message and the pressure parameter holding 5-bits '' (LSB) of the 12-bit sampled data. '' sleeptime con 500 ' time in milliseconds to wait between sensor samples ' Data Variables: bytea var byte ' for holding first 7 MSBs of 12-bit sample byteb var byte ' for holding 5 LSBs of 12-bit sample ' MIDI output variables midicommand con $A0 ' use MIDI aftertouch command to send sensor data midioutpin con 7 ' output pin on which to send MIDI data midirate con 60 ' baude mode for serout: (2500000/31250)-20 msec ' note that this value should be 12 for BS2. ' Serial rate for MIDI is 31,250 data bits/sec. ' Maxim 1270 chip variables clockpin con 11 ' synchronous clock pin to control MAXIM 1270 chip cspin con 10 ' chip select pin for MAXIM 1270 (only needed if more ' than one chip) outputpin con 9 ' for sending the controlbyte to the MAX1270 chip inputpin con 8 ' for reading data from MAXIM 1270 chip msb con 1 ' mode for shiftout functions: msb behindclock con 2 ' mode for shiftin: msb post clock ' The MAX1270 control byte below is composed of several fields ' listed here from MSB to LSB bits: ' bit7: defines the beginning of the control byte ' bits4-6: selects the A/D channel (0-7). ' bit3: choose the A/D voltage range, 0=5v, 1=10v ' bit2: choose polar/bipolar range, 0=positive, 1=positive/negative ' bit0: choose clock mode: 0=internal, 1=external ' bits0-1: 10=standby, 11=power-down (clock mode unaffected) ' ' the control byte 1,000,00,01 means: channel 0, 0 to +5v, external clock: controlbyte con %10000001 ' Commands used below: ' ' SHIFTOUT outputpin, clockpin, mode, [data \ bits] ' outputpin = pin to send synchronous data on ' clockpin = pin to send synchronous clock signal ' mode = 0=LSB, 1=MSB ' data = data to send ' bits = number of bits in data field to send (default = 8); ' ' SHIFTIN inputpin, clockpin, mode, [variable \ bits] ' inputin = pin to receive synchronous data on ' clockpin = pin to send synchronous clock signal ' mode = 0=LSB, 1=MSB ' variable = location to store the bits ' bits = number of bits to input (default = 8); ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' top: gosub ReadSensorData ' take a sensor reading gosub SendSensorData ' send reading out via MIDI ' wait a while before taking another sample pause sleeptime goto top '''''''''''''''''''''''''''''' '' '' ReadSensorData -- sends a command to the A/D converter chip '' requesting data. Then reads the returning data and stores '' the 7 most significant bits in bytea variable and the 5 '' least significant bits in the byteb variable. '' ReadSensorData: ' clear the storage bytes for sensor data bytea = 0 byteb = 0 ' first tell the MAX1270 that you want a sample (from channel 0) shiftout outputpin, clockpin, msb, [controlbyte \ 8] ' wait 4 clock cycles until the sample is ready to be returned shiftout outputpin, clockpin, msb, [0 \ 4] ' read in the returned sample from the MAX1270 chip shiftin inputpin, clockpin, behindclock, [bytea \ 7] ' finish reading in the data (12-bit sample) shiftin inputpin, clockpin, behindclock, [byteb \ 5] debug dec bytea, " ", dec byteb, cr return '''''''''''''''''''''''''''''' '' '' SendSensorData -- send sensor data via a MIDI command through '' a TTL (5 volt) serial line at 31.25 kBit speed. '' SendSensorData: serout midioutpin, midirate, [midicommand, bytea, byteb] return