SPI1 & CS on 0 (pin 2)

flok

Well-known member
Hi,

Question: am I right that I can't use 0_RX1_CRX2_CS1 for chipselect for the 2nd SPI bus? (SPI1 in Arduino)

If that is true, can I just connect solder a wire from pin 2 to pin 29? (= 37_CS) or do I really need to disconnect the CS from 0_RX1_CRX2_CS1?


regards
 
Just to be sure I removed the 0_RX1_CRX2_CS1 pin and soldered CS from the MCP3202 to pin 37 of the T4.1 - no success.

So:
* MISO: 1
* MOSI: 26
* SCK: 27
* CS: 37

900 kHz clock, MSB first, spi_mode0

C++:
#include <SPI.h>

const int CS_PIN = 37;

void setup() {
  Serial.begin(115200);

  pinMode(CS_PIN, OUTPUT);
  digitalWrite(CS_PIN, HIGH);

  SPI1.setMISO(1);
  SPI1.setMOSI(26);
  SPI1.setSCK(27);
  SPI1.begin();
}

uint16_t readMCP3202(uint8_t channel) {
  byte command = channel == 0 ? 0b11010000 : 0b11110000;

  SPISettings settings(900'000, MSBFIRST, SPI_MODE0);

  SPI1.beginTransaction(settings);
  digitalWrite(CS_PIN, LOW);
  SPI1.transfer(1);
  byte highByte = SPI1.transfer(0x20);
  byte lowByte  = SPI1.transfer(0x00);
  digitalWrite(CS_PIN, HIGH);
  SPI1.endTransaction();

  return ((highByte & 0x0F) << 8) | lowByte;
}

void loop() {
  uint16_t valCH0 = readMCP3202(0);
  uint16_t valCH1 = readMCP3202(1);

  Serial.print("CH0: ");
  Serial.print(valCH0);
  Serial.print(" | CH1: ");
  Serial.println(valCH1);

  delay(500);
}
 
Last edited:
As far as I know, the SPI drivers don't use SPI's built-in chip selects. You can assign any pin that is configurable as a digital output. Probably a good idea to show a simple test program.
 
As far as I know, the SPI drivers don't use SPI's built-in chip selects.
They can with T4.1 by explicitly calling setCS() to assign one of the available hardware CS pins. Not really sure what OP is attempting to do though, the only hardware CS pin available for SPI1 is pin 0.
 
They can with T4.1 by explicitly calling setCS() to assign one of the available hardware CS pins. Not really sure what OP is attempting to do though, the only hardware CS pin available for SPI1 is pin 0.
I tried pin 0 as well, didn't work.
 
Your code has no setCS() call. And if you want CS to be hardware controlled, you shouldn't be using digitalWrite to manage it.
 
If you are toggling a pin with digitalWrite and it doesn't physically change state, it's either already blown or about to be (shorted to GND or VCC).
 
If you are toggling a pin with digitalWrite and it doesn't physically change state, it's either already blown or about to be (shorted to GND or VCC).
No that's now that I mean: the output of the program is the same:

Code:
CH0: 288 | CH1: 1
CH0: 0 | CH1: 0
CH0: 288 | CH1: 0
CH0: 257 | CH1: 0
CH0: 1 | CH1: 0
CH0: 0 | CH1: 257
CH0: 0 | CH1: 1
CH0: 0 | CH1: 0
CH0: 257 | CH1: 0
CH0: 1 | CH1: 0
CH0: 0 | CH1: 288
CH0: 0 | CH1: 257
CH0: 0 | CH1: 0
CH0: 288 | CH1: 1
CH0: 0 | CH1: 256
CH0: 257 | CH1: 1
CH0: 0 | CH1: 256
CH0: 257 | CH1: 1
CH0: 0 | CH1: 256
CH0: 257 | CH1: 1
CH0: 0 | CH1: 256
CH0: 257 | CH1: 1
CH0: 257 | CH1: 1
CH0: 257 | CH1: 1
CH0: 257 | CH1: 1
CH0: 0 | CH1: 256
CH0: 257 | CH1: 1

So a few strange values on both channels (while only CH0 is connected).
 
C++:
uint16_t readMCP3202(uint8_t channel) {
  byte command = channel == 0 ? 0b11010000 : 0b11110000;

  SPISettings settings(900'000, MSBFIRST, SPI_MODE0);

  SPI1.beginTransaction(settings);
  digitalWrite(CS_PIN, LOW);
  SPI1.transfer(1);
  byte highByte = SPI1.transfer(0x20);
  byte lowByte  = SPI1.transfer(0x00);
Is it because you're sending "1" instead of the actual command byte?
 
I tried a few variations :)

C++:
uint16_t readMCP3202(uint8_t channel) {
  byte command = channel == 0 ? 0b11010000 : 0b11110000;

  SPI1.beginTransaction(SPISettings(900'000, MSBFIRST, SPI_MODE0));
  digitalWrite(CS_PIN, LOW);
  SPI1.transfer(command);
  byte highByte = SPI1.transfer(0x00);
  byte lowByte = SPI1.transfer(0x00);
  digitalWrite(CS_PIN, HIGH);
  SPI1.endTransaction();

  return ((highByte & 0x0F) << 8) | lowByte;
}

...is what looks most like the datasheet says, producing garbage.

If I do this on the other hand, I constantly get the same value. No sine data, but no random garbage either.

C++:
uint16_t readMCP3202(uint8_t channel) {
  byte command = channel == 0 ? 0b11010000 : 0b11110000;

  SPI1.beginTransaction(SPISettings(900'000, MSBFIRST, SPI_MODE0));
  digitalWrite(CS_PIN, LOW);
  SPI1.transfer(command);
  uint16_t value = SPI1.transfer16(0x00);
  digitalWrite(CS_PIN, HIGH);
  SPI1.endTransaction();

 return value;
}
 
According to the datasheet in chapter 6.1, the first byte sent should be "1" with 3 remaining command bits sent in the top of the second byte.
Data is returned in bytes 2 and 3.
Screenshot 2026-11-04 074657.png


You could do this using transfer(1); uint16_t value = transfer16(cmd<<13) & 0xFFF;
 
Last edited:
So:

C++:
  SPI1.transfer(command >> 7);  // always 1
  byte highByte = SPI1.transfer(command << 1);
  byte lowByte = SPI1.transfer(0x00);

Works!
That, and replacing the ADC by an other.

Thanks everybody!
 
Back
Top