488 lines
14 KiB
C
488 lines
14 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file : main.c
|
|
* @brief : Main program body
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2026 STMicroelectronics.
|
|
* All rights reserved.
|
|
*
|
|
* This software is licensed under terms that can be found in the LICENSE file
|
|
* in the root directory of this software component.
|
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "main.h"
|
|
#include "adc.h"
|
|
#include "i2c.h"
|
|
#include "usart.h"
|
|
#include "gpio.h"
|
|
|
|
/* Private includes ----------------------------------------------------------*/
|
|
/* USER CODE BEGIN Includes */
|
|
#include "ads1015_driver.h"
|
|
#include "digital_outputs_driver.h"
|
|
#include "ad5934_driver.h"
|
|
#include "rs485_driver.h"
|
|
|
|
#include <string.h>
|
|
/* USER CODE END Includes */
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
/* USER CODE BEGIN PTD */
|
|
|
|
/* USER CODE END PTD */
|
|
|
|
/* Private define ------------------------------------------------------------*/
|
|
/* USER CODE BEGIN PD */
|
|
|
|
|
|
/* USER CODE END PD */
|
|
|
|
/* Private macro -------------------------------------------------------------*/
|
|
/* USER CODE BEGIN PM */
|
|
|
|
/* USER CODE END PM */
|
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
|
/* 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[]={'_','_'};
|
|
uint8_t newline_ph[]={'_','p','H','\r','\n'};
|
|
uint8_t newline_admi[]={'_','m','S','\r','\n'};
|
|
uint8_t newline_real[]={'_','°','C','\r','\n'};
|
|
uint8_t newline_imag[]={'_','O','h','m','\r','\n'};
|
|
uint8_t newline_485[]={'_','a','d','\r','\n'};
|
|
uint8_t minus[]={'-',' '};
|
|
|
|
|
|
uint8_t rs485_address=0;
|
|
|
|
ADS1015_I2C i2c;
|
|
|
|
|
|
/* USER CODE END PV */
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
void SystemClock_Config(void);
|
|
/* USER CODE BEGIN PFP */
|
|
void intToStr(int N, char *str);
|
|
void FloatToString(char * buf, double val);
|
|
|
|
/* USER CODE END PFP */
|
|
|
|
/* Private user code ---------------------------------------------------------*/
|
|
/* USER CODE BEGIN 0 */
|
|
|
|
|
|
/* USER CODE END 0 */
|
|
|
|
/**
|
|
* @brief The application entry point.
|
|
* @retval int
|
|
*/
|
|
int main(void)
|
|
{
|
|
|
|
/* 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;
|
|
|
|
// Variables for timing
|
|
uint32_t previous_millis_green = 0;
|
|
uint32_t previous_millis_red = 0;
|
|
uint32_t current_millis;
|
|
|
|
float temperature_RTD, admittance_EC;
|
|
|
|
/*! Temporary variables */
|
|
char tempString[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
|
long double tempValue = 0;
|
|
|
|
/* 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 */
|
|
|
|
/* 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();
|
|
/* USER CODE BEGIN 2 */
|
|
|
|
digital_outputs_init();
|
|
|
|
// Initialize RS-485 driver
|
|
rs485_init();
|
|
|
|
ADS1015(&i2c, &hi2c1, ADS_ADDR_GND);
|
|
ADSsetGain(&i2c, GAIN_SIXTEEN);
|
|
|
|
// Start CE and RTD Measurement
|
|
AD5934_Init();
|
|
|
|
|
|
/* USER CODE END 2 */
|
|
|
|
/* Infinite loop */
|
|
/* USER CODE BEGIN WHILE */
|
|
while (1)
|
|
{
|
|
/* 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) >= 800)
|
|
{
|
|
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_real, strlen((char*)newline_real));
|
|
|
|
|
|
if(edge_state == FALLING_EDGE)
|
|
{
|
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
|
|
temp_calib = temperature_RTD;
|
|
v_ph4 = ADSCalculate_ph_Volts(2048 - ADSreadADC_Differential_0_1(&i2c));
|
|
edge_state = NO_EDGE;
|
|
HAL_Delay(500);
|
|
}else if(edge_state == RISING_EDGE)
|
|
{
|
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);
|
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
|
|
v_ph7 = ADSCalculate_ph_Volts(2048 - ADSreadADC_Differential_0_1(&i2c));
|
|
edge_state = NO_EDGE;
|
|
HAL_Delay(500);
|
|
}
|
|
|
|
ph_compensated = ADSCalculate_ph_Compensated((2048 - ADSreadADC_Differential_0_1(&i2c)),temp_calib);
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 */
|
|
}
|
|
|
|
/**
|
|
* @brief System Clock Configuration
|
|
* @retval None
|
|
*/
|
|
void SystemClock_Config(void)
|
|
{
|
|
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.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
|
|
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
|
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
|
|
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_PLLCLK;
|
|
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
|
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
|
|
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
|
|
|
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 */
|
|
|
|
|
|
|
|
|
|
/***************************************************************************//**
|
|
* @brief Converts a float value to a character array with 3 digits of accuracy.
|
|
*
|
|
* @param *buf - returns the converterd value
|
|
* @param val - value to be converted
|
|
*
|
|
* @return None.
|
|
*******************************************************************************/
|
|
void FloatToString(char * buf, double val)
|
|
{
|
|
long intPart = 0;
|
|
short fracPart = 0;
|
|
short charPos = 0;
|
|
char localBuf[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
|
short i = sizeof(localBuf) - 1;
|
|
|
|
intPart = (long)val;
|
|
fracPart = (short)((val - intPart) * 1000 + 0.5);
|
|
while(i > sizeof(localBuf) - 4)
|
|
{
|
|
localBuf[i] = (fracPart % 10) + 0x30;
|
|
fracPart /= 10;
|
|
i--;
|
|
}
|
|
localBuf[i] = '.';
|
|
if(intPart == 0)
|
|
{
|
|
i --;
|
|
localBuf[i] = '0';
|
|
}
|
|
while(intPart)
|
|
{
|
|
i --;
|
|
localBuf[i] =(intPart % 10) + 0x30;
|
|
intPart /= 10;
|
|
}
|
|
for(charPos = i; charPos < sizeof(localBuf); charPos ++)
|
|
{
|
|
*buf = localBuf[charPos];
|
|
buf ++;
|
|
}
|
|
*buf = 0;
|
|
}
|
|
|
|
|
|
void intToStr(int N, char *str) {
|
|
int i = 0;
|
|
|
|
// Save the copy of the number for sign
|
|
int sign = N;
|
|
|
|
// If the number is negative, make it positive
|
|
if (N < 0)
|
|
N = -N;
|
|
|
|
// Extract digits from the number and add them to the
|
|
// string
|
|
while (N > 0) {
|
|
|
|
// Convert integer digit to character and store
|
|
// it in the str
|
|
str[i++] = N % 10 + '0';
|
|
N /= 10;
|
|
}
|
|
|
|
// If the number was negative, add a minus sign to the
|
|
// string
|
|
if (sign < 0) {
|
|
str[i++] = '-';
|
|
}
|
|
|
|
// Null-terminate the string
|
|
str[i] = '\0';
|
|
|
|
// Reverse the string to get the correct order
|
|
for (int j = 0, k = i - 1; j < k; j++, k--) {
|
|
char temp = str[j];
|
|
str[j] = str[k];
|
|
str[k] = temp;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* USER CODE END 4 */
|
|
|
|
/**
|
|
* @brief This function is executed in case of error occurrence.
|
|
* @retval None
|
|
*/
|
|
void Error_Handler(void)
|
|
{
|
|
/* USER CODE BEGIN Error_Handler_Debug */
|
|
/* User can add his own implementation to report the HAL error return state */
|
|
__disable_irq();
|
|
while (1)
|
|
{
|
|
}
|
|
/* USER CODE END Error_Handler_Debug */
|
|
}
|
|
#ifdef USE_FULL_ASSERT
|
|
/**
|
|
* @brief Reports the name of the source file and the source line number
|
|
* where the assert_param error has occurred.
|
|
* @param file: pointer to the source file name
|
|
* @param line: assert_param error line source number
|
|
* @retval None
|
|
*/
|
|
void assert_failed(uint8_t *file, uint32_t line)
|
|
{
|
|
/* USER CODE BEGIN 6 */
|
|
/* User can add his own implementation to report the file name and line number,
|
|
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
|
/* USER CODE END 6 */
|
|
}
|
|
#endif /* USE_FULL_ASSERT */
|