Go Down

Topic: Arduino UNO SPI with MAX 5318 (Read 281 times) previous topic - next topic

JNie257

Jan 15, 2018, 05:15 pm Last Edit: Jan 15, 2018, 05:20 pm by JNie257
Ive got a project where im trying to hook up a MAX 5318 (Maxim Integrated 18-Bit DAC). It uses SPI

Link to datasheet for max 5318
https://datasheets.maximintegrated.com/en/ds/MAX5318.pdf

Ive got it hooked up along with my volt meter and OSCOPE and its sitting at 2.51V (5 Volts supplied so this is midscale) i need to send data to specific registers if i understand correctly, 0x4 (General Configuration Write Register) to turn on DOUT (not entirely sure if i need this as im just sending a number to get a voltage... it doesnt need to respond back to the MCU. Please correct me if im wrong) then there is 0x1 DIN Write Register. I believe this is the register i want to change to see a voltage change.


Not currently in front of my code or hardware but ive got something along the lines of:

digital.write(SS, LOW);
SPI.transfer(0x1);
SPI.transfer(0x3FFFF); this should be putting out fullscale +/- 1 LSB but regardless of value here, voltage doesnt change
digital.write(SS, HIGH);

I dont know what im doing wrong here. I will post my code as well as my pinout when im infront of it.

pylon

Post a wiring diagram and the complete code you're actually testing with.

SPI.transfer() sends exactly one byte, this may help to improve your code before posting.

JNie257

#2
Jan 18, 2018, 04:16 am Last Edit: Jan 18, 2018, 04:14 pm by JNie257
Thanks for the reply,

here is the non working example code i have written along with the wiring diagram/pinout

EDIT: instead of sending hex i changed it to a binary format and im getting a change in the voltage. also noticed im not specifying the registers address. im just sending data and its working. ill attach the updated code up when i get back in front of it

pylon

Quote
instead of sending hex i changed it to a binary format and im getting a change in the voltage.
The compiler changes the hex values to binary values during compilation, so both results in the same code. It must be something else you changed.

JNie257

#4
Jan 19, 2018, 10:07 pm Last Edit: Jan 19, 2018, 11:14 pm by JNie257
I saw table 9 on page 29 of the datasheet. It says the first 4 bits are the registers address. It requires 4 bits for registers address and 18 for data and 2 bits that it doesnt care about. I guess what im getting at is it was the wrong number as HEX

Now the code being sent doesnt match the voltage output im getting now. Im thinking maybe itd just be easier to post my code here instead of having you guys load it into arduino? Here it is

EDIT: Ive changed it to SPI mode 1 (updated the code below to show the change) and its working. Now is there an easy way of taking my data in (from a sensor) and putting the 4 bits in front of my data and then 2 after for the dont care bits according to table 9?

Code: [Select]
//  Modified 01/19/2018//



#define RESET 8

#define RDY 9

#define MZ 5

#define BUSY 6

#define LDAC 7

#define MOSI 12

#define MISO 11

#define SS 10

#define SCLK 13

#define TCSB 4

#define PD 3

#define DGND 2

#include <SPI.h>

#include <avr/io.h>


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *



void setup() {

 Serial.begin(57600);

 pinMode(RDY , INPUT);

 pinMode(MISO , INPUT);

 pinMode(MOSI , OUTPUT);

 pinMode(RESET , OUTPUT);

 pinMode(MZ , OUTPUT);

 pinMode(BUSY , INPUT);

 pinMode(LDAC , INPUT);

 pinMode(TCSB , OUTPUT);

 pinMode(PD , OUTPUT);

 pinMode(SCLK , OUTPUT);

 pinMode(SS , OUTPUT);

 pinMode(DGND , OUTPUT);



 digitalWrite(SS,HIGH);

 digitalWrite(RESET,HIGH);

 digitalWrite(SS,HIGH);

 delay(10);



 digitalWrite(SS,LOW);

 SPI.begin();

 SPI.beginTransaction(SPISettings(16000000, MSBFIRST, SPI_MODE1));

   SPCR |= (1<<MSTR);               // Set as Master

   SPCR |= (1<<SPR0)|(1<<SPR1);     // divided clock by 128

   SPCR |= (1<<SPE);                // Enable SPI

}



// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *



void loop(){

 digitalWrite(RESET,HIGH);   // Set high for normal operation

 digitalWrite(MZ,HIGH);      // Set output Voltage to midscale

 digitalWrite(TCSB,HIGH);  // Set low for Straight Binary format or high for Two's Complement

 digitalWrite(PD,LOW);       // Set low for normal operation or high for Power Down mode

 digitalWrite(DGND,LOW);     // Set low for digital ground



//  General Configuration Write Register (0x4)

 digitalWrite(SS,LOW);  // Set Slave Select LOW to begin data transfer

 SPI.transfer(0b01000111);   // Write first data byte

 SPI.transfer(0b10000000);   // Write second data byte

 SPI.transfer(0b00000000);   // Write third data byte

 digitalWrite(SS,HIGH); // Set Slave Select HIGH to end data transfer



//  GAIN Write Register (0x3)

 digitalWrite(SS,LOW);  // Set Slave Select LOW to begin data transfer

 SPI.transfer(0b00111111);   // Write first data byte

 SPI.transfer(0b11111111);   // Write second data byte

 SPI.transfer(0b11111100);   // Write third data byte

 digitalWrite(SS,HIGH); // Set Slave Select HIGH to end data transfer



//  OFFSET Register Write in Two's Complement (0x2)

 digitalWrite(SS,LOW);  // Set Slave Select LOW to begin data transfer

 SPI.transfer(0b00100000);   // Write first data byte

 SPI.transfer(0b00000000);   // Write second data byte

 SPI.transfer(0b00000000);   // Write third data byte

 digitalWrite(SS,HIGH); // Set Slave Select HIGH to end data transfer



//  Straight Binary DIN Write Register (TCSB = 0) (0x1)

 digitalWrite(SS,LOW);  // Set Slave Select LOW to begin data transfer

 SPI.transfer(0b00010000);   // Write first data byte

 SPI.transfer(0b00000000);   // Write second data byte

 SPI.transfer(0b00000000);   // Write third data byte

 digitalWrite(SS,HIGH); // Set Slave Select HIGH to end data transfer



// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *



while (1){

 digitalWrite(SS,LOW);  // Set Slave Select LOW to begin data transfer

 SPI.transfer(0b00011110);   // Write first data byte

 SPI.transfer(0b00000000);   // Write second data byte

 SPI.transfer(0b00000000);   // Write third data byte

 digitalWrite(SS,HIGH); // Set Slave Select HIGH to end data transfer

}

}

JNie257

It seems ive got it working. Now i need to read in a sensor and then send that value to the DAC.
the format is: 4 register bits/ 18 data bits/ 2 dont care bits (can be 0 or 1 it does not matter).

How would i have it send the register bits with the data that comes in from a sensor (live data) followed by another 2 bits?

Go Up