Fertirrega v6 with internal temperature (but slow)
This commit is contained in:
+168
-68
@@ -19,7 +19,9 @@
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
#include "adc.h"
|
||||
#include "dma.h"
|
||||
#include "i2c.h"
|
||||
#include "tim.h"
|
||||
#include "usart.h"
|
||||
#include "gpio.h"
|
||||
|
||||
@@ -29,6 +31,8 @@
|
||||
#include "digital_outputs_driver.h"
|
||||
#include "ad5934_driver.h"
|
||||
#include "rs485_driver.h"
|
||||
#include "flash_manager.h"
|
||||
|
||||
|
||||
#include <string.h>
|
||||
/* USER CODE END Includes */
|
||||
@@ -40,8 +44,10 @@
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PD */
|
||||
|
||||
|
||||
#define AVG_SLOPE (4.3F)
|
||||
#define V_AT_25C (1.43F)
|
||||
#define V_REF_INT (1.2F)
|
||||
#define STM32_TEMPERATURE_AVERAGES 4
|
||||
/* USER CODE END PD */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
@@ -62,20 +68,25 @@ uint8_t imag_text[6];
|
||||
uint8_t rs485_text[6];
|
||||
|
||||
|
||||
uint8_t newline[]={'\r','\n'};
|
||||
uint8_t newline[]={'\n','\0'};
|
||||
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 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[]={'-',' '};
|
||||
|
||||
|
||||
uint8_t main_state = STATE_RUNNING_OK;
|
||||
uint8_t rs485_address=0;
|
||||
|
||||
ADS1015_I2C i2c;
|
||||
|
||||
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 */
|
||||
|
||||
@@ -113,11 +124,15 @@ int main(void)
|
||||
uint32_t previous_millis_red = 0;
|
||||
uint32_t current_millis;
|
||||
|
||||
float temperature_RTD, admittance_EC;
|
||||
float temperature_RTD, admittance_EC, ph_compensated;
|
||||
|
||||
|
||||
|
||||
|
||||
/*! Temporary variables */
|
||||
char tempString[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
long double tempValue = 0;
|
||||
uint8_t tempString[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
uint8_t i;
|
||||
|
||||
/* USER CODE END 1 */
|
||||
|
||||
@@ -139,13 +154,24 @@ 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
|
||||
|
||||
digital_outputs_init();
|
||||
|
||||
// Initialize RS-485 driver
|
||||
@@ -158,20 +184,59 @@ int main(void)
|
||||
AD5934_Init();
|
||||
|
||||
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
|
||||
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3) == GPIO_PIN_RESET)
|
||||
{
|
||||
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++)
|
||||
{
|
||||
flash_data.temperature_value = temperature_RTD;
|
||||
flash_data.ph4_value = ADSCalculate_ph_Volts();
|
||||
flash_data.ec1413_value = AD5934_GetImpedance();
|
||||
}
|
||||
|
||||
|
||||
while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3) == GPIO_PIN_RESET);
|
||||
HAL_Delay(500);
|
||||
|
||||
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++)
|
||||
{
|
||||
flash_data.ph7_value = ADSCalculate_ph_Volts();
|
||||
flash_data.ec12880_value = AD5934_GetImpedance();
|
||||
Flash_Save_Page(&flash_data);
|
||||
}
|
||||
|
||||
HAL_Delay(500);
|
||||
|
||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET); // Green LED On
|
||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET); // Red LED On
|
||||
|
||||
main_state = STATE_RUNNING_OK;
|
||||
}
|
||||
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)
|
||||
if ((current_millis - previous_millis_green) >= 500)
|
||||
{
|
||||
previous_millis_green = current_millis;
|
||||
|
||||
@@ -186,6 +251,10 @@ int main(void)
|
||||
|
||||
}*/
|
||||
|
||||
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);
|
||||
@@ -193,24 +262,8 @@ int main(void)
|
||||
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);
|
||||
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));
|
||||
@@ -222,6 +275,28 @@ int main(void)
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -333,11 +408,7 @@ void SystemClock_Config(void)
|
||||
*/
|
||||
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;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
||||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
@@ -347,10 +418,10 @@ void SystemClock_Config(void)
|
||||
*/
|
||||
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
||||
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
|
||||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
||||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
|
||||
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
||||
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||||
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
||||
{
|
||||
@@ -377,43 +448,68 @@ void SystemClock_Config(void)
|
||||
*
|
||||
* @return None.
|
||||
*******************************************************************************/
|
||||
void FloatToString(char * buf, double val)
|
||||
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;
|
||||
char temp[20]; // Buffer auxiliar para construção segura
|
||||
int i = 0;
|
||||
|
||||
intPart = (long)val;
|
||||
fracPart = (short)((val - intPart) * 1000 + 0.5);
|
||||
while(i > sizeof(localBuf) - 4)
|
||||
{
|
||||
localBuf[i] = (fracPart % 10) + 0x30;
|
||||
fracPart /= 10;
|
||||
i--;
|
||||
// 1. Tratar sinal negativo
|
||||
if (val < 0) {
|
||||
temp[i++] = '-';
|
||||
val = -val;
|
||||
}
|
||||
localBuf[i] = '.';
|
||||
if(intPart == 0)
|
||||
{
|
||||
i --;
|
||||
localBuf[i] = '0';
|
||||
|
||||
// 2. Separar parte inteira e fracionária
|
||||
long intPart = (long)val;
|
||||
// Multiplicamos por 1000 para obter 3 casas decimais fixas
|
||||
int fracPart = (int)((val - (double)intPart) * 1000.0 + 0.5);
|
||||
|
||||
// 3. Converter a parte inteira para o buffer temp
|
||||
// Usamos um buffer temporário de inversão para não precisar de lógica complexa de ponteiro
|
||||
char intRev[12];
|
||||
int j = 0;
|
||||
|
||||
if (intPart == 0) {
|
||||
intRev[j++] = '0';
|
||||
} else {
|
||||
while (intPart > 0) {
|
||||
intRev[j++] = (intPart % 10) + '0';
|
||||
intPart /= 10;
|
||||
}
|
||||
}
|
||||
while(intPart)
|
||||
{
|
||||
i --;
|
||||
localBuf[i] =(intPart % 10) + 0x30;
|
||||
intPart /= 10;
|
||||
|
||||
// Inverter a parte inteira de volta para o buffer principal
|
||||
for (int k = j - 1; k >= 0; k--) {
|
||||
temp[i++] = intRev[k];
|
||||
}
|
||||
for(charPos = i; charPos < sizeof(localBuf); charPos ++)
|
||||
{
|
||||
*buf = localBuf[charPos];
|
||||
buf ++;
|
||||
|
||||
// 4. Adicionar o ponto decimal e a parte fracionária (sempre 3 casas)
|
||||
temp[i++] = '.';
|
||||
|
||||
// Garantir que a parte fracionária tenha sempre 3 dígitos (ex: .005 em vez de .5)
|
||||
int fracBuffer[3];
|
||||
fracBuffer[2] = fracPart % 10; // Unidade
|
||||
fracBuffer[1] = (fracPart / 10) % 10; // Dezena
|
||||
fracBuffer[0] = (fracPart / 100) % 10; // Centena
|
||||
|
||||
for (int k = 2; k >= 0; k--) {
|
||||
temp[i++] = fracBuffer[k] + '0';
|
||||
}
|
||||
*buf = 0;
|
||||
|
||||
// 5. Finalizar a string com o caractere nulo
|
||||
temp[i] = '\0';
|
||||
|
||||
// 6. Copiar para o buffer de destino final (sem risco de lixo)
|
||||
int destIdx = 0;
|
||||
while (temp[destIdx] != '\0' && destIdx < 15) { // Limite de segurança
|
||||
buf[destIdx] = temp[destIdx];
|
||||
destIdx++;
|
||||
}
|
||||
buf[destIdx] = '\0';
|
||||
}
|
||||
|
||||
|
||||
|
||||
void intToStr(int N, char *str) {
|
||||
int i = 0;
|
||||
|
||||
@@ -452,6 +548,10 @@ void intToStr(int N, char *str) {
|
||||
}
|
||||
|
||||
|
||||
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
|
||||
{
|
||||
UpdateEvent = 1;
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user