cancel
Showing results for 
Search instead for 
Did you mean: 

USART2 problem with cmsis library

parisa
Senior
Posted on September 07, 2016 at 08:06

Hello

Here is my code shown below.

#include ''stm32f10x_gpio.h''
#include ''stm32f10x_rcc.h''
#include ''stm32f10x_usart.h''
void delay()
{
int i,j;
for(i=0;i<
10000
;i++)
for(
j
=
0
;j<100;j++);
}
GPIO_InitTypeDef GPIOStruc;
USART_InitTypeDef USAR;
int main(){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
//PD5
GPIOStruc.GPIO_Mode
=
GPIO_Mode_AF_PP
;
GPIOStruc.GPIO_Speed
=
GPIO_Speed_50MHz
;
GPIOStruc.GPIO_Pin
=
GPIO_Pin_5
;
GPIO_Init(GPIOD,&GPIOStruc);
//PD6
GPIOStruc.GPIO_Mode
=
GPIO_Mode_IN_FLOATING
;
GPIOStruc.GPIO_Speed
=
GPIO_Speed_50MHz
;
GPIOStruc.GPIO_Pin
=
GPIO_Pin_6
;
GPIO_Init(GPIOD,&GPIOStruc);
GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
USAR.USART_BaudRate
=
9600
;
USAR.USART_StopBits
=
USART_StopBits_1
;
USAR.USART_WordLength
=
USART_WordLength_8b
;
USAR.USART_Parity
=
USART_Parity_No
;
USAR.USART_HardwareFlowControl
=
USART_HardwareFlowControl_None
;
USART_Init(USART2,&USAR);
USART_Cmd(USART2,ENABLE);
RCC->APB2ENR=(1<<
4
);
GPIOC->CRL=(2<< 
8
);
GPIOC->BSRR=(1<<2);
while(1)
{
USART_SendData(USART2, 'X');
delay();
}
}

I want to send and receive data via PD5 and PD6 (remap these pins) but it doesn't work properly , what is my mistake?
1 REPLY 1
Walid FTITI_O
Senior II
Posted on September 08, 2016 at 11:55

Hi mohamadi.parisa,

You should also enable the AFIO. Add the followwing after enabling the GPIO clock:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

-Hannibal-