新手上路
- 积分
- 46
- 金钱
- 46
- 注册时间
- 2020-7-28
- 在线时间
- 10 小时
|
1金钱
问题描述如下:
我买了一个HIM屏幕,HMI屏幕通过RS232协议给单片机串口发送十六进制数据,目前确定能够通过串口收到 5A A5 06 83 54 36 01 00 FF这样的连窜十六进制数,现在需要通过串口接收到这些数据后,去取出里面的两个数据,需要使用这个数据去控制延时拉高GPIO,自己实验了多次只能够通过IF语句实现识别一个数,取出数字始终实现不了,首先识别一个数字的代码如下:
void USART1_IRQHandler(void) //串口1中断服务程序
{ u16 r;
uint16_t rx_buf[1024];
uint16_t num = 0;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断
{
r=USART_ReceiveData(USART1);//(USART1->DR); //读取接收到的数据
if( r==0xff ) //比较字符串
{
GPIO_ResetBits(GPIOC,GPIO_Pin_1);
GPIO_SetBits(GPIOC,GPIO_Pin_2);
GPIO_SetBits(GPIOC,GPIO_Pin_3);
GPIO_SetBits(GPIOC,GPIO_Pin_4);
GPIO_SetBits(GPIOC,GPIO_Pin_5);
GPIO_SetBits(GPIOC,GPIO_Pin_6);
GPIO_SetBits(GPIOC,GPIO_Pin_7);
GPIO_SetBits(GPIOC,GPIO_Pin_8);
GPIO_SetBits(GPIOC,GPIO_Pin_9);
GPIO_SetBits(GPIOC,GPIO_Pin_10);
GPIO_SetBits(GPIOC,GPIO_Pin_11);
GPIO_SetBits(GPIOC,GPIO_Pin_12);
}
}
}
这个中断函数能够识别一个十六进制数,连续发送也能实现,现在想取出来一个的进行判断的代码如下:
void USART1_IRQHandler(void) //串口1中断服务程序
{u8 tx1;
u8 ch;
u16 r;
uint16_t Uart1_Buffer[1024];
uint16_t Uart1_Rx ;
uint16_t Uart1_Tx ;
uint16_t Uart1_Len ;
uint16_t Uart1_Sta ;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断
{
Uart1_Buffer[Uart1_Rx]=USART_ReceiveData(USART1);//(USART1->DR); //读取接收到的数据
Uart1_Rx++;
Uart1_Rx &= 0xFF;
}
if(Uart1_Buffer[Uart1_Rx-1] == 0x5A) //头
Uart1_Tx = Uart1_Rx-1;
if((Uart1_Buffer[Uart1_Tx] == 0x5A)&&(Uart1_Buffer[Uart1_Rx-1] == 0xA5)) //检测到头的情况下检测到尾
{
Uart1_Len = Uart1_Rx-1- Uart1_Tx; //长度
Uart1_Sta=1; //标志位
}
if(USART_GetFlagStatus(USART1,USART_FLAG_ORE) == SET) //溢出
{
USART_ClearFlag(USART1,USART_FLAG_ORE); //读SR
USART_ReceiveData(USART1); //读DR
}
if( Uart1_Sta )
{
for(tx1=0;tx1 <= Uart1_Len;tx1++,Uart1_Tx++)
USART_SendData(USART1, Uart1_Buffer[Uart1_Tx]); //发送数据
Uart1_Rx = 0; //初始化
Uart1_Tx = 0;
Uart1_Sta = 0;
}
if(Uart1_Buffer[Uart1_Rx]==0xFF ) //比较字符串
{
GPIO_ResetBits(GPIOC,GPIO_Pin_1);
GPIO_ResetBits(GPIOC,GPIO_Pin_2);
GPIO_ResetBits(GPIOC,GPIO_Pin_3);
GPIO_SetBits(GPIOC,GPIO_Pin_4);
GPIO_SetBits(GPIOC,GPIO_Pin_5);
GPIO_SetBits(GPIOC,GPIO_Pin_6);
GPIO_SetBits(GPIOC,GPIO_Pin_7);
GPIO_SetBits(GPIOC,GPIO_Pin_8);
GPIO_SetBits(GPIOC,GPIO_Pin_9);
GPIO_SetBits(GPIOC,GPIO_Pin_10);
GPIO_SetBits(GPIOC,GPIO_Pin_11);
GPIO_SetBits(GPIOC,GPIO_Pin_12);
}
}
这个代码判断也实现不了,我还需要取出缓冲数组里面的数据用于设置延时,请各位帮助一下,C语言基础比较差,考了二级就放下,
然后库函数的使用也是稀烂,都不知道哪些变量库函数有,哪些需要定义全局变量?还望各位大佬指教。十分感谢。
|
|