#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);
}
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.As far as I know, the SPI drivers don't use SPI's built-in chip selects.
I tried pin 0 as well, didn't work.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.
Oh indeed, unfortunately that makes no difference. Also: I'm toggling the CS pin by hand using digitalWrite.Your code has no setCS() call.
No that's now that I mean: the output of the program is the same: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).
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
Is it because you're sending "1" instead of the actual command byte?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);
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;
}
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;
}
transfer(1); uint16_t value = transfer16(cmd<<13) & 0xFFF;Just a suggestion, but it's easy to find working libraries for common ADCs and DACs. For example, if you google "github Arduino MCP3202", you'll find the one below, which looks very simple and probably works....Works!