send 32 bits using SPI

Hello there,

on my project I am going to control an Analog Switch chip HV2081 (here the datasheet) using SPI protocol
Attached the truth table of the chip.

What I would to like first is just to switch the switch0

It needs a 32 bit input, so I am going to split it in 4 bytes.

I wrote this code but it doesn’t work:

#include <SPI.h>
const int slaveSelectPin = 53;
int flag = 0;


void setup() {
  pinMode(slaveSelectPin, OUTPUT);
  SPI.begin();
  
}

void loop() {
  
if (flag ==0){1
      
      digitalPotWrite(1);
      flag =1;
      delay(1);
      }
      else 
      {
      digitalPotWrite(0);
      flag =0;
      delay(1);}
     
     
   
}
 

void digitalPotWrite(long int value) {
  
  digitalWrite(slaveSelectPin, LOW);  // take the SS pin low to select the chip:

  
  
  byte byte0 = (value & 0xFF); //0xFF = B11111111
  byte byte1 = (value >> 8);
byte byte2 = (value >> 16);
  byte byte3 = (value >> 24);
  
  SPI.transfer(byte3);
  SPI.transfer(byte2);
  SPI.transfer(byte1);
  SPI.transfer(byte0);
  
  // take the SS pin high to de-select the chip:
  digitalWrite(slaveSelectPin, HIGH);
}

Any suggestions on how to figure out this issue is appreciated.
Thanks.

A link to the datasheet might help. Are you certain you are using the correct SPI mode and bit order?

Thanks for reply.

I have added the link of datasheet in the first post.

In the datasheet is not very clear how to set up the mode and the bit order.

The clock is HIGH with LOW pulses and captures on the rising edge (LOW to HIGH transition). That would be SPI mode 3. The default mode is 0. The bit order appears to be MSBFIRST (default).

SPI.setDataMode(SPI_MODE3);

Are you using the LE pin in your code?

the LE pin is the 53 in my code (slaveSelectPin) and I turn down the level of it before send data.

Are you using a Mega?

Try SPI mode 3. I don’t have that device to test for you.

I am using Arduino Mega.

Can you please have a look to the code. I am not sure if that I wrote is correct.

I don't know about the code. The datasheet isn't as clear as I would like.

The "slave select" is really a latch enable.

I would need to play with that IC to determine how it works. You might have to also.

You are right: the datasheet is very bare to explain how to set up the SPI connection.

I got one chip on my desk and I am trying to do the easier step: use just one switch.

The challenge is: when does the device start clocking in the data bits? Normally this is determined by the slave select. The slave select is taken LOW. That signals the IC to start the data clocking. Then the slave select is taken HIGH, and that stops the data clocking.

The IC has no slave select, and the datasheet is not clear what starts or stops the data clocking cycle.

I think that LE negate is the slave select.

From datasheet:
All 32 switches go to a state retaining their latched condition at the rising edge of LE. When LE is low the shift registers data flow through the latch.
Shift registers clocking has no effect on the switch states if LE is high.

That is not what the datasheet shows. Take a look at page 7 of the datasheet. It shows the LE going LOW and HIGH twice, but no reset of the data bit count. Still goes to bit n-1 on the next clock.

Hi there,

I have figured out how to communicate between Arduino Mega and a 32 bit chip (HV2801) using SPI protocol.

This is the code (generation square wave)that I have used:

#include <SPI.h>
const int csPin = 53;

void setup() {
  SPI.begin();
  SPI.setBitOrder(MSBFIRST); //MSBFIRST or LSBFIRST 
  SPI.setDataMode(SPI_MODE3);
}

void loop() {
  
  digitalWrite(csPin, LOW); //select slave
  SPI.transfer(0x00); //first 8 outputs OFF
  SPI.transfer(0x00); //second 8 outputs OFF
  SPI.transfer(0x00); //third 8 outputs OFF
  SPI.transfer(0x01); //fourth 8 outputs OFF
  digitalWrite(csPin, HIGH); //de-select slave
  delayMicroseconds(200);

  digitalWrite(csPin, LOW); //select slave
  SPI.transfer(0x00); //first 8 outputs OFF
  SPI.transfer(0x00); //second 8 outputs OFF
  SPI.transfer(0x00); //third 8 outputs OFF
  SPI.transfer(0x00); //fourth 8 outputs OFF
  digitalWrite(csPin, HIGH); //de-select slave
  delayMicroseconds(200);
}

Cheers,
Massimo