chrispytoes
Member
I am trying to get two MCP3208 chips working on the same SPI bus, but my program crashes when attempting to read data via the SPI class. I am using this library: https://github.com/labfruits/mcp320x.
I have two MCP3208 chips. They are both connected to the SPI1 interface as such:
Here is my code:
I did some quick and dirty debugging with the LED and found that the code stops at the line I marked with a comment. Inside the adc1.read(chan) call I can see that the code stops on this line in the mcp3208 library.
I'm not sure where to go from here or how I can gather more info about what's happening. From all my research I think I'm doing this right.
I have two MCP3208 chips. They are both connected to the SPI1 interface as such:
Code:
Teensy MCP3208(both chips)
MOSO1(26) -> Data Out(12)
MISI1(39) -> Data In(11)
SCK1(27) -> Clock(13)
(41 and 40 are arbitrary pins I chose to use for device selection)
Teensy MCP3208(chip 1)
CS1(41) -> CS(10)
Teensy MCP3208(chip 2)
CS2(40) -> CS(10)
Here is my code:
Code:
#include <SPI.h>
#include <Arduino.h>
#include "../lib/mcp320x/Mcp320x.h"
#define CS1 41
#define CS2 40
#define CLK 1600000
#define ADC_VREF 3300
MCP3208 adc1(ADC_VREF, CS1);
MCP3208 adc2(ADC_VREF, CS2);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(CS1, OUTPUT);
pinMode(CS2, OUTPUT);
digitalWrite(CS1, HIGH);
digitalWrite(CS2, HIGH);
Serial.begin(9600);
SPISettings settings(CLK, MSBFIRST, SPI_MODE0);
SPI1.begin();
SPI1.beginTransaction(settings);
}
uint16_t readAdc(byte csPin, byte channel) {
MCP3208::Channel chan;
switch (channel) {
case 0:
chan = MCP3208::Channel::SINGLE_0;
break;
case 1:
chan = MCP3208::Channel::SINGLE_1;
break;
case 2:
chan = MCP3208::Channel::SINGLE_2;
break;
case 3:
chan = MCP3208::Channel::SINGLE_3;
break;
case 4:
chan = MCP3208::Channel::SINGLE_4;
break;
case 5:
chan = MCP3208::Channel::SINGLE_5;
break;
case 6:
chan = MCP3208::Channel::SINGLE_6;
break;
case 7:
chan = MCP3208::Channel::SINGLE_7;
break;
default:
chan = MCP3208::Channel::SINGLE_0;
}
if(csPin == 41) {
digitalWrite(CS1, LOW);
digitalWrite(CS2, HIGH);
uint16_t raw = adc1.read(chan); // Code stops on the first execution of this line
return adc1.toAnalog(raw);
}
else if(csPin == 40) {
digitalWrite(CS1, HIGH);
digitalWrite(CS2, LOW);
uint16_t raw = adc2.read(chan);
return adc2.toAnalog(raw);
}
}
void loop() {
int chip1Chan0 = readAdc(41, 0);
Serial.println("Chip 1 Channel 0: " + chip1Chan0);
int chip1Chan1 = readAdc(41, 1);
Serial.println("Chip 1 Channel 1: " + chip1Chan1);
int chip2Chan0 = readAdc(40, 0);
Serial.println("Chip 2 Channel 0: " + chip2Chan0);
int chip2Chan1 = readAdc(40, 1);
Serial.println("Chip 2 Channel 1: " + chip2Chan1);
}
I did some quick and dirty debugging with the LED and found that the code stops at the line I marked with a comment. Inside the adc1.read(chan) call I can see that the code stops on this line in the mcp3208 library.
I'm not sure where to go from here or how I can gather more info about what's happening. From all my research I think I'm doing this right.