Fertirrega_v6 all scaled to 4096 counts

This commit is contained in:
2026-07-30 15:52:49 +01:00
parent c90139bd57
commit 6f39aaccb0
20 changed files with 11855 additions and 13746 deletions
+117 -149
View File
@@ -59,16 +59,6 @@
/* USER CODE BEGIN PV */
uint8_t rx_buffer[256]; /*!< Buffer for received data */
uint8_t tx_data[] = "Hello RS-485 Broadcast!"; /*!< Data to send */
uint8_t adc_text[6];
uint8_t real_text[6];
uint8_t imag_text[6];
uint8_t rs485_text[6];
uint8_t newline[]={'\r','\n'};
uint8_t doubleSpace[]={'_','_','\0'};
uint8_t newline_ph[]={'_','p','H','\n','\0'};
@@ -84,11 +74,13 @@ uint8_t tm_t[]={'T','M',':',' ','\0'};
uint8_t main_state = STATE_RUNNING_OK;
uint8_t rs485_address=0;
/*
uint16_t AD_RES[2];
uint8_t UpdateEvent = 0;
float Temperature, Temp_Sum, V_Sense, V_Ref;
float Temp_Samples[STM32_TEMPERATURE_AVERAGES]={0};
*/
/* USER CODE END PV */
@@ -112,87 +104,64 @@ void FloatToString(uint8_t * buf, double val);
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE BEGIN 1 */
HAL_StatusTypeDef status0,status1;
// Variables to store previous LED states
uint8_t previous_green_state = 0;
uint8_t previous_red_state = 0;
uint8_t mux_connection = 0;
// Variables for timing
uint32_t previous_millis_green = 0;
uint32_t previous_millis_red = 0;
uint32_t current_millis;
float temperature_RTD, admittance_EC, refResistance, update_value, ph7_interp, ph4_interp, ph_compensated;
/*! Temporary variables */
uint8_t tempString[15] = {0};
uint8_t i;
uint8_t mux_connection = 0;
/* USER CODE END 1 */
// Variables for timing
uint32_t previous_millis_green = 0;
uint32_t previous_millis_red = 0;
uint32_t current_millis;
/* MCU Configuration--------------------------------------------------------*/
//float temperature_RTD, admittance_EC, refResistance, update_value, ph7_interp, ph4_interp, ph_compensated;
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE END 1 */
/* USER CODE BEGIN Init */
/* MCU Configuration--------------------------------------------------------*/
/* USER CODE END Init */
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN Init */
/* USER CODE BEGIN SysInit */
/* USER CODE END Init */
/* USER CODE END SysInit */
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_ADC2_Init();
MX_I2C1_Init();
MX_I2C2_Init();
MX_USART1_UART_Init();
MX_TIM3_Init();
/* USER CODE BEGIN 2 */
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_ADC2_Init();
MX_I2C1_Init();
MX_I2C2_Init();
MX_USART1_UART_Init();
MX_TIM3_Init();
Flash_Load_Page(&flash_data);
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim3);
HAL_TIM_Base_Start_IT(&htim3);
/*
TempSensor_Init(&hadc1);
HAL_ADCEx_Calibration_Start(&hadc1);
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)AD_RES, 2); // Internal Temperature conversion
*/
//TempSensor_Init(&hadc1);
/*
HAL_ADCEx_Calibration_Start(&hadc1);
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)AD_RES, 2); // Internal Temperature conversion
*/
digital_outputs_init();
rs485_init();
ADS1015_Init(); // Initializes pH Measurement
AD5934_Init(); // Initializes CE and RTD Measurement
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET); // LED Green Off
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET); // LED Red Off
digital_outputs_init();
rs485_init();
ADS1015_Init(); // Initializes pH Measurement
AD5934_Init(); // Initializes CE and RTD Measurement
/* USER CODE END 2 */
@@ -279,18 +248,16 @@ int main(void)
{
AD5934_Process_System();
ADS1015_Process_System();
AD5934_Process_System();
ADS1015_Process_System();
current_millis = g_ms_counter;
current_millis = g_ms_counter;
if((current_millis % 100) == 0) // Send data each 50ms
{
if((current_millis % 100) == 0) // Send data each 50ms
{
if (g_ref_filter.value_valid) // Reference Resistor on Board: 100 Ohms or 1000 Ohms
{
if (g_ref_filter.value_valid) // Reference Resistor on Board: 100 Ohms or 1000 Ohms
{
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_7) == GPIO_PIN_SET) // OFF = PT1000, ON = PT100, PA7 has internal pull-up and switch connects to GND
mux_connection = AD5934_CH_REF100R_LOW_GAIN; // 100 Ohms Reference Resistor
else
@@ -299,28 +266,28 @@ int main(void)
uint16_t reference_final = AD5934_REF_OUT_SCALE_MAX - AD5934_Compress_To_IntScale(g_ref_filter.filtered_value, mux_connection);
intToStr(reference_final, tempString);
// float ref_final = g_ref_filter.filtered_value;
//FloatToString(tempString, ref_final);
rs485_send_broadcast(tempString, (strlen((uint8_t*)tempString)));
rs485_send_broadcast(newline, (strlen((uint8_t*)newline)));
}
//FloatToString(tempString, ref_final);
rs485_send_broadcast(tempString, (strlen((uint8_t*)tempString)));
rs485_send_broadcast(newline, (strlen((uint8_t*)newline)));
}
if (g_ec_filter.value_valid) // EC
{
if (g_ec_filter.value_valid) // EC
{
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6) == GPIO_PIN_SET) // Hardware Setup: Gain Selection (PA6)
mux_connection = AD5934_CH_EC_HIGH_GAIN;
mux_connection = AD5934_CH_EC_HIGH_GAIN; // 5mS
else
mux_connection = AD5934_CH_EC_MID_GAIN;
mux_connection = AD5934_CH_EC_MID_GAIN; // 10mS
uint16_t ec_final = AD5934_Compress_To_IntScale(g_ec_filter.filtered_value, mux_connection);
intToStr(ec_final, tempString);
/* float ec_final = g_ec_filter.filtered_value;
FloatToString(tempString, ec_final);*/
// float ec_final = g_ec_filter.filtered_value;
// FloatToString(tempString, ec_final);
rs485_send_broadcast(tempString, (strlen((uint8_t*)tempString)));
rs485_send_broadcast(newline, (strlen((uint8_t*)newline)));
}
}
if (g_rtd_filter.value_valid) // PT100 or PT1000 in °C
{
if (g_rtd_filter.value_valid) // PT100 or PT1000 in °C
{
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_7) == GPIO_PIN_SET) // OFF = PT1000, ON = PT100, PA7 has internal pull-up and switch connects to GND
mux_connection = AD5934_CH_RTD_LOW_GAIN; //PT100
else
@@ -328,41 +295,42 @@ int main(void)
uint16_t rtd_final = AD5934_RTD_OUT_SCALE_MAX - AD5934_Compress_To_IntScale(g_rtd_filter.filtered_value, mux_connection);
intToStr(rtd_final, tempString);
// float rtd_final = g_rtd_filter.filtered_value;
// FloatToString(tempString, rtd_final);
//float rtd_final = g_rtd_filter.filtered_value;
//FloatToString(tempString, rtd_final);
rs485_send_broadcast(tempString, (strlen((uint8_t*)tempString)));
rs485_send_broadcast(newline, (strlen((uint8_t*)newline)));
}
}
if (g_ph_filter.value_valid) // pH
{
float ph_final = g_ph_filter.filtered_value;
FloatToString(tempString, ph_final);
rs485_send_broadcast(tempString, (strlen((uint8_t*)tempString)));
rs485_send_broadcast(newline, (strlen((uint8_t*)newline)));
}
if (g_ph_filter.value_valid) // pH
{
uint16_t ph_final = ADS1015_Compress_To_IntScale(g_ph_filter.filtered_value);
intToStr(ph_final, tempString);
//float ph_final = g_ph_filter.filtered_value;
//FloatToString(tempString, ph_final);
rs485_send_broadcast(tempString, (strlen((uint8_t*)tempString)));
rs485_send_broadcast(newline, (strlen((uint8_t*)newline)));
}
rs485_send_broadcast(newline, (strlen((uint8_t*)newline)));
rs485_send_broadcast(newline, (strlen((uint8_t*)newline)));
}
}
// Piscar LED verde em PB4 a cada 1 segundo
if ((current_millis - previous_millis_green) >= 1000)
{
previous_millis_green = current_millis;
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_4);
// 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);
}
}
// Piscar LED vermelho em PB4 a cada 1 segundo
if ((current_millis - previous_millis_red) >= 1000)
/*
// Piscar LED vermelho em PB5 a cada 0,5 segundos
if ((current_millis - previous_millis_red) >= 500)
{
previous_millis_red = current_millis;
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_4);
}
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);
}*/
/*
if(UpdateEvent) // Internal Temperature
@@ -408,40 +376,40 @@ int main(void)
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV8;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV8;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */