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

📄 gpt1.c

📁 开发环境为keil166
💻 C
字号:
//****************************************************************************
// @Module        General Purpose Timer Unit (GPT1)
// @Filename      GPT1.C
// @Project       can_configure.dav
//----------------------------------------------------------------------------
// @Controller    Infineon XC164CS-16F20
//
// @Compiler      Keil
//
// @Codegenerator 2.8
//
// @Description   This file contains functions that use the GPT1 module.
//
//----------------------------------------------------------------------------
// @Date          2009-4-4 17:03:46
//
//****************************************************************************

// USER CODE BEGIN (GPT1_General,1)
//CAN屏蔽码(1与验证码相同 0通过) 验证码正确
//CAN发送数据正常
//串口通讯正常

// USER CODE END



//****************************************************************************
// @Project Includes
//****************************************************************************

#include "MAIN.H"

// USER CODE BEGIN (GPT1_General,2)

// USER CODE END


//****************************************************************************
// @Macros
//****************************************************************************

// USER CODE BEGIN (GPT1_General,3)

// USER CODE END


//****************************************************************************
// @Defines
//****************************************************************************

// USER CODE BEGIN (GPT1_General,4)

// USER CODE END


//****************************************************************************
// @Typedefs
//****************************************************************************

// USER CODE BEGIN (GPT1_General,5)

// USER CODE END


//****************************************************************************
// @Imported Global Variables
//****************************************************************************

// USER CODE BEGIN (GPT1_General,6)

// USER CODE END


//****************************************************************************
// @Global Variables
//****************************************************************************

// USER CODE BEGIN (GPT1_General,7)
char id_select=0;
unsigned char id0=0,id1=0; //110h的计数器和111h的计数器
// USER CODE END


//****************************************************************************
// @External Prototypes
//****************************************************************************

// USER CODE BEGIN (GPT1_General,8)

// USER CODE END


//****************************************************************************
// @Prototypes Of Local Functions
//****************************************************************************

// USER CODE BEGIN (GPT1_General,9)

// USER CODE END


//****************************************************************************
// @Function      void GPT1_vInit(void) 
//
//----------------------------------------------------------------------------
// @Description   This is the initialization function of the GPT1 function 
//                library. It is assumed that the SFRs used by this library 
//                are in its reset state. 
//
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    None
//
//----------------------------------------------------------------------------
// @Date          2009-4-4
//
//****************************************************************************

// USER CODE BEGIN (Init,1)

// USER CODE END

void GPT1_vInit(void)
{
  // USER CODE BEGIN (Init,2)

  // USER CODE END

  ///  -----------------------------------------------------------------------
  ///  Configuration of Timer Block Prescaler 1:
  ///  -----------------------------------------------------------------------
  ///  - prescaler for timer block 1 is 8


  ///  -----------------------------------------------------------------------
  ///  Configuration of the GPT1 Core Timer 3:
  ///  -----------------------------------------------------------------------
  ///  - timer 3 works in timer mode
  ///  - external up/down control is disabled
  ///  - prescaler factor is 32
  ///  - up/down control bit is reset
  ///  - alternate output function T3OUT (P3.3) is disabled
  ///  - timer 3 output toggle latch (T3OTL) is set to 0

  GPT12E_T3CON   =  0x0002;      // load timer 3 control register
  GPT12E_T3      =  0xB1E0;      // load timer 3 register

  ///  -----------------------------------------------------------------------
  ///  Configuration of the GPT1 Auxiliary Timer 2:
  ///  -----------------------------------------------------------------------
  ///  - timer 2 works in timer mode
  ///  - external up/down control is disabled
  ///  - prescaler factor is 32
  ///  - up/down control bit is reset

  GPT12E_T2CON   =  0x0002;      // load timer 2 control register
  GPT12E_T2      =  0xE796;      // load timer 2 register

  ///  -----------------------------------------------------------------------
  ///  Configuration of the GPT1 Auxiliary Timer 4:
  ///  -----------------------------------------------------------------------
  ///  - timer 4 works in timer mode
  ///  - external up/down control is disabled
  ///  - prescaler factor is 8
  ///  - up/down control bit is reset
  ///  - timer 4 run bit is reset

  GPT12E_T4CON   =  0x0000;      // load timer 4 control register
  GPT12E_T4      =  0x0000;      // load timer 4 register

  ///  -----------------------------------------------------------------------
  ///  Configuration of the used GPT1 Port Pins:
  ///  -----------------------------------------------------------------------


  ///  -----------------------------------------------------------------------
  ///  Configuration of the used GPT1 Interrupts:
  ///  -----------------------------------------------------------------------
  ///  timer 2 service request node configuration:
  ///  - timer 2 interrupt priority level (ILVL) = 9
  ///  - timer 2 interrupt group level (GLVL) = 1
  ///  - timer 2 group priority extension (GPX) = 0

  GPT12E_T2IC    =  0x0065;     

  ///  timer 3 service request node configuration:
  ///  - timer 3 interrupt priority level (ILVL) = 11
  ///  - timer 3 interrupt group level (GLVL) = 1
  ///  - timer 3 group priority extension (GPX) = 0

  GPT12E_T3IC    =  0x006D;     


  // USER CODE BEGIN (GPT1_Function,3)

  // USER CODE END

  GPT12E_T2CON_T2R  =  1;        // set timer 2 run bit

  GPT12E_T3CON_T3R  =  1;        // set timer 3 run bit

} //  End of function GPT1_vInit


//****************************************************************************
// @Function      void GPT1_viTmr3(void) 
//
//----------------------------------------------------------------------------
// @Description   This is the interrupt service routine for the GPT1 timer 3. 
//                It is called up in the case of over or underflow of the 
//                timer 3 register.
//                If the incremental interface mode is selected it is called 
//                up if count edge or count direction was detected.
//                
//                Please note that you have to add application specific code 
//                to this function.
//
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    None
//
//----------------------------------------------------------------------------
// @Date          2009-4-4
//
//****************************************************************************

// USER CODE BEGIN (Tmr3,1)	
 int velocity=0;	
// USER CODE END

void GPT1_viTmr3(void) interrupt T3INT
{
  // USER CODE BEGIN (Tmr3,2)
 unsigned char data0=0,data1=0,data2=0,data3=0,
         data4=0,data5=0,data6=0,data7=0;
 unsigned char check=0,velocityTemp=0;
 int velocity=0;
  // USER CODE END


  // USER CODE BEGIN (Tmr3,5)
  GPT12E_T3CON_T3R  =  0; 

  if (id_select == 0)
  	  {
	  CAN_MSGARL1    =  0x0000;      // load arbitration register low
      CAN_MSGARH1    =  0x0440;      // load arbitration register high
	  
	  data1=0xfd;
	  data2=0;
	  data3=0;
	  data4=0;
	  data5=rec_data[1];
	  data6=rec_data[2];

	  check=0x01+0x10+8+id0+data1+data2+data3+data4+data5+data6;  /* data1:雷达工作模式
																	 data2:全0
																	 data3:全0
																	 data4:全0
																	 data5:转弯半径(H)
																	 data6:转弯半径(L)
																  */ 
	  CAN_MSGDRL10  =  0xFD00+id0;
	  CAN_MSGDRH10  =  data2+(data3<<8);
	  CAN_MSGDRL14  =  data4+(data5<<8);
  	  CAN_MSGDRH14  =  data6+(check<<8);      

      id_select = 1;
	  if (id0==255)
	      id0=0;
	  else	 
		 id0++;
  	   }
  else
  	  {
	  CAN_MSGARL1    =  0x0000;      // load arbitration register low
      CAN_MSGARH1    =  0x0444;      // load arbitration register high
	  
	  velocityTemp=	(rec_data[4]>>4)*10+(rec_data[4]&0x0f);	//输入0x15就是15km/h   
	  if (rec_data[3] == 0x01)
	      velocity=-velocityTemp*256; 
	  else
	      velocity=velocityTemp*256;

	  data1=velocity>>8;
	  data2=velocity;
	  data3=0;
	  data4=0;
	  data5=0;
	  data6=0;

	  check=0x01+0x11+8+id1+data1+data2+data3+data4+data5+data6;  /* data1:车速(H)
																	 data2:车速(L)
																	 data3:全0
																	 data4:全0
																	 data5:全0
																	 data6:全0
																  	 */
	  CAN_MSGDRL10  =  (data1<<8)+id1;
	  CAN_MSGDRH10  =  data2+(data3<<8);
	  CAN_MSGDRL14  =  data4+(data5<<8);
  	  CAN_MSGDRH14  =  data6+(check<<8);      

	  id_select = 0;
 	  if (id1==255)
	      id1=0;
	  else	 
		 id1++;
	  }			 
  
  CAN_vTransmit(1);	
  GPT12E_T3      =  0xB1E0;      // load timer 3 register
  GPT12E_T3CON_T3R  =  1; 	    

  // USER CODE END

} //  End of function GPT1_viTmr3


//****************************************************************************
// @Function      void GPT1_viTmr2(void) 
//
//----------------------------------------------------------------------------
// @Description   This is the interrupt service routine for the GPT1 timer 2. 
//                It is called up in the case of over or underflow of the 
//                timer 2 register.
//                If the incremental interface mode is selected and the 
//                interrupt for this mode is not disabled it is called up if 
//                count edge or count direction was detected.
//                
//                Please note that you have to add application specific code 
//                to this function.
//
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    None
//
//----------------------------------------------------------------------------
// @Date          2009-4-4
//
//****************************************************************************

// USER CODE BEGIN (Tmr2,1)
unsigned char ii=1;
unsigned char  count_send=0;
// USER CODE END

void GPT1_viTmr2(void) interrupt T2INT
{
  // USER CODE BEGIN (Tmr2,2)
  unsigned char j=0;
  // USER CODE END


  // USER CODE BEGIN (Tmr2,5)	  
	GPT12E_T2CON_T2R =0;
	while(!ASC0_ubTxDataReady());
	ASC0_vSendData(0xaf); 

  switch(ii)
  {
	case 1:
		   for (j=0;j<9;j++)
		       {
				while(!ASC0_ubTxDataReady());
			    ASC0_vSendData(can_data1[j]);
				}
		   ii++;
		   break;
 	case 2:
		   for (j=0;j<9;j++)
		       {
				while(!ASC0_ubTxDataReady());
			    ASC0_vSendData(can_data2[j]);
				}
		   ii++;
		   break;
 	case 3:
		   for (j=0;j<9;j++)
		       {
				while(!ASC0_ubTxDataReady());
			    ASC0_vSendData(can_data3[j]);
				}
		   ii++;								  
		   break;
 	case 4:
		   for (j=0;j<9;j++)
		       {
				while(!ASC0_ubTxDataReady());
			    ASC0_vSendData(can_data4[j]);
				}
		   ii++;
		   break;
 	case 5:
		   for (j=0;j<9;j++)
		       {
				while(!ASC0_ubTxDataReady());
			    ASC0_vSendData(can_data5[j]);
				}
		   ii++;
		   break;
 	case 6:
		   for (j=0;j<9;j++)
		       {
				while(!ASC0_ubTxDataReady());
			    ASC0_vSendData(can_data6[j]);
				}								

  
		   ii++;
		   break;
 	case 7:
		   for (j=0;j<9;j++)
		       {
				while(!ASC0_ubTxDataReady());
			    ASC0_vSendData(can_data7[j]);
				}
		   ii++;								  
		   break;
 	case 8:
		   for (j=0;j<9;j++)
		       {
				while(!ASC0_ubTxDataReady());
			    ASC0_vSendData(can_data8[j]);
				}
		   ii++;
		   break;
 	case 9:
		   for (j=0;j<9;j++)
		       {
				while(!ASC0_ubTxDataReady());
			    ASC0_vSendData(can_data9[j]);
				}
		   ii++;
		   break;
 	case 10:
		   for (j=0;j<9;j++)								

 
		       {
				while(!ASC0_ubTxDataReady());
			    ASC0_vSendData(can_data10[j]);
				}
		   ii++;
		   break;
 	case 11:
		   for (j=0;j<9;j++)
		       {
				while(!ASC0_ubTxDataReady());
			    ASC0_vSendData(can_data11[j]);			   
				}
		   ii++;
		   break;
 	case 12:
		   for (j=0;j<9;j++)
		       {
				while(!ASC0_ubTxDataReady());
			    ASC0_vSendData(can_data12[j]);
				}
		   ii++;
		   break;
 	case 13:										

	
		  /* for (j=0;j<10;j++)
		       {
				while(!ASC0_ubTxDataReady());
			    ASC0_vSendData(can_data13[j]);
				}  */
		   ii++;
		   break;
	default: break;
  }									 
	//while(!ASC0_ubTxDataReady());
	//ASC0_vSendData(0xbf); 
//	count_send++;
	if ( ii == 13 ) ii=1;

  GPT12E_T2      =  0xE796;
  GPT12E_T2CON_T2R = 1;		 
  // USER CODE END

} //  End of function GPT1_viTmr2




// USER CODE BEGIN (GPT1_General,10)

// USER CODE END

⌨️ 快捷键说明

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