I am using SPI for the first time in a real project and I am experiencing a few issues.
- Teensy 3.2
- ADC: MCP33121-10
https://www.mouser.de/datasheet/2/268/microchip_technology_mchp-s-a0006919354-1-1749951.pdf
Project: The goal is to convert the voltage (set by potentiometer), to learn how the ADC is working (with a bigger project in mind.).
Problem: I only read 2^16 = 65535 (this should be.. 1111 1111 1111 1111) from the serial interface, despite changing (&checking with my multimeter) the analog input voltage Ain+. Below, you also find a Screenshot from my Scope, but I cant really learn whats going wrong from it. The datasheet states that 1111 .....1111 is the "error" code indicating a voltage above VRef, or VRef itself, still it occurs whichever voltage I apply on the input pin. I am quite generous with the timing, so this shouldn´t be a problem of the ADC lacking time to convert the value and present it on the SPI.
Funny thing is that I tried to see what happens, when you don´t connect DIN on the Teensy. Still, I get 2^16, no matter what. Thanks a lot guys! I am ready to learn, so please feel free to mention everything you notice.
I already tried lowering the SPI CLK, as the setup is currently on breakout/bread boards and flimsy jumper wires.
The decoded value on the scope is very unstable and jumping around all the time. The teensy serial monitor stays rock solid at 2^16, no matter what. (CLK: yellow, DIN: blue, CS: pink)
- Teensy 3.2
- ADC: MCP33121-10
https://www.mouser.de/datasheet/2/268/microchip_technology_mchp-s-a0006919354-1-1749951.pdf
Project: The goal is to convert the voltage (set by potentiometer), to learn how the ADC is working (with a bigger project in mind.).
Problem: I only read 2^16 = 65535 (this should be.. 1111 1111 1111 1111) from the serial interface, despite changing (&checking with my multimeter) the analog input voltage Ain+. Below, you also find a Screenshot from my Scope, but I cant really learn whats going wrong from it. The datasheet states that 1111 .....1111 is the "error" code indicating a voltage above VRef, or VRef itself, still it occurs whichever voltage I apply on the input pin. I am quite generous with the timing, so this shouldn´t be a problem of the ADC lacking time to convert the value and present it on the SPI.
Funny thing is that I tried to see what happens, when you don´t connect DIN on the Teensy. Still, I get 2^16, no matter what. Thanks a lot guys! I am ready to learn, so please feel free to mention everything you notice.
I already tried lowering the SPI CLK, as the setup is currently on breakout/bread boards and flimsy jumper wires.
The decoded value on the scope is very unstable and jumping around all the time. The teensy serial monitor stays rock solid at 2^16, no matter what. (CLK: yellow, DIN: blue, CS: pink)
Code:
#include <SPI.h>
#define CS 10
#define CNVST 4
#define BAUDRATE 115200
const uint32_t SPI_CLK = 1E6;
uint16_t MSB;
uint16_t LSB;
uint16_t iVal;
SPISettings settingsA(SPI_CLK, MSBFIRST, SPI_MODE1);
void Read(){
SPI.beginTransaction(settingsA);
digitalWriteFast(CS, LOW); //changed to normal digitalWrite
MSB = SPI.transfer(0); //send 0XFF = 255, so rx only
LSB = SPI.transfer(0); //send 0XFF = 255, so rx only
digitalWriteFast(CS, HIGH); //changed to normal digitalWrite
SPI.endTransaction();
iVal = (((uint16_t)MSB << 8) | LSB);
//iVal = iVal >> 2; //to obtain 14 bit data
}
void setup(){
pinMode(CS, OUTPUT);
pinMode(CNVST, OUTPUT);
digitalWriteFast(CS, HIGH);
digitalWriteFast(CNVST, LOW);
Serial.begin(BAUDRATE);
SPI.begin();
}
void loop(){
delayMicroseconds(4);
digitalWriteFast(CNVST, HIGH);
delayMicroseconds(4);
digitalWriteFast(CNVST, LOW);
Read();
//Serial.println(MSB);
//Serial.println(LSB);
Serial.println(iVal);
iVal = 0;
}