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

📄 lpc2000_can_sys.c

📁 在lpc2468上使用
💻 C
📖 第 1 页 / 共 2 页
字号:
 install_irq(UART0_INT, (void *)IRQ_UART0  , 2 );
 install_irq(CAN_INT,   (void *)IRQ_CAN    , 3 );

}

/****************************************************************************
* Name: Delay()
* Function: Delay 
* Input(s): delaytime		30 about 0.5s
* Returns : none
****************************************************************************/
void  delay(UInt32  delaytime)
{  UInt32  i;

   for(; delaytime>0; delaytime--) 
      for(i=0; i<50000; i++);
}

/****************************************************************************
* Name: uart0_config(UInt32,UART0MODE)
* Function: UART0 config 
* Input(s): Baudrate
*           U0_config
* Returns : none
****************************************************************************/
void uart0_config(UInt32 Baudrate,UART0MODE U0_config)
{
  UInt32 temp_uart0;
//-------------------------
// UART0 Baudrate config
//-------------------------
  U0LCR = 0x80;			// DLAB=1, Can use U0DLM, U0DLL 
  temp_uart0 = (Fpclk/16)/Baudrate;
  
  U0DLM = temp_uart0>>8;
  U0DLL = temp_uart0&0xFF;
  
//-------------------------
// UART0 mode config
//------------------------- 
  temp_uart0 = U0_config.Datalength - 5;				// Data Length prepare
  
  if( U0_config.Stopbit == 2 ) temp_uart0 |= 0x04;		// Data Stop bit prepare
  
  if( U0_config.Paritybit != 0 )						// Data Parity bit prepare
    {
      U0_config.Paritybit = U0_config.Paritybit - 1;
      temp_uart0 |= 0x08;
    }
  temp_uart0 |= U0_config.Paritybit<<4;
  
  
  U0LCR = temp_uart0; 	// Data Length, Stop bit, Parity bit  setup 
  U0FCR = 0x07;		/* Enable and reset TX and RX FIFO. */
  U0IER = IER_RBR  | IER_RLS;	
}

/****************************************************************************
* Name: IRQ_Timer1()
* Function: Timer1 interrupt function, inverse LED4(P0.25)
* Input(s): none
* Returns : none
****************************************************************************/
void __irq IRQ_Timer1(void) 
{  
  
  LED_Counter++;

  if( LED_Counter == 50)		// 10ms*50=0.5s led inverse
    {
      if( LED_Flag == 0) LED_Flag = 1;
      else LED_Flag = 0;
  
      LED_Counter = 0;
    }
             
   T1IR = 0x01;	    			// CLR Timer1 interrupt flag
   VICVectAddr = 0x00;			// Inform VIC interrupt is end
}


/****************************************************************************
* Name: IRQ_UART0()
* Function: UART0 interrupt
* Input(s): none
* Returns : none
****************************************************************************/
void   __irq IRQ_UART0(void)
{  
  UInt32 Temp_UART0_IRQ;
  
  if( (U0IIR & 0x0F) == 0x04 )
    {
	  Temp_UART0_IRQ = U0RBR - 48;
	  
	  switch( Temp_UART0_IRQ )
	  {
        case 1:
		  System_Status = 1;   // indicate CAN simple communication
		   break;
		case 2:
		    System_Status = 2;   // indicate CAN communication with ACC
		    break;
		case 3:
			System_Status = 3;	 // indicate CAN communication with FullCAN
		case 41:                 // receive "Y"
		case 73:				 // receive "y"
		    if( System_Status == 4) System_Status = 5;
			break;
		case 30:				 // receive "N"
		case 62:				 // receive "n"
			if( System_Status == 4 ) System_Status = 7;
			break;
	  	case 49:		         // when receive "a", reverse LED2
            Temp_UART0_IRQ = IOSET1;
            if( (Temp_UART0_IRQ & LED1CON) == 0 )
              {  
              //  FIO2SET = LED1CON;
              }
            else
              {  
              //  FIO2CLR = LED1CON;
              }
		  	break;
	  	case 50:		// when receive "b", reverse LED1	  	
		  	Temp_UART0_IRQ = IOSET1;
            if( (Temp_UART0_IRQ & LED2CON) == 0 )
              {  
              //  FIO2SET = LED2CON;
              }
            else
              {  
             //   FIO2CLR = LED2CON;
              }              
		  	break;
 		default:
			break;
      }      
    }
  
  
  Temp_UART0_IRQ = U0RBR;		//clear UART0-Rx interrupt flag, when U0IIR != 04,
  VICVectAddr = 0x00;
}


/****************************************************************************
* Name: IRQ_CAN()
* Function: IRQ_CANinterrupt
   中断处理:主要接受数据,处理传输中的错误(统计错误数)
* Input(s): none
* Returns : none
****************************************************************************/
void   __irq IRQ_CAN(void) //
{
 Print_Chars_to_Screen( LiuJiaju_IRQ );
   //Check the FullCAN (FCANIC0) bits 
#ifdef LPC2000_CANDRIVER_STD_FULLCAN
   if(FCANIC0 !=0 || (FCANIC1 !=0)){//有中断,AF中
   	CAN2_FULLCANRxCount++;
	Print_Chars_to_Screen(LiuJiaju_FULLCAN_Rec);
        CAN_FULLCAN_Rx2();
			
   }
#endif
	
   CANStatus = CAN_RX_SR;	//非FuLLCAN的形式
   if ( CANStatus & (1 << 8) )      
   {
	CAN1RxCount++;
//	CAN_Rx1();
   }
   if ( CANStatus & (1 << 9) )
   {													  
	CAN2RxCount++;
	Print_Chars_to_Screen( LiuJiaju_AF_Rec );
	CAN_Rx2();

   }
   if ( CAN1GSR & (1 << 6 ) )  //ES,Error Status
   {
	/* The error count includes both TX and RX */
	CAN1ErrCount = (CAN1GSR >> 16 );  //RXERR,the current value of the RX  error counter
   }
  if ( CAN2GSR & (1 << 6 ) )
  {
	/* The error count includes both TX and RX */
	CAN2ErrCount = (CAN2GSR >> 16 );
  }

  VICVectAddr = 0;		/* Acknowledge Interrupt */
}


/****************************************************************************
* Name: CAN_FULLCAN_Rx2()
* Function: CAN_FULLCAN_Rx2
   FULLCAN模式下,接收数据,好像有问题,深究
* Input(s): none
* Returns : none
****************************************************************************/
#ifdef LPC2000_CANDRIVER_STD_FULLCAN
void CAN_FULLCAN_Rx2( void ){
//Print_Chars_to_Screen(LiuJiaju_FULLCAN_Rec);

plpc2000CANdriver_RXObj_t pCAN1_Rcv_Data_IRQ;
FULLCAN_MSG *FULLCAN_msgs;
unsigned int Dat1,DatA,DatB;
unsigned int msg_index = 0;
unsigned char test_bit =0;
int i =0,j=0;
unsigned int interrut_word = FCANIC0;//interrupt 判别位
    for(j=0 ; j <2; j++){				 //选择判别位
	  if((j==0x0) && (FCANIC0 !=0) ){
	      interrut_word = FCANIC0;
	   }
	  else if( (j==1) && (FCANIC1 !=0) ) {
		  interrut_word = FCANIC1;
	  }
	  else{
	 	continue;
	  }
	  if(interrut_word !=0){
	   for(i=0; i<=31;i++){
	     test_bit = interrut_word	& 0x1;	 //????
	     interrut_word = interrut_word >>1;
	     if(test_bit){
	      	msg_index=i;
	      	FULLCAN_msgs= (FULLCAN_MSG *)(CAN_EOT + 0xE0038000 + msg_index*12);
			                    //0xE0038000:CAFMEM,AF RAM的起始地址,CAN_EOT:table的长度
								//CAN_EOT+0xE0038000就是ID's table的结束地址,即ENDofTable
	      	Dat1=FULLCAN_msgs->Dat1;
			do{
			  if((Dat1 & 0x3000000) != 0x1000000){	 //sem=01?AF is updateing the content?
				  if((Dat1 & 0x3000000) == 0x3000000){//sem=11?AF has finished the update content
					  FULLCAN_msgs->Dat1 = Dat1 & 0xFCFFFFFF;//sem=00,cpu is in process of reading
					  DatA = FULLCAN_msgs->DatA;
			      	  DatB = FULLCAN_msgs->DatB;
					  Dat1=FULLCAN_msgs->Dat1;
					  if((Dat1 & 0x3000000) == 0x0000){
						break;
					  }
				  }
			  }
			  Dat1=FULLCAN_msgs->Dat1;
			}while (((Dat1 & 0x3000000) != 0x0)); 
	
	   CAN2Rx_Return_Message_Flag[0] = 1;  //indicate one Rx interrupt happened
	   // put Rcv data to Rcv-Buffer, must assign value to pointer before used
	   pCAN1_Rcv_Data_IRQ = CAN_Rcv_Data + CAN_Rcv_Data_Counter;
	   pCAN1_Rcv_Data_IRQ -> FULLCALmsg = 1; 
	   pCAN1_Rcv_Data_IRQ -> RFS = Dat1;
  	   pCAN1_Rcv_Data_IRQ -> ID = Dat1 & 0x7FF;	   //0....10bit ID
 	   pCAN1_Rcv_Data_IRQ -> DataField[0] = DatA;
   	   pCAN1_Rcv_Data_IRQ -> DataField[1] = DatB;
	  // Return_Value = lpc2000CANdriver_ReceiveMessageCh1 (pCAN1_Rcv_Data_IRQ);
	   CAN_Rcv_Data_Counter++;
	   CAN_Rcv_Status = 1; // communication successful
	                       //main中用它来判断是否打印,接受信息    
	   Print_Chars_to_Screen( LiuJiaju_FULLCAN_Rec_complete );
	   } //if testbit
	  }	//for 0..31
	 } //if interrut_word
	}//for j 0..2
}
#endif
/******************************************************************************
** Function name:		CAN_Rx2
**
** Descriptions:		CAN Rx2 interrupt handler
**接受错误在main中处理,在这里只在CAN2Rx_Return_Message_Flag[i]置位,
**表示是何种错误
** parameters:			None
** Returned value:		None
** 
******************************************************************************/
void CAN_Rx2( void )
{
 // Print_Chars_to_Screen( LiuJiaju_AF_Rec );
  UInt32  Temp_CAN2Rx_IRQ=0;
  UInt8   i=0;
  plpc2000CANdriver_RXObj_t pCAN2_Rcv_Data_IRQ;


  i = 0;
  CAN2Rx_Return_Message_Flag[i] = 1;  //indicate one Rx interrupt happened
  i++;

  Temp_CAN2Rx_IRQ = CAN2ICR;
  
  if( (Temp_CAN2Rx_IRQ & 0x00000001) == 0x00000001 )   //RI 接收中断
    {
	  // put Rcv data to Rcv-Buffer, must assign value to pointer before used
      pCAN2_Rcv_Data_IRQ = CAN_Rcv_Data + CAN_Rcv_Data_Counter;
      Return_Value = lpc2000CANdriver_ReceiveMessageCh2 (pCAN2_Rcv_Data_IRQ);
      CAN_Rcv_Data_Counter++;
      CAN_Rcv_Status = 1;  	   // communication successful
	}
					   
  if( ((Temp_CAN2Rx_IRQ>>2) & 0x00000001) == 0x00000001 ) //error warning interrupt
    {
	  CAN2Rx_Return_Message_Flag[i] = 1;
	  //Add code for "Error Warning Interrupt-EI"	  
	}
	  i++;

  if( ((Temp_CAN2Rx_IRQ>>3) & 0x00000001) == 0x00000001 ) //data overrun interrupt
    {
	  CAN2Rx_Return_Message_Flag[i] = 1;
	  //Add code for "Data Overrun Interrupt-DOI"	  
	}
  i++;

  if( ((Temp_CAN2Rx_IRQ>>4) & 0x00000001) == 0x00000001 ) //Wake up interrupt
    {
	  CAN2Rx_Return_Message_Flag[i] = 1;
	  //Add code for "Wake-Up Interrupt-WUI"	  
	}
  i++;

  if( ((Temp_CAN2Rx_IRQ>>5) & 0x00000001) == 0x00000001 ) //error passive interrupt
    {										 //at least one error counter exceed the CAN protocol defined level of 127
	  CAN2Rx_Return_Message_Flag[i] = 1;
	  //Add code for "Error Passive Interrupt-EPI"	  
	}
  i++;

  if( ((Temp_CAN2Rx_IRQ>>6) & 0x00000001) == 0x00000001 ) //Arbitrition lost interrupt
    {
	  CAN2Rx_Return_Message_Flag[i] = 1;
	  //Add code for "Arbitration Lost Interrupt-ALI"	  
	}
  i++;

  if( ((Temp_CAN2Rx_IRQ>>7) & 0x00000001) == 0x00000001 )//BUS error interrupt
    {
	  CAN2Rx_Return_Message_Flag[i] = 1;
	  //Add code for "Bus Error Interrupt-BEI"	  
	}
  i++;

  if( ((Temp_CAN2Rx_IRQ>>8) & 0x00000001) == 0x00000001 )//ID ready interrupt
    {

	  //Add code for "ID Ready Interrupt-IDI"	  
	}

	Print_Chars_to_Screen( LiuJiaju_AF_Rec_complete );
}



⌨️ 快捷键说明

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