初级会员

- 积分
- 51
- 金钱
- 51
- 注册时间
- 2021-6-21
- 在线时间
- 18 小时
|
3金钱
初学,用的F4探索者的板子,看代码没发现什么问题,但是串口从上电就一直不能正常收发数据
配置串口:
void USART1_Init(u32 baudrate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_PinSource9|GPIO_PinSource10;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
USART_InitStructure.USART_BaudRate = baudrate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
中断函数:
void USART1_IRQHandler(void)
{
u8 data;
if(USART_GetITStatus(USART1,USART_IT_RXNE)==1)
{
data = USART_ReceiveData(USART1);
USART_SendData(USART1,data);
}
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
LEDF9_Init();
LEDF10_Init();
USART1_Init(115200);
PFout(10)=0;
PFout(9)=0;
while(1)
{
USART_SendData(USART1,'a');
delay_ms(500);
PFout(9)=!PFout(9);
PFout(10)=0;
}
}
看到LEDF9一直在闪,LEDF10一直在亮,感觉一直卡在中断里。请教一下各位大佬这是什么原因,要怎么解决呢
|
最佳答案
查看完整内容[请看2#楼]
找到问题了,谢谢各位大佬
问题出在GPIO_PinSource9上,其定义为
#define GPIO_PinSource9 ((uint8_t)0x09)
而正确的应该是GPIO_Pin_9,
#define GPIO_Pin_9 ((uint16_t)0x0200) /* Pin 9 selected */
|