I am trying to connect my STM32F072RBT6 module to pc and send data through USART. I'm pretty new to this and I dont understand few things. Unfornately there are no examples available for this module. I use usb cable to connect stm with pc. Here is my code:
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStructure;
void send_char(char c)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, c);
}
void send_string(const char* s)
{
while (*s)
send_char(*s++);
}
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStructure;
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
//GPIO_PinAFConfig(GPIOA, GPIO_PinSource2,GPIO_AF_1); //tried this too
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
//GPIO_PinAFConfig(GPIOA, GPIO_PinSource3,GPIO_AF_1);
I read that Rx pin needs to be configured as floating input, but there is no mode like this to set. I also tried using GPIO_Mode_AF and commented PinAFConfig.
GPIO_Init(GPIOA, &GPIO_InitStruct);
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
while (1) {
send_string("Hello world!\r\n");
}
}
I am trying to see any results in teraterm. I noticed that when i accidentially touched pins od board, some characters appeared on terminal. Is that a sign that connection works but i have configured something wrong? I also tried to do this with USART1 and it's pins but nothing happened. Can You
e.g. in STM32CubeF0 under Projects/STM32F072RB-Nucleo/Examples/UART
Though they use USART1, so you have to adapt them a bit.
then you could simply generate one using STM32CubeMX. Start a project with your MCU, click on the triangle next to USART2 (on the left), select Mode Asynchronous, then use Project / Generate Code, and there it is.