I come seeking expert knowledge :)
I have a mega connected to two SPI devices. One is a MCP2516 CAN transceiver and the other is a digital potentiometer https://digilent.com/reference/pmod/pmoddpot/start?redirect=1
I seem to have trouble getting the potentiometer to work though. The MCP2516 is connected to dedicated SPI ports and the CS is a digital pin - 12
I then have my potentiometer connected to the ICSP header. the COPI and SCK lines,
I initialise the can bus using the CAN library like so:
// Setup CAN bus interface
CAN.setPins(MCP2515_CS, MCP2515_INT);
CAN.setClockFrequency(MCP2515_FCLK);
// Initialize CAN bus
while (true) {
Serial.print("Initializing CAN bus interface ... ");
if (CAN.begin(CAN_BITRATE)) {
Serial.println("OK");
break;
} else {
Serial.println("FAIL");
delay(1000);
}
//Set up the pot
pinMode(POT_CS_PIN, OUTPUT);
pinMode(POT_CS_PIN, HIGH);
SPI.begin()
Then for the potentiometer I call a function when I need to setup an initial value:
void setDigitalPotentiometer(int level) {
int potValue = constrain(level, 0, 255);
// Set the potentiometer value via SPI
SPISettings potSPISettings(1000000, MSBFIRST, SPI_MODE0);
digitalWrite(POT_CS_PIN, LOW);
SPI.beginTransaction(potSPISettings);
delayMicroseconds(15);
SPI.transfer(level);
delayMicroseconds(15);
digitalWrite(POT_CS_PIN, HIGH);
SPI.endTransaction();
}
I've taken the step to disable the CAN CS pin to avoid collisions, but try as I may no luck. Measuring between the A and W terminals the resistance stays near 0 as if the wiper is just stuck.
Any guidance? Anything obviously wrong?
SPI.transfer(level)
sent ONE byte of data, if yourlevel
is aint
, it will be truncated. To send anint
, useSPI.transfer16(level)
. If this is not the problem, then please provide more complete setup code, including the pin assignment ofMCP2515_CS
, and connection diagram.