can.c
#include "lpc_types.h"
#include "can.h"
CCAN_MSG_OBJ_T msg_obj_rx;
CCAN_MSG_OBJ_T msg_obj_tx;
uint8_t data_0;
uint8_t data_1;
/*CAN receive callback */
/*Function is executed by the Callback handler after a CAN message has been received */
void CAN_rx(uint8_t msg_obj_num) {
msg_obj_rx.msgobj = msg_obj_num;// Determine which CAN message has been received
LPC_CCAN_API->can_receive(&msg_obj_rx);// Now load up the msg_obj structure with the CAN message
data_0 = msg_obj_rx.data[0];
data_1 = msg_obj_rx.data[1];
LPC_GPIO3->DATA ^= (1 << 0);// Switch LED
}
/*****************************************************************************
** Function name:CAN_Init for initializing the CANbus
*****************************************************************************/
void CAN_Init(uint32_t CAN_baudrate) {
uint32_t initTable[2];
initTable[0] = 0x05; // 48 Mhz/(5+1) -> 8 MHz
initTable[1] = CAN_baudrate; // configure bit rate to 250k
LPC_CCAN_API->init_can(&initTable[0], 1); // Initialize the CAN controller
/* Publish CAN Callback Functions */
CCAN_CALLBACKS_T callbacks = { CAN_rx,CAN_tx, CAN_error, NULL, NULL, NULL, NULL, NULL };
LPC_CCAN_API->config_calb(&callbacks);// Configure the CAN callback functions
/* Enable the CAN Interrupt */
NVIC_EnableIRQ(CAN_IRQn);
/* Configure message object 1 to receive only common messages from 0x0FA */
msg_obj_rx.msgobj =0x01;
msg_obj_rx.id =0xFA;
msg_obj_rx.mask =0x1FFFFFFF;
LPC_CCAN_API->config_rxmsgobj(&msg_obj_rx);
}
|
int main(void) {
...
while (1) {
if (LPC_TMR32B0->IR & (1 << 0)) {// check interrupt 0
msg_obj_tx.msgobj= 28;
msg_obj_tx.id= 0x7CC;
msg_obj_tx.dlc= 2;
msg_obj_tx.data[0]= data_0;
msg_obj_tx.data[1]= data_1;
LPC_CCAN_API->can_transmit(&msg_obj_tx);
LPC_TMR32B0->IR |= (1 << 0);// clear interrupt 0
}
...
|