stm32串口接收发送数据处理

/************************************************************
* Function Name : Uart1_Send_Byte
* Description : ´®¿Ú1·¢ËÍ×Ö½ÚÊý¾Ý
* Input : ·¢ËÍ×Ö½Ú
* Return : ÎÞ
*************************************************************/
void Uart1_Send_Byte(u8 Data)
{
USART_SendData(USART1,(u8)Data);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}


/************************************************************
* Function Name : USART1_IRQHandler
* Description : ´®¿Ú1½ÓÊÕÖжÏ
* Input : ·¢ËÍ×Ö½Ú
* Return : ÎÞ
*************************************************************/
void USART1_IRQHandler(void) //´®¿Ú1ÖжϷþÎñ³ÌÐò
{
int Data;

if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //½ÓÊÕÖжÏ
{

Data =USART_ReceiveData(USART1); //¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý
switch(ReceiveState)
{
case RecSta1:
if(Data == 0xFE)
{
ReceiveState = RecSta2;
}
else
{
ReceiveState = RecSta1;
}
break;

case RecSta2:
if(Data == 0xEF)
{
ReceiveState = RecSta3;
}
else
{
ReceiveState = RecSta1;
}
break;

case RecSta3:
if(Data == 0x7F)
{
ReceiveState = RecSta5;
}
else if(Data == 0x02)
{
ReceiveState = RecSta6;
}
else
{
LedChannel = Data;
ReceiveState = RecSta4;
}
break;

case RecSta4:
ReceiveFlag = 1;
LedState = Data;
ReceiveState = RecSta1;
break;

case RecSta5:
if(Data == 0x8F)
{
Uart1_Send_Byte(0xFE);
Uart1_Send_Byte(0xEF);
Uart1_Send_Byte(0x7F);
Uart1_Send_Byte(0x68);

}
else if(Data == 0xF8)
{
Uart1_Send_Byte(0x04);
Uart1_Send_Byte(0x09);
Uart1_Send_Byte(0xB1);
Uart1_Send_Byte(0x03);
}
ReceiveState = RecSta1;
break;

case RecSta6 :
if(Data == 0x02)
{
LedInitial(1);
flag=1;
ReceiveState = RecSta1;
}
break ;

default:
ReceiveState = RecSta1;
break;
}

}
X86_ControlLed(); //X86µãµÆ
}

猜你喜欢

转载自www.cnblogs.com/fengbaobao/p/10684255.html