0

I made my DAC code with DAC5311 and Arduino Uno. I think it has no problem, but it doesn't work well. Just print 0.(I use an oscilloscope.) I don't know where has the problem. Please give me any suggestion.

#include <SPI.h>
#include <math.h>
#define pin_CS 10
#define pin_MOSI 11 
#define pin_SCLK 13
#define max_VOLT 5

void setVoltage(float voltage){
  if (voltage < 0) voltage = 0;
  if (voltage > max_VOLT) voltage = max_VOLT;
  uint16_t temp = int(voltage * pow(2, 8) / max_VOLT);
  temp = temp << 6;
  uint8_t upper = temp >> 8;
  uint8_t lower = (temp & 0xff);
  digitalWrite(pin_CS, LOW);
  SPI.transfer(upper);
  SPI.transfer(lower);
  digitalWrite(pin_CS, HIGH); 
}

void setup() {
  Serial.begin(9600);
  pinMode(pin_CS, OUTPUT);
  pinMode(pin_MOSI, OUTPUT);
  pinMode(pin_SCLK, OUTPUT);              
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);
  digitalWrite(pin_CS, HIGH);
}

void loop() {
  setVoltage(5);
  delay(10);
}
3
  • 1
    a statement, it doesn't work well, has no meaning ... I use an oscilloscope is also meaningless ... please explain what you expected to happen, what actually happened and what you did with the oscilloscope
    – jsotola
    Mar 25 '20 at 4:18
  • Sorry, I'm not good at English. So it might be not clear. The point is that I want to set voltage 5, but the output of DAC5311 is zero. And I check the output with oscilloscope.
    – 임석빈
    Mar 25 '20 at 4:38
  • now, that is clear ... please edit your question ... your english is just fine ... is your oscilloscope functioning correctly and are the controls set correctly? ... double check the output using a voltmeter
    – jsotola
    Mar 25 '20 at 5:22
3

Classical +/- 1 problem

If you do this calculation by hand uint16_t temp = int(voltage * pow(2, 8) / max_VOLT);
you will get temp=256 and this is a value you can not convert with a 8 bit DAC.

Try uint16_t temp = int(voltage * (pow(2, 8)-1) / max_VOLT); instead.

(or event better for a µC uint16_t temp = (uint16_t) (voltage * (255 / max_VOLT));)

2
  • I revised it, but it was not still working. ㅜㅜ
    – 임석빈
    Mar 25 '20 at 9:18
  • So please try SPI_MODE1 or SPI_MODE3 (the device expects the valid bit on the falling clock phase)
    – theSealion
    Mar 25 '20 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.