设置一个接收缓存(数组),在接收中断中把数据接收,然后需要的时候从数组中读取数据,然后再选择你需要的数据。以下作为参考
void UART1_IRQHandler(void) //串口1中断服务程序
{
u8 UartReadTemp;
if (RESET != USART_GetITStatus(USART1, USART_IT_RXNE)) //接收中断
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
UartReadTemp = USART_ReceiveData(USART1);
if (Bit_SET == UartRecvFrameOK)
{
UART1_RXBuffLen = 0;
}
if (UART1_RXBuffLen < (sizeof(UART1_RXBuff) / sizeof(UART1_RXBuff[0])))
{
UART1_RXBuff[UART1_RXBuffLen] = UartReadTemp;
UART1_RXBuffLen++;
}
UartRecvFrameOK = Bit_RESET;
UartRecvNewData = Bit_SET;
TIM_ITConfig(TIM7, TIM_IT_Update, DISABLE);
TIM_Cmd(TIM7, DISABLE);
TIM_SetCounter(TIM7, 0);
TIM_Cmd(TIM7, ENABLE); //当上位机发送数据下来时,开启TIM7
TIM_ITConfig(TIM7, TIM_IT_Update, ENABLE);
}
if (RESET != USART_GetFlagStatus(USART1, USART_IT_ORE))
{
USART_ClearITPendingBit(USART1, USART_IT_ORE); //清溢出中断标志位
}
}
s32 UART1_RecvData(u8* UART1_RecvBuff, u32 Len) //接收数据
{
u32 i = 0;
if ((0 == Len) || (((u8*)NULL) == UART1_RecvBuff)) //检查参数
{
return 0;
}
if ((Bit_RESET == UartRecvFrameOK) || (0 == UART1_RXBuffLen))
{
return 0;
}
if (Len < UART1_RXBuffLen)
{
return -1;
}
Len = UART1_RXBuffLen;
for (i = 0; i < Len; i++)
{
UART1_RecvBuff[i] = UART1_RXBuff[i];
}
UartRecvFrameOK = Bit_RESET;
UART1_RXBuffLen = 0;
return Len;
}
设置一个接收缓存(数组),在接收中断中把数据接收,然后需要的时候从数组中读取数据,然后再选择你需要的数据。以下作为参考
void UART1_IRQHandler(void) //串口1中断服务程序
{
u8 UartReadTemp;
if (RESET != USART_GetITStatus(USART1, USART_IT_RXNE)) //接收中断
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
UartReadTemp = USART_ReceiveData(USART1);
if (Bit_SET == UartRecvFrameOK)
{
UART1_RXBuffLen = 0;
}
if (UART1_RXBuffLen < (sizeof(UART1_RXBuff) / sizeof(UART1_RXBuff[0])))
{
UART1_RXBuff[UART1_RXBuffLen] = UartReadTemp;
UART1_RXBuffLen++;
}
UartRecvFrameOK = Bit_RESET;
UartRecvNewData = Bit_SET;
TIM_ITConfig(TIM7, TIM_IT_Update, DISABLE);
TIM_Cmd(TIM7, DISABLE);
TIM_SetCounter(TIM7, 0);
TIM_Cmd(TIM7, ENABLE); //当上位机发送数据下来时,开启TIM7
TIM_ITConfig(TIM7, TIM_IT_Update, ENABLE);
}
if (RESET != USART_GetFlagStatus(USART1, USART_IT_ORE))
{
USART_ClearITPendingBit(USART1, USART_IT_ORE); //清溢出中断标志位
}
}
s32 UART1_RecvData(u8* UART1_RecvBuff, u32 Len) //接收数据
{
u32 i = 0;
if ((0 == Len) || (((u8*)NULL) == UART1_RecvBuff)) //检查参数
{
return 0;
}
if ((Bit_RESET == UartRecvFrameOK) || (0 == UART1_RXBuffLen))
{
return 0;
}
if (Len < UART1_RXBuffLen)
{
return -1;
}
Len = UART1_RXBuffLen;
for (i = 0; i < Len; i++)
{
UART1_RecvBuff[i] = UART1_RXBuff[i];
}
UartRecvFrameOK = Bit_RESET;
UART1_RXBuffLen = 0;
return Len;
}
举报