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

📄 term3.c

📁 使用单片机C8051F330对无线发射接收模块进行配置与控制,使组件成为无线通讯中的终端,可与基站进行双向通讯
💻 C
📖 第 1 页 / 共 3 页
字号:
    j=j%256;
	
    CK1=j/16;
    CK2=j%16;
    if (CK1>9) CK1=CK1+55;
    else CK1=CK1+48;

    CCF1=1;
    TDR=CK1;	
    while(!STI);
    STI=0; 
	
    if (CK2>9) CK2=CK2+55;
    else CK2=CK2+48;
	
    CCF1=1;
    TDR=CK2;	
    while(!STI);
    STI=0; 			

    CCF1=1; //SEND CR
    TDR=13;	
    while(!STI);
    STI=0; 	

    CCF1=1;//SEND LF
    TDR=10;	
    while(!STI);
    STI=0; 	

}

void Send_Term(void)
{
  unsigned char i;
  unsigned char CK1;
  unsigned char CK2;
  short int j=0,m,n;

    STI=0;
//--------------------
//数据侦头	
    j=j+'S';	
    CCF1=1;
    TDR='S';
    while(!STI);       
    STI=0;
	
    j=j+'J';
    CCF1=1;
    TDR='J';
    while(!STI);
    STI=0;

    j=j+'-';
    CCF1=1;
    TDR='-';
    while(!STI);
    STI=0;
	
	j=j+'T';
    CCF1=1;
    TDR='T';
    while(!STI);
    STI=0;
		
    j=j+'e';
    CCF1=1;	
    TDR='e';
    while(!STI);
    STI=0;
  
    j=j+'r';
    CCF1=1;
    TDR='r';
    while(!STI);
    STI=0;
	
    j=j+'m';
    CCF1=1;	
    TDR='m';
    while(!STI);
    STI=0;
	
    j=j+',';
    CCF1=1;
    TDR=',';	
    while(!STI);
    STI=0;
//--------------------*/
//反馈终端的地址--方案1
    m = ADDRESS /10000+48;
	n = ADDRESS %10000;
	j=j+m;
	CCF1=1;
	TDR=m;
	while(!STI);
	STI=0;

    m = n /1000+48;
	n = n %1000;
	j=j+m+48;
	CCF1=1;
	TDR=m;
	while(!STI);
	STI=0;

    m = n /100+48;
	n = n %100;
	j=j+m;
	CCF1=1;
	TDR=m;
	while(!STI);
	STI=0;

    m = n /10+48;
	n = n %10+48;
	j=j+m;
	CCF1=1;
	TDR=m;
	while(!STI);
	STI=0;

	j=j+n;
	CCF1=1;
	TDR=n;
	while(!STI);
	STI=0;

    j=j+',';
    CCF1=1;
    TDR=',';	
    while(!STI);
    STI=0;
//--------------------*/
/*反馈终端的地址--方案2
    j=j+ADDRESS;
    CCF1=1;
    TDR=m;	
    while(!STI);
    STI=0;

    j=j+ADDRESS>>8;
    CCF1=1;
    TDR=m>>8;	
    while(!STI);
    STI=0;

    j=j+',';
    CCF1=1;
    TDR=',';	
    while(!STI);
    STI=0;
//--------------------*/
//命令
    j=j+'1';
    CCF1=1;
    TDR='1';	
    while(!STI);
    STI=0;

    j=j+'E';
    CCF1=1;
    TDR='E';	
    while(!STI);
    STI=0;

    j=j+',';
    CCF1=1;
    TDR=',';	
    while(!STI);
    STI=0;
//--------------------*/
//发送年月日
 /*
   for (i=0;i<6;i++)               //send UTC time
   {
      j=j+SW_Date[i];
      CCF1=1;
      TDR=SW_Date[i]; 
      while(!STI);
      STI=0;		   
   }	
*/   	
   for (i=0;i<6;i++)               //send UTC time
   {
      j=j+UTCtime[i];
      CCF1=1;
      TDR=UTCtime[i]; 
      while(!STI);
      STI=0;		   
    }		 	
	
    j=j+',';
    CCF1=1;
    TDR=',';	
    while(!STI);
    STI=0;	
//--------------------*/
//发送纬度
    for (i=0;i<9;i++)	          //send Longitude
    {
      j=j+Longitude[i];
      CCF1=1;
      TDR=Longitude[i];
      while(!STI);
      STI=0;
    }
		 
    j=j+',';
    CCF1=1;
    TDR=',';	
    while(!STI);
    STI=0;	
//--------------------*/
//发送经度
    for (i=0;i<10;i++)          //send Latitude
    {
      j=j+Latitude[i];
      CCF1=1; 
      TDR=Latitude[i];
      while(!STI);
      STI=0;		   
    }
		 
    j=j+',';
    CCF1=1;	
    TDR=',';	
    while(!STI);
    STI=0;			 
//--------------------*/
//发送DGPS
    j=j+DGPS;
    CCF1=1;                   //snd DGPS
    TDR=DGPS;	
    while(!STI);
    STI=0;	

    j=j+',';
    CCF1=1;	
    TDR=',';	
    while(!STI);
    STI=0;
//--------------------*/
//发送接收卫星颗数
 for(i=0;i<2;i++)
{   
    j=j+weixing[i];
    CCF1=1;	
    TDR=weixing[i];	
    while(!STI);
    STI=0;
}
	
    j=j+',';
    CCF1=1;	
    TDR=',';	
    while(!STI);
    STI=0;

//--------------------*/
//发送信号强度
    for (i=0;i<4;i++)          //send HDOP
    {
      j=j+HDOP[i];
      CCF1=1;
      TDR=HDOP[i];
      while(!STI);
      STI=0;		   
    }
		 
    j=j+',';	
    CCF1=1;
    TDR=',';	
    while(!STI);
    STI=0;
//--------------------*/
//发送电池状态
    j=j+Batt_State+0x30;	
    CCF1=1;
    TDR=Batt_State+0x30;	
    while(!STI);
    STI=0;

    j=j+',';                 
    CCF1=1;
    TDR=',';	
    while(!STI);
    STI=0;       
//--------------------*/
//发送GPS天线状态 
    for (i=0;i<2;i++)          
    {
      j=j+GPS_Alarm[i];
      CCF1=1;
      TDR=GPS_Alarm[i];
      while(!STI);
      STI=0;
    }  
	 
    j=j+',';
    CCF1=1;
    TDR=',';
    while(!STI);
    STI=0;
 
    CCF1=1;
    TDR='*';	
    while(!STI);
    STI=0; 	

    j=j%256;
	
    CK1=j/16;
    CK2=j%16;
    if (CK1>9)
      CK1=CK1+55;
    else
      CK1=CK1+48;

    CCF1=1;
    TDR=CK1;	
    while(!STI);
    STI=0; 
	
    if (CK2>9)
      CK2=CK2+55;
    else
      CK2=CK2+48;
	
    CCF1=1;
    TDR=CK2;	
    while(!STI);
    STI=0; 			

    CCF1=1; //SEND CR
    TDR=13;	
    while(!STI);
    STI=0; 	

    CCF1=1;//SEND LF
    TDR=10;	
    while(!STI);
    STI=0; 	
}

void Delay_1uS(unsigned int  j)
{
	unsigned char i;
	do{
	     for(i=0;i<200;i++);
	    }while(--j);
}

void Variable_Init(void)
{
  RecvByte=0;
  LcdBackLight_Delay_AfterS2410=0;

  key_press_time=0;

  key_zt=0;key_time=0;

  riqi[0]=0+0x30;
  riqi[1]=6+0x30;
  riqi[2]=0+0x30;
  riqi[3]=7+0x30;
  riqi[4]=1+0x30;
  riqi[5]=8+0x30;

  GPS_Alarm[0]=8+0x30;
  GPS_Alarm[1]=5+0x30;
  GPS_SW = 0;

  weixing[0]=0+0x30;
  weixing[1]=0+0x30;

  GPS_Current_pre=0;
  GPS_Current=0;
  GPS_time=0;
  GPS_Alarm_js=0;

  Batt_State = 9+0x30;

  for (ii=0;ii<9;ii++)	       // Longitude
  {
    Longitude[ii]=0x30;
  }
  for (ii=0;ii<10;ii++)        // Latitude
  {
    Latitude[ii]=0x30;
  }
    
  DGPS=0x30;

  for (ii=0;ii<4;ii++)         // HDOP
  {
    HDOP[ii]=0x30;
  }	

  SW_RecvByte = 0;
  
  for(ii=0;ii<7;ii++)
      SW_Header[ii] = 0;
  SW_Recv_Command = 0;
  for(ii=0;ii<5;ii++)
      SW_Address[ii] = 0;
  SW_FrameLength = 0;
  SW_IsFrame = 0;
  SW_RecvOver = 0;
  SRI=0;
  surt_js=0;
  b_light=0x30;
}

//----------------------------------------------------------------------------
//Interrupt Service Routine
//----------------------------------------------------------------------------
void PCA_ISR(void) interrupt 11
{
    static char SUTXST = 0;              //SW_UART TX????
	static char SURXST = 0;              //SW_UART RX????
	static unsigned RXSHIFT = 0;         //SW_UART RX?????
    unsigned int PCA_TEMP;               //??????,????PCA?????
	                                     //???

   // Check receive interrupt flag first; service if CCF0 is set.
	if (CCF0){
		CCF0 = 0;										// Clear interrupt flag.
		switch (SURXST){
			
			// State 0: START bit received.
			// In this state, a negative edge on SW_TX has caused the interrupt,
			// meaning a START has been detected and the PCA0CP0 registers have 
			// captured the value of PCA0.
			// - Check for receive enable and good START bit
			// - Switch PCA module 0 to software timer mode
			// - Add 3/2 bit time to module 0 capture registers to sample LSB.
			// - Increment RX state variable.
			case 0:
			    
				if (SREN & ~SW_RX){					// Check for receive enable and a good
													// START bit.  
																
					PCA_TEMP = (PCA0CPH0 << 8);	    // Read module 0 contents into
					PCA_TEMP |= PCA0CPL0;			// PCA_TEMP.

					PCA_TEMP += TH_TIME_COUNT;		// Add 3/2 bit times to PCA_TEMP

					PCA0CPL0 = PCA_TEMP;				// Restore PCA0CPL0 and PCA0CPH0
					PCA0CPH0 = (PCA_TEMP >> 8);	// with the updated value

					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.

			
				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:
                    STXBSY = 1;                         // SW_UART TX?
					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);	// module 1 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 1 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.					
					STXBSY = 0;							// SW_UART TX free.	
					break;
				}
}


}

⌨️ 快捷键说明

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