Help on my very first use of spi without other libraries helping me

Hello this will be my very first time creating a SPI code from scratch, my only experience with SPI is with a library helping me and making it simpler. The chip is TPL0202 digital Potentiometer

I need help building the code wherein i can send controll the digital potentiometer fully.

Here is my proposed code. I am using the Arduino due, i did not run the code yet just to make sure i dont damage anything. Especially this chip has non volatile memory i might immediately burn out its lifespan quickly.

#include <SPI.h>

const int slaveSelectPin = 71;

void setup() {

  pinMode(slaveSelectPin, OUTPUT);
  SPI.begin();
  digitalWrite(slaveSelectPin, HIGH);
  
  SPI.beginTransaction(SPISettings(5000000, MSBFIRST, SPI_MODE0));
  digitalWrite(slaveSelectPin, LOW);
  SPI.transfer16(0b0000001110000000);
  SPI.endTransaction();
  digitalWrite(slaveSelectPin, HIGH);

  SPI.beginTransaction(SPISettings(5000000, MSBFIRST, SPI_MODE0));
  digitalWrite(slaveSelectPin, LOW);
  SPI.transfer16(0b0010001100000000);
  SPI.endTransaction();
  digitalWrite(slaveSelectPin, HIGH);
}

void loop() {


}

According to arduino’s documentation on SPISettings the first parameter is the maximum allowable speed of my slave device which is 5MHz in this case. As to what type of Bit First my slave device use i do not know where to find it in the datasheet. And so does the data mode i do not know which one to use.

after beginTransaction i activate the chip by setting the SS to low, i proceed then to transfer the 16bit code according the page 22 of the datasheet. 0b0000001110000000 should set the Wiper A and B to to the midpoint

The second transaction 0b0010001100000000 would be saving the value to the non volatile memory

I am using the Arduino due, i did not run the code yet just to make sure i dont damage anything. Especially this chip has non volatile memory i might immediately burn out its lifespan quickly.

To damage a chip using code is much harder than by using a wrong wiring. You failed to provide a wiring diagram. As you have no code in the loop() routine, that bit is run just once after you startup the Due. So you try it safely, the code will not damage your hardware (a wrong wiring might do that immediately).

According to arduino's documentation on SPISettings the first parameter is the maximum allowable speed of my slave device which is 5MHz in this case.

Wrong, the first parameter is the actual speed of the communication. This value must be less or equal to the maximum allowed speed of your slave. Usually you should not use the maximum if you're not forced to.

And so does the data mode i do not know which one to use.

Page 21 tells you which mode to use. Your chip has SCKL idle high (CPOL=1) and data is valid on the rising edge (CPHA=1), so mode 3 is used.

pylon:
To damage a chip using code is much harder than by using a wrong wiring. You failed to provide a wiring diagram. As you have no code in the loop() routine, that bit is run just once after you startup the Due. So you try it safely, the code will not damage your hardware (a wrong wiring might do that immediately).

im pretty confident the wiring is okay since im using proteus which throws an error if the pcb does not match the schematic, if i still managed to mess it up i do not have much a choice since the traces on the board are permanent. I have to order a new one.

pylon:
Wrong, the first parameter is the actual speed of the communication. This value must be less or equal to the maximum allowed speed of your slave. Usually you should not use the maximum if you’re not forced to.

Alright so ill slow it down. Also are th value i use in the parameter actually the speed of the SPI, from what i read the speed is based on the a fraction of the mCU’s clock. So does it automatically round down to the nearest speed?

pylon:
Page 21 tells you which mode to use. Your chip has SCKL idle high (CPOL=1) and data is valid on the rising edge (CPHA=1), so mode 3 is used.

So if digram shows that SCKL is idle at high CPOL = 1 and if SCKL is idle at low CPOL = 0. By valid you mean that a bit on a DIN is captured at the rising edge CPHA=1 and if its captured at falling its CPHA =0

As for the code does it look alright? I do have other SPI devices conneected but they have libraries hopefully they wont be much trouble when i get to them

im pretty confident the wiring is okay since im using proteus which throws an error if the pcb does not match the schematic, if i still managed to mess it up i do not have much a choice since the traces on the board are permanent. I have to order a new one.

If you have schematics why don't you post it?

Also are th value i use in the parameter actually the speed of the SPI, from what i read the speed is based on the a fraction of the mCU's clock. So does it automatically round down to the nearest speed?

Correct.

So if digram shows that SCKL is idle at high CPOL = 1 and if SCKL is idle at low CPOL = 0. By valid you mean that a bit on a DIN is captured at the rising edge CPHA=1 and if its captured at falling its CPHA =0

Correct.

As for the code does it look alright?

I already mentioned what you have to change. In the posted version the code is not correct.

I do have other SPI devices conneected but they have libraries hopefully they wont be much trouble when i get to them

If these devices are already connected to the bus, your code won't work. That's why we always ask for a wiring diagram (schematics are even better).

pylon:
If you have schematics why don't you post it?

The schematics is very big if i take a snapshot it would make the part number and value unreadable. but I have isolated all the spi devices in this image (image — ImgBB)

pylon:
If these devices are already connected to the bus, your code won't work. That's why we always ask for a wiring diagram (schematics are even better).

And i just recently posted a question about the spi not working, it aligns to what you said. Can you elaborate why it will not work?

The schematics is very big if i take a snapshot it would make the part number and value unreadable. but I have isolated all the spi devices in this image

Post images to the forum, don’t use temporary web storage!

And i just recently posted a question about the spi not working, it aligns to what you said. Can you elaborate why it will not work?

Because the devices get active if CS is not pulled HIGH.