cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F072 usart doesn't reveive data

karatepe101
Associate II
Posted on January 11, 2016 at 16:44

Hi,i am working with Stm32f072 microcontroller and i can send datasuccessful.But my code doesn't enter usart interrupt.My code is below,thanks for your support.

void USART_Configuration()
{
GPIO_InitTypeDef GPIO_Struct;
USART_InitTypeDef USART_Struct;
NVIC_InitTypeDef NVIC_Struct;
/*USART1 Configuration*/
RCC_AHBPeriphClockCmd(USART1_GPIO_Bus,ENABLE);
RCC_APB1PeriphClockCmd(USART1_Bus,ENABLE);
GPIO_PinAFConfig(USART1_GPIO_Port,USART2_GPIO_Pinsource1,USART1_Alternate);
GPIO_PinAFConfig(USART1_GPIO_Port,USART2_GPIO_Pinsource2,USART1_Alternate);
GPIO_Struct.GPIO_Pin=(USART1_Pin_Tx|USART1_Pin_Rx);
GPIO_Struct.GPIO_Mode=GPIO_Mode_AF;
GPIO_Struct.GPIO_OType=GPIO_OType_PP;
GPIO_Struct.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Struct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(USART1_GPIO_Port,&GPIO_Struct);
USART_Struct.USART_Mode=((USART_Mode_Rx)|(USART_Mode_Tx));
USART_Struct.USART_BaudRate=9600;
USART_Struct.USART_WordLength=USART_WordLength_8b;
USART_Struct.USART_Parity=USART_Parity_No;
USART_Struct.USART_StopBits=USART_StopBits_1;
USART_Struct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_Struct);
USART_Cmd(USART1,ENABLE);
/**************************************************************************/

/*USART2 Configuration*/
RCC_AHBPeriphClockCmd(USART2_GPIO_Bus,ENABLE);
RCC_APB1PeriphClockCmd(USART2_Bus,ENABLE);

GPIO_Struct.GPIO_Pin=(USART2_Pin_Rx|USART2_Pin_Tx);
GPIO_Struct.GPIO_Mode=GPIO_Mode_AF;
GPIO_Struct.GPIO_OType=GPIO_OType_PP;
GPIO_Struct.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Struct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(USART2_GPIO_Port,&GPIO_Struct);
GPIO_PinAFConfig(USART2_GPIO_Port,USART2_GPIO_Pinsource1,USART2_Alternate);
GPIO_PinAFConfig(USART2_GPIO_Port,USART2_GPIO_Pinsource2,USART2_Alternate);
USART_Struct.USART_Mode=((USART_Mode_Rx)|(USART_Mode_Tx));
USART_Struct.USART_BaudRate=9600;
USART_Struct.USART_WordLength=USART_WordLength_8b;
USART_Struct.USART_Parity=USART_Parity_No;
USART_Struct.USART_StopBits=USART_StopBits_1;
USART_Struct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART2,&USART_Struct);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
NVIC_Struct.NVIC_IRQChannel=USART2_IRQn;
NVIC_Struct.NVIC_IRQChannelCmd=ENABLE;
NVIC_Struct.NVIC_IRQChannelPriority=0;
NVIC_EnableIRQ(USART2_IRQn);
NVIC_Init(&NVIC_Struct);
USART_Cmd(USART2,ENABLE);
/********************************************************************/
/*USART3 Configuration*/
RCC_AHBPeriphClockCmd(USART3_GPIO_Bus,ENABLE);
RCC_APB1PeriphClockCmd(USART3_Bus,ENABLE);
GPIO_PinAFConfig(USART3_GPIO_Port,USART3_GPIO_Pinsource1,USART3_Alternate);
GPIO_PinAFConfig(USART3_GPIO_Port,USART3_GPIO_Pinsource2,USART3_Alternate);
GPIO_Struct.GPIO_Pin=((USART3_Pin_Tx)|(USART3_Pin_Rx));
GPIO_Struct.GPIO_Mode=GPIO_Mode_AF;
GPIO_Struct.GPIO_OType=GPIO_OType_PP;
GPIO_Struct.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Struct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(USART3_GPIO_Port,&GPIO_Struct);
USART_Struct.USART_Mode=(USART_Mode_Rx|USART_Mode_Tx);
USART_Struct.USART_BaudRate=9600;
USART_Struct.USART_WordLength=USART_WordLength_8b;
USART_Struct.USART_Parity=USART_Parity_No;
USART_Struct.USART_StopBits=USART_StopBits_1;
USART_Struct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART3,&USART_Struct);
USART_Cmd(USART3,ENABLE);
/********************************************************************/
}
/*******************************/
void USART1_IRQHandler(void)
{ 
}
void USART2_IRQHandler(void)
{ 
//
}

2 REPLIES 2
Posted on January 11, 2016 at 17:29

The magic of defines you don't provide...

If you are just trying to demo functionality why even use a whole other level of custom defines?

Test it without interrupts first. If the USART doesn't actually receive anything, it's not going to interrupt. Consider an external loop back of the TX/RX pins if you need that level of functional confirmation.

Have some service code in the IRQ Handler otherwise it will never leave interrupt context.

If this is .cpp code watch out for name mangling.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Walid FTITI_O
Senior II
Posted on January 12, 2016 at 15:50

Hi tunder.pinyin,

You have missed to enable the USART interruption each time before enabling USART peripheral through the following function :

USART_ITConfig(USART_TypeDef* USARTx, uint32_t USART_IT, FunctionalState NewState);

-Hannibal-