提问人:Annie 提问时间:8/29/2023 最后编辑:Annie 更新时间:8/30/2023 访问量:40
微控制器没有通过串行线路发送数据,但似乎正在收集数据
Microcontroller is not sending data over serial line, but does appear to be collecting it
问:
我的代码/设置有问题吗?我尝试使用 putty 的终端来查看输出;但是,即使我使用波特率设置等正确配置了它,也没有出现任何数据。不过,我能够在终端中看到“hello world”。 它位于 NUCLEO-F767ZI 微处理器上。我正在使用STM32CubeIDE。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include "stm32f7xx_hal.h"
/* Define your 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 your 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
}
}
答:
0赞
Greg The Packrat
8/29/2023
#1
我不熟悉这一系列的控制器,但看起来您正在使用 printf 和 fflush 将输出发送到 stdout;如何仔细检查串行通道是否映射到 STDOUT?
评论
1赞
Annie
8/30/2023
我现在能够在同样使用 printf 的终端中看到“hello world”,但我看不到我需要的任何数据。
评论
5000000
滴答不是 5 秒,而是 5000 秒。