DIO protocol

From BitWizard Wiki
Revision as of 16:13, 16 November 2012 by Rew (talk | contribs) (→‎Example)
Jump to navigation Jump to search

Introduction

The protocol for the DIO, 3FETs, 7FETs, RELAY and Pushbutton will be explained on this page. Most functions apply to all boards, but some don't.

This page describes both the SPI and the I2C version. See SPI versus I2C protocols for the explanation about how the protocols work in general.

Please see this page for the default adresses.

write ports

On the DIO and related boards all ports just set a single value. So writing more than one byte to such a port is redundant. The last value is the one used. The DIO boards don't have any ports that are logically a stream of bytes. So writing more than one or two bytes is not encouraged.

The DIO, 3FETS and 7FETS boards define several ports:

port available on function
DIO 3/7FETs RELAY pushbutton
0x10 X X X set all outputs (bit 0 is output 0, etc).
0x20 .. 0x27 X X X set one output (0x20 for output 0, 0x21 for output 1 etc)
0x30 X X X define pins as inputs or outputs. 0 means input, 1 means output.
0x40 X X set current position.
0x41 X X set target position.
0x42 X X set relative position.
0x43 X X set stepdelay. (in tenths of a microsecond, default 200: 20ms between steps).
0x50 .. 0x57 v1.1 and up X Set PWM value. 0x50: output 0, 0x51 output 1 etc.
0x5f v1.1 and up X Set PWM mask. PWM is enabled on the outputs, who's bit is high. send 0x01 as data, to enable PWM on output 0
0x70 .. 0x77 v1.2 and up Select which i/o is coupled to which ADC channel
0x80 v1.2 and up Set number of ADC channels to read
0x81 v1.2 and up Set number of samples to add (we suggest using a power of 2) (two bytes)
0x82 v1.2 and up Set ammounts of bits to shift accumulated sample value
0xf0 X X X X change address.

All the above ports are read/write. I.e. if you read from that port, you will get the current value.

read ports

The DIO, 3FETS, and 7FETS boards support the following read ports:

port available on function
DIO 3/7FETs RELAY pushbutton
0x01 X X X X identification string. (terminated with 0).
0x02 X X X X read eeprom (serial number).
0x10 X X read all inputs
0x20 .. 0x27 X X read one input (0x20 for input 0, 0x21 for input 1 etc)
0x40 X X read current position.
0x41 X X read target position.
0x43 X X read stepdelay. (in tenths of a microsecond, default 200: 20ms between steps).
0x50 v1.1 and up X Return PWM value for output 0
0x51 v1.1 and up X Return PWM value for output 1
0x52 v1.1 and up X Return PWM value for output 2
0x53 v1.1 and up X Return PWM value for output 3
0x54 v1.1 and up X Return PWM value for output 4
0x55 v1.1 and up X Return PWM value for output 5
0x56 v1.1 and up X Return PWM value for output 6
0x5f v1.1 and up X Return PWM mask. PWM is enabled on the outputs, who's bit is high. send 0x01 as data, to enable PWM on output 0
0x60.. 0x67 v1.2 and up Return analog value (2 bytes)
0x68 .. 0x6f v1.2 and up Return added and bitshifted analog value (2 bytes)
0x70 .. 0x77 v1.2 and up Return which i/o is coupled to which ADC channel
0x80 v1.2 and up Return number of ADC channels to read
0x81 v1.2 and up Return number of samples to add (two bytes)
0x12 v1.2 and up Return ammounts of bits to shift accumulated sample value

Using the analog inputs

Taking measurements

The built-in ADC has 10 bits of resolution, and can be read in two different ways:

  • Just read the latest sample
  • Add x samples, and optionally bitshift the result by n bits.

The first option is easy, but prone to some noise.

The second option gives you the ability to reduce the noise and/or obtain higher resolution.

To take the average of a number of samples, set the "nsamp" (0x81) register to 2^n, and set the shift (0x82) register to n. This tells the controller to sum 2^n samples, and then divide by 2^n, resulting in the average value, which can be read from registers 0x68 .. 0x6f, depending on the channel.


For more than 10 bits of precision, it's possible to skip the bitshifting. In theory, this gives you a higher accuracy than the ADC's 10 bits. To do this, set register 0x81 to 2^n, and set register 0x82 to 0. The result can then be read from registers 0x68 .. 0x6f, depending on the channel. Do take note that statisticians have proven that you gain only n/2 of significant bits this way.

You can set nsamp (register 0x81) to 4096 (0x1000 = 2^12), and then the shift register (0x82) to 6. This will give you a 16bit results with about 16 bits of significance. The performance is such that you can get an update about every second in this mode. This is ideal for things like temperature readings.

Setting up the ADC

First decide which i/o pins you want to use as analog inputs, and which i/o pin is analog 0, 1, etc. These settings need to be written to registers 0x70 .. 0x76, 0x70 being analog0, and 0x76 being analog6. Tho following values are valid:

IO pin value
0 0x07
1 0x03
2 analog not available
3 0x02
4 0x01
5 analog not available
6 0x00

Now decide how you want to sample the analog values, and set those registers.

Finally, you need to write the amount of analog channels you want to use to register 0x80. The controller wil now start sampling those channels.

Example

We want to use IO 4 and IO 0 for reading analog values, and want an average value over 16 samples. To do this, we need to send the following commands:

command explanation
0x84 0x70 0x01 Couple ADC channel 0 to IO4
0x84 0x71 0x07 Couple ADC channel 1 to IO0
0x84 0x81 0x00 0x01 Add 256 (2^8) samples
0x84 0x82 0x04 Bitshift result by 4 bits
0x84 0x80 0x02 Set number of channels to sample to 2

examples

For SPI in the examples below, "data sent" means the data on the MOSI line, while "data received" means the data on the MISO line. when MISO reads "xx" you should ignore the data. When MOSI reads "xx" it doesn't matter what you send.

For I2C in the examples below, you should first initiate a "write" transaction with the data in the "data sent column". Don't send the "xx" bytes. Then you initiate a "read" transaction, and you will get the data in the "data received" column (and again not the "xx" bytes).


read identification

read the identification string of the board. (SPI_DIO)

data sent data received explanation
0x85 xx select destination with address 0x84 for READ.
0x01 xx identify
xx 0x73 's'
xx 0x70 'p'
xx 0x69 'i'
xx ... etc.

read the identification string of the board. (I2C_DIO)

I2C master I2C slave (i2c_dio) explanation
START -- start I2C transaction
0x84 -- select destination with address 0x84 for write (set port).
0x01 -- identify
STOP -- terminate I2C transaction.
START -- start I2C transaction
0x85 -- select destination with address 0x84 for READ.
-- 0x69 'i'
-- 0x32 '2'
-- 0x63 'c'
-- ... etc.

Note that in the SPI example, there is bidirectional datatransfer on every cycle, but the data is "don't care" or "must ignore" (indicated by xx), while in the I2C case, the other side cannot send as there is only one data-transfer direction (indicated by "--").

turn on all outputs

data sent data recieved explanation
0x88 xx select destination with address 0x88 for WRITE
0x10 xx set outputs as in bitpattern (next byte)
0xff xx All outputs active.

turn on output 4

data sent data recieved explanation
0x88 xx select destination with address 0x88 for WRITE
0x24 xx port 0x24: output 4...
0xff xx ... active.

move stepper to step 0x1234

data sent data recieved explanation
0x88 xx select destination with address 0x88 for WRITE
0x41 xx port 0x41: set target position
0x34 xx low byte
0x12 xx high byte.