Ok I am trying to learn how to use the SPI library to communicate with chips. Right now I am trying to get an mcp4922 dual channel 12 bit DAC working. I have already tested the DAC using example code and know the wiring is sound, and the chip operates the way it should. The example code Im using is direct port manipulation which is over my head right now. Here is my code using the SPI Library...
#include <SPI.h>
//SPI PINS
#define PIN_MOSI 11
#define PIN_MISO 12
#define PIN_SCLK 13
#define PIN_CS 10
void setup() {
pinMode(PIN_CS, OUTPUT);
digitalWrite(PIN_CS, HIGH);
SPI.begin();
}
void loop() {
digitalWrite(PIN_CS, LOW);
SPI.transfer(0x01111000);
SPI.transfer(0x00000000);
digitalWrite(PIN_CS, HIGH);
}
Here is the data sheet 12-bit DAC output - Project Guidance - Arduino Forum. According to it there is an upper byte and a lower byte, The upper byte has 4 bits which set some options and the lower 4 bits along with the lower byte are the 12 bit DAC value. So I would think my code sets the dac up to use port A, the next bit then sets the output to buffered, the third bit sets the gain to 1, and finally the fourth bit turns off the Hi-Z state of the outA pin, the remaining 12 bits should set the DACs output to 2048. The code in this post 12-bit DAC output - Project Guidance - Arduino Forum seems to be using the same technique as me and works.
I also have a few questions about the library and SPI in general, first of almost all example code I see controls the CS pin manually but the SPI.begin page says SPI.begin handles the CS pin and you can not use general I/O functions on it?
Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high.
Extended method for Arduino Due
If you specify one of the Arduino Due's Slave Select (SS) pin in the call to SPI.begin(), the pin is configured to be directly managed by the SPI interface.
Note that once the pin is configured, you can't use it anymore as a general I/O, unless you call the SPI.end() method on the same pin.
The only pins that can be configured to be managed by SPI interface are the Arduino Due's Slave Select pins: 4, 10, 52 and 54 (correspond to A0).
Secondly should I be setting up the clock polarity/phase and the MSB/LSB stuff?