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

📄 cantest.c

📁 LPC2368上通过验证的CAN通讯程序
💻 C
字号:
/*****************************************************************************
 *   cantest.c:  CAN test module file for NXP LPC23xx Family Microprocessors
 *
 *   Copyright(C) 2006, NXP Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2006.09.13  ver 1.00    Prelimnary version, first Release
 *
******************************************************************************/
#include "LPC23xx.h"			/* LPC23xx definitions */
#include "type.h"
#include "irq.h"
#include "can.h"
#include "uart.h"
#include <stdio.h>

#define BUFFER_SIZE	0x40
char RUN_INFOR[BUFFER_SIZE];


CAN_MSG MsgBuf_TX1, MsgBuf_TX2; // TX and RX Buffers for CAN message
CAN_MSG MsgBuf_RX1, MsgBuf_RX2; // TX and RX Buffers for CAN message

volatile DWORD CAN1RxDone, CAN2RxDone;

/*****************************************************************************
** Function name:		main
**
** Descriptions:		CAN2 Local Self Test for LPC2368 Kit,
** In can.H			    #define LOCAL_SELF_TEST				1
** 
** parameters:			None
** Returned value:		int
** 
*****************************************************************************/
int main( void )
{
  
  UARTInit(0, 115200);	/* baud rate setting */
  /* Please note, this PCLK is set in the target.h file. Since the example
  program is set to test USB device, there is only a limited number of
  options to set CCLK and PCLK when USB is used. The default setting is 
  CCLK 57.6MHz, PCLK is 1/2 CCLK 28.8MHz. The bit timing is based on the 
  setting of the PCLK, if different PCLK is used, please read can.h carefully 
  and set your CAN bit timing accordingly. */
  if(!CAN_Init(BITRATE100K28_8MHZ ))
  {
  		sprintf( RUN_INFOR, "CAN_Init( BITRATE100K28_8MHZ ) == FALSE\r\n" );
    	UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );
  }
  sprintf( RUN_INFOR, "CAN_Init( BITRATE100K28_8MHZ )\r\n" );
  UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );	
  /* This test program is connect CAN1 and CAN2 on the MCB2300 board,
  send one message from CAN1(TX) and verify received message on CAN2(RX)
  if it's a match, both CAN TX and RX are working. 
	
  For more details on acceptance filter program, see Philips
  appnote AN10438 and the zip file associated with this appnote. */

#if !ACCEPTANCE_FILTER_ENABLED
  // Initialize MsgBuf
  MsgBuf_TX2.Frame = 0x80080000; // 29-bit, no RTR, DLC is 8 bytes
  MsgBuf_TX2.MsgID = 0x00012345; // CAN ID
  MsgBuf_TX2.DataA = 0x3C3C3C3C;
  MsgBuf_TX2.DataB = 0xC3C3C3C3;

  MsgBuf_RX2.Frame = 0x0;
  MsgBuf_RX2.MsgID = 0x0;
  MsgBuf_RX2.DataA = 0x0;
  MsgBuf_RX2.DataB = 0x0;
  CAN_SetACCF( ACCF_BYPASS );
  
  sprintf( RUN_INFOR, "CAN_SetACCF( ACCF_BYPASS );\r\n" );
  UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );
  /* Test bypass */
  while ( 1 )
  {
	// Transmit initial message on CAN 2
	while ( !(CAN2GSR & (1 << 3)) );
	if ( CAN2_SendMessage( &MsgBuf_TX2 ) == FALSE )
	{
	  sprintf( RUN_INFOR, "CAN2_SendMessage( &MsgBuf_TX2 ) == FALSE\r\n" );
      UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );
	  continue;
	}
	sprintf( RUN_INFOR, "CAN2_SendMessage( &MsgBuf_TX2 ) \r\n" );
    UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );

 	if ( CAN2RxDone == TRUE )
	{
	  CAN2RxDone = FALSE;
	  sprintf( RUN_INFOR, "CAN2RxDone == TRUE\r\n" );
      UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );
	  if ( MsgBuf_RX2.Frame & (1 << 10) )	/* by pass mode */
	  {
		MsgBuf_RX2.Frame &= ~(1 << 10 );
	  }
	  if ( ( MsgBuf_TX2.Frame != MsgBuf_RX2.Frame ) ||
			( MsgBuf_TX2.MsgID != MsgBuf_RX2.MsgID ) ||
			( MsgBuf_TX2.DataA != MsgBuf_RX2.DataA ) ||
			( MsgBuf_TX2.DataB != MsgBuf_RX2.DataB ) )
	  {
		sprintf( RUN_INFOR, "MsgBuf_TX2 != MsgBuf_RX2.\r\n" );
        UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );
		while ( 1 );
	  }
	  // Everything is correct, reset buffer
	  sprintf( RUN_INFOR, "Everything is correct, reset buffer.\r\n" );
      UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );
	  MsgBuf_RX2.Frame = 0x0;
	  MsgBuf_RX2.MsgID = 0x0;
	  MsgBuf_RX2.DataA = 0x0;
	  MsgBuf_RX2.DataB = 0x0;
	  while(1);  
	} // Message on CAN 2 received
  }
#else

	//DWORD Frame; 	// Bits 16..19: DLC - Data Length Counter
					// Bit 30: Set if this is a RTR message
					// Bit 31: Set if this is a 29-bit ID message


  /* Test Acceptance Filter */
  /* Even though the filter RAM is set for all type of identifiers,
  the test module tests explicit standard identifier only */
  MsgBuf_TX2.Frame = 0x80080000;	// 11-bit, no RTR, DLC is 8 bytes
  MsgBuf_TX2.MsgID = 0x08ffff01;	// EXP_STD_ID; // Explicit Standard ID
  MsgBuf_TX2.DataA = 0x55AA55AA;
  MsgBuf_TX2.DataB = 0xAA55AA55;

  MsgBuf_RX2.Frame = 0x0;
  MsgBuf_RX2.MsgID = 0x0;
  MsgBuf_RX2.DataA = 0x0;
  MsgBuf_RX2.DataB = 0x0;
  CAN_SetACCF( ACCF_ON );

  sprintf( RUN_INFOR, "CAN_SetACCF( ACCF_ON );\r\n" );
  UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );

  while (1)
  {
	// Transmit initial message on CAN 2
	while(1) {	//dengting
	while ( !(CAN2GSR & (1 << 3)) );
	if ( CAN2_SendMessage( &MsgBuf_TX2 ) == FALSE )
	{
	  sprintf( RUN_INFOR, "CAN2_SendMessage( &MsgBuf_TX2 ) == FALSE\r\n" );
      UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );
	  continue;
	}
	}


	sprintf( RUN_INFOR, "CAN2_SendMessage( &MsgBuf_TX2 ) \r\n" );
    UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );
	/* please note: FULLCAN identifier will NOT be received as it's not set 
	in the acceptance filter. */
 	if ( CAN2RxDone == TRUE )
	{
	  sprintf( RUN_INFOR, "CAN2RxDone == TRUE\r\n" );
      UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );
	  CAN2RxDone = FALSE;
	  /* The frame field is not checked, as ID index varies based on the
	  entries set in the filter RAM. */
	  if ( ( MsgBuf_TX2.MsgID != MsgBuf_RX2.MsgID ) ||
			( MsgBuf_TX2.DataA != MsgBuf_RX2.DataA ) ||
			( MsgBuf_TX2.DataB != MsgBuf_RX2.DataB ) )
	  {
		sprintf( RUN_INFOR, "MsgBuf_TX2 != MsgBuf_RX2.\r\n" );
        UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );
		while ( 1 );
	  }
	  // Everything is correct, reset buffer
	  sprintf( RUN_INFOR, "Everything is correct, reset buffer.\r\n" );
      UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );
	  MsgBuf_RX2.Frame = 0x0;
	  MsgBuf_RX2.MsgID = 0x0;
	  MsgBuf_RX2.DataA = 0x0;
	  MsgBuf_RX2.DataB = 0x0;
	  while(1);
	} // Message on CAN 2 received
  }
#endif
  return ( 0 );
}

/******************************************************************************
**                            End Of File
******************************************************************************/

⌨️ 快捷键说明

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