2012-07-22 08:00 AM
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 #stm32f1xx2012-07-22 11:03 AM
// 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);
}
/**************************************************************************************/
2012-07-22 04:19 PM
Thanks a lot
2012-07-24 04:18 PM
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 analyzerhttp://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..
2012-07-24 05:44 PM
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.2012-07-25 05:38 AM
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?2012-07-25 07:19 AM
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.