AnsweredAssumed Answered

MSCAN Question

Question asked by edison wu on Jan 13, 2019
Latest reply on Jan 15, 2019 by Diana Batrlova

Hi Sir,

 

IC:mc9s12xet256 

Output:CAN4 = PIN117(TXD),PIN118(RXD)

 

I try to use the MSCAN Rx interrupter, IC always not pass at "EnableInterrupts;".

 

please help me , what happen ?

 

thanks so much.

 

#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)
/* Task FlagDefinitions */
unsigned char Task_CAN =0x00;
unsigned char errorflag;
unsigned char txbuff[8] ="12345";
unsigned char rxbuff[8] ="";
unsigned int rxID;
/* Function Definitions */
void Task(void);

/* MSCAN0 Init */
void can4Init()
{
//MSCAN Initial mode
CAN4CTL0 = 0x01;

//Check Entry the MSCAN Initial mode
while(!(CAN4CTL1 & 0x01));

//MSCAN Enable & Loop Enable
CAN4CTL1 = 0x80;

//fOSC=4MHz,CAN speed=250kbps
CAN4BTR0 = 0xC0;
CAN4BTR1 = 0x67;

/* Close Filters */
CAN4IDAC = 0x30;
CAN4IDAR0=CAN4IDAR1=CAN4IDAR2=CAN4IDAR3=0xFF;
CAN4IDAR4=CAN4IDAR5=CAN4IDAR6=CAN4IDAR7=0xFF;
CAN4IDMR0=CAN4IDMR1=CAN4IDMR2=CAN4IDMR3=0xFF;
CAN4IDMR4=CAN4IDMR5=CAN4IDMR6=CAN4IDMR7=0xFF;
//CAN0IDAR0 = ACC_CODE_ID100_HIGH; //|\ 16-bit Filter 0
//CAN0IDMR0 = MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg
//CAN0IDAR1 = ACC_CODE_ID100_LOW; //| / with ID 0x100
//CAN0IDMR1 = MASK_CODE_ST_ID_LOW; //|/
/* Acceptance Filters */
//CAN0IDAC = 0x10; /* Set four 16-bit Filters */
//CAN0IDAR2 = 0x00; //|\ 16-bit Filter 1
//CAN0IDMR2 = MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg
//CAN0IDAR3 = 0x00; //| / with ID 0x100
//CAN0IDMR3 = MASK_CODE_ST_ID_LOW; //|/
//CAN0IDAR4 = 0x00; //|\ 16-bit Filter 2
//CAN0IDMR4 = MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg
//CAN0IDAR5 = 0x00; //| / with ID 0x100
//CAN0IDMR5 = MASK_CODE_ST_ID_LOW; //|/
//CAN0IDAR6 = 0x00; //|\ 16-bit Filter 3
//CAN0IDMR6 = MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg
//CAN0IDAR7 = 0x00; //| / with ID 0x100
//CAN0IDMR7 = MASK_CODE_ST_ID_LOW; //|/

/* Exit Initialization Mode Request */
CAN4CTL0 = 0x00;

/* Wait for Normal Mode */
while ((CAN4CTL1&0x00) != 0);
}
unsigned char can4ReadFrame(void)
{
unsigned char length, index;
// unsigned char rxdata[8];
// unsigned int rxID;
unsigned char rxRTR;
unsigned char IDE;

if(CAN4RFLG & CAN4RFLG_RXF_MASK) return(0);
// if no new message=>return
//if(!(CAN4RFLG & CAN4RFLG_RXF_MASK)) return(0);
// if not standard identifier=>return
//if(CAN4RXIDR1 & 0x08) return(0);
// if not data frame=>return
//if(CAN4RXIDR1 & 0x10) return(0);

/* Get Can ID */
rxID= CAN4RXIDR0;
rxID= (rxID<<3) + (CAN4RXIDR1>>5);
/* Get received data */
length = (CAN4RXDLR & 0x0F);
for (index=0; index<length; index++)
rxbuff[index] = *(&CAN4RXDSR0 + index);

CAN4RFLG = CAN4RFLG_RXF_MASK;

return 1;
}
unsigned char can4SendFrame(unsigned long id, unsigned char priority, unsigned char length, unsigned char *txdata)
{
unsigned char txbuffer;
unsigned char index;

//Check TXE Buffer
if(!CAN4TFLG) return 1;

//Set TXE Buffer
CAN4TBSEL = CAN4TFLG;
txbuffer = CAN4TBSEL;

//Write ID
*((unsigned long *) ((unsigned long)(&CAN4TXIDR0))) = id;
CAN4TXIDR0=0B10101010;
CAN4TXIDR1=0B10100111;


//Write Data
for (index=0;index<length;index++) {
*(&CAN4TXDSR0 + index) = txdata[index];
}
/* Set Data Length Code */
CAN4TXDLR = length;
/* Set Priority */
CAN4TXTBPR = priority;

/* Start transmission */
CAN4TFLG = txbuffer;

/* Wait transmission */
while ( (CAN4TFLG & txbuffer) != txbuffer);
}
void Delay (void) {
unsigned int counter;
for (counter=0;counter<50000;counter++);
}
#pragma CODE_SEG NON_BANKED

void interrupt VectorNumber_Vcan4rx IQR_INT(void) {
unsigned char length, index;
unsigned char rxdata[8];

length = (CAN4RXDLR & 0x0F);
for (index=0; index<length; index++)
rxdata[index] = *(&CAN4RXDSR0 + index); /* Get received data */

CAN4RFLG = 0x01; /* Clear RXF */
}

#pragma CODE_SEG DEFAULT

void main(void)
{

// SET Port-M For CAN4
DDRM_DDRM7=1;
DDRM_DDRM6=1;
MODRR_MODRR2=0;
MODRR_MODRR3=1;
//MSCAN Setting Function
can4Init();
//Wait CAN BUS SYNCH
while (!(CAN4CTL0 & 0x10));
//Clear CAN0 RXF flag
CAN4RFLG = 0x01;
//Enable CAN0 RX interrupt
CAN4RIER = 0x01;
//Enable all interrupt
EnableInterrupts;

for (;;)
{
/* CAN0 Send Data */
// errorflag = can4SendFrame((ST_ID_100), 0x00, sizeof(txbuff)-1, txbuff);
Delay();
//Delay();
// errorflag = can4ReadFrame();
//Task_CAN = can4ReadFrame();
// Task();
}
}
void Task(void)
{
if(Task_CAN & 0x01){
/* CAN0 Send Data */
Task_CAN=can4SendFrame((ST_ID_100), 0x00, sizeof(txbuff)-1, txbuff);
//Delay();
Delay();
Task_CAN=0x00;
}

}

Outcomes