Pages: [1]   Go Down
Author Topic: Short SPI controlled DAC demo with MCP4921  (Read 291 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
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.

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()
     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);
     digitalWrite(chipSelectPin, HIGH);
« Last Edit: November 04, 2013, 08:14:33 pm by mcgavinz26 » Logged

Pages: [1]   Go Up
Jump to: