cancel
Showing results for 
Search instead for 
Did you mean: 

UART STM32F030

sky_brake
Associate II
Posted on February 07, 2014 at 05:07

hello all,

I want to try the UART of STM32F030 using USART1 (pinA9 Tx, pinA10 Rx). I have try to modify some code from internet to make mikro send 'A'(0100 0001)via Tx. but when I connect Tx pin (PA9) to osciloscope, output signal is just flat 1 volt. I expecting pulse signal(0100 0001). Pleasehelp me find wrong part from the code. this is my code

#include ''stm32f0xx_gpio.h''
#include ''stm32f0xx_rcc.h''
#include ''stm32f0xx.h''
#include ''stm32f0xx_usart.h''
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructur;
USART_ClockInitTypeDef USART_ClockInitStructur;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
USART_ClockInitStructur.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructur.USART_CPOL = USART_CPOL_High;
USART_ClockInitStructur.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructur.USART_LastBit = USART_LastBit_Enable;
USART_ClockInit(USART1, &USART_ClockInitStructur);
USART_InitStructur.USART_BaudRate = 9600;
USART_InitStructur.USART_WordLength = USART_WordLength_8b;
USART_InitStructur.USART_StopBits = USART_StopBits_1;
USART_InitStructur.USART_Parity = USART_Parity_No;
USART_InitStructur.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructur.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructur);
USART_Cmd(USART1,ENABLE);
}
int main(void)
{
GPIO_Configuration();
USART_Configuration();
while(1)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'A');
}
}

thanks for your help. #uart #stm32f030 #usart
3 REPLIES 3
chen
Associate II
Posted on February 07, 2014 at 18:39

Hi

Your set up code looks OK but I am not that familiar with a) the USART and b)Standard Peripheral Library. The one thing I did spot :

while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);

If this is true - then the ''A'' will never be sent. Try debugging it and see if the sending code ever runs.
sky_brake
Associate II
Posted on February 10, 2014 at 03:07

thanks for your respon sung.chen_chung.

I don't get it. 

while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);

why this line make the ''A'' will never be sent?

Posted on February 10, 2014 at 03:46

#include ''stm32f0xx.h''
#include ''stm32f0xx_gpio.h''
#include ''stm32f0xx_rcc.h''
#include ''stm32f0xx_usart.h''
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1); // USART1 TX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
int main(void)
{
GPIO_Configuration();
USART_Configuration();
while(1)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'A');
}
}

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