Fertirrega_v6 Scaled to 4096
This commit is contained in:
+113
-105
@@ -69,7 +69,7 @@ uint8_t imag_text[6];
|
||||
uint8_t rs485_text[6];
|
||||
|
||||
|
||||
uint8_t newline[]={'\n','\0'};
|
||||
uint8_t newline[]={'\r','\n'};
|
||||
uint8_t doubleSpace[]={'_','_','\0'};
|
||||
uint8_t newline_ph[]={'_','p','H','\n','\0'};
|
||||
uint8_t newline_admi[]={'_','u','S','\n','\0'};
|
||||
@@ -95,8 +95,8 @@ float Temp_Samples[STM32_TEMPERATURE_AVERAGES]={0};
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
void SystemClock_Config(void);
|
||||
/* USER CODE BEGIN PFP */
|
||||
void intToStr(int N, char *str);
|
||||
void FloatToString(char * buf, double val);
|
||||
void intToStr(int16_t N, uint8_t *str);
|
||||
void FloatToString(uint8_t * buf, double val);
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
@@ -121,6 +121,8 @@ int main(void)
|
||||
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;
|
||||
@@ -132,7 +134,7 @@ int main(void)
|
||||
|
||||
|
||||
/*! Temporary variables */
|
||||
uint8_t tempString[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
uint8_t tempString[15] = {0};
|
||||
|
||||
uint8_t i;
|
||||
|
||||
@@ -182,20 +184,13 @@ int main(void)
|
||||
|
||||
digital_outputs_init();
|
||||
|
||||
// Initialize RS-485 driver
|
||||
|
||||
rs485_init();
|
||||
|
||||
|
||||
ADS1015(&i2c_ads1015, &hi2c1, ADS1015_ADDR_GND);
|
||||
//ADSsetGain(&i2c_ads1015, GAIN_FOUR);
|
||||
ADS1015_Init(); // Initializes pH Measurement
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
ADG715_ResetChannels();
|
||||
// Start CE and RTD Measurement
|
||||
AD5934_Init();
|
||||
AD5934_Init(); // Initializes CE and RTD Measurement
|
||||
|
||||
|
||||
|
||||
@@ -290,33 +285,74 @@ int main(void)
|
||||
current_millis = g_ms_counter;
|
||||
|
||||
|
||||
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(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
|
||||
mux_connection = AD5934_CH_REF1K_MID_GAIN; // 1K Reference Resistor
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
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;
|
||||
else
|
||||
mux_connection = AD5934_CH_EC_MID_GAIN;
|
||||
|
||||
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);*/
|
||||
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(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
|
||||
mux_connection = AD5934_CH_RTD_MID_GAIN; //PT1000
|
||||
|
||||
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);
|
||||
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)));
|
||||
}
|
||||
|
||||
rs485_send_broadcast(newline, (strlen((uint8_t*)newline)));
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 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
|
||||
|
||||
}
|
||||
/*
|
||||
status1 = rs485_send_broadcast(ph_t, strlen((char*)ph_t));
|
||||
|
||||
|
||||
ph_compensated = ADSCalculate_ph_mV() ;
|
||||
|
||||
|
||||
FloatToString(tempString, ph_compensated);
|
||||
status0 = rs485_send_broadcast(tempString, (strlen((char*)tempString)-1));
|
||||
status1 = rs485_send_broadcast(newline_ph, strlen((char*)newline_ph));
|
||||
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
// Piscar LED vermelho em PB4 a cada 1 segundo
|
||||
@@ -325,54 +361,6 @@ int main(void)
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (g_rtd_filter.value_valid)
|
||||
{
|
||||
float rtd_final = g_rtd_filter.filtered_value;
|
||||
|
||||
status1 = rs485_send_broadcast(tm_t, strlen((char*)tm_t));
|
||||
//temperature_RTD = AD5934_GetTemperature(refResistance);
|
||||
FloatToString(tempString, rtd_final);
|
||||
status0 = rs485_send_broadcast(tempString, (strlen((char*)tempString)-1));
|
||||
status1 = rs485_send_broadcast(newline_temp, strlen((char*)newline_temp));
|
||||
}
|
||||
|
||||
if (g_ec_filter.value_valid)
|
||||
{
|
||||
float ec_final = g_ec_filter.filtered_value;
|
||||
status1 = rs485_send_broadcast(ad_t, strlen((char*)ad_t));
|
||||
//admittance_EC = AD5934_GetImpedance(temperature_RTD);
|
||||
FloatToString(tempString, ec_final);
|
||||
status0 = rs485_send_broadcast(tempString, (strlen((char*)tempString)-1));
|
||||
status1 = rs485_send_broadcast(newline_admi, strlen((char*)newline_admi));
|
||||
}
|
||||
|
||||
if (g_ref_filter.value_valid)
|
||||
{
|
||||
//float reference_final = g_ref_filter.filtered_value;
|
||||
status1 = rs485_send_broadcast(ph_t, strlen((char*)ph_t));
|
||||
ph_compensated = g_ph_filter.filtered_value;
|
||||
FloatToString(tempString, ph_compensated);
|
||||
|
||||
status0 = rs485_send_broadcast(tempString, (strlen((char*)tempString)-1));
|
||||
status1 = rs485_send_broadcast(newline_ph, strlen((char*)newline_ph));
|
||||
|
||||
status1 = rs485_send_broadcast(newline, strlen((char*)newline));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -469,7 +457,7 @@ void SystemClock_Config(void)
|
||||
*
|
||||
* @return None.
|
||||
*******************************************************************************/
|
||||
void FloatToString(char *buf, double val)
|
||||
void FloatToString(uint8_t *buf, double val)
|
||||
{
|
||||
char temp[20]; // Buffer auxiliar para construção segura
|
||||
int i = 0;
|
||||
@@ -531,38 +519,58 @@ void FloatToString(char *buf, double val)
|
||||
|
||||
|
||||
|
||||
void intToStr(int N, char *str) {
|
||||
int i = 0;
|
||||
void intToStr(int16_t N, uint8_t *str)
|
||||
{
|
||||
int16_t i = 0;
|
||||
int16_t sign = N;
|
||||
uint8_t max_len = 15; // Tamanho máximo do buffer
|
||||
uint8_t width = 4; // mínimo 4 caracteres
|
||||
|
||||
// Save the copy of the number for sign
|
||||
int sign = N;
|
||||
// Trata o caso do número zero isoladamente
|
||||
if (N == 0)
|
||||
{
|
||||
// Preenche com zeros à esquerda até atingir a largura desejada
|
||||
while (width > 1 && i < (max_len - 1))
|
||||
{
|
||||
str[i++] = '0';
|
||||
width--;
|
||||
}
|
||||
str[i++] = '0';
|
||||
str[i] = '\0';
|
||||
return;
|
||||
}
|
||||
|
||||
// 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;
|
||||
// Extração dos dígitos
|
||||
while (N > 0)
|
||||
{
|
||||
if (i >= (max_len - 1))
|
||||
break; // Proteção de estouro
|
||||
str[i++] = (N % 10) + '0';
|
||||
N /= 10;
|
||||
}
|
||||
|
||||
// If the number was negative, add a minus sign to the
|
||||
// string
|
||||
if (sign < 0) {
|
||||
str[i++] = '-';
|
||||
// Adiciona o sinal de menos se necessário
|
||||
if (sign < 0)
|
||||
{
|
||||
if (i < (max_len - 1))
|
||||
str[i++] = '-';
|
||||
}
|
||||
|
||||
// Preenche com zeros à esquerda (considerando o espaço ocupado pelos dígitos e sinal)
|
||||
while (i < width && i < (max_len - 1))
|
||||
{
|
||||
str[i++] = '0';
|
||||
}
|
||||
|
||||
// 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];
|
||||
// Inverte a string para colocar na ordem correta
|
||||
for (uint8_t j = 0, k = i - 1; j < k; j++, k--)
|
||||
{
|
||||
uint8_t temp = str[j];
|
||||
str[j] = str[k];
|
||||
str[k] = temp;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user