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

📄 serial1.c

📁 采用C8051F020单片机的串口通信应用资料
💻 C
字号:
//串口中断服务程序,仅需做简单调用即可完成串口输入输出的处理
//出入均设有缓冲区,大小可任意设置。
//*************************************************************************
#include "TestSerial.h"	
#define	BAUDRATE1		115200		// 用户定义的UART1 波特率

#define DB_SENDMAXSIZE1 0xf0
#define DB_RECMAXSIZE1 0xf0


extern unsigned char Count1ms;
bit FlagRecComm1,SendItComm1;
unsigned char	CommSendBufferHead1, CommSendBufferTail1;
unsigned char	xdata CommSendBuffer1[DB_SENDMAXSIZE1]; 
unsigned char	CommRecBufferHead1, CommRecBufferTail1;
unsigned char	xdata CommRecBuffer1[DB_RECMAXSIZE1]; 

/*****************************************************************
Function: OpenComm
Description: Sets the serial port up for debug use and resets
the ring buffer pointers to 0.
Parameters: None.
Returns: Nothing.
*****************************************************************/
//                0xfd=19200,0xfa=9600,0xf4=4800,0xe8=2400,0xd0=1200

void OpenComm1(void) 
{
//	SCON1=0xf0; 		/*设置串行口1工作于方式3,主从式*/
			        /*SM0_1  SM1_1 SM2_1 REN_1  TB8_1 RB8_1 TI_1 RI_1*/
			        /* 1       1     1     1      0     0     0    0 */
			        /*when SM2_1=1 receice addr    */       
	TMOD |= 0x20;   /* timer 1 mode 2: 8-Bit reload          */
	PCON|=0x10;//-->SMOD_1=1;       /*bps double*/                                  
	CKCON |= 0x10;		// Timer 1 derived from SYSCLK
   	TH1 = -(SYSCLK/BAUDRATE1/16);  //11.0592  ->4800-115200
	TR1 = 1;    			// Start Timer 1
	SCON1 = 0x50;			// Configure UART1 for mode 1, receiver enabled.
	CommSendBufferHead1=CommSendBufferTail1=0; // set the head and tail to the base of the ring buffer
	CommRecBufferHead1=CommRecBufferTail1=0; // set the head and tail to the base of the ring buffer
	SCON1|=0x10;//-->   REN1=1; 
	EIE2|=0x40; //-->	ES1=1; 			// allow the serial interrupt
	SendItComm1=1;
}
/*
void SendCommAddress1(char ch)
{
	while (CommSendBufferTail1!=CommSendBufferHead1);
	CommSendBufferTail1++;
	if (CommSendBufferTail1==DB_SENDMAXSIZE1)
	{ 	
		CommSendBufferTail1=0;
	}
	SCON1|=0x08;//-->TB8_1=1;
	SBUF1=ch;
}
*/
void SendCommChar1(char ch)
{
	CommSendBuffer1[CommSendBufferTail1]=ch; // copy the current byte
	CommSendBufferTail1++; 	// move the pointer
	if (CommSendBufferTail1==DB_SENDMAXSIZE1)
	{ 						// check for pointer rollover
		CommSendBufferTail1=0;
	}
	if (SendItComm1)
	{	 					// if a byte needs to be sent,
		SCON1&=0xf7;//-->TB8_1=0;
		SBUF1=CommSendBuffer1[CommSendBufferHead1]; // do it
	}
	return ;
}
/*
bit CommSendBufferIsEmpty1(void)
{
	if (CommSendBufferTail1==CommSendBufferHead1) return true;
	else return false;     
}


bit CommRecBufferIsEmpty1(void)
{
	if (CommRecBufferTail1==CommRecBufferHead1) return TRUE;
	else return FALSE;     
}
void ClearCommSendBuffer1(void)
{
	CommSendBufferHead1=CommSendBufferTail1;
}
void ClearCommRecBuffer1(void)
{
	CommRecBufferHead1=CommRecBufferTail1;
	RecDataFlag1=0;
}
*/
void SendCommBuffer1(unsigned char *base, unsigned char size) 
{
unsigned char i=0;
	if (size==0) return;
	while (i<size) 
	{	 				// copy bytes while the buffer has space and the block has data
		CommSendBuffer1[CommSendBufferTail1]=base[i]; // copy the current byte
		i++;
		CommSendBufferTail1++; // move the pointer
		if (CommSendBufferTail1==DB_SENDMAXSIZE1)
		{ 				// check for pointer rollover
			CommSendBufferTail1=0;
		}
	}
	if (SendItComm1)
	{	 				// if a byte needs to be sent,
		SCON1&=0xf7;//-->TB8_1=0;
		SBUF1=CommSendBuffer1[CommSendBufferHead1]; // do it
	}
}

void SendCommString1(unsigned char *base) 
{
unsigned char i=0;
	if (base[0]==0) return;
	for (;;)
	{	
		if (base[i]==0) break;
		CommSendBuffer1[CommSendBufferTail1]=base[i]; // copy the current byte
		CommSendBufferTail1++; // move the pointer
		if (CommSendBufferTail1==DB_SENDMAXSIZE1)
		{ 						// check for pointer rollover
			CommSendBufferTail1=0;
		}
		i++;
	}
	if (SendItComm1)
	{	 						// if a byte needs to be sent,
		SCON1&=0xf7;//-->TB8_1=0;
		SBUF1=CommSendBuffer1[CommSendBufferHead1]; // do it
	}
}

/*****************************************************************
Function: CommISR
Description: ISR for the serial port. Increments the ring
buffer head pointer and sends out the next byte if
the pointer has not caught the tail pointer.
Parameters: None.
Returns: Nothing.
*****************************************************************/
void CommISR1(void) interrupt 20 
{
	if (SCON1&0x02);//-->_testbit_(TI_1))
	{							// check and clear TI
		CommSendBufferHead1++; 	// advance the head pointer
		if (CommSendBufferHead1==DB_SENDMAXSIZE1)
		{						// watch out for rollover
			CommSendBufferHead1=0;
		}
		if (CommSendBufferHead1!=CommSendBufferTail1)
		{						// check for more data in the buffer
			SBUF1=CommSendBuffer1[CommSendBufferHead1]; // send the next byte
			SendItComm1=0;
		}
		else
		{
			SendItComm1=1;
		}
	}
	if (SCON1&0x01) //if (_testbit_(RI_1))	
	{	
		CommRecBuffer1[CommRecBufferTail1]=SBUF1;     //receive data           
	    CommRecBufferTail1++;
	    if (CommRecBufferTail1==DB_RECMAXSIZE1)
	    {
	    	CommRecBufferTail1=0;
	    }
		FlagRecComm1=1;
   	}
	SCON1&=0xfc;
}

//*************************************
//read one byte from receive buffer

bit GetCommChar1(unsigned char idata *ch)      
{ 
	if (CommRecBufferTail1==CommRecBufferHead1) return 0;     
	*ch=CommRecBuffer1[CommRecBufferHead1];
	CommRecBufferHead1++;
	if (CommRecBufferHead1==DB_RECMAXSIZE1)
	{
		CommRecBufferHead1=0;
	}
	if (CommRecBufferTail1==CommRecBufferHead1) FlagRecComm1=0;
	return 1;
}

/*
bit SendBufferIsEmpty1(void)
{
	if (CommSendBufferTail1==CommSendBufferHead1) return TRUE;
	else return FALSE;     
}
bit RecBufferIsEmpty1(void)
{
	if (CommRecBufferTail1==CommRecBufferHead1) return TRUE;
	else return FALSE;     
}
*/
bit GetCommCharWait1(unsigned char idata *ch,unsigned char T)  //T ms    
{ 
	Count1ms=T;*ch=0;
	while (Count1ms)
	{
		if (CommRecBufferTail1!=CommRecBufferHead1) break;
	}
	if (Count1ms==0) return 0;
	*ch=CommRecBuffer1[CommRecBufferHead1];
	CommRecBufferHead1++;
	if (CommRecBufferHead1==DB_RECMAXSIZE1)
	{
		CommRecBufferHead1=0;
	}
	if (CommRecBufferTail1==CommRecBufferHead1) FlagRecComm1=0;
	return 1;
}

void ClearCommRecBuffer1(void)
{
	CommRecBufferHead1=CommRecBufferTail1;
}

⌨️ 快捷键说明

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