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

📄 serial.c

📁 这是用W77E58编写的控制GSM模块
💻 C
📖 第 1 页 / 共 2 页
字号:


uchar getUartWait(void)
{

       char idata c ;
       Uart0Timer=3;
       while(1)
	   {
	     if(inbufsign) break;       //缓冲区空等待
         else 
	     if(Uart0Timer==0) return 0;
	   } 
       
       ES=0;
       c= *getlast;            //取数据
       getlast++;              //最后取走的数据位置加一
       inbufful=0;            //输入缓冲区的满标志清零
       if(getlast==inbuf+ILEN) getlast=inbuf; //地址到顶部回到底部
       if (getlast==inlast) inbufsign=0;       //地址相等置接收缓冲区空空标志,再取数前要检该标志
       ES=1;
       return (c);        //取回数据



}



uchar getbyte1 (void)
{ 

       char idata c ;
       while (!inbufsign_1);     //缓冲区空等待
       ES1=0;
       c= *getlast_1;            //取数据
       getlast_1++;              //最后取走的数据位置加一
       inbufful_1=0;             //输入缓冲区的满标志清零
       if (getlast_1==inbuf_1+ILEN1) getlast_1=inbuf_1; //地址到顶部回到底部
       if (getlast_1==inlast_1) inbufsign_1=0;       //地址相等置接收缓冲区空空标志,再取数前要检该标志
       ES1=1;
       return (c);        //取回数据
	   
	   
}


uchar getdp310(void)
{

       char idata c ;
       SysTimer=3;
       while(1)
	   {
	     if(inbufsign_1) break;       //缓冲区空等待
         else 
	     if(SysTimer==0) return NULL;
	   } 
       ES1=0;
       c= *getlast_1;            //取数据
       getlast_1++;              //最后取走的数据位置加一
       inbufful_1=0;             //输入缓冲区的满标志清零
       if (getlast_1==inbuf_1+ILEN1) getlast_1=inbuf_1; //地址到顶部回到底部
       if (getlast_1==inlast_1) inbufsign_1=0;       //地址相等置接收缓冲区空空标志,再取数前要检该标志
       ES1=1;
       return (c);        //取回数据



}


//void ReadSerail(char xdata  *pData, int nLength, int nNumRead,int time_out) 
void ReadSerail(char* pData, int nLength, int nNumRead,int time_out) 

{
       int i;
       ClearBuf(pData,nLength);
	   nNumRead=0;        
	   for(i=0;i<nLength;i++)
       {
          if((pData[i]=getUartWait())==0)break;
       	  nNumRead++;
	   }

}


#if 0
//*****************************************
//接收一行数据,必须定义放数据串的指针位置和大小    del=0x7f,backspace=0x08,cr=0x0d,lf=0x0a
void getline (uchar idata *line, unsigned char xdata n)
{ 
     unsigned char xdata cnt = 0;  //定义已接收的长度
     char xdata c;
     do 
	 {
         if ((c = getbyte ()) == 0x0d)  c = 0x00;    //读一个字节,如果是回车换成结束符
         if (c == 0x08  ||  c == 0x7f)               //BACKSPACE 和 DEL 的处理
         {   if (cnt != 0)                          //已经输入退掉一个字符
            { 
			   cnt--;                              //总数目减一
               line--;                             //指针减一
               putbyte (0x08);                     //屏幕回显的处理
               putbyte (' ');
               putbyte (0x08);
            }
         }
         else
         {
		       putbyte (*line = c);                 //其他字符取入,回显
               line++;                               //指针加一
               cnt++;                               //总数目加一
         }
     }  while (cnt < n - 1  &&  c != 0x00 && c!=0x1b); //数目到了,回车或ESC停止
     *line = 0;                                //再加上停止符0
}
#endif


#if 0
//****************************
//人工输入一个字符到输入缓冲区
putinbuf(uchar xdata c)
{
        ES=0;  
		if(!inbufful)
        {
		    *inlast= c;                           //放入数据
            inlast++;                             //最后放入的位置加一
            if (inlast==inbuf+ILEN) inlast=inbuf; //地址到顶部回到底部
            if (inlast==getlast)inbufful=1;       //接收缓冲区满置满标志
            inbufsign=1;
        }
        ES=1;
}
#endif



//*****************************************
//串口中断处理
//serial_0  0023H
void serial(void) interrupt 4   //using 2
{   
    if (TI)
    {    
             
            TI = 0;
            if (outbufsign)
            {
			    SBUF=*outlast;                             //未发送完继续发送
                outlast++;                                 //最后传出去的字节位置加一
                if (outlast==outbuf+OLEN) outlast=outbuf;  //地址到顶部回到底部
                if (putlast==outlast) outbufsign=0;        //数据发送完置发送缓冲区空标志
             }
             else outbufsign0=0;
        
    }
	if (RI)
    {  
            RI = 0;
       		if(!inbufful)
            {
              *inlast= SBUF;              //放入数据
              inlast++;                   //最后放入的位置加一
              inbufsign=1;
              if (inlast==inbuf+ILEN)     inlast=inbuf; //地址到顶部回到底部
              if (inlast==getlast)        inbufful=1;   //接收缓冲区满置满标志
            }
   }

}




//SERIAL 1 003BH
void serial_1(void) interrupt 7  //using 2
{    
    
    if(RI_1)
    {
            RI_1 = 0;
           
			if(!inbufful_1)
            {
              *inlast_1= SBUF1;              //放入数据
              inlast_1++;                    //最后放入的位置加一
              inbufsign_1=1;
              if (inlast_1==inbuf_1+ILEN1)     inlast_1=inbuf_1; //地址到顶部回到底部
              if (inlast_1==getlast_1)        inbufful_1=1;   //接收缓冲区满置满标志
            }
        		
    }
    if(TI_1)
    {
       TI_1=0;
   	        if (outbufsign1)
            {
			    SBUF1=*outlast1;                             //未发送完继续发送
                outlast1++;                                 //最后传出去的字节位置加一
                if (outlast1==outbuf1+OLEN) outlast1=outbuf1;  //地址到顶部回到底部
                if (putlast1==outlast1) outbufsign1=0;        //数据发送完置发送缓冲区空标志
            }
            else outbufsign01=0;

    }
} 


#if 0
void main(void)
{char c;
idata unsigned char free[16]; 
unsigned char idata *freep=free; 
serial_init();</P><P>
putstring("jdioptuejls;j;klj");
delay(10);</P><P> while(1) 
{ putstring("com is ready! ");}
c=getbyte();
putbyte(0x20);
puthex(c);
switch(c)
{case 'r':
putbytes(inbuf,ILEN); 
break;
case 'g':
getline(freep,10);
putbyte(0x20);
putstring(freep); 
break;
default:
putbyte(c);
// }
}
}
#endif

⌨️ 快捷键说明

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