Untitled
unknown
plain_text
a year ago
4.6 kB
0
Indexable
Never
/* * MAX31865_lib.c * * Created on: Aug 6, 2023 * Author: mertcangelbal */ #include "MAX31865_lib.h" #include "stm32f7xx_hal.h" //#define CS_Pin HAL_GPIO_PIN_ARD_D9 #define MAX31865_PT100_R0 (double)100.0 #define MAX31865_Rref (double)428.5 //Referans direnç değeri #define MAX31865_A (double)0.0039083 #define MAX31865_B (double)0.0000005775 double MAX31865_PT100_R = 0.0; //PT100 Receive Değeri double MAX31865_PT100_T = 0.0; //PT100 Transmit Değeri bool MAX31865_Sensor_Error = 0; //#define cs_set() CS_GPIO_Port->BSRR = (uint32_t) CS_Pin << 16u; //CS Pin << 16u ===> HAL_GPIO_PIN_ //#define cs_reset() CS_GPIO_Port->BSRR = CS_Pin; extern SPI_HandleTypeDef hspi2; Max31865_t max_pt100; void MAX31865_Init(uint8_t num_wires){ uint8_t MAX31865_Reinitialization_cnt = 0; MAX31865_Sensor_Error = 0; uint8_t MAX31865_Configuration_register_write[] = {0x80,0x00}; if((num_wires == 2) || (num_wires == 4)){ MAX31865_Configuration_register_write[1] = 0xC3; } else if(num_wires == 3){ MAX31865_Configuration_register_write[1] = 0xD3; } // cs_set(); HAL_GPIO_WritePin(max_pt100.cs_gpio,max_pt100.cs_pin,GPIO_PIN_RESET); HAL_SPI_Transmit(&hspi2, MAX31865_Configuration_register_write, 2, 100); HAL_GPIO_WritePin(max_pt100.cs_gpio,max_pt100.cs_pin,GPIO_PIN_SET); // cs_reset(); while(MAX31865_Configuration_info() != 0xD1 && MAX31865_Configuration_info() != 0xC1){ MAX31865_Reinitialization_cnt++; // cs_set(); HAL_GPIO_WritePin(max_pt100.cs_gpio,max_pt100.cs_pin,GPIO_PIN_RESET); HAL_SPI_Transmit(&hspi2, MAX31865_Configuration_register_write, 2, 100); HAL_GPIO_WritePin(max_pt100.cs_gpio,max_pt100.cs_pin,GPIO_PIN_SET); // cs_reset(); if(MAX31865_Reinitialization_cnt == 100){ break; } } } uint8_t MAX31865_Configuration_info(void){ uint8_t read_data = 0x00; uint8_t MAX31865_Configuration = 0x00; // cs_set(); HAL_GPIO_WritePin(max_pt100.cs_gpio,max_pt100.cs_pin,GPIO_PIN_RESET); HAL_SPI_Transmit(&hspi2, &read_data, 1, 100); HAL_SPI_Receive(&hspi2, &MAX31865_Configuration, 1, 100); HAL_GPIO_WritePin(max_pt100.cs_gpio,max_pt100.cs_pin,GPIO_PIN_SET); // cs_reset(); return MAX31865_Configuration; } double MAX31865_Get_Temperature(void){ double data; struct rx_data_MAX31865{ uint16_t RTD_Resistance_Registers; //Direnç Kayıtları uint16_t High_Fault_Threshold; //Üst Hata Eşiği uint16_t Low_Fault_Threshold; //Alt Hata Eşiği uint8_t Fault_Status; //Hata Durumu }; struct rx_data_MAX31865 MAX31865_Receive_Data; uint8_t MAX31865_Start_Address_of_the_poll = 0x00; //Adres Başlangıç Seçimi uint8_t MAX31865_rx_buffer[7]; // cs_set(); HAL_GPIO_WritePin(max_pt100.cs_gpio,max_pt100.cs_pin,GPIO_PIN_RESET); HAL_SPI_Transmit(&hspi2, &MAX31865_Start_Address_of_the_poll, 1, 100); HAL_SPI_Receive(&hspi2, MAX31865_rx_buffer, 7, 100); HAL_GPIO_WritePin(max_pt100.cs_gpio,max_pt100.cs_pin,GPIO_PIN_SET); // cs_reset(); MAX31865_Receive_Data.RTD_Resistance_Registers = ((MAX31865_rx_buffer[0] << 8) | MAX31865_rx_buffer[1]) >> 1; //Direnç Kaydı MAX31865_Receive_Data.High_Fault_Threshold = ((MAX31865_rx_buffer[2] << 8) | MAX31865_rx_buffer[3]) >> 1; //Üst Hata Eşik Verisi MAX31865_Receive_Data.Low_Fault_Threshold = (MAX31865_rx_buffer[4] << 8) | MAX31865_rx_buffer[5];; //Düşük Hata Eşik Verisi MAX31865_Receive_Data.Fault_Status = MAX31865_rx_buffer[6]; //Hata Verisi if(MAX31865_Receive_Data.Fault_Status > 0x00){ MAX31865_Sensor_Error = 1; //Hata Sıfırlamak için sistemi yeniden başlatıyor MAX31865_Init(3); MAX31865_Sensor_Error = 0; } data = ((double)MAX31865_Receive_Data.RTD_Resistance_Registers * MAX31865_Rref) / (double)32768.0; //PT100'ün direnç değeri 4000 ile 400 arasında return MAX31865_Get_Temperatur_Math(data); } double MAX31865_Get_Temperatur_Math(double PT100_Resistance){ //double PT100_Resistance -> data if(PT100_Resistance >= (double)100.0){ double MAX31865_Math_Discriminant = (double) 0.00001527480889 - ((double) -0.00000231 * (1 - (PT100_Resistance / MAX31865_PT100_R0 ))); MAX31865_PT100_T = ((double) -0.0039083 + sqrt(MAX31865_Math_Discriminant)) / (double) -0.000001155; } else{ MAX31865_PT100_T = (double) 0.000000000270 * pow(PT100_Resistance, 5) - (double) 0.000000066245 * pow(PT100_Resistance, 4) - (double) 0.000000184636 * pow(PT100_Resistance, 3) + (double) 0.002320232987 * pow(PT100_Resistance, 2) + (double) 2.229927824035 * PT100_Resistance - (double) 242.090854986215; } return MAX31865_PT100_T; }