初级会员

- 积分
- 62
- 金钱
- 62
- 注册时间
- 2018-3-21
- 在线时间
- 16 小时
|
1金钱
由于项目原因,使用了STM32F103C6T6芯片,PB6、PB7引脚重映射为USART1功能。在实际测试时发现程序无法进入接收中断,但是能向电脑发送数据。
串口配置
void uart_init(u32 bound){
//GPIO¶Ë¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷USART1
//USART 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound;//ò»°ãéèÖÃÎa9600;
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_Even;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿aÆôÖD¶Ï
USART_Cmd(USART1, ENABLE); //ê1Äü′®¿ú
}
主程序while循环
while(1)
{
if(MessageGet==1) //接收标志位
{
len=USART_RX_STA&0x3FFF;//
MessageGet=0;
USART_SendData(USART1,Status);//
delay_ms(1000);
USART_SendData(USART2,0x02); //
USART_RX_STA=0;
}
}
接收中断
void USART1_IRQHandler(void) //′®¿ú1ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
MessageGet=1;
if(USART_RX_STA>199)USART_RX_STA=0;//
}
}
|
|