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

📄 qd.c

📁 基于GPRS无线网络和IP协议的数据传输系统 通信模块C51单片机程序
💻 C
📖 第 1 页 / 共 5 页
字号:
					PCA0CPM0 = 0x49;					// Change module 0 to software
															// timer mode, interrupts enabled.

					SURXST++;							// Update RX state variable.
				}
				break;
			
			// States 1-8: Bit Received
			// - Sample SW_RX pin
			// - Shift new bit into RXSHIFT
			// - Add 1 bit time to module 0 capture registers
			// - Increment RX state variable
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
					
				RXSHIFT = RXSHIFT >> 1;				// Shift right 1 bit
				if (SW_RX)								// If SW_RX=1, 
					RXSHIFT |= 0x80;					// shift '1' into RXSHIFT msb
				
				PCA_TEMP = (PCA0CPH0 << 8);		// Read module 0 contents into
				PCA_TEMP |= PCA0CPL0;				// PCA_TEMP.

				PCA_TEMP += TIME_COUNT;				// Add 1 bit time to PCA_TEMP

				PCA0CPL0 = PCA_TEMP;					// Restore PCA0CPL0 and PCA0CPH0
				PCA0CPH0 = (PCA_TEMP >> 8);		// with the updated value
				
				SURXST++;								// Update RX state variable.
				break;

			// State 9: 8-bits received, Capture STOP bit.
			// - Move RXSHIFT into RDR.
			// - Set SRI (indicate receive complete).
			// - Prepare module 0 for next transfer.
			// - Reset RX state variable.
			// - Trigger IE7 if user-level interrupt support is enabled.
			case 9:

				RDR = RXSHIFT;							// Move received data to receive register.
				SRI = 1;									// Set receive complete indicator.

				PCA0CPM0 = 0x11;						// Switch module 0 to negative capture
															// mode; interrupt enabled for START
															// detection.

				SURXST = 0;								// Reset RX state variable.

				if (SES){								// If user-level interrupt support enabled
					EIE2 |= 0x20;						// Enable IE7.
					PRT1IF |= 0x80;					// Trigger IE7.
				}
				break;
				
			}
		}
		
		// Check Transmit interrupt; service if CCF1 is set.
		else if (CCF1){ 
			CCF1 = 0;									// Clear interrupt flag
			switch (SUTXST){
				
				// State 0: Transmit Initiated.
				// Here, the user has loaded a byte to transmit into TDR, and set the
				// module 1 interrupt to initiate the transfer.
				// - Transmit START bit (drop SW_TX)
				// - Read PCA0, add one bit time, & store in module 1 capture registers
				//   for first bit.
				// - Increment TX state variable.
				case 0:

					SW_TX = 0;							// Drop TX pin as START bit.
					
					PCA_TEMP = PCA0L;					// Read PCA counter value into
					PCA_TEMP |= (PCA0H << 8);		// PCA_TEMP.

					PCA_TEMP += TIME_COUNT;			// Add 1 bit time.

					PCA0CPL1 = PCA_TEMP;				// Store updated match value into
					PCA0CPH1 = (PCA_TEMP >> 8);	// module1 capture/compare registers.

					PCA0CPM1 |= 0x48;					// Enable module 1 software timer.

					SUTXST++;							// Update TX state variable.				
					break;

				// States 1-9: Transmit Bit.
				// - Output LSB of TDR onto TX
				// - Shift TDR 1 bit right.
				// - Shift a '1' into MSB of TDR for STOP bit in State 9.
				// - Add 1 bit time to module 1 capture register
				case 1:
				case 2:
				case 3:
				case 4:
				case 5:
				case 6:
				case 7:
				case 8:
				case 9:
					
					SW_TX = (TDR & 0x01);			// Output LSB of TDR onto SW_TX pin.
					TDR >>= 1;							// Shift TDR right 1 bit.
					TDR |= 0x80;						// Shift '1' into MSB of TDR for
															// STOP bit in State 9.

					PCA_TEMP = (PCA0CPH1 << 8);	// Read module 0 contents into
					PCA_TEMP |= PCA0CPL1;			// PCA_TEMP.

					PCA_TEMP += TIME_COUNT;			// Add 1 bit time to PCA_TEMP
	
					PCA0CPL1 = PCA_TEMP;				// Restore PCA0CPL1 and PCA0CPH1
					PCA0CPH1 = (PCA_TEMP >> 8);	// with the updated value	

					SUTXST++;                     // Update TX state variable.
					break;
					
				// State 10: Last bit has been transmitted.  Transmit STOP bit
				// and end transfer.  
				// - Transmit STOP bit
				// - Set TX Complete indicator, clear Busy flag
				// - Reset TX state
				// - Prepare module 1 for next transfer.
				// - Trigger IE7 interrupt if user-level interrupts enabled.
				case 10:

					STI = 1;								// Indicate TX complete.
					SUTXST = 0;							// Reset TX state.
					SW_TX = 1;							// SW_TX should remain high.

					PCA0CPM1 = 0x01;					// Disable module 1 software timer; leave
															// interrupt enabled for next transmit.					
					
					if (SES){							// If user-level interrupt support enabled:
						EIE2 |= 0x20;					// Enable IE7.
						PRT1IF |= 0x80;				// Trigger IE7.
					}
					STXBSY = 0;							// SW_UART TX free.	
					break;
				}
		}
}
//-----------------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------

//WAVECOM 数据接收子程序:串口0中断服务程序

void  Serial0_WAVECOM(void)   interrupt 4

{   
    EA=0;
	if (RI0)	
	{	 
		RI0=0;
		if(RECEIVE_FLAG==0)
		{
			if(SBUF0==0x7E)
			{
				RECEIVE_FLAG=1;
				RECEIVEBUFFERPOINT=RECEIVEBUFFER;
			}
        }
		else
		{
			if(SBUF0==0x7E)
			{ 
			  if(RECEIVEBUFFERPOINT!=RECEIVEBUFFER)     //排除连续收到两个7E,数据长度为0的错误情况
				 {
				  RECEIVE_FLAG=0;
				  PPP_Rec_Complete = 1;
                 }
			}
			else
			{
				if(PADDING_Flag==1)
	  			{
	  				*RECEIVEBUFFERPOINT=SBUF0;
	  	        	*RECEIVEBUFFERPOINT^=0x20;
	  	        	PADDING_Flag=0;
	  	        	RECEIVEBUFFERPOINT++;
	    		}
	    		else
	    		{
	    			if(SBUF0==0x7D)
	    				PADDING_Flag=1;
	    			else
	    			{
	    				*RECEIVEBUFFERPOINT=SBUF0;
	  	           		RECEIVEBUFFERPOINT++;
	  	        	}
	  	        }
			}
		}
   	}

    EA=1;
}



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


//MDB数据接收子程序 :串口1中断服务程序
void Receive_MDB(void) interrupt 20 
{			
	unsigned char temp,Rec_Data,i;
    
	EIE2 &= 0xbf; //关闭串口1中断 
 
 	if(SCON1 & 0x01==0x01){ 
     
	    SCON1 &= 0xfe; // 清串口1标字位
	   
	    Rec_Data=SBUF1;
	    
	if (ReceiveFlag==0)
      {   
                                            //Has received the first byte 
	    switch ( Rec_Data)
	    {
	    case 0x78:                                        //receive the command reset   
		     ReceiveFlag=1;
			 Information_Type=0;    
		     
			  break;
		case 0x79:                                        //receive the device error status          
		     ReceiveFlag=1;
			 Information_Type=1;                          
	         
			  break;
		case 0x7a:                                        //receive the column error status
		     ReceiveFlag=1;
			 Information_Type=2; 	                      
		     
			  break;
		case 0x7b:                                        //receive the goods status
			 ReceiveFlag=1; 
			 Information_Type=3; 	
	        
			   break;
		case 0x7c:                                        //receive the sale info 
		     ReceiveFlag=1;
			 Information_Type=4;                            
	          break;
	    case 0x7d:                                        //receive the device info        
		     ReceiveFlag=1;
			 Information_Type=5;                           
	          break;  
	
		case 0x7e:                                        //request the control info 
	         ReceiveFlag=1;
			 Information_Type=6;
               break;
		case 0x0 :
		      
			 SCON1 |= 0x20;                            // SM2位置1,只在地址时中断
	          Rec_Ack_Flag=1;
		     // Ctrl_Complete_Flag=1;
		//	 Deal_Flag=0;
		//	 if(Ctrl_Info_Flag==1)                        //控制信息已下发
			   // Ctrl_Info_Flag=0;
			 break;
		default:
		     break; 
	    }
	    Receive_Data_Num=0;                              //clear the number of the receiving data	   
        InfoData_BufferPoint=InfoData_Buffer;
        *InfoData_BufferPoint=Rec_Data;
		Receive_Data_Num++;
	  	  
	    SCON1 &= 0xdf; //  清SM2位,允许中断,无论数据还是地址  
	   
	  } 
	  else 
	   {  
         
          *(InfoData_BufferPoint+Receive_Data_Num)=Rec_Data;
          Receive_Data_Num++;
       	  
		  switch (Information_Type)
		    {
			case 0: 
			      if( Receive_Data_Num==2)
		           {  
				   		SCON1 |= 0x20;   // SM2位置1,只在地址时中断   
                        if (Check_Info_Data(InfoData_Buffer,Receive_Data_Num))       //?????judge if the check_num is right
                        { 	
						 Deal_Flag=1;					
						
                    	}
                        else
						{ 
						 	Send_Retransmit(); // 不做任何操作,相当于NAK 
						    Receive_Data_Num=0;
					        ReceiveFlag=0;}               //enable to receive the information data again 					                
                 		}
                    break;
			 
			 
			 case 1:
			      if( Receive_Data_Num==8) 
                    {   
						SCON1 |= 0x20;   // SM2位置1,只在地址时中断      
					    if (Check_Info_Data(InfoData_Buffer,Receive_Data_Num))  
    	                 {  
						   Deal_Flag=1;
						    
                         	  	  						
				/*		 if (First_Rec_Flag==0)
						      {
							   counter=5;                           //3秒为回应时间         
						       EX1=1;
                               First_Rec_Flag=1;
							   }*/ 
						   }                             
                         else
						 {
						 	Send_Retransmit(); //不做任何操作,相当于NAK 
						    Receive_Data_Num=0;
     					  	ReceiveFlag=0; 
						  }            //enable to receive the information data again 					  
	                }
               		break;
			  case 2 :
			        if( Receive_Data_Num==8) 
                    {    
						SCON1 |= 0x20;   // SM2位置1,只在地址时中断      
					    if (Check_Info_Data(InfoData_Buffer,Receive_Data_Num))   
    	                 {  
						     Deal_Flag=1;
						                           
						 /* if (First_Rec_Flag==0)
						      {
							   counter=5;         
						       EX1=1;//打开外部中断1
                               First_Rec_Flag=1;
							   } */ 
						  				  
						  }                             
                         else
						 { 
						  Send_Retransmit(); //不做任何操作,相当于NAK 
						  Receive_Data_Num=0;
     					  ReceiveFlag=0;
						  }              //enable to receive the information data again 					  
	                } 
	           		break;
			  case 3: 
			       if( Receive_Data_Num==8) 
                    {
					   SCON1 |= 0x20;   // SM2位置1,只在地址时中断    
					   if (Check_Info_Data(InfoData_Buffer,Receive_Data_Num))  
    	                  {  
						    Deal_Flag=1;
						                            
						 /* if (First_Rec_Flag==0)
						      {
							   counter=5;         
						       EX1=1;// EIE2|=0x01;
                               First_Rec_Flag=1;
							   }  */
					   
						   }                             
                       
					  else
						 
						 {
						  Send_Retransmit(); //不做任何操作,相当于NAK  
					   	  Receive_Data_Num=0;
     					  ReceiveFlag=0;
						  }               //enable to receive the information data again 					  
	                 }    
	          		break;
			   case 4:
			        if( Receive_Data_Num==4) 
                    { 
					     SCON1 |= 0x20;   // SM2位置1,只在地址时中断   
					     if (Check_Info_Data(InfoData_Buffer,Receive_Data_Num))  
    	                 {     Deal_Flag=1;
						     
                          			  
					 	   /*if (First_Rec_Flag==0)
						      {
							   counter=5;         
						       EX1=1; // EIE2|=0x01;
                               First_Rec_Flag=1;
							   }*/  
						   
						  }                             
                        
					     else
						 
						  {
						  	Send_Retransmit(); //不做任何操作,相当于NAK 
							Receive_Data_Num=0;
     					  	ReceiveFlag=0;
						  }               //enable to receive the information data again 					  
	                  }
	            	break;
			   case 5:
			    	if( Receive_Data_Num==30) 
                    {
					     SCON1 |= 0x20;   // SM2位置1,只在地址时中断 
					    
						 if (Check_Info_Data(InfoData_Buffer,Receive_Data_Num))  
    	                 {
						      Deal_Flag=1;
						   
                               
						  	/*if (First_Rec_Flag==0)
						      {
							   counter=5;         
						       EX1=1; //   EIE2|=0x01;
                               First_Rec_Flag=1;
							   }*/  
					     }                             

⌨️ 快捷键说明

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