I am beginner in embedded systems. I am trying to write data on UART2 of STM32F103C8 (i. e., the Blue Pill board) and want to see the data in one of the ports of my computer using an FTDI adapter, which is connected to UART2 of the STM32F103C8 board. But on my console I receive some random square block instead of the character which I want to transmit.
Here is my code written in Keil IDE.
#include "stm32f10x.h" // Device header
void usart2_init(void);
void USART_write(int data);
void delayMs(int delay);
int main(void)
{
usart2_init();
while(1)
{
USART_write('A');
delayMs(5000);
}
}
void usart2_init(void)
{
// Enable clock source for USART2
RCC->APB1ENR |= 0x20000; // 0b 0000 0000 0000 0010 0000 0000 0000 0000
RCC->APB2ENR |= 0x4;
GPIOA->CRL |= 0x900; // Set PA2 as TX pin (AF)
USART2->BRR = 0x341; // Setting Baudrate to 9600 @8 MHz.
USART2->CR1 |= 0x00008; // Enable TX only
USART2->CR1 |= 0x02000; // Enable USART module by setting bit 13 to 1 i
USART->CR1 register
}
void USART_write(int data)
{
// We need to wait until Tx buffer is empty for sending data.
while(!(USART2->SR & 0x0080)); // 0x0080
USART2->DR = (data & 0xFF);
}
void delayMs(int delay)
{
int i;
for( ; delay>0 ; delay--)
{
for(i=0; i<3195; i++)
{
}
}
}
Below I attached the screenshot while debugging.
You can see the unwanted square block instead of character instead of the characters I want to transmit. In the image you can also see the UART registers and their values. I am using ST-LINK2 to upload the firmware.
Am I missing some information or doing some mistake while dealing with FTDI and Tera Term? This is my Tera Term configuration:
- Baud rate = 9600
- Data = 8 bit
- Parity = none
- Stop bit = 1
- Control flow = none
How can I fix this?