1
votes

I am working with stm32f103 and I want to send and receive data with synchronous mode of USART2. So I have a question, how can I set the clock for example to 10 MHZ for USART? Which register?

GPIO_InitTypeDef GPIO_InitStructure;                    

RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO ,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);                                       

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;               
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;         
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;       
GPIO_Init(GPIOA, &GPIO_InitStructure);                  

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;                       
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;               
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;         
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;       
GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_InitTypeDef USART_InitStructure;

USART_ClockInitTypeDef USART_ClkInitStructure;                        

USART_InitStructure.USART_BaudRate            = 4390 ;              
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(USART2, &USART_InitStructure);                               
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);                            
__nop(); __nop();

USART_ClkInitStructure.USART_Clock=USART_Clock_Enable;
USART_ClkInitStructure.USART_CPOL=USART_CPOL_Low;
USART_ClkInitStructure.USART_CPHA=USART_CPHA_2Edge;
USART_ClkInitStructure.USART_LastBit=USART_LastBit_Enable;
USART_ClockInit(USART2, &USART_ClkInitStructure);

USART_Cmd(USART2, ENABLE);
1
Did you have a look at the examples provided with the STM32 standard library?Étienne
yes i checked several examples but all of them worked on asynchronous mode and i couldnt find anything about clock setting.Mahtab
Check the stm32 standard library example: STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/USART/SynchronousÉtienne
Thank you so much. I checked it too and was helpful but there wasnt any setting about frequency clock . i think i found the solution . there is a prescaler for APB1 and i changed it to reach 9MHZ clock for APB1(or PCLK1). BUT there is a big problem. my usart2 clock pin shows 200 to 400 HZ!!!! in any case. why is wrong even when i use default clock(36MHZ)???Mahtab
What is your problem exactly? The baud rate of the USART is not the same as the clock of the bus (APB1 in your case) to which it is connected. Did you set the baud-rate correctly?Étienne

1 Answers

1
votes

You should check the example provided in the STM32 standard peripheral library (located in STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/USART/Synchr‌​onous)

The two important lines in your case are:

USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;

and:

USART_InitStructure.USART_BaudRate = 115200;

In your code, you should replace:

GPIO_Speed_10MHz

by:

GPIO_Speed_50MHz