Fertirrega_v6 AD5934 Bug Fix
This commit is contained in:
@@ -170,6 +170,9 @@ void AD5934_RestartSweep(void)
|
||||
// Initialize starting frequency, Start frequency sweep, standby
|
||||
AD5934_SetRegisterValue(AD5934_CONTROL_REG_HB, ((AD5934_CONTROL_FUNCTION(AD5934_INIT_START_FREQ)) | AD5934_CONTROL_RANGE(AD5934_400mVpp_RANGE) | AD5934_PGA_GAIN(AD5934_PGA_GAIN_X5)), 1);
|
||||
|
||||
// Disable Internal Reset State
|
||||
AD5934_SetRegisterValue(AD5934_CONTROL_REG_LB, AD5934_CONTROL_FUNCTION(AD5934_RESERVED), 1);
|
||||
|
||||
// Configure Range Output, PGA gain andPlace AD5934 in sweep
|
||||
AD5934_SetRegisterValue(AD5934_CONTROL_REG_HB, (AD5934_CONTROL_FUNCTION(AD5934_START_FREQ_SWEEP) | AD5934_CONTROL_RANGE(AD5934_400mVpp_RANGE) | AD5934_PGA_GAIN(AD5934_PGA_GAIN_X5)), 1);
|
||||
|
||||
@@ -207,6 +210,7 @@ uint32_t AD5934_Sweep(void)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* @brief Calculate Temperature.
|
||||
*
|
||||
@@ -324,68 +328,76 @@ float AD5934_GetTemperature(void)
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* @brief Calculate impedance.
|
||||
*
|
||||
* @param none.
|
||||
*
|
||||
* @return impedance.
|
||||
******************************************************************************/
|
||||
float AD5934_GetImpedance(void)
|
||||
/**
|
||||
* @brief Calculates impedance and converts it to conductivity using a 2-point
|
||||
* temperature-compensated model.
|
||||
*
|
||||
* @param temperature_dut The current temperature of the DUT (Device Under Test).
|
||||
* @return float Calculated conductivity in uS/cm.
|
||||
*/
|
||||
float AD5934_GetImpedance(float temperature_dut)
|
||||
{
|
||||
uint8_t i, ec_gain;
|
||||
uint32_t sample_dut;
|
||||
int32_t dut_sum[2];
|
||||
float real_number, imag_number, mag_dut, slope;
|
||||
uint8_t i, ec_gain;
|
||||
uint32_t sample_dut;
|
||||
int32_t dut_sum[2] = {0, 0};
|
||||
float real_avg, imag_avg, mag_dut, slope, target_cond;
|
||||
|
||||
/* 1. Hardware Setup: Gain Selection (PA6) */
|
||||
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6) == GPIO_PIN_SET)
|
||||
ec_gain = AD5934_CH_EC_HIGH_GAIN;
|
||||
else
|
||||
ec_gain = AD5934_CH_EC_MID_GAIN;
|
||||
|
||||
/* STEP 2: Set the Amplification related to the maximum set value */
|
||||
if((ec_gain != flash_data.EC10mS_EC5mS_switch) && (main_state != STATE_SETUP_CALIBRATION))
|
||||
return -1.0f; // Fatal error because the gain used in calibration is different
|
||||
|
||||
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6) == GPIO_PIN_SET) // PA6 has internal pull-up and the Switch connects to GND
|
||||
ec_gain = AD5934_CH_EC_HIGH_GAIN; // Switch OFF = 5 mS/cm, HIGH GAIN,
|
||||
else
|
||||
ec_gain = AD5934_CH_EC_MID_GAIN; // Switch ON = 10 mS/cm, MID GAIN,
|
||||
ADG715_Update(ec_gain);
|
||||
HAL_Delay(2);
|
||||
|
||||
ADG715_Update(ec_gain);
|
||||
HAL_Delay(2);
|
||||
/* 2. Data Acquisition */
|
||||
sample_dut = AD5934_Sweep();
|
||||
|
||||
sample_dut = AD5934_Sweep(); // Get PT100/PT1000 Values from ADC
|
||||
// Shift buffer for moving average
|
||||
for (i = (AD5934_EC_AVERAGES - 1); i > 0; i--)
|
||||
{
|
||||
ec_dut_samples[i][0] = ec_dut_samples[i-1][0];
|
||||
ec_dut_samples[i][1] = ec_dut_samples[i-1][1];
|
||||
}
|
||||
|
||||
for (i = (AD5934_EC_AVERAGES-1); i > 0; i--)
|
||||
// Deconstruct 32-bit sample into Real and Imaginary components
|
||||
ec_dut_samples[0][0] = (float)(int16_t)(sample_dut & 0xFFFF);
|
||||
ec_dut_samples[0][1] = (float)(int16_t)((sample_dut >> 16) & 0xFFFF);
|
||||
|
||||
/* 3. Compute Averages */
|
||||
for (i = 0; i < AD5934_EC_AVERAGES; i++)
|
||||
{
|
||||
dut_sum[0] += (int32_t)ec_dut_samples[i][0];
|
||||
dut_sum[1] += (int32_t)ec_dut_samples[i][1];
|
||||
}
|
||||
|
||||
real_avg = (float)dut_sum[0] / (float)AD5934_EC_AVERAGES;
|
||||
imag_avg = (float)dut_sum[1] / (float)AD5934_EC_AVERAGES;
|
||||
|
||||
/* 4. Magnitude Calculation */
|
||||
mag_dut = sqrtf((real_avg * real_avg) + (imag_avg * imag_avg));
|
||||
|
||||
/* 5. Calibration Logic Implementation */
|
||||
|
||||
// Mode: CALIBRATION SETUP (User is defining the reference magnitude)
|
||||
if(main_state == STATE_SETUP_CALIBRATION)
|
||||
{
|
||||
return mag_dut;
|
||||
}
|
||||
// Mode: MEASUREMENT (Applying temperature compensation)
|
||||
else if(flash_data.ec0_mag != flash_data.ec1413_mag)
|
||||
{
|
||||
ec_dut_samples[i][0] = ec_dut_samples[i-1][0]; //real
|
||||
ec_dut_samples[i][1] = ec_dut_samples[i-1][1]; //imag
|
||||
slope = (AD5934_GAIN_FACTOR_1413US/(flash_data.ec1413_mag-flash_data.ec0_mag));
|
||||
|
||||
return(AD5934_GAIN_FACTOR_1413US + (mag_dut - flash_data.ec1413_mag) * slope);
|
||||
}
|
||||
else
|
||||
return 0.0f; // Calibration error fallback
|
||||
|
||||
// Read real and imaginary data
|
||||
ec_dut_samples[0][0] = (int16_t)(sample_dut & 0x0000FFFF); //real
|
||||
ec_dut_samples[0][1] = (int16_t)((sample_dut & 0xFFFF0000)>>16); //imag
|
||||
|
||||
for (i = 0, dut_sum[0]=0, dut_sum[1]=0; i < AD5934_EC_AVERAGES; i++)
|
||||
{
|
||||
dut_sum[0] += (int32_t)ec_dut_samples[i][0]; //real
|
||||
dut_sum[1] += (int32_t)ec_dut_samples[i][1]; //imag
|
||||
}
|
||||
|
||||
real_number = ((float)dut_sum[0])/((float)AD5934_EC_AVERAGES);
|
||||
imag_number = ((float)dut_sum[1])/((float)AD5934_EC_AVERAGES);
|
||||
|
||||
// Calculate unknown magnitude
|
||||
mag_dut = sqrtf((real_number * real_number) + (imag_number * imag_number));
|
||||
|
||||
|
||||
// Calculate gain factor for the unknown magnitude
|
||||
|
||||
if(main_state == STATE_SETUP_CALIBRATION)
|
||||
return(mag_dut);
|
||||
else if(flash_data.ec12880_value != flash_data.ec1413_value)
|
||||
{
|
||||
slope = ((AD5934_GAIN_FACTOR_12880US - AD5934_GAIN_FACTOR_1413US)/(flash_data.ec12880_value - flash_data.ec1413_value));
|
||||
|
||||
return(AD5934_GAIN_FACTOR_1413US + (mag_dut - flash_data.ec1413_value) * slope);
|
||||
}
|
||||
else
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
|
||||
@@ -462,6 +474,46 @@ float AD5934_Linear_Correction(float raw_value)
|
||||
return (raw_value * slope) + offset;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Internal helper to interpolate conductivity from the temperature matrix.
|
||||
* @param temp The current temperature in Celsius.
|
||||
* @return Interpolated conductivity value in uS/cm.
|
||||
*/
|
||||
float AD5934_Get_Target_Conductivity(float temp)
|
||||
{
|
||||
/* Matrix: Row 0 = Temperature (ºC), Row 1 = Conductivity (uS/cm) */
|
||||
const float matrix[2][12] = {
|
||||
{5, 10, 15, 18, 20, 22, 25, 30, 35, 40, 45, 50},
|
||||
{893, 1018, 1149, 1225, 1278, 1329, 1413, 1552, 1691, 1843, 1988, 2145}
|
||||
};
|
||||
|
||||
// Boundary Check: Lower
|
||||
if (temp <= matrix[0][0])
|
||||
temp = 5;
|
||||
|
||||
// Boundary Check: Upper
|
||||
if (temp >= matrix[0][11])
|
||||
temp = 50;
|
||||
|
||||
// Linear Interpolation logic
|
||||
for (int i = 0; i < 11; i++)
|
||||
{
|
||||
if (temp >= matrix[0][i] && temp <= matrix[0][i+1])
|
||||
{
|
||||
float t0 = matrix[0][i];
|
||||
float t1 = matrix[0][i+1];
|
||||
float c0 = matrix[1][i];
|
||||
float c1 = matrix[1][i+1];
|
||||
|
||||
// Formula: y = y0 + (x - x0) * ((y1 - y0) / (x1 - x0))
|
||||
return c0 + (temp - t0) * ((c1 - c0) / (t1 - t0));
|
||||
}
|
||||
}
|
||||
return (matrix[1][6]); // Mid point Fallback
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* ADG715
|
||||
*****************************************************************************
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
|
||||
ADC_HandleTypeDef hadc1;
|
||||
ADC_HandleTypeDef hadc2;
|
||||
DMA_HandleTypeDef hdma_adc1;
|
||||
|
||||
/* ADC1 init function */
|
||||
void MX_ADC1_Init(void)
|
||||
@@ -45,12 +44,12 @@ void MX_ADC1_Init(void)
|
||||
/** Common config
|
||||
*/
|
||||
hadc1.Instance = ADC1;
|
||||
hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
|
||||
hadc1.Init.ContinuousConvMode = DISABLE;
|
||||
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
|
||||
hadc1.Init.ContinuousConvMode = ENABLE;
|
||||
hadc1.Init.DiscontinuousConvMode = DISABLE;
|
||||
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T3_TRGO;
|
||||
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
||||
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||
hadc1.Init.NbrOfConversion = 2;
|
||||
hadc1.Init.NbrOfConversion = 1;
|
||||
if (HAL_ADC_Init(&hadc1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
@@ -58,18 +57,9 @@ void MX_ADC1_Init(void)
|
||||
|
||||
/** Configure Regular Channel
|
||||
*/
|
||||
sConfig.Channel = ADC_CHANNEL_VREFINT;
|
||||
sConfig.Channel = ADC_CHANNEL_0;
|
||||
sConfig.Rank = ADC_REGULAR_RANK_1;
|
||||
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
|
||||
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure Regular Channel
|
||||
*/
|
||||
sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
|
||||
sConfig.Rank = ADC_REGULAR_RANK_2;
|
||||
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
|
||||
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
@@ -97,7 +87,7 @@ void MX_ADC2_Init(void)
|
||||
*/
|
||||
hadc2.Instance = ADC2;
|
||||
hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;
|
||||
hadc2.Init.ContinuousConvMode = DISABLE;
|
||||
hadc2.Init.ContinuousConvMode = ENABLE;
|
||||
hadc2.Init.DiscontinuousConvMode = DISABLE;
|
||||
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
||||
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||
@@ -143,23 +133,6 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/* ADC1 DMA Init */
|
||||
/* ADC1 Init */
|
||||
hdma_adc1.Instance = DMA1_Channel1;
|
||||
hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
||||
hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
|
||||
hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
|
||||
hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
|
||||
hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
|
||||
hdma_adc1.Init.Mode = DMA_CIRCULAR;
|
||||
hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
|
||||
if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
__HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
|
||||
|
||||
/* USER CODE BEGIN ADC1_MspInit 1 */
|
||||
|
||||
/* USER CODE END ADC1_MspInit 1 */
|
||||
@@ -204,8 +177,6 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOA, AIN1_Pin|AIN2_Pin);
|
||||
|
||||
/* ADC1 DMA DeInit */
|
||||
HAL_DMA_DeInit(adcHandle->DMA_Handle);
|
||||
/* USER CODE BEGIN ADC1_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END ADC1_MspDeInit 1 */
|
||||
|
||||
@@ -290,6 +290,9 @@ float ADSCalculate_ph_Volts(void)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Calcula o valor de pH compensado pela temperatura.
|
||||
*
|
||||
@@ -298,23 +301,21 @@ float ADSCalculate_ph_Volts(void)
|
||||
*
|
||||
* @return float Valor de pH calculado (0.0 a 14.0).
|
||||
*/
|
||||
float ADSCalculate_ph_Compensated(int16_t adc_raw, float temp_dut)
|
||||
float ADSCalculate_ph_Compensated(float temp_dut)
|
||||
{
|
||||
|
||||
|
||||
// 1. Converter leitura bruta do ADC para tensão real (Volts)
|
||||
float v_measured = ((float)adc_raw * ADS1015_ADC_VREF) / ADS1015_ADC_MAX;
|
||||
float v_measured = ((float)(2048 - ADSreadADC_Differential_0_1(&i2c)) * ADS1015_ADC_VREF) / ADS1015_ADC_MAX;
|
||||
|
||||
// 2. Calcular o Slope original (medido na temperatura da calibração)
|
||||
// Delta pH é fixo em 3.0 (de pH 7 para pH 4)
|
||||
float delta_v_calib = flash_data.ph7_value - flash_data.ph4_value;
|
||||
float delta_v_calib = flash_data.ph7_volts - flash_data.ph4_volts;
|
||||
|
||||
if (delta_v_calib == 0.0f)
|
||||
{
|
||||
return 0.0f; // Proteção contra erro de calibração/divisão por zero
|
||||
}
|
||||
|
||||
float slope_at_calib = 3.0f / delta_v_calib;
|
||||
float slope_at_calib = PH_DELTA_CALIB / delta_v_calib;
|
||||
|
||||
// 3. Calcular o Fator de Correção Térmica (Equação de Nernst)
|
||||
float temp_k_now = temp_dut + ADS1015_KELVIN_OFFSET;
|
||||
@@ -328,7 +329,7 @@ float ADSCalculate_ph_Compensated(int16_t adc_raw, float temp_dut)
|
||||
* O Slope ajustado para a temperatura atual é: slope_at_calib / thermal_factor
|
||||
* Fórmula: pH = pH_ref + (V_medido - V_ref_7) * Slope_ajustado
|
||||
*/
|
||||
float ph_result = 7.0f + ((v_measured - flash_data.ph7_value) * (slope_at_calib / thermal_factor));
|
||||
float ph_result = 7.0f + ((v_measured - flash_data.ph7_volts) * (slope_at_calib / thermal_factor));
|
||||
|
||||
// 5. Clamping (Garantir limites físicos)
|
||||
if (ph_result < 0.0f) ph_result = 0.0f;
|
||||
@@ -336,3 +337,95 @@ float ADSCalculate_ph_Compensated(int16_t adc_raw, float temp_dut)
|
||||
|
||||
return ph_result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates the pH value without temperature compensation.
|
||||
*
|
||||
* This version ignores the current temperature (temp_dut) and assumes
|
||||
* the electrode slope remains constant as calibrated.
|
||||
*
|
||||
* @param temp_dut Unused in this non-compensated version.
|
||||
* @return float Calculated pH value [0.0, 14.0]. Returns 0.0 if calibration error detected.
|
||||
*/
|
||||
float ADSCalculate_ph_Uncompensated(void)
|
||||
{
|
||||
/*
|
||||
* 1. Convert raw ADC reading to actual voltage (Volts).
|
||||
* Note: The subtraction of 2048 assumes a differential configuration
|
||||
* where the midpoint is at the mid-scale of the ADS1015.
|
||||
*/
|
||||
float v_measured = ((float)(2048 - ADSreadADC_Differential_0_1(&i2c)) * ADS1015_ADC_VREF) / ADS1015_ADC_MAX;
|
||||
|
||||
/*
|
||||
* 2. Calculate the original Slope (measured during calibration).
|
||||
* PH_DELTA_CALIB is a constant representing the pH difference between
|
||||
* calibration points (e.g., |7.0 - 4.0| = 3.0).
|
||||
*/
|
||||
float delta_v_calib = flash_data.ph7_volts - flash_data.ph4_volts;
|
||||
|
||||
/* Safety check: Prevent division by zero if calibration data is invalid */
|
||||
if (delta_v_calib == 0.0f)
|
||||
{
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
/* Slope = Delta pH / Delta Voltage */
|
||||
float slope_at_calib = PH_DELTA_CALIB / delta_v_calib;
|
||||
|
||||
/*
|
||||
* 3. Final pH Calculation (Non-compensated)
|
||||
* Formula: pH = pH_ref + (V_measured - V_ref_7) * Slope
|
||||
* Where pH_ref is 7.0.
|
||||
*/
|
||||
float ph_result = 7.0f + ((v_measured - flash_data.ph7_volts) * slope_at_calib);
|
||||
|
||||
/* 4. Clamping (Ensure physical limits of the pH scale) */
|
||||
if (ph_result < 0.0f)
|
||||
{
|
||||
ph_result = 0.0f;
|
||||
}
|
||||
else if (ph_result > 14.0f)
|
||||
{
|
||||
ph_result = 14.0f;
|
||||
}
|
||||
|
||||
return ph_result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Performs linear interpolation for a given temperature within the LUT.
|
||||
*
|
||||
* @param temp_target The target temperature to interpolate for.
|
||||
* @param row_index Index of the buffer row (1 for pH4, 2 for pH7).
|
||||
* @return float Interpolated value from the matrix.
|
||||
*/
|
||||
float ADSinterpolate_ph(float temp_target, uint8_t row_index)
|
||||
{
|
||||
|
||||
/* Matrix with {Temperature, Ph 4 Buffer, Ph 7 Buffer} */
|
||||
const float lut_matrix_buffer_ph[ADS1015_INTERPOLATION_ROWS][ADS1015_PH_TEMP_INTER_POINTS] = {
|
||||
{0.0f, 5.0f, 10.0f, 15.0f, 20.0f, 25.0f, 30.0f, 35.0f, 40.0f, 45.0f, 50.0f, 55.0f}, // Temperature
|
||||
{4.01f, 4.00f, 4.00f, 4.00f, 4.00f, 4.01f, 4.01f, 4.02f, 4.03f, 4.05f, 4.06f, 4.10f}, // Buffer ph 4
|
||||
{7.12f, 7.07f, 7.05f, 7.03f, 7.01f, 7.00f, 6.98f, 6.97f, 6.96f, 6.96f, 6.96f, 6.96f},}; // Buffer ph 7
|
||||
uint8_t i = 0;
|
||||
|
||||
/* Boundary Check: Clamp to first or last index if out of range */
|
||||
if (temp_target <= lut_matrix_buffer_ph[0][0]) return lut_matrix_buffer_ph[row_index][0];
|
||||
if (temp_target >= lut_matrix_buffer_ph[0][ADS1015_PH_TEMP_INTER_POINTS-1]) return lut_matrix_buffer_ph[row_index][ADS1015_PH_TEMP_INTER_POINTS-1];
|
||||
|
||||
/* Find interpolation interval */
|
||||
while (i < (ADS1015_PH_TEMP_INTER_POINTS-1) && lut_matrix_buffer_ph[0][i+1] < temp_target)
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
float x0 = lut_matrix_buffer_ph[0][i];
|
||||
float x1 = lut_matrix_buffer_ph[0][i+1];
|
||||
float y0 = lut_matrix_buffer_ph[row_index][i];
|
||||
float y1 = lut_matrix_buffer_ph[row_index][i+1];
|
||||
|
||||
/* Linear Interpolation: y = y0 + (x - x0) * ((y1 - y_0) / (x1 - x0)) */
|
||||
return y0 + (temp_target - x0) * ((y1 - y0) / (x1 - x0));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -96,8 +96,10 @@ void MX_GPIO_Init(void)
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : RS485_Addr4_Pin RS485_Addr5_Pin RS485_Addr6_Pin RS485_Addr7_Pin */
|
||||
GPIO_InitStruct.Pin = RS485_Addr4_Pin|RS485_Addr5_Pin|RS485_Addr6_Pin|RS485_Addr7_Pin;
|
||||
/*Configure GPIO pins : RS485_Addr4_Pin RS485_Addr5_Pin RS485_Addr6_Pin RS485_Addr7_Pin
|
||||
Calib_PH_Pin */
|
||||
GPIO_InitStruct.Pin = RS485_Addr4_Pin|RS485_Addr5_Pin|RS485_Addr6_Pin|RS485_Addr7_Pin
|
||||
|Calib_PH_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
@@ -109,16 +111,6 @@ void MX_GPIO_Init(void)
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(TX_EN_RS485_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : Calib_PH_Pin */
|
||||
GPIO_InitStruct.Pin = Calib_PH_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
HAL_GPIO_Init(Calib_PH_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/* EXTI interrupt init*/
|
||||
HAL_NVIC_SetPriority(EXTI3_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI3_IRQn);
|
||||
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 2 */
|
||||
@@ -130,9 +122,12 @@ void MX_GPIO_Init(void)
|
||||
* @param GPIO_Pin: Specifies the pins connected to the EXTI line.
|
||||
* @retval None
|
||||
*/
|
||||
|
||||
|
||||
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
|
||||
{
|
||||
if ((GPIO_Pin == Calib_PH_Pin) && (main_state == STATE_RUNNING_OK))
|
||||
/* External Interrupt
|
||||
if ((GPIO_Pin == Calib_PH_Pin) && (main_state == STATE_RUNNING_OK))
|
||||
{
|
||||
uint32_t current_time = HAL_GetTick();
|
||||
GPIO_PinState pin_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3);
|
||||
@@ -148,14 +143,14 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
|
||||
{
|
||||
if(pin_state == GPIO_PIN_RESET)
|
||||
{
|
||||
/* --- LOGIC FOR FALLING EDGE HERE --- */
|
||||
// --- LOGIC FOR FALLING EDGE HERE ---
|
||||
//HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
|
||||
//HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
|
||||
edge_state = FALLING_EDGE;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* --- LOGIC FOR RISING EDGE HERE --- */
|
||||
// --- LOGIC FOR RISING EDGE HERE ---
|
||||
//HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);
|
||||
//HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
|
||||
edge_state = RISING_EDGE;
|
||||
@@ -166,6 +161,7 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
+113
-174
@@ -19,9 +19,7 @@
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
#include "adc.h"
|
||||
#include "dma.h"
|
||||
#include "i2c.h"
|
||||
#include "tim.h"
|
||||
#include "usart.h"
|
||||
#include "gpio.h"
|
||||
|
||||
@@ -73,7 +71,6 @@ uint8_t doubleSpace[]={'_','_'};
|
||||
uint8_t newline_ph[]={'_','p','H','\n','\0'};
|
||||
uint8_t newline_admi[]={'_','u','S','\n','\0'};
|
||||
uint8_t newline_temp[]={' ','°','C','\n','\0'};
|
||||
uint8_t newline_real[]={'_','°','C','\n','\0'};
|
||||
uint8_t newline_imag[]={'_','O','h','m','\n','\0'};
|
||||
uint8_t newline_485[]={'_','a','d','\n','\0'};
|
||||
uint8_t minus[]={'-',' '};
|
||||
@@ -154,23 +151,22 @@ int main(void)
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_DMA_Init();
|
||||
MX_ADC1_Init();
|
||||
MX_ADC2_Init();
|
||||
MX_I2C1_Init();
|
||||
MX_I2C2_Init();
|
||||
MX_USART1_UART_Init();
|
||||
MX_TIM3_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
Flash_Load_Page(&flash_data);
|
||||
|
||||
//TempSensor_Init(&hadc1);
|
||||
|
||||
|
||||
/*
|
||||
HAL_TIM_Base_Start(&htim3);
|
||||
HAL_ADCEx_Calibration_Start(&hadc1);
|
||||
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)AD_RES, 2); // Start ADC Conversion
|
||||
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)AD_RES, 2); // Internal Temperature conversion
|
||||
*/
|
||||
|
||||
digital_outputs_init();
|
||||
|
||||
@@ -190,18 +186,28 @@ int main(void)
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
|
||||
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3) == GPIO_PIN_RESET)
|
||||
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3) == GPIO_PIN_RESET) // When PB3 (with internal pull up) in the Prog Header is set to GND
|
||||
{
|
||||
|
||||
main_state = STATE_SETUP_CALIBRATION;
|
||||
|
||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET); // Red LED On
|
||||
|
||||
for(i=0;i<32;i++)
|
||||
for(i=0;i<32;i++) // Loop to get stability and get averages
|
||||
{
|
||||
flash_data.temperature_value = temperature_RTD;
|
||||
flash_data.ph4_value = ADSCalculate_ph_Volts();
|
||||
flash_data.ec1413_value = AD5934_GetImpedance();
|
||||
flash_data.ph4_volts = ADSCalculate_ph_Volts();
|
||||
flash_data.temperature_value = AD5934_GetTemperature(); // Air Temperature is not stored, just used to calculate impedance
|
||||
flash_data.ec0_mag = AD5934_GetImpedance(flash_data.temperature_value); // Dry Probe
|
||||
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6) == GPIO_PIN_SET)
|
||||
flash_data.EC10mS_EC5mS_switch = AD5934_CH_EC_HIGH_GAIN; // 5mS/cm option
|
||||
else
|
||||
flash_data.EC10mS_EC5mS_switch = AD5934_CH_EC_MID_GAIN; //10ms/cm option
|
||||
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_7) == GPIO_PIN_SET)
|
||||
flash_data.PT100_PT1000_switch = AD5934_CH_RTD_LOW_GAIN; //PT100 option
|
||||
else
|
||||
flash_data.PT100_PT1000_switch = AD5934_CH_RTD_MID_GAIN; //PT1000 option
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -211,11 +217,12 @@ int main(void)
|
||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET); // Green LED On
|
||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
|
||||
|
||||
for(i=0;i<32;i++)
|
||||
for(i=0;i<32;i++) // Loop to get stability and get averages
|
||||
{
|
||||
flash_data.ph7_value = ADSCalculate_ph_Volts();
|
||||
flash_data.ec12880_value = AD5934_GetImpedance();
|
||||
Flash_Save_Page(&flash_data);
|
||||
flash_data.temperature_value = AD5934_GetTemperature(); // Liquid Temperature is stored
|
||||
flash_data.ph7_volts = ADSCalculate_ph_Volts();
|
||||
flash_data.ec1413_mag = AD5934_GetImpedance(flash_data.temperature_value); // Get the impedance of the reference liquid
|
||||
Flash_Save_Page(&flash_data); // Store Calibration parameters in Flash
|
||||
}
|
||||
|
||||
HAL_Delay(500);
|
||||
@@ -227,169 +234,100 @@ int main(void)
|
||||
}
|
||||
while (1)
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
current_millis = HAL_GetTick();
|
||||
|
||||
|
||||
// Piscar LED verde em PB5 a cada 0,5 segundos
|
||||
if ((current_millis - previous_millis_green) >= 500)
|
||||
{
|
||||
previous_millis_green = current_millis;
|
||||
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);
|
||||
|
||||
// Check if state changed and send via RS485
|
||||
uint8_t current_green_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5);
|
||||
if (current_green_state != previous_green_state)
|
||||
{
|
||||
previous_green_state = current_green_state;
|
||||
uint8_t led_data[2] = {0x01, current_green_state}; // Command 0x01 for green LED
|
||||
|
||||
}
|
||||
|
||||
temperature_RTD = AD5934_GetTemperature();
|
||||
FloatToString(tempString, temperature_RTD);
|
||||
status0 = rs485_send_broadcast(tempString, (strlen((char*)tempString)-1));
|
||||
status1 = rs485_send_broadcast(newline_temp, strlen((char*)newline_temp));
|
||||
|
||||
admittance_EC = AD5934_GetImpedance(temperature_RTD);
|
||||
FloatToString(tempString, admittance_EC);
|
||||
status0 = rs485_send_broadcast(tempString, (strlen((char*)tempString)-1));
|
||||
status1 = rs485_send_broadcast(newline_admi, strlen((char*)newline_admi));
|
||||
|
||||
}
|
||||
|
||||
// Piscar LED vermelho em PB4 a cada 1 segundo
|
||||
if ((current_millis - previous_millis_red) >= 1000)
|
||||
{
|
||||
previous_millis_red = current_millis;
|
||||
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_4);
|
||||
|
||||
// Check if state changed and send via RS485
|
||||
uint8_t current_red_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4);
|
||||
if (current_red_state != previous_red_state)
|
||||
{
|
||||
previous_red_state = current_red_state;
|
||||
uint8_t led_data[2] = {0x02, current_red_state}; // Command 0x02 for red LED
|
||||
|
||||
}
|
||||
|
||||
|
||||
ph_compensated = ADSCalculate_ph_Uncompensated();
|
||||
FloatToString(tempString, ph_compensated);
|
||||
status0 = rs485_send_broadcast(tempString, (strlen((char*)tempString)-1));
|
||||
status1 = rs485_send_broadcast(newline_ph, strlen((char*)newline_ph));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
if(UpdateEvent) // Internal Temperature
|
||||
{
|
||||
|
||||
for (i = (STM32_TEMPERATURE_AVERAGES-1); i > 0; i--)
|
||||
Temp_Samples[i] = Temp_Samples[i-1];
|
||||
|
||||
|
||||
// Read real and imaginary data
|
||||
if(AD_RES[0]>0.0f)
|
||||
V_Ref = (float)((V_REF_INT * 4095.0)/AD_RES[0]);
|
||||
else
|
||||
V_Ref = 0.0f;
|
||||
V_Sense = (float)(AD_RES[1] * V_Ref) / 4095.0;
|
||||
Temp_Samples[0] = (((V_AT_25C - V_Sense) * 1000.0) /AVG_SLOPE) + 25.0;
|
||||
|
||||
for (i = 0, Temp_Sum=0; i < STM32_TEMPERATURE_AVERAGES; i++)
|
||||
Temp_Sum += Temp_Samples[i];
|
||||
|
||||
Temperature = Temp_Sum/((float)STM32_TEMPERATURE_AVERAGES);
|
||||
|
||||
UpdateEvent = 0;
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
current_millis = HAL_GetTick();
|
||||
|
||||
|
||||
|
||||
|
||||
// Piscar LED verde em PB5 a cada 0,5 segundos
|
||||
if ((current_millis - previous_millis_green) >= 500)
|
||||
{
|
||||
previous_millis_green = current_millis;
|
||||
|
||||
/*HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);
|
||||
|
||||
// Check if state changed and send via RS485
|
||||
uint8_t current_green_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5);
|
||||
if (current_green_state != previous_green_state)
|
||||
{
|
||||
previous_green_state = current_green_state;
|
||||
uint8_t led_data[2] = {0x01, current_green_state}; // Command 0x01 for green LED
|
||||
|
||||
}*/
|
||||
|
||||
FloatToString(tempString, Temperature);
|
||||
status0 = rs485_send_broadcast(tempString, (strlen((char*)tempString)-1));
|
||||
status1 = rs485_send_broadcast(newline_temp, strlen((char*)newline_temp));
|
||||
|
||||
|
||||
temperature_RTD = AD5934_GetTemperature();
|
||||
FloatToString(tempString, temperature_RTD);
|
||||
status0 = rs485_send_broadcast(tempString, (strlen((char*)tempString)-1));
|
||||
status1 = rs485_send_broadcast(newline_real, strlen((char*)newline_real));
|
||||
|
||||
|
||||
|
||||
ph_compensated = ADSCalculate_ph_Compensated((2048 - ADSreadADC_Differential_0_1(&i2c)),flash_data.temperature_value);
|
||||
FloatToString(tempString, ph_compensated);
|
||||
status0 = rs485_send_broadcast(tempString, (strlen((char*)tempString)-1));
|
||||
status1 = rs485_send_broadcast(newline_ph, strlen((char*)newline_ph));
|
||||
|
||||
|
||||
admittance_EC = AD5934_GetImpedance();
|
||||
FloatToString(tempString, admittance_EC);
|
||||
status0 = rs485_send_broadcast(tempString, (strlen((char*)tempString)-1));
|
||||
status1 = rs485_send_broadcast(newline_admi, strlen((char*)newline_admi));
|
||||
|
||||
}
|
||||
else if(UpdateEvent)
|
||||
{
|
||||
|
||||
for (i = (STM32_TEMPERATURE_AVERAGES-1); i > 0; i--)
|
||||
Temp_Samples[i] = Temp_Samples[i-1];
|
||||
|
||||
|
||||
// Read real and imaginary data
|
||||
if(AD_RES[0]>0.0f)
|
||||
V_Ref = (float)((V_REF_INT * 4095.0)/AD_RES[0]);
|
||||
else
|
||||
V_Ref = 0.0f;
|
||||
V_Sense = (float)(AD_RES[1] * V_Ref) / 4095.0;
|
||||
Temp_Samples[0] = (((V_AT_25C - V_Sense) * 1000.0) /AVG_SLOPE) + 25.0;
|
||||
|
||||
for (i = 0, Temp_Sum=0; i < STM32_TEMPERATURE_AVERAGES; i++)
|
||||
Temp_Sum += Temp_Samples[i];
|
||||
|
||||
Temperature = Temp_Sum/((float)STM32_TEMPERATURE_AVERAGES);
|
||||
|
||||
UpdateEvent = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Piscar LED vermelho em PB4 a cada 1 segundo
|
||||
/* if ((current_millis - previous_millis_red) >= 700)
|
||||
{
|
||||
previous_millis_red = current_millis;
|
||||
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_4);
|
||||
|
||||
// Check if state changed and send via RS485
|
||||
uint8_t current_red_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4);
|
||||
if (current_red_state != previous_red_state)
|
||||
{
|
||||
previous_red_state = current_red_state;
|
||||
uint8_t led_data[2] = {0x02, current_red_state}; // Command 0x02 for red LED
|
||||
|
||||
}
|
||||
|
||||
// Check if state changed and send via RS485
|
||||
rs485_address = rs485_get_address();
|
||||
|
||||
intToStr(rs485_address, rs485_text);
|
||||
|
||||
// Send broadcast message
|
||||
status0 = rs485_send_broadcast(rs485_text, strlen((char*)rs485_text));
|
||||
status1 = rs485_send_broadcast(newline_485, strlen((char*)newline_485));
|
||||
status1 = rs485_send_broadcast(newline, strlen((char*)newline));
|
||||
|
||||
|
||||
// Set channels in the Analog Switch
|
||||
|
||||
|
||||
//tempByte = ad5934_sample_BL.bytes[0];
|
||||
//ad5934_sample_BL.bytes[0] = ad5934_sample_BL.bytes[1];
|
||||
//ad5934_sample_BL.bytes[1] = tempByte;
|
||||
//tempByte = ad5934_sample_BL.bytes[2];
|
||||
//ad5934_sample_BL.bytes[2] = ad5934_sample_BL.bytes[3];
|
||||
//ad5934_sample_BL.bytes[3] = tempByte;
|
||||
//ad5934_sample_IL.number = ad5934_sample_BL.number;
|
||||
|
||||
|
||||
// ad5934_sample_IL.number = AD5934_Sweep();
|
||||
// intToStr((ad5934_sample_IL.ints[0]), real_text);
|
||||
// status0 = rs485_send_broadcast(real_text, strlen((char*)real_text));
|
||||
// status1 = rs485_send_broadcast(newline_real, strlen((char*)newline_real));
|
||||
// intToStr((ad5934_sample_IL.ints[1]), imag_text);
|
||||
//status0 = rs485_send_broadcast(imag_text, strlen((char*)imag_text));
|
||||
//status1 = rs485_send_broadcast(newline_imag, strlen((char*)newline_imag));
|
||||
|
||||
|
||||
|
||||
|
||||
//AD5934_GetImpedance(CH_EC);
|
||||
//FloatToString(tempString, ad5934_impedances_average[0]);
|
||||
//status0 = rs485_send_broadcast(tempString, strlen((char*)tempString));
|
||||
//status1 = rs485_send_broadcast(newline_imag, strlen((char*)newline_imag));
|
||||
|
||||
|
||||
//impedance_EC = AD5934_CalculateImpedance();
|
||||
//FloatToString(tempString, impedance_EC);
|
||||
//status0 = rs485_send_broadcast(tempString, strlen((char*)tempString));
|
||||
//status1 = rs485_send_broadcast(newline_imag, strlen((char*)newline_imag));
|
||||
|
||||
|
||||
|
||||
// Read ADC value
|
||||
int16_t adc_value = 2048 - ADSreadADC_Differential_0_1(&i2c);
|
||||
intToStr(adc_value, adc_text);
|
||||
|
||||
//
|
||||
status0 = rs485_send_broadcast(adc_text, strlen((char*)adc_text));
|
||||
status1 = rs485_send_broadcast(newline_ph, strlen((char*)newline_ph));
|
||||
status1 = rs485_send_broadcast(newline, strlen((char*)newline));
|
||||
|
||||
|
||||
}*/
|
||||
|
||||
|
||||
// Read ADC value
|
||||
// int16_t adc_value = 2048 - ADSreadADC_Differential_0_1(&i2c);
|
||||
// intToStr(adc_value, adc_text);
|
||||
|
||||
//
|
||||
// status0 = rs485_send_broadcast(adc_text, strlen((char*)adc_text));
|
||||
// status1 = rs485_send_broadcast(newline_ph, strlen((char*)newline_ph));
|
||||
// status1 = rs485_send_broadcast(newline, strlen((char*)newline));
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
|
||||
@@ -547,11 +485,12 @@ void intToStr(int N, char *str) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
|
||||
{
|
||||
UpdateEvent = 1;
|
||||
UpdateEvent = 1; // Internal TEmperature
|
||||
}
|
||||
*/
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
|
||||
@@ -55,7 +55,6 @@
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/* External variables --------------------------------------------------------*/
|
||||
extern DMA_HandleTypeDef hdma_adc1;
|
||||
extern UART_HandleTypeDef huart1;
|
||||
/* USER CODE BEGIN EV */
|
||||
|
||||
@@ -199,34 +198,6 @@ void SysTick_Handler(void)
|
||||
/* please refer to the startup file (startup_stm32f1xx.s). */
|
||||
/******************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief This function handles EXTI line3 interrupt.
|
||||
*/
|
||||
void EXTI3_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN EXTI3_IRQn 0 */
|
||||
|
||||
/* USER CODE END EXTI3_IRQn 0 */
|
||||
HAL_GPIO_EXTI_IRQHandler(Calib_PH_Pin);
|
||||
/* USER CODE BEGIN EXTI3_IRQn 1 */
|
||||
|
||||
/* USER CODE END EXTI3_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles DMA1 channel1 global interrupt.
|
||||
*/
|
||||
void DMA1_Channel1_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN DMA1_Channel1_IRQn 0 */
|
||||
|
||||
/* USER CODE END DMA1_Channel1_IRQn 0 */
|
||||
HAL_DMA_IRQHandler(&hdma_adc1);
|
||||
/* USER CODE BEGIN DMA1_Channel1_IRQn 1 */
|
||||
|
||||
/* USER CODE END DMA1_Channel1_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles USART1 global interrupt.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user