📄 comu.c.c
字号:
#include <reg52.h>
#include <intrins.h>
#include <ctype.h>
#define uchar unsigned char
#define ulong unsigned long
#define uint unsigned int
#define ESC 0x1b
#define END 0x05
#define NUM 42
void hex_asc(uchar *dst, uchar *src, uchar len);
void asc_hex(uchar *data_ptr,uchar length);
void sendA0(uchar a0);
void wr_fun(uint WR_adr,uchar WR_len);
void send(uchar *send_data,uchar len);
void send_byte(uchar c);
bit have_scmd; //一帧数据接收完毕标志
uchar idata receive[NUM];
uchar s1_p; //通讯接收数据长度
uchar rcv_en;
//---------------------------------
//字符串比较子程序
//---------------------------------
bit str_cmp(uchar *p1,uchar *p2,uchar len)
{
bit error;
uchar i;
error=0;
for (i=0;i<len;i++)
{
if ((*(p1+i))!=(*(p2+i)))
{
error=1;
break;
}
}
return error;
}
//-------------------------------
//字符串拷贝函数
//-------------------------------
void str_cpy(uchar *p1,uchar *p2,uchar len)
{
uchar i;
for (i=0;i<len;i++)
{
*(p1+i)=*(p2+i);
}
}
/*****************************
通讯处理模块
*****************************/
void comu(void)
{
idata union{uint a;uchar b;}xx;
uint idata addr;
//send_byte('9');
if(have_scmd)
{
have_scmd=0;
asc_hex(receive+3,4);
addr=(uint)*(receive+3)<<8|(uint)*(receive+4);
str_cpy(&xx.b,receive+1,2); //取命令字
if(xx.a==0x5752) //WR
{
;
}
else if(xx.a==0x5244) //RD/
{
;
}
else if(xx.a==0x5355) //command:SU learn controller address
{
;
}
}
}
/************************************
串行通讯接收子程序
*************************************/
uchar c;
void s_int(void) interrupt 4 //串行口中断程序
{
uchar idata j,check_sum;
if(RI==1){
c=SBUF;
if (c==ESC) //命令行是否以esc开头
{
s1_p=0;
rcv_en=0xaa; //rcv_en=0xaa为允许接收标志
}
if (rcv_en==0xaa)
{
*(receive+s1_p)=c; //字符存入命令行
s1_p++;
if (c==END) //收到enter键
{
c=0x00;
s1_p--; //结束命令正文(不含校验码)字符串
*(receive+s1_p)=c; //字符存入命令行
check_sum=0;
for (j=1; j<s1_p-2; j++)
{
check_sum+=receive[j]; //求命令行的校验和
}
*(receive+s1_p-2)=(char)(toint(*(receive+s1_p-2))*16+toint(*(receive+s1_p-1))); //收到的校验和转换为hex
if (check_sum==receive[s1_p-2]) //校验和正确
{
have_scmd=1;
}
rcv_en=0;
}
if (s1_p>=NUM) //命令行长度大于253个字符
{
rcv_en=0; //清接受到esc标志
s1_p=0;
}
}
}
RI=0; //清接受中断标志
return;
}
/***********************
在串口发送一字节
***********************/
void send_byte(uchar c){
ES=0;
REN=0;
TI=0;
SBUF=c;
while (!TI);
TI=0;
REN=1;
ES=1;
}
/****************
发送子程序
****************/
void send(uchar *send_data,uchar len)
{
uchar idata i;
uchar chs=0; //校验和存放单元
for(i=0;i<len;i++)
chs+=*(send_data+i);
hex_asc(send_data+len,&chs,1);
send_byte(ESC); //发ESC
for(i=0;i<len+2;i++) //DATA+CHS
send_byte(*(send_data+i));
send_byte(END); //发END
}
/*********************
send AA or 00
*********************/
void sendA0(uchar a0)
{
*receive=a0;
*(receive+1)=a0;
send(receive,2);
}
/**********************
HEX码至ASCII码转换
**********************/
void hex_asc(uchar *dst, uchar *src, uchar len)
{
uchar j,temp;
uchar code ascii[]={"0123456789ABCDEF"}; /*ascii 字符表*/
for (j=0;j<len;j++)
{
temp=*(src+j);
*(dst+j*2)=ascii[_cror_(temp&0xf0,4)];
*(dst+j*2+1)=ascii[temp&0x0f];
}
}
/******************
ascii to hex
******************/
void asc_hex(uchar *data_ptr,uchar length)
{
uchar i,k,j;
for (i=0;i<(length/2);i++)
{
k=*(data_ptr+i*2);
j=*(data_ptr+i*2+1);
if (k>0x39)
{k=k-0x37;}
else {k=k-0x30;}
if (j>0x39)
{j=j-0x37;}
else {j=j-0x30;}
*(data_ptr+i)=k*16+j;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -