Interfacing DAC1220 20 bit DAC with Teensy 3.5

Status
Not open for further replies.

ahmet

Member
Hi all,

I am trying to interface DAC1220, 20 bit high resolution DAC from TI, with Teensy 3.5. I attached the code I am using, wiring schematic and DAC1220 datasheet. I can control the DAC1220 with Arduino uno, but I cannot control it with teensy 3.5. The details are as follows.

DAC1220 AVDD and DVDD are supplied with 5V. Teensy 3.5 is powered from USB. I checked the teensy 3.5, input voltages have 5V tolerance. Also, DAC1220 logic input levels are ok with teensy logic levels. (DAC1220 min input high voltage is 2V, output high voltage is max around DVDD, i.e 5V in my case) When I am trying to control DAC with Arduino, I can easily set whatever voltage I want, but when I am trying to control it with teensy 3.5, DAC1220 output is always stays at around 2.5V and no change occurs. I tried to power DAC with 3.3V and see if the issue is related to DAC power supplies, but this time output stayed at 3.3V and no change occured whatever voltage I set in software.

Wiring:
Teensy 37 -> DAC SCLK
Teensy 38 -> DAC SDIO
Teensy 20 -> DAC CS

Could you please look at my code and help me where I am making mistake. I am new to teensy and any help would be great.

I used the code posted in this web page (https://www.breadboarding.de/programmable-voltage-reference-part-1/) and slightly modified it.

Thank you very much for your help in advance.

Ahmet

Code:
#define PIN_CS  20//enable
#define PIN_CLK  37//spi clock
#define PIN_DAT 38//spi data

uint32_t Millis = 0;
uint8_t  Buf[10];
uint8_t  Len = 0;
float cal = 1.00015; 
float offset = 0.0004; 
float voltageLimit = 4.99; 
float v=3.58; // DAC out voltage
void setup() {
  Serial.begin(9600);
  pinMode(PIN_CS, OUTPUT);
  pinMode(PIN_CLK, OUTPUT);
  pinMode(PIN_DAT, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(PIN_CS, HIGH);
    digitalWrite(13, HIGH);

  reset();
}

void loop() {
  sendData(v);
delay(1000);
  }


void sendData (float setVoltage) {
  if (setVoltage < offset) {
    setVoltage = offset;
  }
  else if (setVoltage > voltageLimit) {
    setVoltage = voltageLimit;
  }
  uint32_t bitCode = voltageToBits(setVoltage * cal - offset);
  if (bitCode > 0xFFFFF) {
    bitCode = 0xFFFFF;
    Serial.println("input value clipped.");
  }
  Serial.print("Voltage: ");
  Serial.print(setVoltage, 4);
  Serial.print("  Bits: ");
  Serial.println(bitCode);
  Serial.println("-----------------------");
  Serial.println("");
  bitCode = bitCode << 4;

  digitalWrite(13, HIGH);
  digitalWrite(PIN_CS, LOW);
  shiftOut(PIN_DAT, PIN_CLK, MSBFIRST, 0x40);
  shiftOut(PIN_DAT, PIN_CLK, MSBFIRST, (bitCode & 0x00FF0000) >> 16);
  shiftOut(PIN_DAT, PIN_CLK, MSBFIRST, (bitCode & 0x0000FF00) >> 8);
  shiftOut(PIN_DAT, PIN_CLK, MSBFIRST, (bitCode & 0x000000FF));
  digitalWrite(PIN_CS, HIGH);
  digitalWrite(13, LOW);
}

uint32_t voltageToBits(float voltage) {
  return (voltage / 5.0) * pow(2, 20);
}

void reset () {
  digitalWrite(13, HIGH);
  digitalWrite(PIN_CS, LOW);
  //Reset DAC
  pinMode(PIN_CLK, OUTPUT);
  digitalWrite(PIN_CLK, LOW);
  delay(1);
  digitalWrite(PIN_CLK, HIGH);
  delayMicroseconds(240); //First high period (600 clocks)
  digitalWrite(PIN_CLK, LOW);
  delayMicroseconds(5);
  digitalWrite(PIN_CLK, HIGH);
  delayMicroseconds(480); //Second high period (1200 clocks)
  digitalWrite(PIN_CLK, LOW);
  delayMicroseconds(5);
  digitalWrite(PIN_CLK, HIGH);
  delayMicroseconds(960); //Second high period (2400 clocks)
  digitalWrite(PIN_CLK, LOW);
  delay(1);
  //Start Self-Calibration
  shiftOut(PIN_DAT, PIN_CLK, MSBFIRST, 0x05);
  //20-bit resolution
  shiftOut(PIN_DAT, PIN_CLK, MSBFIRST, 0xA1); //20-bit resolution
  digitalWrite(PIN_CS, HIGH);
  delay(600);
  digitalWrite(13, LOW);
  Serial.println("DAC reset sucessful");
  sendData(2.5);
}

void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, int val, uint8_t bits = 8, uint8_t del = 10) {
  uint8_t i;
  for (i = 0; i < bits; i++)  {
    if (bitOrder == LSBFIRST)
      digitalWrite(dataPin, !!(val & (1 << i)));
    else
      digitalWrite(dataPin, !!(val & (1 << ((bits - 1 - i)))));
    digitalWrite(clockPin, HIGH);
    delayMicroseconds(del);
    digitalWrite(clockPin, LOW);
  }
}
 
Hi all,

Update: I also tested it with 3.3V logic level arduino. The DAC1220 was powered with 5V (both AVDD and DVDD) and Arduino supply was 3.3V (I uploaded sample codes to Arduino Uno and then disconnected it from USB and applied 3.3V to its VCC rails, all digital signals were at 3.3V level). I tested using this setup and the setup worked and I was able to adjust DAC output.

Therefore, I think the reason is most probably related to code. The code maybe is not ok with Teensy. Maybe some syntax or definitions are not same both for teensy and arduino uno. Any ideas?

Thank you for the help.

Ahmet
 
Hello,

check your func

Code:
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, int val, uint8_t bits = 8, uint8_t del = 10) {
  uint8_t i;
  for (i = 0; i < bits; i++)  {
    if (bitOrder == LSBFIRST)
      digitalWrite(dataPin, !!(val & (1 << i)));
    else
      digitalWrite(dataPin, !!(val & (1 << ((bits - 1 - i)))));
    digitalWrite(clockPin, HIGH);
    delayMicroseconds(del);
    digitalWrite(clockPin, LOW);
  }
}

I don't know how the compiler will interpret this, but it seems your if and else statement are not in one row, so maybe put {} brackets around your codeblock for the case statements are true
 
A wild guess - it's the speed of the teensy. I'd try adding more delays, for example at the beginning and end of ShiftOut().
 
@ahmet - Sorry Your post got stuck in a list of several BAD spam messages, And I accidentally screwed up... Trying to figure out how to undo... Did not mean to ban you.

Again Sorry!

Edit - I think I fixed it
 
Last edited:
@KurtE - Thank you for fixing it very quickly

@larry_berlin and @jonr , I will modify as you said and check the results. Thank you for the help
 
Hi all,

@larry_berlin I have put brackets for if and else code statements, again nothing changed.

@jonr I tried with delay(1) inside shifhout (at the begining and end of the function). I also tried with delay(1000) but it also did not work.

Is the syntax I am using correct? Any ideas why the code does not work.

Thank you for the help.
 
Maybe help to see picture of your setup.

For example to help eliminate some possible obvious issues.

Like how are the signals connected to the Teensy. For example are you using a breadboard? Are the pins soldered to the Teensy or are they just hoping that friction of the pins will be enough. There have been many times when someone did not solder the pins and had issues like this.

If it is all hooked up correct, I would then maybe check for anything that maybe is defined with things like int or unsigned int as they are different sizes on UNO versus T3.5...

Next up I would probably check your output with something like Logic Analyzer or Scope. If I did not have that, I would maybe add Print 1, 0 for each bit and see if they look like they are translated properly...

Edit, I would also probably look to see if I could put this on an SPI buss and use things like SPI.transfer. Instead of bit bang output
 
@KurtE thank you for the guidance.

Both T3.5 and DAC1220 are on PCB, soldered. Pins of T3.5 are soldered to PCB with headers (Headers have soldered to teensy also). T3.5 and DAC1220 have solid connection. All T3.5 GND are connected to PCB GND plane. T3.5 is powered from PC USB, PCB board is powered from external supply.

I have scope, I will check the interface pins and try to see the outputs. I will also check the issue you said about int or unsigned int.

Ahmet
 
@KurtE, I used the method you mentioned (using print function for bits) and made some debugging. I am now able to change the output of DAC, the problem was related to shiftout function. Right now, the outputs are not correct, but at least I am able to change them. I will make some more debug. But I think I will solve it.

Thank you all for the help.
 
Status
Not open for further replies.
Back
Top