📄 can_test.c
字号:
/**************************************************************************
MODULE: Main for LPC_CANAll
CONTAINS: CAN Example using the LPC_CANAll Driver
Tested with MCB2100 board from Keil Software
DOES:
COPYRIGHT: Embedded Systems Academy, Inc. 2004.
LICENSE: THIS VERSION CREATED FOR FREE DISTRIBUTION
FOR PHILIPS SEMICONDUCTORS www.philipsmcu.com
FOR KEIL SOFTWARE www.keil.com
VERSION: 1.00, Pf 14-JUL-04, First published release
---------------------------------------------------------------------------
HISTORY: 1.00, Pf 14-JUL-04, First published release
***************************************************************************/
#include "board.h"
#include "utils.h"
#include "LPC2294.h"
#include "LPC_CANAll.h"
// This value defines the number of timer ticks (of 100 microcseconds)
// that the program waits before sending the next CAN message. Here: 100ms
#define SLOWDOWN 1000
// Interrupt Service Routines
void DefaultISR( void );
void Timer0ISR( void );
// Global Timer Tick
unsigned int volatile gTimerTick;
/**************************************************************************
DOES: Default Service Routine for interrupts
GLOBALS: none
RETURNS: never
***************************************************************************/
void DefaultISR( void )
{
while ( 1 )
{
// We should never get here, if we do it is an error
}
}
/**************************************************************************
DOES: Timer 0 Interrupt Service Routine
GLOBALS: Increment timer tick
RETURNS: never
***************************************************************************/
void Timer0ISR( void )
{
gTimerTick++;
T0IR = 1; // Clear interrupt flag
VICVectAddr = 0xFFFFFFFF; // Acknowledge Interrupt
}
/**************************************************************************
DOES: Checks if a timestamp expired
GLOBALS: none
RETURNS: one if it expired, else zero
***************************************************************************/
short IsTimeExpired( unsigned int timestamp )
{
unsigned int time_now;
time_now = gTimerTick;
if ( time_now > timestamp )
{
if ( ( time_now - timestamp ) < 0x80000000L )
return 1;
else
return 0;
}
else
{
if ( ( timestamp - time_now ) > 0x80000000L )
return 1;
else
return 0;
}
}
/**************************************************************************
DOES: Waits until a timeout expires. Actual time waited is in the range
from [0 to 100us*(delay-1)] to [0 to 100us*delay]
GLOBALS: none
RETURNS: nothing
***************************************************************************/
void SlowDown( unsigned int delay )// Number of 100us timeouts used for waiting
{
delay += gTimerTick;
while ( !IsTimeExpired( delay ) ) ;
#if( CAN_DEBUG )
printf( "CAN Slow Down!\n" ) ;
#endif
}
/**************************************************************************
DOES: Main routine of demo program:
1.) Initialization
2.) Send a single CAN message
3.) Wait for CAN message,
send response to received messages
GLOBALS: none
RETURNS: never
***************************************************************************/
void Can_Test(void)
{
CANALL_MSG MsgBuf_TX1, MsgBuf_TX2 ; // TX Buffers for CAN message
CANALL_MSG MsgBuf_RX1, MsgBuf_RX2 ; // RX Buffers for CAN message
// Initialize MsgBuf
MsgBuf_TX1.Frame = 0x80080000L; // 29-bit, no RTR, DLC is 8 bytes
MsgBuf_TX1.MsgID = 0x00012345L; // CAN ID
MsgBuf_TX1.DatA = 0x5a5a5a5aL; // all zeros
MsgBuf_TX1.DatB = 0xa5a5a5a5L; // all zeros
// Initialize MsgBuf
MsgBuf_TX2.Frame = 0x80080000L; // 29-bit, no RTR, DLC is 8 bytes
MsgBuf_TX2.MsgID = 0x00098765L; // CAN ID
MsgBuf_TX2.DatA = 0xc3c3c3c3L; // all zeros
MsgBuf_TX2.DatB = 0x3c3c3c3cL; // all zeros
printf( "CAN Bus Test \n" ) ;
// Init Vector Interrupt Controller
VICIntEnClr = 0xFFFFFFFFL; // Disable all Ints
VICIntSelect = 0x00000000L;
// Install Default IRQVec
VICDefVectAddr = ( unsigned long ) DefaultISR; // set interrupt vector
// Initialisation of CAN interfaces
// CAN interface 1, use IRQVec0, at 125kbit
CANAll_Init( 1 , 0 , CANBitrate125k_12MHz );
// CAN interface 2, use IRQVec1, at 125kbit
CANAll_Init( 2 , 1 , CANBitrate125k_12MHz );
// Set CAN Err ISR to IRQVec2
CANAll_SetErrIRQ( 2 );
Delay( 100 );
printf( "Press 'ESC' key to exit the test \n" ) ;
while ( getkey() != ESC_KEY )
{
// Transmit initial message on CAN 1
CANAll_PushMessage( 1 , &MsgBuf_TX1 );
// Check if message received on CAN 2
if ( CANAll_PullMessage( 2 , &MsgBuf_RX2 ) )
{
printf( "\tCAN 2 Receive ID = 0x%x\n", MsgBuf_RX2.MsgID ) ;
printf( "\tCAN 2 Receive DATA = 0x%x\n", MsgBuf_RX2.DatA ) ;
printf( "\tCAN 2 Receive DATB = 0x%x\n", MsgBuf_RX2.DatB ) ;
} // Message on CAN 2 received
// Transmit initial message on CAN 2
CANAll_PushMessage( 2 , &MsgBuf_TX2 );
// Check if message received on CAN 1
if ( CANAll_PullMessage( 1 , &MsgBuf_RX1 ) )
{
printf( "\tCAN 1 Receive ID = 0x%x\n", MsgBuf_RX1.MsgID ) ;
printf( "\tCAN 1 Receive DATA = 0x%x\n", MsgBuf_RX1.DatA ) ;
printf( "\tCAN 1 Receive DATB = 0x%x\n", MsgBuf_RX1.DatB ) ;
} // Message on CAN 1 received
Delay( 100 );
} // while(1)
// return TRUE ;
}
/*----------------------- END OF FILE ----------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -