Last active
May 31, 2016 12:05
-
-
Save nnarain/882248a800c624aebc60b6d8f941fd0b to your computer and use it in GitHub Desktop.
HCS12 CAN Example from NXP App note
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <hidef.h> /* common defines and macros */ | |
#include "derivative.h" /* derivative-specific definitions */ | |
#define ST_ID_100 0x100 | |
#define ACC_CODE_ID100 0x2000 | |
#define ACC_CODE_ID100_HIGH ((ACC_CODE_ID100 & 0xFF00)>>8) | |
#define ACC_CODE_ID100_LOW (ACC_CODE_ID100 & 0x00FF) | |
/* Mask Code Definitions */ | |
#define MASK_CODE_ST_ID 0x0007 | |
#define MASK_CODE_ST_ID_HIGH ((MASK_CODE_ST_ID & 0xFF00)>>8) | |
#define MASK_CODE_ST_ID_LOW (MASK_CODE_ST_ID & 0xFF) | |
void canInit() | |
{ | |
CANCTL0 = 0x01; | |
while(!(CANCTL1 & 0x01)); | |
CANCTL1 = 0xA0; | |
CANBTR0 = 0xC7; | |
CANBTR1 = 0x3A; | |
/* Acceptance Filters */ | |
CANIDAC = 0x10; /* Set four 16-bit Filters */ | |
CANIDAR0 = ACC_CODE_ID100_HIGH; //|\ 16-bit Filter 0 | |
CANIDMR0 = MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg | |
CANIDAR1 = ACC_CODE_ID100_LOW; //| / with ID 0x100 | |
CANIDMR1 = MASK_CODE_ST_ID_LOW; //|/ | |
/* Acceptance Filters */ | |
CANIDAC = 0x10; /* Set four 16-bit Filters */ | |
CANIDAR2 = 0x00; //|\ 16-bit Filter 1 | |
CANIDMR2 = MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg | |
CANIDAR3 = 0x00; //| / with ID 0x100 | |
CANIDMR3 = MASK_CODE_ST_ID_LOW; //|/ | |
CANIDAR4 = 0x00; //|\ 16-bit Filter 2 | |
CANIDMR4 = MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg | |
CANIDAR5 = 0x00; //| / with ID 0x100 | |
CANIDMR5 = MASK_CODE_ST_ID_LOW; //|/ | |
CANIDAR6 = 0x00; //|\ 16-bit Filter 3 | |
CANIDMR6 = MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg | |
CANIDAR7 = 0x00; //| / with ID 0x100 | |
CANIDMR7 = MASK_CODE_ST_ID_LOW; //|/ | |
CANCTL0 = 0x00; /* Exit Initialization Mode Request */ | |
while ((CANCTL1&0x00) != 0); /* Wait for Normal Mode */ | |
} | |
unsigned char canSendFrame(unsigned long id, unsigned char priority, unsigned char length, unsigned char *txdata) | |
{ | |
unsigned char txbuffer; | |
unsigned char index; | |
if(!CANTFLG) return 1; | |
CANTBSEL = CANTFLG; | |
txbuffer = CANTBSEL; | |
*((unsigned long *) ((unsigned long)(&CANTXIDR0))) = id; | |
for (index=0;index<length;index++) { | |
*(&CANTXDSR0 + index) = txdata[index]; | |
} | |
CANTXDLR = length; /* Set Data Length Code */ | |
CANTXTBPR = priority; /* Set Priority */ | |
CANTFLG = txbuffer; /* Start transmission */ | |
while ( (CANTFLG & txbuffer) != txbuffer); | |
} | |
interrupt 38 void canRxHandler(void) | |
{ | |
unsigned char length, index; | |
unsigned char rxdata[8]; | |
length = (CANRXDLR & 0x0F); | |
for (index=0; index<length; index++) | |
rxdata[index] = *(&CANRXDSR0 + index); /* Get received data */ | |
CANRFLG = 0x01; /* Clear RXF */ | |
} | |
void main(void) | |
{ | |
unsigned char errorflag; | |
unsigned char txbuff[] = "abcd"; | |
canInit(); | |
while (!(CANCTL0 & 0x10)); | |
CANRFLG = 0xC3; | |
CANRIER = 0x01; | |
EnableInterrupts; | |
for (;;) | |
{ | |
errorflag = canSendFrame((ST_ID_100), 0x00, sizeof(txbuff)-1, txbuff); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment