Go Down

Topic: Short SPI controlled DAC demo with MCP4921 (Read 341 times) previous topic - next topic

Nov 04, 2013, 02:37 am Last Edit: Nov 05, 2013, 02:14 am by mcgavinz26 Reason: 1
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);
  }

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy