cancel
Showing results for 
Search instead for 
Did you mean: 

USART STM32f1xx discovery

malahsalah2
Associate
Posted on July 22, 2012 at 17:00

Hello everybody :)

I'm a beginner in stm32 discovery programming & I want to use usart but I haven't found any example using this peripheral please can you help me

  

#usart #stm32f1xx
6 REPLIES 6
Posted on July 22, 2012 at 20:03

// STM32 USART1 (Tx PA.9, Rx PA.10) - STM32 VLDiscovery - sourcer32@gmail.com 
#include ''stm32F10x.h''
#include ''STM32vldiscovery.h''
/**************************************************************************************/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
while(1)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART1, 0x49); // Send 'I'
}
while(1); // Don't want to exit
}
/**************************************************************************************/
void RCC_Configuration(void)
{
// clock for GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Enable UART clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // PA.09 USART1.TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PA.10 USART1.RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/**************************************************************************************/
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USART configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 115200;
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 configuration */
USART_Init(USART1, &USART_InitStructure);
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
}
/**************************************************************************************/

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
malahsalah2
Associate
Posted on July 23, 2012 at 01:19

Thanks a lot

baris_togrul
Associate II
Posted on July 25, 2012 at 01:18

Hi!

I also have interesting trouble with uart. My problem is uart does not send proper character.let me show; output from uart decoding logic analyzer

http://postimage.org/image/5fn3u8l65/

I am tring to send 'I' here as you posted.But receiver gets something like 'àà' ?? My code is unsigned char uart=0x49; ........ uartport.GPIO_Pin = GPIO_Pin_10; uartport.GPIO_Mode = GPIO_Mode_AF ; uartport.GPIO_OType = GPIO_OType_PP; uartport.GPIO_Speed = GPIO_Speed_100MHz; uartport.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB,&uartport); GPIO_PinAFConfig(GPIOB,10,GPIO_AF_USART3); 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(USART3, &USART_InitStructure) ; while(1) {

while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
USART_SendData(USART3, 0x49); // Send 'I'
}

but i am always getting characters such as 'ààààà ' My thought is that receiver could not take data as a single character. Thanks..
Posted on July 25, 2012 at 02:44

Probably the wrong thread as you don't have an F1 device.

But, your problem is that there is a mismatch between what the software thinks your HSE crystal is and what you've got on the board. The F4 software assumes a 25 MHz HSE, but the STM32F4-Discovery has an 8 MHz crystal.

You need to ensure that the system file has the right crystal and PLL settings, and that the project has HSE_VALUE defined as 8000000.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
baris_togrul
Associate II
Posted on July 25, 2012 at 14:38

vooovvv this is what i called wisdom and experience

.

Thanks a lot.i did adjust pll settings for 8mhz crystal but i forgat crystal itself :)

BTW how can i test my operating frequency.i create 168000000 cycle with for loop.it must be delay system 1 sec.but it gives about 8 sec?

Posted on July 25, 2012 at 16:19

BTW how can i test my operating frequency.i create 168000000 cycle with for loop.it must be delay system 1 sec.but it gives about 8 sec?

 

You can output fractional versions of the clock using the MCO pins, or via a timer channel.

As for loop iterations, it's going to depend a lot on your code and the cycles it consumes. The core, or more specifically the Trace Unit, has a cycle counter tied to the processor clock.

You could use the SysTick interrupt to toggle a GPIO pin.

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