这几天在做一个案子,使用到了F0 USART收发功能。有些小伙伴们说F0不能像F1那样长期开放TC中断,否则将会卡死在中断里,应该在需要的时候开启,用完再关闭。事实真的这样吗?其实不然,那下面我来说说我是如何实现一次性初始化设置的。先来一段初始化代码:
void initUart(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
/* USART2 Pins configuration ******/
/* Connect pin to Periph */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
/* Configure pins as AF pushpull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
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(USART1,&USART_InitStructure);
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_ITConfig(USART1,USART_IT_TC,ENABLE);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE); //使能串口
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); //使能串口中断
}
在初始化代码中,使能串口和使能串口中断这两个的顺序不要搞反了,否则如果你已允许了TC中断,程序将会一直进入中断,既然在中断中调用USART_ClearFlag(USART1,USART_FLAG_TC)也无济于事。经过分析发现,数据手册中提到Set the TE bit in USARTx_CR1 to send an idle frame as first transmission。也就是说一旦使能了TE,芯片会自动地发出了一次数据,并会引起TC_FLAG置位,而想要清除该flag的先决条件是使能了串口。因此,假如在使能串口钱已经使能了串口中断,那程序就一直进入中断了。这也是小伙伴们疑惑的地方,误以为TC是无法清除的。
完成了初始化,那么只要把需要发送的数据填写到发送缓冲区,并且调用一次USART_SendData()把第一个字节发送出去之后,剩下的就交给中断处理啦!So easy!
void USART1_IRQHandler(void)
{
if (USART_GetFlagStatus(USART1, USART_FLAG_TC) == SET) {
USART_ClearFlag(USART1,USART_FLAG_TC); //清除TC_FLAG
if(gbyteUartTxIndex < gbyteUartTxLenght)
{
USART_SendData(USART1,gbyteSendBuf[gbyteUartTxIndex++]);
}
}
}
|