Short SPI controlled DAC demo with MCP4921

I wanted to generate an analog signal so I read through several of the DAC solutions here. I've used SPI commands before so the MCP4921 seemed like an easy solution. The demo's I read seemed like they were heavier than they needed to be just to teach someone how to generate a DAC signal. They are good write ups, but I just wanted to do my own twist on it. So here it is.

I wrote this up fairly quickly and will probably make updates. Criticism welcome.

[quote author=code from the site]#include const int chipSelectPin = 10; const int analogInPin = A0; int sensorValue = 0; int outputValue = 0; byte outputValueByte0 = 0; byte outputValueByte1 = 0;

void setup() { SPI.begin(); SPI.setBitOrder(MSBFIRST); SPI.setClockDivider(SPI_CLOCK_DIV4); pinMode(chipSelectPin, OUTPUT); digitalWrite(chipSelectPin, HIGH); }

void loop() { sensorValue = analogRead(analogInPin); outputValue = map(sensorValue, 0, 1023, 0, 4095); outputValueByte0 = byte(outputValue); outputValue = outputValue >> 8; outputValueByte1 = byte(outputValue | 0b00110000);

digitalWrite(chipSelectPin, LOW); SPI.transfer(outputValueByte1); SPI.transfer(outputValueByte0); digitalWrite(chipSelectPin, HIGH); }[/quote]