一、串口中断发送数据
/*COM Send Data Structure*/
typedef struct
{
uint8_t ubs_Index; //send index
uint8_t ubs_Len; //send len
uint8_t ubs_Buffer[255]; //Send data buffer
}ComSendData;
ComSendData Usart0SendData = {0};
void USART0_IRQHandler(void)
{
if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_TBE))
{
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_TBE);
usart_data_transmit(USART0, Usart0SendData.ubs_Buffer[Usart0SendData.ubs_Index++]);
if(Usart0SendData.ubs_Index >= Usart0SendData.ubs_Len)
{
Usart0SendData.ubs_Index = 0;
usart_interrupt_disable(USART0, USART_INT_TBE);
}
}
}
usart_interrupt_enable(USART0, USART_INT_TBE);//使能发送使能即可发送数据
二、使用中断函数发送数据的好处
一般我们采用死等发送数据,此时MCU除了发送字节和while等待外没有处理其他任务,严重影响系统实时性。
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); //等待上一字节发送完成
USART1->DR=txbuf[cnt]; //要发送的字节存入串口数据寄存器
而如果采用中断发送函数,他使用的时间是分散的,在等待数据发送完成的期间MCU可以处理其他任务,系统的实时性高
三、如何定义串口发送和接收数据的结构体
/*COM Received Data Structure*/
typedef struct
{
uint8_t ubr_EndFlag; //Received data end flag
uint8_t ubr_buffer[300]; //Received data buffer
uint8_t ubr_bufferTemp[300]; //Received data buffer temp
uint16_t ubr_index; //Received data index
uint16_t ubr_len; //Received data len
}ComRevData;
判断ubr_EndFlag标志位就可以得知数据是否接收完成,ubr_bufferTemp这个的作用在于如果接收数据失败就不会把数据存放在buffer数组里面
/*COM Send Data Structure*/
typedef struct
{
uint8_t ubs_Index; //send index
uint8_t ubs_Len; //send len
uint8_t ubs_Buffer[255]; //Send data buffer
}ComSendData;
四、RS485切换模式需要时间
因为RS485通信是采用半双工通信,有一个引脚作用是使能接收还是发送,但是MCU切换引脚电平需要一定的时间,在这段时间里面MCU的引脚是高阻态。
RS485芯片从接收模式切换到发送模式需要经过3.5us才有驱动能力输出