Friday, March 8, 2019

Getting Arduino to talk SPI

As I wrote in my previous post I will be using a DAC to control a few analog voltages with an Arduino. The DAC that I've chosen is the DAC 8534 from texas instruments. It has four channels, meaning that it has four different outputs on which different voltages can be generated. I have now managed to control these four outputs. The digital instructions that the chip receives are sent from my Arduino using the SPI protocol. SPI stands for Serial Peripheral Interface and is a digital communication standard. In other words it describes a way to send digital information between devices. The rules for this standard are pretty loose which means that It is up to the device manufacturers to specify exactly how the communication will occur. In the case of the DAC 8534, the datasheet specifies rather specific communication rules, which happen to fall within the SPI standard. For this reason SPI, along with some other similar protocols, are listed as compatible interfaces for the DAC 8534. Since it's compatible, I could use the SPI library when programming my Arduino, which meant that a lot of the code was already written for me.



Communicating with the DAC 8534

The DAC 8534 takes three signals. One Data signal, one clock signal and one sync signal. During data transfer the clock signal is simply a square wave of a certain frequency. This tells the chip how often it should take in data from the data signal. At each falling edge of the clock signal the chip stores one bit. If the Data signal has a high voltage during this time, the bit is stored as a 1, and if it has a low voltage, the bit is stored as a 0. When 24 bits have been stored this way, the transfer is complete, and the DAC 8534 will ignore the clock and data signals until a new transfer is started. The sync signal simply tells the chip when a transfer starts, and can also be used to cancel a transfer before it's complete.

(When talking about digital logic, a "high" voltage is usually around the supply voltage, which in this case is 5Vdc. A "low" voltage is typically close to 0V) 




How the DAC 8534 interprets information

The Image above shows the data and clock signals during a typical data transfer. The green trace is the data signal and the red trace is the clock signal. The sync signal is not shown, but it stays low during the entire transfer. As described above, the data signal is stored as a bit on each falling edge of the clock signal. I've written the value of each bit under the clock signal and assigned them into five groups. When telling the DAC 8534 to generate a certain voltage on one of its outputs, these five groups of bits control different parameters.

Group A
The bits in group A are addressing bits. The DAC 8534 has two addressing pins, which can be tied to a high or a low voltage to select an address. If the address bits do not match the address of the chip, the transfer will be ignored. This way up to four DAC 8534 chips can be used on a single communication bus. I only use a single chip, with both address pins tied to ground. Hence, the address bits are both 0 in this example. 

Group B
The Dac 8534 has a data buffer on each channel, these can store instructions for later use. When data is transferred, the bits in group B control whether the data should be used to generate a voltage on the channel output, or if it should just be saved to the data buffer. When a voltage output is generated, all other channels can be told to simultaneously generate an output based on their data buffers previously stored instructions. The group B bits can also be used to broadcast, so that all channels receive the same instructions. Broadcast also overwrites the address bits, so If several DAC 8534s are used on the same spi bus, the broadcast instructions reach each chip. In this example, these bits hold the value 01, which simply tells the chip to generate an output on the receiving channel.


There is one bit between groups B and C, This bit is a "don't care bit" and can be either 1 or 0.


Group C
The value of the bits in group C determines which channel should receive the instructions. In this case, They hold the value 2 (10 in binary), which corresponds to channel C. 

Group D
When the bit in group D is set to 1, the receiving channel enters one of three power down modes. All power down modes disconnect the output from the rest of the chip. Two of the modes also connect the output to ground through a 1kohm or 10kohm resistor. When a power down command is transmitted the first two bits in group E determines which mode is used. These power down instructions can be stored and updated for all channels just like regular voltage data. In this example I want the DAC to operate normally and produce a voltage output, so the bit in group D is a 0.

Group E
When no power down instructions are being sent the 16 bits in group E control the actual output voltage. They can hold a value between 0 and 65 535 (2^16 - 1). in this example, their value is 4040, which in my test circuit corresponds to an output of about 0,3V.

Why use a DAC?

The Arduino board does have a few "Analog" outputs on its own, but those are actually not true analog, those are what's called pwm, or Pulse Width Modulation. It's a technique used to emulate analog, and works well for controlling motor speed or the brightness of a led.


This picture shows the difference between PWM and a true analog signal. The PWM signal tries to emulate the analog signal with pulses at a certain frequency. In order to imitate a strong or weak signal the duration of each pulse is varied. It's essentially like turning something on and off really quickly to make it seem analog.


I could probably get a decent signal by filtering the pwm signal, which would make it smoother, but a dedicated DAC works better as it outputs a steady voltage to begin with. The DAC 8534 also has a higher, 16 bit resolution, compared to Arduinos 8 bit.

Next time

I'm getting closer to generating an image now! before I can start interfacing my electronics with a computer I need to create a piezo driver. the piezo driver is an analog circuit that takes the X, Y and Z signals and converts them into four signals that move the piezo disc accordingly. I am almost certain that I'll also have to improve the feedback loop, and eventually the mechanics, if I want to produce a nice image. 


No comments:

Post a Comment