⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 485com4.c

📁 485通讯源代码,编程思路新颖,可靠稳定
💻 C
字号:
#include<REG52.h>
#include<INTRINS.H>
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<absacc.h>

//#define WITHPARITY                //whith  parity
 

//---------------------------------
 
//------------------------------------
 

 
#define  uchar    unsigned char
#define  unshort  unsigned short
#define  UNINT    unsigned int   
#define   TRUE    1
#define   FALSE   0 
#define   DEAL_SERIAL_STATE      34
 
uchar     idata   Serial_Buf_Num;
uchar     idata    Sendbuf[30]  ;
uchar     idata   Serial_Buf[30];
uchar     idata dispbit,disptime;
bdata uchar  sendata ;
sbit sendbit_0=sendata^0;
bit       Find_Head;
sbit      REPTT =P3^3;
sbit      TXPTT=P3^2; 
sbit      SER=P0^6;                       /*display */
sbit      CLK=P0^5; 
sbit      SRCLK=P0^4;  
UNINT   idata  Delaytime  ;
uchar   idata  SYS_STATE;
#define   EnableRec()  REPTT=1; TXPTT=1
#define   EnableSend()   REPTT=0; TXPTT=0
 
uchar code disptab[24]={0x88,0xeb,0x4c,0x49,0x2b,0x19,0x18,0xcb,0x08,0x0b,
                        0x7f,0x0e,0xff,0x80,0xe3,0x44,0x41,0x23,0x11,0x10,
                        0xc3,0x00,0x03,0x19};
void   Clearbuf(uchar  *buf,uchar len); 
void   Iosend(uchar  buf[],uchar CharLen);  
void   sendsign(char buf[]); 
void   send(uchar buf[],uchar len); 
void   Timer1_ISR(void) ; 
void   Timer0_ISR(void) ; 
void   Receipt_ISR(void); 
void   INT0_ISR(void) ; 
void sendchar(uchar *disbuf1);
void  StarTimer1(UNINT  time); 
 
 
 
//uchar   FindChar(char *string, char   Char); 
 
 
void initialize(void); 
/***********************************************************************
/*主程序
/*
/***********************************************************************/ 
main()
{
 
 
uchar   i; 
 
  
SP=0Xd0;
initialize();
/*Sendbuf[0]=0xaa;
Sendbuf[1]=0x55; 
for(i=0;i<20;i++)
Sendbuf[2+i]=i; 
Sendbuf[22]=0x55;
Sendbuf[23]=0xaa; 
    
send(Sendbuf,24);*/    
   
while(1)                                     //主循环
{
  if(SYS_STATE==DEAL_SERIAL_STATE)  
  {
      sendchar(Serial_Buf);
	  send(Serial_Buf,24);
	  Clearbuf(Serial_Buf,30);
  }
 

 
} //WHILE(1)
}
 
 
//----------------------------------------
void   Clearbuf(uchar  *buf,uchar len)
{
uchar  idata  i;
for(i=0;i<len;i++)
buf[i]=0;
}
 

 
//---------------------------------------------------
void send(uchar buf[],uchar len)
{
  uchar  data   i ; 
  EnableSend();
  Delaytime=0;
  while(Delaytime!=3);
  EA=0;  
  for(i=0;i<len;i++)
   {		  
	  TI=0;
     SBUF=buf[i];	  
	  while(!TI);
   }   
  TI=0;
  EnableRec();
  EA=1;
}
//-------------------------------------------------------------
 
//-------------------------------------------------------------
//
//
//--------------------------------------------------------------
void  Timer0_ISR() interrupt 1 using 2
{
	TR0=0;
	TH0=0xD8;         //定时20ms 
	TL0=0xED;  
	TR0=1;    
	Delaytime++;     

}
/***************************************************************
/*接收字符串
/*
/***************************************************************/
void Receipt_ISR()interrupt 4 using 2
{ 
  
    if(RI) 
  {    
	   Serial_Buf[Serial_Buf_Num]=SBUF; 
	   RI=0;
	   Serial_Buf_Num++; 
	   if((!Find_Head)&&(Serial_Buf_Num==2))
	   {         
	     if((Serial_Buf[1]==0x55)&&(Serial_Buf[0]==0xaa))
		 {
	     Serial_Buf_Num=0;
	     Find_Head=TRUE;
		 }
	   }
	   else
	   {
	     if(Serial_Buf_Num>2)
		 {
		     if((Serial_Buf[Serial_Buf_Num-1]==0xaa)&&(Serial_Buf[Serial_Buf_Num-2]==0x55))
			 {
		     SYS_STATE= DEAL_SERIAL_STATE  ;
             Serial_Buf_Num=0;
		     Find_Head=FALSE;
			 }
		 }	
	   }  
  } 
  if(TI)  
  TI=0;
}
//------------------------------------------------------------------
//
//
//------------------------------------------------------------------  
void INT0_ISR() interrupt 2 using 1
{
 
}
 
void sendchar(uchar *disbuf1)
 { 
   CLK=0;
   for(dispbit=0;dispbit<6;dispbit++)
     {sendata=disptab[disbuf1[dispbit]];
      for(disptime=0;disptime<8;disptime++)
       {SRCLK=0;
        SER=sendbit_0;
        SRCLK=1;
        sendata=sendata>>1;
       }
     }
   CLK=1;
 }
 
 
//------------------------------------------------------------
//
//
//------------------------------------------------------------
/*uchar   FindChar(char *string, char   Char)
{
	uchar  Len,i;
	Len=strlen(string);
	i=Len;
	while(Len--)
	{
	if((*string)==Char)	
	return(i-Len);
	string++;
	}
    return(255);
}*/
 
/*****************************************************************************
/*初始化程序
/*
/*****************************************************************************/
 void initialize()
{ 
 // status=0;
//	status1=0;
    EA=0;	
	EnableRec();
    TMOD=0x21; 		   //T0、为方式2,8位自动重装   T1 16位	             
 
    
	TH1=0XE8;
	TL1=0XE8;
    TR1=1;

    ET0=1;   
	TR0=0;
	TH0=0xD8;         //定时20ms 
	TL0=0xED;  
	TR0=1;    
       
 
    ES=1;
	PCON=0x00;
	SCON=0x70;      //串行方式1	
	 
 
	EA=1;    

} 
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -