PinMode(2, INPUT) // Set pins 2-9 as inputs #include // Links prewritten SPI library into the code * Pins 2-9 are used to read in values of the 8 bits for the byte * This routine uses pin 10 as the chip select for the * The SPI library uses pin 13 of the Arduino Uno for clock. * in variable “pot”, and then sends “pot” out via the SPI. * The routine reads in 8 bit values, stores the value * Arduino program and an SPI enabled peripheral chip. * the Arduino IDE) to enable communication between an * This module uses the Arduino SPI library (comes bundled with * SPI test (Driving a Digital Potentiometer in this case) Note the comments in the code which tell you what each part of the code is doing. Insert the following code into the Arduino IDE. You must download the Arduino IDE (Integrated Development Environment) from This development tool allows you to write code that can be uploaded to the Arduino board and then executed. I used an oscilloscope to display the signals for this Instructables. The AD7376 digital potentiometer is a surface mount device so I soldered it onto a surface-mount proto-PCB so it could be plugged into the proto-board. It could be any 8-bit SPI device using this circuit.įor this Instructables, I used an Arduino Uno board, a proto-board, some DIP (Dual Inline Package) switches (they are optional), a variety of jumper wires, a DC power supply, an ohm meter, a few capacitors for DC power supply smoothing, and a few pull-up resistors for the digital potentiometer. This Instructables will show you how to set up and program a simple proto board circuit using the Arduino Uno to drive SPI data to a peripheral circuit which, in this case, is an Analog Devices AD7376 digital potentiometer. Oftentimes, you just want to test the electronic device to make sure it and its associated circuitry is working properly. Many hobbyists use microcontrollers such as the Arduino to control and use SPI devices. The devices receive data serially from a microcontroller using a 3-wire set-up that includes a chip select signal (usually titled CS - when this signal is at logic 0, a chip recognizes it will be receiving or sending data), a clock signal for clocking the serial data into the device, and the serial data stream itself. various sensors, LCD displays, digital potentiometers, D/A and A/D converters, wireless transmitters and receivers, audio volume controls). Serial.print(b2, BIN) Serial.There are many electronic devices that use the SPI bus, or Serial Peripheral Interface bus, for communications (e.g. Serial.print(b1, BIN) Serial.print(" ") ī2 = SPI.transfer(b2) // don't care what we send set the top 3 don't care bits so it will format nicely second byte Bx0000000 leftmost bit is D0 channel bitĬommandbits |= (channel << 7) // if D0 is set it will be the leftmost bit nowī1 = SPI.transfer(commandbits) // send out second byte of command bits SPI.transfer(commandbits) // send out first byte of command bits first byte will always be B000010xx where xx are the D2 and D1 channel bitsĬommandbits |= (channel > 1) // high bit of channel maximum clock frequency is 2.1 MHz 5VĭigitalWrite (SELPIN, LOW) // Select adc do whatever you want with these readings SPI.setDataMode(SPI_MODE0) // SPI 0,0 as per MCP330x data sheet SPI.setClockDivider( SPI_CLOCK_DIV8 ) // slow the SPI bus down read the MCP3304 in quad differential mode, non-bit-banging version (the read_adc function provides 3 differential input channels, I didn't bother to code up for the 4th differential channel) Note that this code hard-codes the command bits: It can sustain about 32ksps with a 2MHz SPI clock on an Uno or Mega Code: Select all This code uses the SPI library rather than bit-banging with digitalWrite() so is much more efficient.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |