Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read Temperature #2

Open
henriBusch opened this issue Mar 20, 2023 · 8 comments
Open

Read Temperature #2

henriBusch opened this issue Mar 20, 2023 · 8 comments

Comments

@henriBusch
Copy link

Hello, first of all, big fan of the library. I need a way to read the temperature sensor permanently. How does everything need to be set up to make this work? I have enabled the temperature sensor mod by setting

#define CELSIUS_PER_LSB 0.0078125f

ADS1220_Parameter_t Parameter = {0};
...
Parameter.TempeSensorMode = 1;
...
ADS1220_Init(&Handler, &Parameter);
...
int32_t ADC_Data;
ADS1220_ReadData(&Handler, &ADC_Data);

Then in the while(1) I set everything like this.

float temp = ADCValueToTemperature(ADC_Data, CELSIUS_PER_LSB);
printf("ADC Raw: 0x%lx | Temperature in C : %f\r\n", (unsigned long)ADC_Data, temp);
HAL_Delay(4000);

The hardware is on AIN0-AIN3 and REFN0 and REFP0 are not connected. Could you give me an example? I work with the Cube IDE from STM

Thank you,
Henri

@AliMoal
Copy link
Member

AliMoal commented Mar 23, 2023

Hi, did you fill parameters of Handler structure?
See in .h file:

typedef struct
ADS1220_Handler_s {
  void (*ADC_CS_HIGH)(void);                     // Must be initialized
  void (*ADC_CS_LOW)(void);                      // Must be initialized
  void (*ADC_Transmit)(uint8_t Data);            // Must be initialized
  uint8_t (*ADC_Receive)(void);                  // Must be initialized
  uint8_t (*ADC_TransmitReceive)(uint8_t Data);  // Can be initialized - Initialize this when you want to use ReadAllContinuous functions
  uint8_t (*ADC_DRDY_Read)(void);                // Can be initialized - Initialize this when you want to use ReadAllContinuous functions
  void (*ADC_Delay_US)(uint32_t);                // Must be initialized (Place here your delay in MicroSecond)
  ADS1220_OneSample_t ADCDataValues;              //!!! DO NOT USE OR EDIT THIS !!!
} ADS1220_Handler_t;

After that, as you mentioned, in while(1) you did not read the new data and the ADS1220_ReadData(&Handler, &ADC_Data); function is before while(1).
If you need more help, I'll make a real example.

@henriBusch
Copy link
Author

Hello,
thanks for the quick reply, I have now applied this. But somehow that still does not quite want. For example, I would be very grateful to you. In the appendix is everything I have so far. I know I still have to convert the data as in the sheet on page 31. I have also done the same in the function getTemperature(...). But I receive zero on the serial console, that should not be. The raw data is mostly 0x00 or 0xFF but nothing in between. I will load my code from the main in the attachment, maybe you can see my error quickly.

Greetings,
Henri

float getTemperature(int32_t rawData){

	uint16_t curData = (uint16_t)(rawData >>18);

	if(curData>>13){
		curData = ~(curData-1) & 0x3777;
		return curData *(-0.03125);
	}

	return curData * 0.03125;

}

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */
  Handler.ADC_CS_HIGH = CS_UP;
  Handler.ADC_CS_LOW = CS_DOWN;
  Handler.ADC_Transmit = TRANSMIT;
  Handler.ADC_Receive = RECEIVE;
  Handler.ADC_Delay_US = DELAY;


  Parameter.GainConfig = _1_;       // Set the gain to 1
  Parameter.DataRate = _20_SPS_;      // Set the data rate to 1000 SPS
  Parameter.OperatingMode = NormalMode;          // Set the ADC to normal mode
  Parameter.ConversionMode = 0;// Set the ADC to single-shot mode
  Parameter.VoltageRef = Internal;        // Set the ADC reference to internal
  Parameter.TempeSensorMode = 1;         // Enable temperature sensor mode
  Parameter.InputMuxConfig = P0NAVSS;
  Parameter.PGAdisable = 1;
  Parameter.BurnOutCurrentSrc = 0;
  Parameter.FIRFilter = No50or60Hz;
  Parameter.LowSidePwr = 0;
  Parameter.IDACcurrent = Off;
  Parameter.IDAC1routing = Disabled;
  Parameter.IDAC2routing = Disabled;
  Parameter.DRDYMode = 0;

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SPI1_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  // Passing Parameters as NULL to use default configurations.
  ADS1220_Init(&Handler, &Parameter);

  // Default conversion mode is Single-shot
  ADS1220_StartSync(&Handler);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  // GPIO_DRDY_GROUP and GPIO_DRDY_PIN depend on your schematic.
  while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == GPIO_PIN_SET){
	  printf("Data is not ready \r\n");
	  Handler.ADC_Delay_US(1000);
  }

  int32_t ADC_Data;
  ADS1220_ReadData(&Handler, &ADC_Data);


  uint8_t new_ADC_Data;

  float temperature;


  while (1)
  {

	new_ADC_Data = Handler.ADC_Receive();

	temperature = getTemperature(new_ADC_Data);

	printf("ADC Raw: 0x%X | ADC Temperature : %f\r\n", new_ADC_Data, temperature);
	Handler.ADC_Delay_US(1000);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

@AliMoal
Copy link
Member

AliMoal commented Mar 23, 2023

Your way to read data is not correct. That is because I did not explain explicitly. After initializing Handler structure, there is no need to change it or use it. One more thing that I should mention is you called Read Data function before While(1) loop, while you should call it inside While(1).
At last It's better to send me your complete program, because I don't know how you implemented SPI and GPIO functions or what your SPI config is. My example in README.md file is just for reading one time data NOT repeatedly!
For calculating temperature, you can also use macro in .h file. I implemented them based on datasheet.

@henriBusch
Copy link
Author

Okay, I can do that, but I would also appreciate an example of how to continuously read the internal sensor.

Thank you and greetings

@AliMoal
Copy link
Member

AliMoal commented Mar 24, 2023

At first be sure that SPI and GPIO configurations is correct. As I said in .h file:
SPI Configuration : 8Bits, CPOL=LOW(0), CPHA=2EDGE(1), Max speed (period): 150ns (6.66MHz)
Then to convert voltage to temperature, I implemented 2 macros for Thermocouple and RTD in .h file. If you are using specific device for temperature, you should convert it by your own.
To measure voltage use ADCValueToVoltage macro.

#include "ADS1220.h"
void CS_UP(void)
{
    // GPIO_CS_GROUP and GPIO_CS_PIN depend on your schematic.
    HAL_GPIO_WritePin(GPIO_CS_GROUP, GPIO_CS_PIN, GPIO_PIN_SET);
}
void CS_DOWN(void)
{
    // GPIO_CS_GROUP and GPIO_CS_PIN depend on your schematic.
    HAL_GPIO_WritePin(GPIO_CS_GROUP, GPIO_CS_PIN, GPIO_PIN_RESET);
}
void TRANSMIT(uint8_t data)
{
    // SPI_DRIVER depends on your configuration.
    HAL_SPI_Transmit (SPI_DRIVER, &data, sizeof(uint8_t), HAL_MAX_DELAY);
}
uint8_t RECEIVE(void)
{
    uint8_t dataR = 0;
    // SPI_DRIVER depends on your configuration.
    HAL_SPI_Receive (SPI_DRIVER, &dataR, sizeof(uint8_t), HAL_MAX_DELAY);
    return dataR;
}
void DELAY(uint32_t us)
{
    // DELAY_US depends on your code.
    DELAY_US(us);
}
ADS1220_Handler_t Handler = {0};

void main (void)
{
    // Initialize all STM32 peripherals including SPI needed for ADS1220 with desire configuration before call ADS1220 init function.

    Handler.ADC_CS_HIGH = CS_UP;
    Handler.ADC_CS_LOW = CS_DOWN;
    Handler.ADC_Transmit = TRANSMIT;
    Handler.ADC_Receive = RECEIVE;
    Handler.ADC_Delay_US = DELAY;
    
    // Passing Parameters as NULL to use default configurations.
    ADS1220_Init(&Handler, NULL);

    int32_t ADC_Data;
   
    while(1)
    {
       // Default conversion mode is Single-shot
       ADS1220_StartSync(&Handler);

       // GPIO_DRDY_GROUP and GPIO_DRDY_PIN depend on your schematic.
       while(HAL_GPIO_ReadPin(GPIO_DRDY_GROUP, GPIO_DRDY_PIN) == GPIO_PIN_SET){}

       ADS1220_ReadData(&Handler, &ADC_Data);
       
       // 2.048 is internal voltage reference and is used as default config.
       // 1 is default adc gain value and it must be equivalent to the gain config in ADS1220_Parameters_t.
       printf("ADC Raw: 0x%X | ADC Voltage : %f\r\n", ADC_Data, ADCValueToVoltage(ADC_Data, 2.048, 1));
    }
}

@henriBusch
Copy link
Author

I don't use a specific device to measure the temperature I just want to use the internal temperature sensor of the ADS1220 to display the temperature. I also have the whole part AIN0, AIN1, AIN2, and AIN3 not connected.

What would I have to select for t in the macro then? I am using this ADCValueToDisplacement(x/ADCvalue/, md /Max Displacement/) ((float)x * md / (float)0x7FFFFF). For x I take the ADC value and for t I take a constant. But I am not sure if this is correct.

But at least now everything worked with the voltage values. I already get values for the voltage of the ADS1220, the values make physical sense. Only for the values temperature values not yet. The data sheet is on page 31 on how one could do that. Unfortunately, the values when I calculate the temperature, then not sense more. Can I calculate that with it, or are the ADS Raw values other than what we get through your macro?

My goal is simply to determine the ambient temperature with the ADC1220.

@AliMoal
Copy link
Member

AliMoal commented Mar 25, 2023

I'm sorry for misunderstanding. I thought you want to change the voltage value to temperature. Unfortunately, in this library I did not Implement reading internal temperature sensor of ADS1220. If you can realize how private functions works in .c file, you can use them by your own to read internal temperature sensor OR you have to wait for me to take free time to implement a function for that purpose.

@henriBusch
Copy link
Author

Yes, sorry for the misunderstanding. I will try it myself for now. But will follow with interest in what you will upload next.

Thanks for the explanations so far. Definitely helped me a lot in understanding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants