2023-08-29 01:21 PM
I added in another printf statement just to make sure it was working. In the putty terminal I was able to see "Hello world", but not the actual data I needed to see: printf("Time: %lu ms, Input Voltage: %.2f V\n", HAL_GetTick() - start_time, input_voltage); I believe the microcontroller is not sending data over serial line, but does appear to be collecting it, however there could be another issue. I am using a NUCLEO-F767ZI microcontroller, and using STM32CubeIDE.
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include "stm32f7xx_hal.h"
/* Define UART handle */
UART_HandleTypeDef huart3;
ADC_HandleTypeDef hadc; // Declare 'hadc' here
void Error_Handler(void) { while (1); }
void SystemClock_Config(void) { /* Configure system clock here */ }
/* Initialize UART */
void init_uart(void) {
huart3.Instance = USART3;
huart3.Init.BaudRate = 9600; // Baud rate: 9600 bps
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1; // Stop bits: 1
huart3.Init.Parity = UART_PARITY_NONE; // Parity: None
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&huart3);
}
/* Custom implementation of _write for printf redirection */
int _write(int file, char *ptr, int len) {
HAL_UART_Transmit(&huart3, (uint8_t*)ptr, len, HAL_MAX_DELAY);
return len;
}
void init_gpio(void) {
// GPIO initialization code
}
void init_timer(void) {
// Timer initialization code
}
void init_adc(void) {
// ADC initialization code
}
int main(void) {
HAL_Init();
SystemClock_Config();
init_uart();
init_gpio();
init_timer();
init_adc();
uint32_t start_time = 0;
float input_voltage = 0.0f;
while (1) {
printf("Hello World\n");
start_time = HAL_GetTick(); // Start time for capturing voltage data
while (HAL_GetTick() - start_time < 5000000) // Capture data for 5 seconds
{
// Read data from ADC and process if needed
// (similar to previous ADC reading code)
// Print voltage and time
HAL_ADC_Start(&hadc); // Declare 'hadc' here
HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
uint32_t adc_value = HAL_ADC_GetValue(&hadc);
input_voltage = (float)adc_value / 4095.0f * 3.3f;
HAL_ADC_Stop(&hadc);
printf("Time: %lu ms, Input Voltage: %.2f V\n", HAL_GetTick() - start_time, input_voltage);
// No fflush(stdout) here
HAL_Delay(1); // Delay between voltage measurements
}
printf("Measurement done\n"); // Output to indicate end of measurement
}
}
Solved! Go to Solution.
2023-08-29 01:39 PM
> ADC_HandleTypeDef hadc;
You never initialize this. No doubt the first ADC call fails. Look at ADC examples. Cube generates this code for you, how did it get lost?
Step through, monitor return value from HAL functions to verify success or failure.
2023-08-29 01:39 PM
> ADC_HandleTypeDef hadc;
You never initialize this. No doubt the first ADC call fails. Look at ADC examples. Cube generates this code for you, how did it get lost?
Step through, monitor return value from HAL functions to verify success or failure.