在使用
STM32f103做一个项目,使用串口中断发送数据时,数据出现了断帧,断帧的间隔时间从串口打印log来看,最大的达到40多ms,小的时间间隔也有20ms左右,不知道是不是因为操作系统造成了。
1.系统是用的ucos ii
2.串口使用了串口1 和 2 都是利用串口中断接收和发送
3.部分驱动代码
谢谢各位
/*=======================================================================================================
*Func
tion: Bsp_UartNVIC_Config( ) =>
*Input : *Uart
*Output : None
========================================================================================================*/
void Bsp_UartNVIC_Config( UART_Def *Uart )
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = Uart->UARTx_IRQn; //Enable the USARTy Interrupt
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = Uart->PreemPriority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = Uart->SubPriority;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*=======================================================================================================
*Function: Bsp_UartConfig( ) =>
*Input : *Uart
*Output : None
========================================================================================================*/
void Bsp_UartConfig( UART_Def *Uart )
{
USART_InitTypeDef USART_InitStructure1;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( Uart->RCC_PinPort, ENABLE ); // config USARTx Pin clock
if( Uart->USARTx == USART2 || Uart->USARTx == USART3 ) // config USARTx clock
{
RCC_APB1PeriphClockCmd( Uart->RCC_Uartx, ENABLE );
}
else if( Uart->USARTx == USART1 ) // config USARTx clock
{
RCC_APB2PeriphClockCmd( Uart->RCC_Uartx, ENABLE );
}
GPIO_InitStructure.GPIO_Pin = Uart->Rx_Pinx; // Configure USARTy Rx as input floating
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init( Uart->PinPort, &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Pin = Uart->Tx_Pinx; //Configure USARTy Tx as alternate function push-pull
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init( Uart->PinPort, &GPIO_InitStructure );
USART_InitStructure1.USART_BaudRate = Baud_Table[ Uart->Baud_Rate ]; //USART1 mode config
USART_InitStructure1.USART_WordLength = USART_WordLength_8b;
USART_InitStructure1.USART_StopBits = USART_StopBits_1;
USART_InitStructure1.USART_Parity = Parity_Table[ Uart->Parity ] ;
USART_InitStructure1.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure1.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init( Uart->USARTx, &USART_InitStructure1 );
USART_ITConfig( Uart->USARTx, USART_IT_RXNE , ENABLE );
USART_ITConfig( Uart->USARTx, USART_IT_IDLE , ENABLE );
USART_ITConfig( Uart->USARTx, USART_IT_TXE , DISABLE );
USART_Cmd( Uart->USARTx, ENABLE );
}
/*=======================================================================================================
*Function: Bsp_UartRingSend( ) => USARTx send data from ring buff
*Input : *Uart, data
*Output : None
========================================================================================================*/
void Bsp_UartIntRingSend( UART_Def *Uart )
{
uint8_t data;
if( Get_bytes_count( &Uart->T_RingBuff ) != 0 )
{
#if RS485_EN_USE == true
RS485_TX_EN();
#endif
Read_ring_buffer( &Uart->T_RingBuff, &data, 1 );
Bsp_UartSendData( Uart, data );
if( Uart->TxBusy == false )
{
Uart->TxBusy = true;
USART_ITConfig( Uart->USARTx, USART_IT_TXE , ENABLE );
}
}
else
{
#if RS485_EN_USE == true
g_Time3Count.rxdelay = RS485_RxDelayTable[ Uart->Baud_Rate ];
#endif
Uart->TxBusy = false;
USART_ITConfig( Uart->USARTx, USART_IT_TXE , DISABLE );
}
}
一周热门 更多>