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

📄 lpc2000_can_sys.c

📁 在lpc2468上使用
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
* FILENAME:      LPC2000_CAN_SYS.C                                            *  
*                                                                             *
* VERSION:       V1.0                                                         *
*                                                                             *
* DESCRIPTION:   NXP LPC2300 CAN Controller Driver with SJA1000               *                     
*	        	 Demo by UART0 with KEIL MCB2300 Evaluation Board	          *
*	        	 Code runing indicate by LED4 with Timer1 interrupt	       	  *
*                UART0 tested with Receive "a" NOT LED1, Receive "b" NOT LED2 *
*                                                                             *
* TOOLS:         KEIL uVision3                                     			  *
*                KEIL MCB2300 Evaluation Board               				  *
*                                                                             *
* REVISION HISTORY                                                            *
* Version  Author           Date        Remarks								  *
* 1.0      Anthony Xie      10/10/2005  CAN Driver without Full CAN           *
* 2.0      Antonio Ake      06/22/2007	Full CAN Driver			   			  *
* 2.1      Antonio Ake      10/11/2007  Add target MCB2300 RAM				  *
******************************************************************************/

// -----------------------------------------------
// Project include files                                                     
// -----------------------------------------------
#include "LPC230x.h"                            /* LPC21xx definitions */
#include "type.h"	
#include "target.h"
#include "LPC2000_CAN.h"
#include "irq.h"

//-----------------------------------------------
//Constant Definitions
//-----------------------------------------------
#define	  LED4CON	(1<<3)		// P2.3 control LED4
#define   LED3CON	(1<<2)		// P2.2 control LED3
#define	  LED2CON	(1<<1)		// P2.1 control LED2
#define   LED1CON	(1<<0)		// P2.0 control LED1

#define IER_RBR		0x01
#define IER_THRE	0x02
#define IER_RLS		0x04

//---------------------------------------------
//Global Variable Definitions
//---------------------------------------------

//System Global Variable
UInt32 System_Counter,LED_Counter=0;
UInt8 LED_Flag=0;
UInt8 System_Status=0;

UInt32 CANStatus;
UInt32 CAN1RxCount = 0;
UInt32 CAN2RxCount = 0,CAN2_FULLCANRxCount=0;
UInt32 CAN1ErrCount = 0, CAN2ErrCount = 0;
typedef struct
{
unsigned int Dat1; // Bits 0..10: CAN Message ID
				   // Bits 13..15: CAN interface number (1..4)
				   // Bits 16..19: DLC - Data Length Counter
				   // Bits 24..25: Semaphore bits
unsigned int DatA; // CAN Message Data Bytes 0-3
unsigned int DatB; // CAN Message Data Bytes 4-7

} FULLCAN_MSG;

//---------------------------------------------
//* Functions
//---------------------------------------------
void init(void);
void delay(UInt32);
void __irq IRQ_Timer1(	void);
void uart0_config(UInt32,UART0MODE);
void __irq IRQ_UART0(void);
void __irq IRQ_CAN(void);

void CAN_Rx2( void );
void CAN_FULLCAN_Rx2( void );
unsigned char msg_count = 0;



//***************************************************************************
//* main()
//* Input(s): none.
//* Returns : none.
//* Description : main loop
//***************************************************************************
int  main(void)
{ 
  UInt32 i=0;
  plpc2000CANdriver_TXObj_t pCAN_Send_Data;
  plpc2000CANdriver_RXObj_t pCAN_Rcv_Data;
    
  delay(20);
  init();  
  
  
  Print_Chars_to_Screen( Demo_Start );

  for(;;)	
  {        

    if( LED_Flag == 0 )
       FIO2SET = LED4CON;	//Control LED4
    else 
       FIO2CLR = LED4CON;

	    switch( System_Status )
	  {
	    case 0:	    // wait input (1,2 choice) status
		  break;
		case 1:									   // "1" be pressed
		  lpc2000CANdriver_SetACFMode( LPC2000_ACC_BYPASS );
		  Print_Chars_to_Screen( Demo_Sent_Data );
		  Print_Chars_to_Screen( Data_Sent_Start_or_Not ); 
		  System_Status = 4;
		  break;
		case 2:									   // "2" be pressed
          lpc2000CANdriver_SetACFMode( LPC2000_ACC_ON );//screen
		 // lpc2000CANdriver_LoadAcceptanceFilter();
		  Print_Chars_to_Screen( Demo_Sent_Data );
		  Print_Chars_to_Screen( Acceptance_Filter_Tables );
		  Print_Chars_to_Screen( Data_Sent_Start_or_Not ); 
		  System_Status = 4;
		  break;
		case 3:									   // "3" be pressed
          lpc2000CANdriver_SetACFMode( LPC2000_ACCF_FULLCAN );
		 // lpc2000CANdriver_LoadAcceptanceFilter();
		  FCANIE = 0x1; 	       //Enable FullCAN Interrupt
		  Print_Chars_to_Screen( Demo_Sent_Data );
		  Print_Chars_to_Screen( FULLCAN_Acceptance_Filter_Tables );
		  Print_Chars_to_Screen( Data_Sent_Start_or_Not ); 
		  System_Status = 4;
		  break;
		case 4:	   // wait input (y,n choice) status
		  break;

		case 5:	   // "y or Y" be pressed, 
		  pCAN_Send_Data = CAN_Send_Data;
          	  for( i=0;i<11;i++)
	            {
                      Return_Value = lpc2000CANdriver_CertainTxBufTransmitMessage(LPC2000_CANCHANNEL_1,pCAN_Send_Data,LPC2000_TXB_1);
		      		  pCAN_Send_Data++;
		    }

		  System_Status = 0;
		//  Print_Chars_to_Screen(LiuJiaju_Send) ;
		  break;				
		  				
		case 7:	 // "N or n " be pressed
		  System_Status = 0;
		  break;
		default:
		  break;
	  }
//	  CAN_Rcv_Status=1;
    if( CAN_Rcv_Status == 1 ) //communication successed
	  {
          Print_Chars_to_Screen( Rcv_Data_OK );          
		  pCAN_Rcv_Data = CAN_Rcv_Data;	  // print to uart0 all data in Rcv_Buffer
		  for( i=0;i<CAN_Rcv_Data_Counter;i++)				  
		    {
			  
			  Rcv_Data_Output_to_Screen( pCAN_Rcv_Data );		  Print_Chars_to_Screen( Demo_Sent_Choice );

			  pCAN_Rcv_Data++; 
			}
		  Print_Chars_to_Screen( Label_Explanation );	
		  CAN_Rcv_Data_Counter = 0;     // reset Rcv_Buffer, point to head, means empty it.
		  Print_Chars_to_Screen( Demo_Sent_Choice );

		  CAN_Rcv_Status = 0;
	  }


    if( CAN2Rx_Return_Message_Flag[0] != 0 ) // if one CAN1Rx interrupt happened
	  {										 // print massage of communication status
		
		for( i=1;i<10;i++)
		{
		  if( CAN2Rx_Return_Message_Flag[i] != 0)
		    {
              switch(i)
			    {
				  case 1: 
				    Print_Chars_to_Screen( CAN2Rx_Return_Message_EI );
				    Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );      
				    break;
 				  case 2: 
				    Print_Chars_to_Screen( CAN2Rx_Return_Message_DOI );
				    Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
					break;
				  case 3: 
				    Print_Chars_to_Screen( CAN2Rx_Return_Message_WUI );
				    Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
					break;
				  case 4: 
				    Print_Chars_to_Screen( CAN2Rx_Return_Message_EPI );
				    Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
					break;
				  case 5: 
				    Print_Chars_to_Screen( CAN2Rx_Return_Message_ALI );
				    Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
					break;
				  case 6: 
				    Print_Chars_to_Screen( CAN2Rx_Return_Message_BEI );
				    Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
					break;
				  default:
				    break;
				}
			  CAN2Rx_Return_Message_Flag[i]=0;			  
			}
		}

	    CAN2Rx_Return_Message_Flag[0] = 0;
	  }

    if( (Return_Value != 0) && (Return_Value != 5))	  // Report CAN error status
	  {
	    switch(Return_Value)
		  {
		    case 10:
			  Print_Chars_to_Screen( CAN_Error_10 );
			  Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
			  break;
		    case 30:
			  Print_Chars_to_Screen( CAN_Error_30 );
			  Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
			  break;
		    case 31:
			  Print_Chars_to_Screen( CAN_Error_31 );
			  Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
			  break;
		    case 40:
			  Print_Chars_to_Screen( CAN_Error_40 );
			  Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
			  break;
		    case 50:
			  Print_Chars_to_Screen( CAN_Error_50 );
			  Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
			  break;
		    case 60:
			  Print_Chars_to_Screen( CAN_Error_60 );
			  Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
			  break;
		    case 90:
			  Print_Chars_to_Screen( CAN_Error_90 );
			  Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
			  break;
		    case 100:
			  Print_Chars_to_Screen( CAN_Error_100 );
			  Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
			  break;
		    case 110:
			  Print_Chars_to_Screen( CAN_Error_110 );
			  Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
			  break;
		    case 120:
			  Print_Chars_to_Screen( CAN_Error_120 );
			  Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
			  break;
		    case 130:
			  Print_Chars_to_Screen( CAN_Error_130 );
			  Print_Chars_to_Screen( CAN_ERROR_STOP_DEMO );
			  break;
			default:
			  break;
		  }

		Return_Value = 0;
	  }
      
  }
   
  
}

                               
//***************************************************************************
//* init()
//* Input(s): none.
//* Returns : none.
//* Description : initialization each function
//***************************************************************************
void init(void)
{
  UART0MODE Uart0_Config;


//-----------------------------------------------
//Config GPIO
//-----------------------------------------------
  PINSEL0  = 0x00000000;		// All be GPIO, Note: 2 bits config 1 pin!
  PINSEL0 |= 0x00000A55;		// P0.0-CANRD1,P0.1-CANTD1,P0.2-UARTTXD0,
  				             	// P0.3-UARTRXD0,P0.4-CANRD2,P0.5-CANTD2


  FIO2DIR = 0x000000FF;          // P2.0-7 output, other input, 
                                 // 0000 0000 0000 0000 0000 0000 0000 0000
  FIO2CLR = 0x000000FF;          // Turn off LED1 - LED8


//-----------------------------------------------
//Config Timer1
//Fcclk = Fosc*4 = 12MHz*4 = 48MHz
//Fpclk = Fcclk/2 = 48MHz/2 = 24MHz
//-----------------------------------------------  
  T1PR = 0;				// Disable prescaler
  T1MCR = 0x03;			// Using T1MR0 match T1TC,Enable interrupt,reset T1TC
  T1MR0 = Fpclk/100;	// 10ms each Timer interrupt
  T1TCR = 0x02;			// Reset T1TC, by set TCR:2
  T1TCR = 0x01;			// Must CLR TCR:2, then can Start T1TC


//-----------------------------------------------
//Config UART0
//-----------------------------------------------  
  Uart0_Config.Datalength = 8;
  Uart0_Config.Stopbit = 1;
  Uart0_Config.Paritybit = 0;
  uart0_config(9600,Uart0_Config);
  
  

//-----------------------------------------------
//Config CAN1
//-----------------------------------------------  
  lpc2000CANdriver_CANInit( LPC2000_CANCHANNEL_1, LPC2000_CANDRIVER_CANBITRATE33K24MHZ );
  
  
  //CAN1IER = 0x1;  //Enable receive CAN1 RX interrupt

  


//-----------------------------------------------
//Config CAN2
//-----------------------------------------------  
  lpc2000CANdriver_CANInit( LPC2000_CANCHANNEL_2, LPC2000_CANDRIVER_CANBITRATE33K24MHZ );

  CAN2IER = 0x1;  //Enable receive CAN2 RX interrupt
  
  lpc2000CANdriver_LoadAcceptanceFilter();

//-----------------------------------------------
//Config VIC, 
//In <Starup.s> enable IRQ by CLR I bit in CPSR
//Vectored interrupt Timer1 by slot-0
//Vectored interrupt UART0 by slot-1
//Vectored interrupt CAN1Rx by slot-2
//-----------------------------------------------  
 
 install_irq(TIMER1_INT,(void *)IRQ_Timer1 , 1 );

⌨️ 快捷键说明

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