SPI Communication with Arduino and maxim chip.

#include <SPI.h>
#define SCLOCK 9
#define MOSI 11
#define MISO 12
#define CS 10

void setup()
{
  SPI.begin();
  SPI.setDataMode(SPI_MODE1);
  SPI.setBitOrder(LSBFIRST);
  Serial.begin(9600);
  pinMode(SCLOCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(CS, OUTPUT);
  SPI.setClockDivider(SPI_CLOCK_DIV4);
  digitalWrite(CS, HIGH);
  digitalWrite(SCLOCK, LOW);
  digitalWrite(MOSI, LOW);
  
}
void loop()
{

}

Hello,
I am trying to communicate between this chip(max13362) and Arduino through SPI. I actually have the EV kit(data sheet also attached).I know what to do, but just dont know how to get started since first time doing SPI. My main goal is just reading the 32 bits. I realize it should be as LSBFIRST, that first 24 bits will be 0xFF and last 8 should be 0x0A(reverse this since LSB). I dont know where to go from there.

MAX13362EVKIT.pdf (534 KB)

MAX13362.pdf (260 KB)

Basic control register transfer:

digitalWrite (ssPin, LOW);
SPI.transfer(controlArray[0]);
SPI.transfer(controlArray[1]);
SPI.transfer(controlArray[2]);
SPI.transfer(controlArray[3]);
digitalWrite (ssPin, LOW);

Basic read transfer:

digitalWrite (ssPin, LOW);
incomingArray[0] = SPI.transfer(dummyByte);
incomingArray[1] = SPI.transfer(dummyByte);
incomingArray[2] = SPI.transfer(dummyByte);
incomingArray[3] = SPI.transfer(dummyByte);
digitalWrite (ssPin, HIGH);

Set up the controlArray bits as needed.

@CALIFORNIA2013, do not cross-post. Other thread removed.

What did you mean by dummy bite? new, to programming!

#include <SPI.h>
#define SCLOCK 9
#define MOSI 11
#define MISO 12
#define CS 10

byte incomingArray[4];

void setup()
{
  SPI.begin();
  SPI.setDataMode(SPI_MODE1);
  SPI.setBitOrder(LSBFIRST);
  Serial.begin(9600);
  pinMode(SCLOCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(CS, OUTPUT);
  SPI.setClockDivider(SPI_CLOCK_DIV4);
  digitalWrite(CS, HIGH);
  digitalWrite(SCLOCK, LOW);
  digitalWrite(MOSI, LOW);
  
}
void loop()
{
  digitalWrite(CS, LOW);
  SPI.transfer(0x0A);
  SPI.transfer(0xFF);
  SPI.transfer(0xFF);
  SPI.transfer(0xFF);
  digitalWrite(CS, HIGH);
  
  digitalWrite(CS, LOW);
  incomingArray[0] = SPI.transfer(0xFF);
  incomingArray[1] = SPI.transfer(0xFF);
  incomingArray[2] = SPI.transfer(0xFF);
  incomingArray[3] = SPI.transfer(0xFF);
  digitalWrite(CS, HIGH);
  
  Serial.println(incomingArray[1]);
}

Also, the way i am transfering in first will it send: 0100 0000 00…00(32ndbit)?
I was trying to read the input from the chip in serial monitor by Serial.print but it just gives me bunch of zeros?

dummyByte is data that will go out on MOSI while slave data is clocked on MISO.

SPI uses dedicated hardware pins. Take these & their pinModes out:

define SCLOCK 9

define MOSI 11

define MISO 12

SPI.begin(); in setup will take care of SCK (Clock), MOSI (serial data to the device), and MISO (serial dat from the device). Rewire accordingly & try again.

Got it. Tried everything you said still get zeros. Here is the code, does it look right? Now i guess CLOCK(13), MISO(12), MOSI(11).

#include <SPI.h>
#define CS 10

byte incomingArray [4];

void setup()
{
  SPI.begin();
  SPI.setDataMode(SPI_MODE1);
  SPI.setBitOrder(LSBFIRST);
  Serial.begin(9600);
  SPI.setClockDivider(SPI_CLOCK_DIV4);
  pinMode(CS, OUTPUT);
  digitalWrite(CS, HIGH);
 
}
void loop()
{
  digitalWrite(CS, LOW);
  SPI.transfer(0x0A);
  SPI.transfer(0xFF);
  SPI.transfer(0xFF);
  SPI.transfer(0xFF);
  digitalWrite(CS, HIGH);
  
  digitalWrite(CS, LOW);
  incomingArray [0] = SPI.transfer(0xFF);
  incomingArray [1] = SPI.transfer(0xFF);
  incomingArray [2] = SPI.transfer(0xFF);
  incomingArray [3] = SPI.transfer(0xFF);
  digitalWrite(CS, HIGH);
  
  Serial.print(incomingArray [0]);
  Serial.print(incomingArray [1]);
  Serial.print(incomingArray [2]);
  Serial.print(incomingArray [3]);
}

See page 11 of the data sheet: "Command Register Three 32-bit command registers are used to configure the MAX13362 for various modes of operation and are accessed by the SPI-compatible interface (see Table 2)." I'm wondering if you need 3 sets of writes to populate those registers before you can do reads that return results which make sense.

I dont think so, because the first two commands, most significant bytes are about the polling times. The reason i think its only last one because its significant bytes are LH(which is switch configuration). So it should all be zero but CB1 should be 1. I am still not sure if the way i am sending it. Does it send this: 0100 0000 0000 0000 0000 0000 0000 0000

Still think i am messing up code somewhere.

Take that back, i think you are right I probably need to send all three commands. I would do that the same way, as i sent the first one?

Yes. Read the datasheet a little more, perhaps send out all 12 bytes between one set of ssPIN LOW, ssPIN HIGH commands?

Yeah I tried that but still, when i open serial monitor i get all zeroes. Here is the code once again let me know if you see any errors. Also i sent LSB first.

#include <SPI.h>
#define CS 10

byte incomingArray [4];

void setup()
{
  SPI.begin();
  SPI.setDataMode(SPI_MODE1);
  SPI.setBitOrder(LSBFIRST);
  Serial.begin(9600);
  SPI.setClockDivider(SPI_CLOCK_DIV4);
  pinMode(CS, OUTPUT);
  digitalWrite(CS, HIGH);
 
}
void loop()
{
  digitalWrite(CS, LOW);
  SPI.transfer(0x00);  // CB0 = 0 && CB1 = 0
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  SPI.transfer(0x00);
  
  SPI.transfer(0x9F);  //CB0 = 1 && CB1 = 0
  SPI.transfer(0xFF);
  SPI.transfer(0xFF);
  SPI.transfer(0xE0);
  
  SPI.transfer(0x5F); // CB0 = 0 && CB1 = 1
  SPI.transfer(0xFF);
  SPI.transfer(0xFF);
  SPI.transfer(0xE0);
  digitalWrite(CS, HIGH);
  
  digitalWrite(CS, LOW);
  incomingArray [0] = SPI.transfer(0xFF);
  incomingArray [1] = SPI.transfer(0xFF);
  incomingArray [2] = SPI.transfer(0xFF);
  incomingArray [3] = SPI.transfer(0xFF);
  digitalWrite(CS, HIGH);
  
  Serial.print(incomingArray [0]);

}

Print all 4 bytes, see what the others have.

Check the eval kit drawing, may give some other ideas to try.

2 problems that I can see.

  1. You’re attempting to put a WHOLE BUNCH of commands into one session. You’re supposed to pull the CS line LOW, clock in 32 bits, than pull CS HIGH. Repeat for each register. You can’t transfer in multiple registers in one go.

  2. It looks like you’re getting the control bits ass-backwards. You set the SPI order to LSBFIRST, and according to the data sheet the control bits have to be clocked out first. However, it looks like you’re changing the MSBs, not the LSBs, in the first control byte. Rather than doing the bit math yourself, do this:

#define CB0 0
#define CB1 1

digitalWrite( CS, LOW );
SPI.transfer( _BV(CB1) | (0x3F<<2) ); // CB0 = 0, CB1 = 1
--- etc
digitalWrite( CS, HIGH );

Also, according to the datasheet and the Wikipedia article (http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_numbers), it looks like you want SPI_MODE0, not MODE1. GIve that a try.

I thought all the commands need to go once for it to respond. I did the way you said in part 1 still getting all zeros. The reason its SPI MODE 1 not 0 because in this chip CPOL = 0, CPHA =1, which is mode 1 default is zero, not in this case.
Can you elaborate on what you mean by this, would i have to define CB0 and CB1 every time since it changes:

#define CB0 0
#define CB1 1

digitalWrite( CS, LOW );
SPI.transfer( _BV(CB1) | (0x3F<<2) ); // CB0 = 0, CB1 = 1
--- etc
digitalWrite( CS, HIGH );

My code:

#include <SPI.h>
#define CS 10

byte incomingArray [4];

void setup()
{
  SPI.begin();
  SPI.setDataMode(SPI_MODE1);
  Serial.begin(9600);
  SPI.setClockDivider(SPI_CLOCK_DIV4);
  pinMode(CS, OUTPUT);
  digitalWrite(CS, HIGH);
 
}
void loop()
{
  digitalWrite(CS, LOW);
  SPI.transfer(0x00000000);  // CB0 = 0 && CB1 = 0
  digitalWrite(CS, HIGH);
  
  digitalWrite(CS, LOW);  
  SPI.transfer(0x9FFFFFE0);  //CB0 = 1 && CB1 = 0
  digitalWrite(CS, HIGH);
  
  digitalWrite(CS, LOW);
  SPI.transfer(0x5FFFFFE0); // CB0 = 0 && CB1 = 1
  digitalWrite(CS, HIGH);
  
  digitalWrite(CS, LOW);
  incomingArray [0] = SPI.transfer(0xFF);
  incomingArray [1] = SPI.transfer(0xFF);
  incomingArray [2] = SPI.transfer(0xFF);
  incomingArray [3] = SPI.transfer(0xFF);
  digitalWrite(CS, HIGH);
  
  Serial.print(incomingArray [0]);

}