RAK3172 SPI communication

HI,guys,I am using RAK3172 to control MCP3913 for data acquisition, using SPI for communication, but the information I receive using SPI.transfer() function is always FFFFFF (receiving 24-bit data), and I am using this function to read other data with the same problem as mentioned above, I would like to ask if it’s a problem with my SPI settings?

#include "Arduino.h"

#include <SPI.h>

int val_1 = 0;

uint8_t test_0 ;

uint8_t test_1 ;

uint8_t test_2 ;

uint8_t test_3 ;

void setup() {

  // put your setup code here, to run once:

  Serial.begin(115200);

  SPI.begin();

  pinMode(PB2, INPUT);

  pinMode(PB12, OUTPUT);

  pinMode(PA4, OUTPUT);

  digitalWrite(PA4, HIGH);


  MCP3913_Init();
}

void loop() {

  // put your main code here, to run repeatedly:

    //if(i == 0){

        SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));

    //发送读取命令

        digitalWrite(PA4, LOW);

        delayMicroseconds(10);

        SPI.transfer(0x01);  // 读寄存器命令

        uint8_t receivedVal_0H = SPI.transfer(0x00);  

        uint8_t receivedVal_0M = SPI.transfer(0x00);  

        uint8_t receivedVal_0L = SPI.transfer(0x00);  // 

        uint8_t receivedVal_1H = SPI.transfer(0x00);  //

        uint8_t receivedVal_1M = SPI.transfer(0x00);  // 

        uint8_t receivedVal_1L = SPI.transfer(0x00);  // 

        uint8_t receivedVal_2H = SPI.transfer(0x00);  //

        uint8_t receivedVal_2M = SPI.transfer(0x00);  // 

        uint8_t receivedVal_2L = SPI.transfer(0x00);  // 

        uint8_t receivedVal_3H = SPI.transfer(0x00);  //

        uint8_t receivedVal_3M = SPI.transfer(0x00);  // 

        uint8_t receivedVal_3L = SPI.transfer(0x00);  // 

        uint8_t receivedVal_4H = SPI.transfer(0x00);  //

        uint8_t receivedVal_4M = SPI.transfer(0x00);  // 

        uint8_t receivedVal_4L = SPI.transfer(0x00);  //

        uint8_t receivedVal_5H = SPI.transfer(0x00);  //

        uint8_t receivedVal_5M = SPI.transfer(0x00);  // 

        uint8_t receivedVal_5L = SPI.transfer(0x00);  // 
       

        uint32_t adcData0 = (receivedVal_0H << 16) | (receivedVal_0M << 8) | receivedVal_0L;

        uint32_t adcData1 = (receivedVal_1H << 16) | (receivedVal_1M << 8) | receivedVal_1L;

        uint32_t adcData2 = (receivedVal_2H << 16) | (receivedVal_2M << 8) | receivedVal_2L;

        uint32_t adcData3 = (receivedVal_3H << 16) | (receivedVal_3M << 8) | receivedVal_3L;

        uint32_t adcData4 = (receivedVal_4H << 16) | (receivedVal_4M << 8) | receivedVal_4L;

        uint32_t adcData5 = (receivedVal_5H << 16) | (receivedVal_5M << 8) | receivedVal_5L;

        Serial.print("ADC ");

        Serial.print(0);

        Serial.print(": ");

        Serial.println(adcData0);

        Serial.print("ADC ");

        Serial.print(1);

        Serial.print(": ");

        Serial.println(adcData1);

        Serial.print("ADC ");

        Serial.print(2);

        Serial.print(": ");

        Serial.println(adcData2);

        Serial.print("ADC ");

        Serial.print(3);

        Serial.print(": ");

        Serial.println(adcData3);

        Serial.print("ADC ");

        Serial.print(4);

        Serial.print(": ");

        Serial.println(adcData4);

        Serial.print("ADC ");

        Serial.print(5);

        Serial.print(": ");

        Serial.println(adcData5);

        digitalWrite(PA4, HIGH);

        // Serial.println(test_0);

        // Serial.println(test_1);

        // Serial.println(test_2);

        // Serial.println(test_3);
}

void MCP3913_Init() {

  // digitalWrite(PB12, LOW);

  // delay(1);

  // digitalWrite(PB12, HIGH);

  SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));

  digitalWrite(PA4, LOW);

  delayMicroseconds(10);

  SPI.transfer(0x14);// 

  delayMicroseconds(10);

  SPI.transfer(0xC9);

  SPI.transfer(0x00);

  SPI.transfer(0x00);

  digitalWrite(PA4, HIGH);

  delay(1);

  digitalWrite(PA4, LOW);

  delayMicroseconds(10);

  SPI.transfer(0x1A);

  delayMicroseconds(10);

  SPI.transfer(0x38);

  SPI.transfer(0x60);

  SPI.transfer(0x50);  // 

  digitalWrite(PA4, HIGH);

  delay(1);

  digitalWrite(PA4, LOW);

  delayMicroseconds(10);

  SPI.transfer(0x1E);

  delayMicroseconds(10);

  SPI.transfer(0x00);

  SPI.transfer(0x00);

  SPI.transfer(0x00);  // 

  digitalWrite(PA4, HIGH);

  SPI.endTransaction();
}

Difficult to pin down the problem, I never heard of the MCP3913.

Is PA4 connected as SPI CS signal? Did you try to NOT set PA4 by your code. I am not 100% sure, but I think SPI CS (NSS) is controlled by the SPI library of RUI3 and you do not need to care.

If my assumption is not true, try to increase the delay between setting PA4 and starting the SPI transfer.

And last not least, does the MCP3913 support the SPI frequency of 1MHz?
Is the bit order (MSBFIRST) matching with the MCP3913?
Is SPI_MODE0 the correct mode for the MCP3913?

1 Like