User Manual

35
Figure 24: The wiring diagram for the dad test, which allows you to test the A/D and D/A converters together,
without the aid of a multimeter or potentiometer.
D/A and A/D tests in C
Since the D/A and A/D converters both use the SPI bus, the common SPI bus code has been placed
into a separate file, gb_spi.c. There is also an associated header file, gb_spi.h, which contains
many macros and constants needed for interacting with the SPI bus, as well as the declarations for the
functions in gb_spi.c. These functions are setup_spi, read_adc, and write_dac.
setup_spi sets the clock speed for the bus and clears status bits. read_adc takes an argument
specifying the channel (should be 0 or 1) and returns an integer with the value read from the A/D
converter. The value returned will be between 0 and 1023 (i.e. only the least significant 10 bits are
set), with 0 returned when the input pin for that channel is 0V and 1023 returned for 3.3V.
The write_dac routine takes two arguments, a channel number (0 or 1) and a value to write. The
value written requires some explanation. The MCP48xx family of digital to analogue converters all
accept a 12 bit value. The MCP4822 uses all the bits; the MCP4812 ignores the last two; and the
MCP4802 ignores the last four. Since any of those chips might appear on the Gertboard (depending
on availability), write_dac is written in so that it will work with all three, so it simply sends to the
D/A the value it was given. If Gertboard is fitted with the MCP4802, it can only handle values
between 0 and 255, but these must be in bits 4 through 11 (assuming the least significant bit is bit 0)
of the bit string it is sent. Thus if the desired number to be sent to the D/A is between 0 and 255, it
must be multiplied by 16 (which effectively shifts the information 4 bits to the left) before sending
this value to write_dac.
dtoa
To test the D/A, the dtoa program first asks which channel to use and prints out the connections
needed to make on Gertboard to run the program. Then it calls setup_io to get the GPIO ready to