Can interfacing diagram:
Code:
can.c
#include "LPC1768_Includes.h"
#include "CAN.h"
#include "stdio.h"
void CANInit(void)
{
unsigned int temp;
PCONP |= (1<<13) | (1<<14);
PINSEL0 &= ~0x0000000F; //CAN1 is p0.0 and p0.1
PINSEL0 |= 0x00000005;
PINSEL4 &= ~0x0003C000; //CAN2 is p2.7 and p2.8 */
PINSEL4 |= 0x00014000;
CAN1MOD = CAN2MOD = 0x01; //CAN reset, write to CAN registers
now
CAN1IER = CAN2IER = 0;
CAN1GSR = CAN2GSR = 0;
CAN1CMR = CAN2CMR = (1<<1)|(1<<2)|(1<<3); //Request command to release Rx, Tx buffer
and clear data overrun
temp = CAN1ICR; //Read to clear
interrupts
temp = CAN2ICR;
CAN1BTR = CAN2BTR = 0x001C0008; //Bit Timing Values for 18MHz pclk
frequency, 1/4 of 72Mhz CCLK
CAN1MOD = CAN2MOD = 0x00; //CAN normal operation, CAN
registers can't be written now
printf("CAN initializedn");
}
void SetAccFilter(void)
{
unsigned int address = 0;
unsigned int *RAMptr = (unsigned int*)CANAF_RAM_BASE;
printf("Setting Access Filtern");
AFMR = 0x01; //Acc Filter Off
SFF_sa = address; //Set Explicit Std Frame
*RAMptr++ = (0<<29) | (EXP_STD_ID1<<16) | (1<<13) | EXP_STD_ID1; //CAN1 accept
EXP_STD_ID1; CAN2 accept EXP_STD_ID1
address = address+4;
*RAMptr++ = (0<<29) | (EXP_STD_ID2<<16) | (1<<13) | EXP_STD_ID2; //CAN1 accept
EXP_STD_ID2; CAN2 accept EXP_STD_ID2
address = address+4;
SFF_GRP_sa = address; //Set Range of Std Frame (can be same as Explicit Std
Frame)
EFF_sa = address; //Set Explicit Extended Frame
*RAMptr++ = (1<<29) | (EXP_EXT_ID1); //CAN2 accept EXP_EXT_ID1
address = address+4;
*RAMptr++ = (1<<29) | (EXP_EXT_ID2); //CAN2 accept EXP_EXT_ID2
address = address+4;
EFF_GRP_sa = address; //Set Range of Extended Frame (can be same as Explicit
Extended Frame)
ENDofTable = address; //Set end of Look Up Table
AFMR = 0x00; //Acc Filter On
printf("Access Filter Setn");
}
void CAN1SendMsg(unsigned int TxMsgID, unsigned int DATA1, unsigned int DATA2)
//Function to send msg over CAN1
{
unsigned int TxFrame = 0x00080000; //set data length 8 bytes
while(!(CAN1GSR & (1<<3))); //Wait till all the previous
transmission requests are complete
CAN1TFI1 = TxFrame & 0xC00F0000; //Send msg through buffer 1
CAN1TID1 = TxMsgID;
CAN1TDA1 = DATA1;
CAN1TDB1 = DATA2;
CAN1CMR = 0x21; //transmit buffer1
while(!(CAN1GSR & (1<<3))); //Wait till all the
transmission requests are complete
printf("CAN1 Msg Sent!n");
}
main.c
#include "LPC1768_Includes.h"
#include "UART.h"
#include "stdio.h"
#include "CAN.h"
int main()
{
UartInit(9600);
printf("Hellon");
CANInit();
SetAccFilter(); //set acceptance filter for receiving
msg
CAN1SendMsg(EXP_STD_ID1, 0x11223344, 0x55667788);
//send msg via CAN1
while(!(CAN2GSR & (1<<0))); //wait till CAN2 Rx buffer receives
printf("nCAN2 Msg Receivedn");
printf("ID = 0x%08Xn",CAN2RID); //display received data
printf("FRAME = 0x%08Xn",CAN2RFS);
printf("DATA1 = 0x%08Xn",CAN2RDA);
printf("DATA2 = 0x%08Xn",CAN2RDB);
CAN2CMR = 1<<2; //release Rx buffer for next
reception
while(1); //stop here
return 0;
}
uart.c
#include<stdio.h>
#include "UART.h"
void UartInit(unsigned int baudrate)
{
unsigned int FDiv;
PINSEL0 |= 0x00000050;
PCONP |= 1<<3;
U0LCR = 0x83;
U0FDR = 0x10; // Line control register :DLAB=1 ; 8
bits ; 1 stop bit ; no parity
FDiv = (18000000 / 16 ) / baudrate ; //
U0DLM = FDiv /256; //0x00;
U0DLL = FDiv %256; //0x97;
U0LCR = 0x03; // Line control register :DLAB=0 ; 8
bits ; 1 stop bit ; no parity
U0TER = 0x80;
}
int UART_GetChar(void)
{
while(!(U0LSR & 0x1));
return(U0RBR);
}
int UART_PutChar(unsigned char Ch)
{
if (Ch == 'n') {
while (!(U0LSR & 0x20));
U0THR = 0x0D; /* output CR */
}
while(!(U0LSR & 0x20));
return( U0THR = Ch);
}
int fputc(int ch, FILE *f) {
return (UART_PutChar(ch));
}
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
can.h
#ifndef CAN_H
#define CAN_H
#define EXP_STD_ID1 0x100 //included in acceptance filter
#define EXP_STD_ID2 0x200 //included in acceptance filter
#define EXP_STD_ID3 0x201 //not included in acceptance filter
#define EXP_EXT_ID1 0x100000 //included in acceptance filter
#define EXP_EXT_ID2 0x200000 //included in acceptance filter
#define EXP_EXT_ID3 0x200001 //not included in acceptance filter
void CANInit(void);
void SetAccFilter(void);
void CAN1SendMsg(unsigned int, unsigned int, unsigned int);
#endif
Output:
CAN interfacing on LPC1768 (ARM Cortex M3 based Micro controller)

CAN interfacing on LPC1768 (ARM Cortex M3 based Micro controller)

  • 1.
    Can interfacing diagram: Code: can.c #include"LPC1768_Includes.h" #include "CAN.h" #include "stdio.h" void CANInit(void) { unsigned int temp; PCONP |= (1<<13) | (1<<14); PINSEL0 &= ~0x0000000F; //CAN1 is p0.0 and p0.1 PINSEL0 |= 0x00000005; PINSEL4 &= ~0x0003C000; //CAN2 is p2.7 and p2.8 */ PINSEL4 |= 0x00014000; CAN1MOD = CAN2MOD = 0x01; //CAN reset, write to CAN registers now
  • 2.
    CAN1IER = CAN2IER= 0; CAN1GSR = CAN2GSR = 0; CAN1CMR = CAN2CMR = (1<<1)|(1<<2)|(1<<3); //Request command to release Rx, Tx buffer and clear data overrun temp = CAN1ICR; //Read to clear interrupts temp = CAN2ICR; CAN1BTR = CAN2BTR = 0x001C0008; //Bit Timing Values for 18MHz pclk frequency, 1/4 of 72Mhz CCLK CAN1MOD = CAN2MOD = 0x00; //CAN normal operation, CAN registers can't be written now printf("CAN initializedn"); } void SetAccFilter(void) { unsigned int address = 0; unsigned int *RAMptr = (unsigned int*)CANAF_RAM_BASE; printf("Setting Access Filtern"); AFMR = 0x01; //Acc Filter Off SFF_sa = address; //Set Explicit Std Frame *RAMptr++ = (0<<29) | (EXP_STD_ID1<<16) | (1<<13) | EXP_STD_ID1; //CAN1 accept EXP_STD_ID1; CAN2 accept EXP_STD_ID1 address = address+4; *RAMptr++ = (0<<29) | (EXP_STD_ID2<<16) | (1<<13) | EXP_STD_ID2; //CAN1 accept EXP_STD_ID2; CAN2 accept EXP_STD_ID2 address = address+4;
  • 3.
    SFF_GRP_sa = address;//Set Range of Std Frame (can be same as Explicit Std Frame) EFF_sa = address; //Set Explicit Extended Frame *RAMptr++ = (1<<29) | (EXP_EXT_ID1); //CAN2 accept EXP_EXT_ID1 address = address+4; *RAMptr++ = (1<<29) | (EXP_EXT_ID2); //CAN2 accept EXP_EXT_ID2 address = address+4; EFF_GRP_sa = address; //Set Range of Extended Frame (can be same as Explicit Extended Frame) ENDofTable = address; //Set end of Look Up Table AFMR = 0x00; //Acc Filter On printf("Access Filter Setn"); } void CAN1SendMsg(unsigned int TxMsgID, unsigned int DATA1, unsigned int DATA2) //Function to send msg over CAN1 { unsigned int TxFrame = 0x00080000; //set data length 8 bytes while(!(CAN1GSR & (1<<3))); //Wait till all the previous transmission requests are complete CAN1TFI1 = TxFrame & 0xC00F0000; //Send msg through buffer 1 CAN1TID1 = TxMsgID; CAN1TDA1 = DATA1;
  • 4.
    CAN1TDB1 = DATA2; CAN1CMR= 0x21; //transmit buffer1 while(!(CAN1GSR & (1<<3))); //Wait till all the transmission requests are complete printf("CAN1 Msg Sent!n"); } main.c #include "LPC1768_Includes.h" #include "UART.h" #include "stdio.h" #include "CAN.h" int main() { UartInit(9600); printf("Hellon"); CANInit(); SetAccFilter(); //set acceptance filter for receiving msg CAN1SendMsg(EXP_STD_ID1, 0x11223344, 0x55667788); //send msg via CAN1
  • 5.
    while(!(CAN2GSR & (1<<0)));//wait till CAN2 Rx buffer receives printf("nCAN2 Msg Receivedn"); printf("ID = 0x%08Xn",CAN2RID); //display received data printf("FRAME = 0x%08Xn",CAN2RFS); printf("DATA1 = 0x%08Xn",CAN2RDA); printf("DATA2 = 0x%08Xn",CAN2RDB); CAN2CMR = 1<<2; //release Rx buffer for next reception while(1); //stop here return 0; } uart.c #include<stdio.h> #include "UART.h" void UartInit(unsigned int baudrate) { unsigned int FDiv; PINSEL0 |= 0x00000050; PCONP |= 1<<3; U0LCR = 0x83; U0FDR = 0x10; // Line control register :DLAB=1 ; 8 bits ; 1 stop bit ; no parity FDiv = (18000000 / 16 ) / baudrate ; // U0DLM = FDiv /256; //0x00; U0DLL = FDiv %256; //0x97;
  • 6.
    U0LCR = 0x03;// Line control register :DLAB=0 ; 8 bits ; 1 stop bit ; no parity U0TER = 0x80; } int UART_GetChar(void) { while(!(U0LSR & 0x1)); return(U0RBR); } int UART_PutChar(unsigned char Ch) { if (Ch == 'n') { while (!(U0LSR & 0x20)); U0THR = 0x0D; /* output CR */ } while(!(U0LSR & 0x20)); return( U0THR = Ch); } int fputc(int ch, FILE *f) { return (UART_PutChar(ch)); } struct __FILE { int handle; /* Add whatever you need here */ }; FILE __stdout;
  • 7.
    can.h #ifndef CAN_H #define CAN_H #defineEXP_STD_ID1 0x100 //included in acceptance filter #define EXP_STD_ID2 0x200 //included in acceptance filter #define EXP_STD_ID3 0x201 //not included in acceptance filter #define EXP_EXT_ID1 0x100000 //included in acceptance filter #define EXP_EXT_ID2 0x200000 //included in acceptance filter #define EXP_EXT_ID3 0x200001 //not included in acceptance filter void CANInit(void); void SetAccFilter(void); void CAN1SendMsg(unsigned int, unsigned int, unsigned int); #endif
  • 8.