Pages: [1]   Go Down
Author Topic: Short SPI controlled DAC demo with MCP4921  (Read 198 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

http://www.speedlimit88.com/arduino/adc2dac/

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

Quote from: code from the site
#include <SPI>
  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);
  }
« Last Edit: November 04, 2013, 08:14:33 pm by mcgavinz26 » Logged

Pages: [1]   Go Up
Jump to: