Fertirrega_v6 AD5934 Bug Fix
This commit is contained in:
+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 */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user