|
发表于 2024-11-12 17:05:11
|
显示全部楼层
void uart2_isr() interrupt 4
{
u8 rec;//½ÓÊÕÊý¾ÝµÄÖмä±äÁ¿
u16 crc16_cal;//CRC16УÑé·µ»Ø´æ´¢±äÁ¿
u8 crch,crcl;//CRC16УÑé¸ßλµÍλ
if(RI0)//ÊÇ´®¿Ú½ÓÊÜÖжÏ
{
RI0 = 0;//Çå³ý½ÓÊÜÖжϱêÖ¾
rec= SBUF0;//¶ÁÈ¡Êý¾Ý
if(recv_flag == 0)//Êý¾ÝÊÇ·ñ´¦ÀíÍê±Ï£¬´¦ÀíÍê±Ï£¬flag=0
{
switch(step)
{
case 0://½âÎöÖ¡Í·Æðʼλ
{
if((rec >= 0x01)&&(rec<=0x10))
{
recv_buf[index] = rec; //cnt=0
index = 1;
step = 1;
// u2_send_byte('A');
}
else
{
index = 0;
step = 0;
}
break;
}
case 1://½ÓÊÕ³¤¶È
{
recv_buf[index] = rec;
if( recv_buf[index] == 7)
{
index = 2;
step = 2;
}
else
{
index = 0;
step = 0;
}
break;
}
case 2://½ÓÊÕ¹¦ÄÜÂë 01 02 03 04 05 06 07 08 09 0a 0b
{
if((rec >= 0x01)&&(rec <= 0x0B))//ÔÚ¹¦ÄÜÂ뷶Χ֮¼ä
{
recv_buf[index] = rec;
index = 3;
step = 3;
}
else
{
index = 0;
step = 0;
}
break;
}
case 3: //½ÓÊÕÊý¾Ý(¸ß×Ö½Ú)
{
recv_buf[index]=rec;
index = 4;
step = 4;
break;
}
case 4://½ÓÊÕÊý¾Ý((µÍ×Ö½Ú)
{
recv_buf[index]=rec;//Êý¾Ýλ½ÓÊÕÍê±Ï
index = 5;
step = 5;
crc16_cal= CRC_16_int(recv_buf,5);//¼ÆËãÇ°5¸ö×Ö½ÚµÄcrc16Öµ
crcl= crc16_cal & 0xFF;//CRC16УÑéµÍ×Ö½Ú
crch = crc16_cal >> 8;//CRC16УÑé¸ß×Ö½Ú
break;
}
case 5://УÑéCRC16µÍ×Ö½Ú
{
if(rec == crcl)
{
step = 6;
}
else
{
step = 0;
}
break;
}
case 6://УÑéCRC16¸ß×Ö½Ú
{
if(rec == crch)//ÈôУÑéÕýÈ·
{
index = 0;
step = 0;
recv_flag = 1;//±ê־λÖÃ1£¬´ú±íÊý¾Ý½ÓÊճɹ¦
}
else
{
index = 0;
step = 0;
}
break;
}
}
}
}
}我写的这个接收不了数据 |
|