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

📄 serial.c

📁 射频遥控器串行通讯程序
💻 C
字号:
//=====================================================
#include <SST89x5x4.H>
//#include <REG51F.H>

#include    "serial.h"
#include   "lcdm11264.h"
#define		McuClk	12
extern void  delay(uint num);
uchar  idata  cDataBuffer[32] ;			// Command Received Buffer
uchar  cpIn = 0, cpOut = 0, TmpcpOut ;  // Manage the Command Buffer

bit    bdata  bBufferFull = FALSE, bTxBufEmpty = TRUE ;
uchar recflag=0;
uchar  cDataCheckSum, TimeClick ;
uint TimeDelay;
uchar  cRecFlag, uCommandLen, uCommandID ;

unsigned int  KeyDelay;

void   SendByteToModem( uchar cbyte )
{
	while(!bTxBufEmpty);
	SBUF = cbyte;
	bTxBufEmpty = FALSE;
	cDataCheckSum ^= cbyte;		//Calculate the CheckSum
}

// Check the Checksum of the buffered command
char CheckCommandSum( char ComLen )
{
 uchar iN, uCheckSum, tpr, tByte ;
	
	uCheckSum = fRFSOH ;
	tpr = cpOut ;			// The CommandLen include the Checksum
	
	for( iN=0; iN<ComLen; iN++ ) { // The Command Header--- SOH ComId ComplexId Len
		tByte = cDataBuffer[tpr] ;
		uCheckSum  ^= tByte ;
		tpr = (++tpr)&0x1F ;
	}
	
	tByte = cDataBuffer[tpr] ;  	 		// The Send CheckSum
	if( uCheckSum==tByte ) 					// The Command is Right
		return  TRUE ;		
	else 
		return FALSE ;
}


//=========================================================
//Send RF Packget Header
//=========================================================
void	SendRFSOH( void )
{

	TimeDelay = 0 ;
//	while( TimeDelay<6 ) ;
	SendByteToModem( 0x55 ) ;
	SendByteToModem( 0xAA ) ;
	SendByteToModem( fModemFb ) ;
	SendByteToModem( fModemCb ) ;		//Send RF Protocol
	cDataCheckSum = 0 ;				//CheckSum reset
}


//-----------------------------------------------------
void OpenComm( unsigned long Baud ) 
{
	cpIn = 0 ;  cpOut = 0 ;	 
	TmpcpOut = 0 ;			//Init the Command Buffer
	bBufferFull = FALSE ;  	 
	bTxBufEmpty = TRUE ;
	
	SCON = 0x52 ;
	PCON = 0x80 ;
	TMOD = 0x21 ;
	
	TL1 = 256 - (SysOscRate/Baud/McuClk/16) ;
	TH1 = 256 - (SysOscRate/Baud/McuClk/16) ;

	PS = 1;
	TR1 = 1;
	ES = 1;		ET1 = 0 ;

	RI=0;					// Clear HW_UART receive and transmit
	TI=0;					// complete indicators.
}


//-------------------------------------------------------------
// Timer0 used for the System Clk, Timer1 for the Xray Counter
//-------------------------------------------------------------
void InitTimer0(void)
{
	TMOD  = 0x21 ;		//Timer 0, 16 Bit Timer Ways Timer 1 8 Bit autoload

	TL0 = (65536 - (SysOscRate/McuClk * TIMER0/1000)) % 256 ;    
	TH0 = (65536 - (SysOscRate/McuClk * TIMER0/1000)) / 256 ;

	ET0 = 1 ;
	TR0 = 1 ;
}

//=====================================================
static void  Timer0( void )  interrupt  1 
{
	TF0 = 0 ;
	TR0 = 0 ;

	++ TimeClick ;  //Led Light time
	++ TimeDelay ;

	++ KeyDelay;

	TL0 = (65536 - (SysOscRate/McuClk * TIMER0/1000)) % 256 ;
	TH0 = (65536 - (SysOscRate/McuClk * TIMER0/1000)) / 256 ;

  TR0 = 1 ;
}

// Communication with the Host CPU Data Buffer
/*static void SerialTxRx(void)  interrupt  4  /* UART1 */
/*{uchar i;
	if( RI ) 
	{
		if( !bBufferFull ) 
		{

			i=SBUF;
		//	outhz12(1,2,&i);
//OutNum12xy(1,5,0,&i);
			if(recflag==4)
				{
					cDataBuffer[cpIn] = i;
					cpIn = (++cpIn)&0x1F ;
					if( cpIn == cpOut )	
					{ bBufferFull = TRUE ;recflag=0;}
				}
			else
			{
				switch(recflag)
		    	{
				case 3:
					if(i==0x00)
    					recflag=4;
					else if(i==0x55) 
						recflag=1;
					else
						recflag=0;
					break;
				case 2:
					if(i==0xFF)
    					recflag=3;
					else if(i==0x55) 
						recflag=1;
					else
						recflag=0;
					break;		
				case 1:
					if(i==0xAA)
    					recflag=2;
					else if(i==0x55) 
						recflag=1;
					else
						recflag=0;
					break;
				case 0:
					if(i==0x55)
    					recflag=1;
					break;	
         		default : recflag=10;
				}
				if(recflag!=0&&recflag!=10)
				{
					cDataBuffer[cpIn] = i;
					cpIn = (++cpIn)&0x1F ;
					if( cpIn == cpOut )	
						{ bBufferFull = TRUE ;recflag=0;}
				}
			}
		}
				
		/*	if(recflag==0)
  				if(i==0x55)
    				recflag=1;
  
				if(recflag==1)
				{
            		cDataBuffer[cpIn] = i;

					cpIn = (++cpIn)&0x1F ;
					if( cpIn == cpOut )	
					{ bBufferFull = TRUE ;recflag=0;}*/
				//}	
			
		//} 
/*		RI = 0 ; // End the Receive
	}
	else 
	{
		bTxBufEmpty = TRUE ;
		TI = 0 ;
	}
}
*/
static void SerialTxRx(void)  interrupt  4  /* UART1 */
{uchar i;
static uchar  tempid;
	if( RI ) 
	{
		if( !bBufferFull ) 
		{

			i=SBUF;
		//	outhz12(1,2,&i);
//OutNum12xy(1,5,0,&i);
			if(recflag==4)
				{
					cDataBuffer[cpIn] = i;
					cpIn = (++cpIn)&0x1F ;
					if( cpIn == cpOut )	
					{ bBufferFull = TRUE ;recflag=0;}
				}
			else
			{
				switch(recflag)
		    	{
				case 3:
					if(i==(0xFF^tempid))
    					recflag=4;
					else if(i==0x00) 
						recflag=1;
					else
						recflag=0;
					break;
				case 2:
					if(i!=0x00)
					{
						tempid=i;
    					recflag=3;
					}
					else //if(i==0x55) 
						recflag=1;
					//else
					//	recflag=0;
					break;		
				case 1:
					if(i==0x21)
    					recflag=2;
					else if(i==0x00) 
						recflag=1;
					else
						recflag=0;
					break;
				case 0:
					if(i==0x00)
    					recflag=1;
					break;	
         		default : recflag=10;
				}
				if(recflag!=0&&recflag!=10)
				{
					cDataBuffer[cpIn] = i;
					cpIn = (++cpIn)&0x1F ;
					if( cpIn == cpOut )	
						{ bBufferFull = TRUE ;recflag=0;}
				}
			}
		}
				
		/*	if(recflag==0)
  				if(i==0x55)
    				recflag=1;
  
				if(recflag==1)
				{
            		cDataBuffer[cpIn] = i;

					cpIn = (++cpIn)&0x1F ;
					if( cpIn == cpOut )	
					{ bBufferFull = TRUE ;recflag=0;}*/
				//}	
			
		//} 
		RI = 0 ; // End the Receive
	}
	else 
	{
		bTxBufEmpty = TRUE ;
		TI = 0 ;
	}
}


/*-------------------------------------------------------------------
Process the Serial1 Receiver buffer to determain whether have command
have received
Command Style  #SOH #COMID #COMPLEXID #DATA LENGTH (2 BYTE)  XXXXX...XXX (DATA Body) CheckSum
--------------------------------------------------------------------*/
char	CheckDataBuffer( void )
{
 uchar  tByte, bBufLen ;
 IntToByte bToi ;

	bBufLen = (cpIn-cpOut) & 0x1F ;		//Test the Bytes In Buffer
	if( bBufferFull ) {
		bBufLen = 31 ;
//		bBufferFull = FALSE ;
	}
	if( bBufLen	>= 5 ) {	   //Have Received a Message Header
		if(cRecFlag==0x04) 
		{   //Get the Command Data Length
			tByte = cDataBuffer[TmpcpOut] ;
			bToi.Bytes.lByte = tByte ;
			TmpcpOut = (++TmpcpOut)&0x1F ;
			tByte = cDataBuffer[TmpcpOut] ;
			bToi.Bytes.hByte = tByte ;
			TmpcpOut = (++TmpcpOut)&0x1F ;
			uCommandLen = bToi.uValue + 1 ;	 	//LOW BYTES FIRST   Include the Checksum
			if( uCommandLen>30 ) {
				cRecFlag = 0 ; 
				cpOut = (TmpcpOut-2)&0x1F ; 
			}	// All the Command <= 31
			else  cRecFlag = 0x08 ;
		}

		if( cRecFlag==0x08 ) 
		{ 	// Receiver the Command Data Body
			bBufLen =  (cpIn-TmpcpOut)&0x1F ;		// The Data Body Length
			if( bBufLen >= uCommandLen ) {
				TmpcpOut =  (TmpcpOut+uCommandLen)&0x1F ; 	 // Set the TmpcpOut to the next Command
				cRecFlag = 0x10 ;    // Have Received the full Command	cpOut Point to the Command Header
			}
		}
		else 
		{	//Search the Header
			while( TmpcpOut != cpIn |(bBufferFull ==TRUE))//changed by zdy
			{//Searching for the Header of the command
				tByte = cDataBuffer[TmpcpOut] ;
				TmpcpOut = (++TmpcpOut)&0x1F ;
				switch ( cRecFlag ) {
					case 2 :
						if( (uCommandID^tByte)==0xFF ) cRecFlag = 0x04 ; 
						else { cRecFlag = 0 ;	 TmpcpOut = cpOut ; }
						break ;
					case 1 :
						uCommandID = tByte ;
						cRecFlag = 0x02 ;
						break ;
					case 0 :
						if(tByte==fRFSOH) cRecFlag = 0x01 ;
						else cRecFlag = 0x00 ;
						cpOut = TmpcpOut ;		// Set the cpOut To the Begine of the Command
						break ;					// Search the header SOH of the command
					default : 
						cRecFlag = 0x00 ;
						cpOut = TmpcpOut ;
						break ;		
				}
				if ( cRecFlag == 0x04 )  break ;   // Exit the while
				else if(TmpcpOut == cpIn)  break ;//added by zdy
			}// End while
		}
		bBufferFull = FALSE ;
 	}// End if BufferLen < 5  

if(cRecFlag==0x10)  return TRUE ;
	else return FALSE ;
//return TRUE ;
}

⌨️ 快捷键说明

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