2025-10-17 6:52 PM - edited 2025-10-18 3:43 PM
#include <stdint.h>
#include <stdio.h>
#include "stm32f4xx.h"
volatile uint8_t rxdata = 0;
int main(void)
{
SystemInit();
SystemCoreClockUpdate();
// clock configutation
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // TX: PB6 RX: PB7
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); // LED LED4: PD12 LED3: PD13
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
// gpio initialization
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);
// usart initialization
USART_InitTypeDef USART_InitStruct;
USART_StructInit(&USART_InitStruct);
USART_InitStruct.USART_BaudRate = 115200;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &USART_InitStruct);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE); // enable usart1
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
char msg[64];
while(1){
USART_SendData(USART1, 'A');
if (rxdata!= 0) {
int idx = 0;
int len = snprintf(msg, sizeof(msg), "Recieved: %c", (char)rxdata);
for (int idx = 0; idx < len; idx++) {
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, (uint16_t)msg[idx]);
}
rxdata = 0;
}
}
}
void USART1_IRQHandler(void) {
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
rxdata = (uint8_t)USART_ReceiveData(USART1); // echo
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
Hardware Setup: STM32F407 Discovery Board connected with ttl. Developing in Keil. USART uses PB6 and PB7 as the default PA9 is used as VBUS in USB and prints random characters.
The code is expected to keep printing 'A' until user inputs; but the serial monitor does not show anything until I turn on the debug mode.
Edited to apply source code formatting - please see How to insert source code for future reference.
Solved! Go to Solution.
2025-10-18 3:44 PM
Have done so!