📄 serl.c
字号:
#define C_SERL
/*******************串口通讯**********************/
//#include <reg52.h>
#include "includes.h"
unsigned char xdata com_buf[46]; //
unsigned char idata buf_p=0,com_s=0,com_s2=0;
unsigned char idata COMM=0,WAITE=0;
sbit RTS=P3^5;//串口切换
unsigned char idata com_key[8]={1,2,3,4,5,6,7,8};
#define ulong unsigned long
/*****************************************/
/*
void send_comm(unsigned char *buf,unsigned char len) //发送串口
{
unsigned char i;
RTS=1;
len=((len+7)/8)*8;
for(i=1;i<=len;i++)com_buf[i]=buf[i+1];
for(i=0;i<len/8;i++)encipher( (unsigned long *)(com_buf+8*i+1),(unsigned long *)com_key,(unsigned long *)(com_buf+8*i+1));
len++;
com_buf[0]=len+1;
com_s2=len+2;
*(unsigned int *)(com_buf+len)=calcrc(com_buf,len);
SBUF=com_buf[0];
com_s++;
}
*/
/**************
void ack_com(unsigned char ack)
{
com_buf[6]=ack;
send_comm(3);
}
*****************/
void int_comm() interrupt 4 //中断发送
{
unsigned char temp;
if(RI)
{
RI=0;
temp=SBUF;
if( !COMM )
{
if( temp==device_no)
{
if(!WAITE)COMM=4;
}
else WAITE=4;
return ;
}
else;
if(buf_p >= 40)
{
buf_p=0;
return ;
}
COMM=4;
com_buf[buf_p++]=SBUF;
}
if(TI)
{
TI=0;
if(com_s != com_s2)SBUF=com_buf[com_s++];
else
{
RTS=0;
com_s=0;
com_s2=0;
}
}
}
void init_com()
{
TMOD=0x21; //T1,T0 initial B0010 0001
// TH1=0xfd;
// TL1=0xfd;
TH1=0xfA;
TL1=0xfA;
TR1=1;
TH0=0xED; //定时时间5ms
TL0=0xED;
TR0=1;
SCON=0x50; //model 2
PCON=0x00;
REN=1; //serial port receive begin
IE=0x90;
ET0=1;
RTS=0;
}
void comm_count()
{
if(WAITE)WAITE--;
if(COMM>1)COMM--;
TH0=0xED;
TL0=0xED;
}
{
unsigned char check_com() //每10隔100ms执行一次
{
bit result=0;
unsigned char i;
if(COMM == 1)
{
if( com_buf[0] == buf_p-1 )
{
if( calcrc(com_buf,buf_p) == 0 )
{
for(i=0;i<(buf_p-3)/8;i++)decipher( (unsigned long *)(com_buf+8*i+1),(unsigned long *)com_key,(unsigned long *)(com_buf+8*i+1) );
if( calcrc(com_buf+1,buf_p-3) == 0 )result=1;
}
else
result=0;
}
else
result=0;
buf_p=0;
COMM=0;
}
return result;
}
//----------------------------------------------------------------------串行通讯-------------*/
/*
void main()
{
init_com();
while(1)
{
if( check_com() )
send_comm("123456",5);
}
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -