A2D with SPI

08 May 2012

I am working with a max112010 a2d chip and need to use spi interface to read the a2d data. The a2d data is stored in 24bit data register. The example shows how one can read a register. There are control registers that I need to WRITE to them and I am not clear how that is done. I have tried consecutive write commands with the register address first and then the command number but all I am getting is 0xFF from all registers. I have tired different clock polarity and phase values as well. I would appreciate any comments or feedback on my code bellow.

  1. include "mbed.h"
  1. define STAT1 0x0
  2. define CTRL1 0x1
  3. define CTRL2 0x2
  4. define CTRL3 0x3
  5. define DATAR 0x4
  6. define CTRL1C 0x88

function defintion void calibrate11200(void); void reset(void);

SPI spi(p11, p12, p13); mosi, miso, sclk SPI spi(p5, p6, p7); mosi, miso, sclk DigitalOut cs(p8);

Serial pc(USBTX, USBRX); tx, rx

int main() { Setup the spi for 8 bit data, high steady state clock, second edge capture, with a 1MHz clock rate spi.format(8,0); format ( # of bits, poloarti = 0,1,2,3) spi.frequency(5000000); spi clock frequency 5MHz (ds, page 4) reset(); while(1) { pc.printf(".......................begine whil loop here \n\r"); cs = 0; Select the device by seting chip select low calibrate11200(); see calibration fun. spi.write(STAT1); int stat1returns = spi.write(0x00); pc.printf("STAT1 returned = 0x%X \r\n", stat1returns);

spi.write(CTRL1); int CTRL1returns = spi.write(CTRL1C); pc.printf("CTRL1 returned = 0x%X \r\n", CTRL1returns);

spi.write(DATAR); int data1 = spi.write(0x00); pc.printf("data1 register = 0x%X \r\n", data1); pc.printf("\n\r.......................loop ended here \n\r"); cs = 1; } }

14 May 2012

Just a hint: use <<code>> and <</code>> tags around your code to make it more readable

#include "mbed.h"
#define STAT1 0x0
#define CTRL1 0x1
#define CTRL2 0x2
#define CTRL3 0x3
#define DATAR 0x4
#define CTRL1C 0x88

//function defintion
void calibrate11200(void);
void reset(void);

//SPI spi(p11, p12, p13); //mosi, miso, sclk
SPI spi(p5, p6, p7); //mosi, miso, sclk
DigitalOut cs(p8);

Serial pc(USBTX, USBRX); //tx, rx

int main() {
  //Setup the spi for 8 bit data, high steady state clock, second edge capture, with a 1MHz clock rate
  spi.format(8,0); //format ( # of bits, poloarti = 0,1,2,3)
  spi.frequency(5000000); //spi clock frequency 5MHz (ds, page 4)
  reset();

  while(1) {
   pc.printf(".......................begine whil loop here \n\r");

   cs = 0; //Select the device by seting chip select low
   calibrate11200(); //see calibration fun.

   spi.write(STAT1);
   int stat1returns = spi.write(0x00);
   pc.printf("STAT1 returned = 0x%X \r\n", stat1returns);

   spi.write(CTRL1);
   int CTRL1returns = spi.write(CTRL1C);
   pc.printf("CTRL1 returned = 0x%X \r\n", CTRL1returns);

   spi.write(DATAR);
   int data1 = spi.write(0x00);
   pc.printf("data1 register = 0x%X \r\n", data1);

   pc.printf("\n\r.......................loop ended here \n\r");
   cs = 1;
  }
}

I did a quick check of the datasheet of the max11210. Some hints:

  1. You probably need to take cs low before each write and high again after the write.
  2. The register address needs to be inserted as bit 1-4 of the first byte you send. You also need to set bit 6 and 7 to select the right startbit and modebit and bit 0 must be '0' for write operations. See table 6 in the datasheet

That means for the CTRL1 reg

cs = 0;
spi.write(0xC0 | (CTRL1<<1) | 0x00); // select the reg for writing
spi.write(CTRL1C);                   // write the command
cs = 1;

You cant write and read back in the same instruction. First write the value and then perform another instruction where you read back the new value

cs = 0;
spi.write(0xC0 | (CTRL1<<1) | 0x01); // select the reg for reading
int CTRL1returns = spi.write(0x00);  // write a dummy value and read back the register
cs = 1;
pc.printf("CTRL1 returned = 0x%X \r\n", CTRL1returns);