cancel
Showing results for 
Search instead for 
Did you mean: 

Communication with external ADC by SPI

YShe.1
Associate II

Hello I ask to help check if all right with my code, this is my first SPI...

I have use ADC1247 and seems it alive and responce for me by some of data, but ADC is sending something like "I see some of load" or "I dont see nothing". This is for 4 wire PT100 temperature sensor. Will be good to be shure that's code is right and then will check again shematic... Thanks for any of help...

#include "main.h"
#include "settings.h"
#include "string.h"
 
extern SPI_HandleTypeDef hspi1;
 
const uint8_t READ_CMD = 0x01;
const uint8_t BUFFER_SIZE = 3;
 
uint32_t ads_read_data() {
    static uint32_t data = 0;
    uint8_t buffer[BUFFER_SIZE];
	
    // Reset the pin stage
    HAL_GPIO_WritePin(GPIOB, START_Pin, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(RESET_GPIO_Port, RESET_Pin, GPIO_PIN_RESET);
 
    // Enable the device by setting the START pin high
    HAL_GPIO_WritePin(GPIOB, START_Pin, GPIO_PIN_SET);
 
    // Reset the device
    HAL_GPIO_WritePin(RESET_GPIO_Port, RESET_Pin, GPIO_PIN_SET);
 
    HAL_Delay(16);
 
    // Send read command to ADS1247
    HAL_SPI_Transmit(&hspi1, &READ_CMD, sizeof(READ_CMD), HAL_MAX_DELAY);
 
    HAL_Delay(16);
 
    // Wait for DRDY pin to go low to indicate data is ready to be read
    uint32_t wait_count = 0;
    while (HAL_GPIO_ReadPin(SPI1_DRDY_PB0_GPIO_Port, SPI1_DRDY_PB0_Pin) == GPIO_PIN_SET) {
        wait_count++;
        if (wait_count > 1000) { // If the device is not ready after 10 second, exit the function
            return 0;
        }
        HAL_Delay(10);
    }
 
    HAL_Delay(10);
 
    // Read data from ADS1247
    HAL_SPI_Receive(&hspi1, buffer, BUFFER_SIZE, 10); // Read 3 bytes (24 bits) of data with a timeout of 10ms
 
    // Copy 3 bytes of data into a 32-bit variable using memcpy()
    memcpy(&data, buffer, sizeof(data));
 
    return data;
}
 
void PT100(){
	if (Holding_Registers_Database[5] == 1) {
		// Read data from ADS1247
		uint32_t data = ads_read_data();
 
		//Mathematics for PT100 using the data
		float voltage = data / 3.3;
		float resistance = (voltage * 1000 - 500) / 10;
		float temperature = (resistance - 100) / 0.385;
 
		// Store the temperature in the input registers database
		Input_Registers_Database[0] = (int16_t)resistance;
	}
}

SPI configuration:

static void MX_SPI1_Init(void)
{
 
  /* USER CODE BEGIN SPI1_Init 0 */
 
  /* USER CODE END SPI1_Init 0 */
 
  /* USER CODE BEGIN SPI1_Init 1 */
	// Declare hspi1 as a global variable
 
  /* USER CODE END SPI1_Init 1 */
  /* SPI1 parameter configuration*/
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 7;
  hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI1_Init 2 */
 
  /* USER CODE END SPI1_Init 2 */
 
}

5 REPLIES 5
AScha.3
Chief II

R14 ... seem useless in this position, except wasting some current.

If you feel a post has answered your question, please click "Accept as Solution".
YShe.1
Associate II

The value of resistor is 820 ohms as requed by data sheet. And schematic also from it. I trying to figure out what can be wrong.

AScha.3
Chief II

should be this ...?

0693W00000bhQMqQAM.png

If you feel a post has answered your question, please click "Accept as Solution".
YShe.1
Associate II

Yes this is 3 wire connection of RTD PT100. Any way it's not working even in 3 wire mode.

I have changed Rref from 820 ohms to 4.7 kohm and value unpredictable and jumping...

YShe.1
Associate II

Well this code does not help, but litle bit changed the scale:

#include "main.h"
#include "settings.h"
#include "string.h"
 
// SPI handler
extern SPI_HandleTypeDef hspi1;
 
// Constants for ADS1247 configuration
const uint8_t ADS1247_MUX1_ADDR = 0x02;
const uint8_t ADS1247_IDAC0_ADDR = 0x0A;
const uint8_t ADS1247_IDAC1_ADDR = 0x0B;
const uint8_t ADS1247_READ_CMD = 0x01;
 
const uint8_t ADS1247_MUX1_VALUE = 0x01;
const uint8_t ADS1247_REFSELT_VALUE = 0x00;
const uint8_t ADS1247_IDAC0_IMAG_VALUE = 0x06;
const uint8_t ADS1247_IDAC1_I1DIR_VALUE = 0x00;
const uint8_t ADS1247_IDAC1_I2DIR_VALUE = 0x03;
 
// Function to read data from ADS1247
uint32_t ads_read_data() {
    static uint32_t data = 0;
    uint8_t buffer[3];
    uint32_t wait_count = 0;
 
    // Reset the pin stage
    HAL_GPIO_WritePin(GPIOB, START_Pin, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(RESET_GPIO_Port, RESET_Pin, GPIO_PIN_RESET);
 
    // Enable the device by setting the START pin high
    HAL_GPIO_WritePin(GPIOB, START_Pin, GPIO_PIN_SET);
    // Reset the device
    HAL_GPIO_WritePin(RESET_GPIO_Port, RESET_Pin, GPIO_PIN_SET);
		HAL_Delay(16);
 
    // Configure MUX1 register
    uint8_t mux1_config[3] = {ADS1247_MUX1_ADDR, (ADS1247_MUX1_VALUE << 4) | ADS1247_REFSELT_VALUE, 0x00};
    HAL_SPI_Transmit(&hspi1, mux1_config, sizeof(mux1_config), HAL_MAX_DELAY);
 
    // Configure IDAC0 register
    uint8_t idac0_config[3] = {ADS1247_IDAC0_ADDR, ADS1247_IDAC0_IMAG_VALUE, 0x00};
    HAL_SPI_Transmit(&hspi1, idac0_config, sizeof(idac0_config), HAL_MAX_DELAY);
 
    // Configure IDAC1 register
    uint8_t idac1_config[3] = {ADS1247_IDAC1_ADDR, (ADS1247_IDAC1_I1DIR_VALUE << 4) | ADS1247_IDAC1_I2DIR_VALUE, 0x00};
    HAL_SPI_Transmit(&hspi1, idac1_config, sizeof(idac1_config), HAL_MAX_DELAY);
		
    // Send read command to ADS1247
    HAL_SPI_Transmit(&hspi1, (uint8_t*)&ADS1247_READ_CMD, sizeof(ADS1247_READ_CMD), HAL_MAX_DELAY);
    HAL_Delay(16);
 
    // Wait for DRDY pin to go low to indicate data is ready to be read
    while (HAL_GPIO_ReadPin(SPI1_DRDY_PB0_GPIO_Port, SPI1_DRDY_PB0_Pin) == GPIO_PIN_SET) {
        wait_count++;
        if (wait_count > 1000) { // If the device is not ready after 10 second, exit the function
            return 0;
        }
        HAL_Delay(10);
    }
 
    // Read data from ADS1247
    HAL_SPI_Receive(&hspi1, buffer, sizeof(buffer), 10); // Read 3 bytes (24 bits) of data with a timeout of 10ms
 
    // Copy 3 bytes of data into a 32-bit variable using memcpy()
    memcpy(&data, buffer, sizeof(data));
 
    return data;
}