本帖最后由 FSL_TICS_A 于 2014-6-3 16:28 编辑
现在在用K60做项目,需要使用用串口中断接收上位机命令,现在遇到的问题是,通过串口调试软件发送命令只能一个字节一个字节的发送,不能连续的发送一串字节,比如发送握手指令:fe 68 11 00 00 00 00 11 16,只能依次发送fe,68,11,00,00,00,00,11,16,发送9次,而不能一次发fe 68 11 00 00 00 00 11 16,此时调试只接收到fe这一个字节,中断程序如下:
#define FRAME_PRECURSOR_CHAR 0xfe
#define FRAME_START_CHAR 0x68
#define FRAME_END_CHAR 0x16
#define RS_NOT_KNOW 0
#define RS_AWAKEN 1
#define RS_ORDER 2
#define RS_DATA 3
#define RS_CHECK 4
#define RS_END 5
void uart4_isr(void)
{
uint8 ch;
uint8 flag = 1;
static unsigned char ucCount;
static unsigned char ucDataLen;
static unsigned char ucRecState=RS_NOT_KNOW;
hw_uart_disable_re_int(UART_4);
ch = hw_uart_re1(UART_4, &flag);
if (0 == flag)
{
hw_uart_send1(UART_4, ch); //测试
switch(ucRecState)
{
case RS_NOT_KNOW:
if(ch==FRAME_PRECURSOR_CHAR)
ucRecState++;
else
ucRecState=RS_NOT_KNOW;
break;
case RS_AWAKEN:
if(ch==FRAME_START_CHAR)
ucRecState++;
else
ucRecState=RS_NOT_KNOW;
break;
case RS_ORDER:
frame[0]=ch;
ucRecState++;
ucCount=1;
break;
case RS_DATA:
frame[ucCount++]=ch;
if(ucCount==5)
ucRecState++;
break;
case RS_CHECK:
frame[5]=ch;
ucRecState++;
break;
case RS_END:
check_sum =0;
for(ucDataLen=0;ucDataLen<5;ucDataLen++)
check_sum+=frame[ucDataLen];
if((ch==FRAME_END_CHAR)&&(frame[5]==check_sum))
process_flag=1;
ucRecState=RS_NOT_KNOW;
break;
default:
ucRecState=RS_NOT_KNOW;
break;
}
}
hw_uart_enable_re_int(UART_4);
}
|