Skip to main content
Associate
August 29, 2023
Question

Microcontroller is not sending data over serial line, but does appear to be collecting it

 

Is there something wrong with my code/settings? I tried using putty's terminal to view the output; however, even though I configured it correctly with the baud settings, etc., no data is appearing.
It is on a NUCLEO-F767ZI Microprocessor. I am using STM32CubeIDE.
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include "stm32f7xx_hal.h"
TIM_HandleTypeDef htim;
GPIO_InitTypeDef GPIO_InitStruct;
ADC_HandleTypeDef hadc;
#define PWM_PIN GPIO_PIN_0
#define PWM_PORT GPIOA
#define ADC_PIN GPIO_PIN_1
#define ADC_PORT GPIOA
void Error_Handler(void) {return;}
void SystemClock_Config(void) {return;}
void init_gpio(void) {
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = PWM_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(PWM_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = ADC_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(ADC_PORT, &GPIO_InitStruct);
}
void init_timer(void) {
__HAL_RCC_TIM2_CLK_ENABLE();
htim.Instance = TIM2;
htim.Init.Prescaler = 0;
htim.Init.CounterMode = TIM_COUNTERMODE_UP;
htim.Init.Period = SystemCoreClock / 5000000;
htim.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_PWM_Init(&htim);
TIM_OC_InitTypeDef sConfig;
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.Pulse = htim.Init.Period / 2;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim, &sConfig, TIM_CHANNEL_1);
}
void init_adc(void) {
__HAL_RCC_ADC1_CLK_ENABLE();
hadc.Instance = ADC1;
hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc.Init.Resolution = ADC_RESOLUTION_12B;
hadc.Init.ScanConvMode = DISABLE;
hadc.Init.ContinuousConvMode = DISABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.NbrOfDiscConversion = 0;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.NbrOfConversion = 1;
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
HAL_ADC_Init(&hadc);
ADC_ChannelConfTypeDef sConfig;
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
HAL_ADC_ConfigChannel(&hadc, &sConfig);
}
int main(void) {
HAL_Init();
init_gpio();
init_timer();
init_adc();
uint32_t start_time = 0;
float input_voltage = 0.0f;
HAL_TIM_PWM_Start(&htim, TIM_CHANNEL_1);
printf("Hello World\n");
fflush(stdout);
start_time = HAL_GetTick(); // Start time for capturing voltage data
while (HAL_GetTick() - start_time < 5000000) // Capture data for 5 seconds
{
HAL_ADC_Start(&hadc);
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);
fflush(stdout);
HAL_Delay(1); // Delay between voltage measurements
}
HAL_TIM_PWM_Stop(&htim, TIM_CHANNEL_1);
while (1) {
// Do nothing
}
}

    This topic has been closed for replies.

    1 reply

    Tesla DeLorean
    Guru
    August 29, 2023

    Thread then went here  https://community.st.com/t5/stm32cubeide-mcu/microcontroller-is-not-sending-data-over-serial-line-but-does/td-p/586115

    Please in the future, edit your thread if you need to fix content or formatting.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..