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

📄 serial.c

📁 51avr http协议 代码
💻 C
字号:
//-----------------------------------------------------------------------------
// Net SERIAL.C
//
// This module handles RS-232 messages and associated tasks
//-----------------------------------------------------------------------------
#include <reg52.h>
#include <intrins.h>
#include "net.h"
#include "serial.h"

//------------------------------------------------------------------------
// This function converts an integer to an ASCII string.  It is a 
// normally provided as a standard library function but the Keil
// libraries do not include it.  Caution: The string passed to this
// must be at least 12 bytes long
//------------------------------------------------------------------------
char * itoa(UINT value, char * buf, UCHAR radix)
{
	UINT i;
	char * ptr;
	char * temphold;

  	temphold = buf;
	ptr = buf + 12;
	*--ptr = 0;		// Insert NULL char
	do
	{
	   // First create string in reverse order
	   i = (value % radix) + 0x30;
		if(i > 0x39) i += 7;
		*--ptr = i;
      value = value / radix;
  	} while(value != 0);

	// Next, move the string 6 places to the left
	// Include NULL character
	for( ; (*buf++ = *ptr++); );	
	return(temphold);
}

unsigned char xdata TxBuf[LenTxBuf],RxBuf[LenRxBuf];//收发缓冲区实体
unsigned char  xdata * data inTxBuf,xdata * data outTxBuf,xdata * data inRxBuf,xdata * data outRxBuf;//收发缓冲区读写指针
bit TIflag=1;//Note:It must be 1.

void InitSerial()//串口初始化
{
 PCON=0x80;
 SCON=0x50;

 RCLK=1;
 TCLK=1;

 RCAP2H=0xff;	//19200
 RCAP2L=0xdc;
 
 TR2=1;
}

void InitSerialBuffer(void)//串口缓冲区初始化
{
	inTxBuf=TxBuf;outTxBuf=TxBuf;
	inRxBuf=RxBuf;outRxBuf=RxBuf;
	EA=1;ES=1;
}
//====================================================================
void serial(void) interrupt 4{//串口中断服务子程序
	unsigned char xdata * data t;

	if(TI){
		TI=0;
		if(inTxBuf==outTxBuf) {TIflag=1;return;}//TxBuf Empty
		SBUF=*outTxBuf; outTxBuf++;
		if(outTxBuf==TxBuf+LenTxBuf) outTxBuf=TxBuf;	
	}
	if(RI){
		RI=0;
		t=inRxBuf;t++;
		if(t==RxBuf+LenRxBuf) t=RxBuf;
		if(t==outRxBuf) return;			//RxBuf Full
		*inRxBuf=SBUF;
		inRxBuf=t;
	}
}
//=====================================================================
bit yygetch(unsigned char *ch)//从串口缓冲区读1字节数据
{
	ES=0;
	if(inRxBuf==outRxBuf) {ES=1;return 0;}   	//RxBuf Empty
	*ch=*outRxBuf;  outRxBuf++;
	if(outRxBuf==RxBuf+LenRxBuf) outRxBuf=RxBuf;
	ES=1;
	return 1;
}

void PrintChar(unsigned char ch)//显示字符
{
	unsigned char xdata * data t;

	ES=0;
	if(TIflag){		
		TIflag=0;
		TI=1;
	}
	t=inTxBuf;t++;
	if(t==TxBuf+LenTxBuf) t=TxBuf;
	if(t==outTxBuf) {ES=1;return;}			//TxBuf Full
	*inTxBuf=ch;
	inTxBuf=t;
	ES=1;
}

void PrintCh(unsigned char ch)//内部使用,不建议用户看到。
{
	if(ch>=0&&ch<=9) ch=ch+'0';
	else if(ch>=10&&ch<=15) ch=ch+'A'-10;
	PrintChar(ch);
}

void PrintByte(unsigned char Byte)//以十六进制格式显示1个字节数据
{
	unsigned char c;
	c=Byte;
	c=c>>4;PrintCh(c);
	c=Byte;c=c&0x0F;PrintCh(c);
}

void PrintWord(unsigned int Word)//以十六进制格式显示1个字数据
{
	PrintByte(Word>>8);
	PrintByte(Word&0xFF);
}

void PrintInteger(unsigned int value)//以十进制格式显示
{

	unsigned char data buf[5];
	unsigned char i;
	for(i=5;i;i--)
	 {
	  buf[i-1]=value%10;
	  value/=10;
	 }
   for(i=0;i<4;i++)
     if(buf[i]!=0) break;
  for(;i<5;i++) PrintCh(buf[i]+'0');

}

void PrintLong(unsigned long LongWord)//以十六进制格式显示1个长字数据
{
	PrintWord(LongWord>>16);
	PrintWord(LongWord&0xFFFF);
}

void PrintStr(unsigned char * str)//显示字符串
{
	int i;
	unsigned char j;
	unsigned char ch;
	for(i=0;i<MaxLenStr;i++){
		ch=*(str+i);
		if(ch=='\0') break;
		else if(ch=='\n'){PrintChar(10);PrintChar(13);} 
		else if(ch=='\t'){
			for(j=0;j<TABNum;j++)
				PrintChar(' ');
		}
		else PrintChar(ch);
	}
}

void clrscr()//清屏
{
	int data i;
	for(i=0;i<25;i++)
		PrintStr("\n");	
}

⌨️ 快捷键说明

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