Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

CAN module in normal mode and dspic30f4012 +code required

Status
Not open for further replies.

yvp

Newbie level 4
Joined
Jun 14, 2009
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
kanpur
Activity points
1,419
c1rx1con

Has anyone of u tried CAN in normal mode operation? i interfaced transceiver (MCP2551) with the dspic, also the loopback test is responding.

please suggest a way to get this CAN module working!!! HELP :(

( the code is a modified form of the code available at microchip's website for loopback test )
here is the code for transmission

#include <p30f4012.h>


#define FCY 7370000 // 7.37 MHz
#define BITRATE 1000000 // 1Mbps
#define NTQ 15 // Number of Tq cycles which will make the
//CAN Bit Timing .
#define BRP_VAL ((FCY/(2*NTQ*BITRATE))-1) //Formulae used for C1CFG1bits.BRP

//---------------------------------------------------------------------

// Buffer Registers for CAN data to be send out in the transmit mode.

unsigned int OutData0[4] = {0x5251, 0x5453, 0x5655, 0x5857};
unsigned int OutData1[2] = {0x5A59, 0x5C5B};
unsigned int OutData2[4] = {0x6261, 0x6463, 0x6665, 0x6867};
unsigned int OutData3[2] = {0x6A69, 0x6C6B};

// Intilializing the receive registers to be 0

unsigned int InData0[4] = {0, 0, 0, 0};
unsigned int InData1[2] = {0, 0};
unsigned int InData2[4] = {0, 0, 0, 0};
unsigned int InData3[2] = {0, 0};

//---------------------------------------------------------------------

int main(void)
{
//____________________________________________________________________________________________
// Osc initialization
//____________________________________________________________________________________________

OSCCONbits.COSC=1; //set internal oscillator as clock
OSCCONbits.TUN0=0; //set it to its default frequency
OSCCONbits.TUN1=0;
OSCCONbits.TUN2=0;
OSCCONbits.TUN3=0;
OSCCONbits.LPOSCEN=0;






TRISE = 0x0000; // Initialize the PORTD as output
LATE = 0x0000; // Initially LEDs of dsPICDEM1.1 Plus GP Board are off


//--------------------------------------------------------------------------------------------------------------------
//Initialization of CAN1 Module and Enabling of CAN1 Interrupts
//--------------------------------------------------------------------------------------------------------------------



C1CTRLbits.CANCKS = 1; // Select the CAN Master Clock . It is equal to Fcy here.
// equal to Fcy=7.37MHz)


C1CFG1bits.SJW=00; //Synchronized jump width time is 1 x TQ when SJW is equal to 00
C1CFG1bits.BRP = BRP_VAL; //((FCY/(2*NTQ*BITRATE))-1)

C1CFG2 = 0x03F5; // SEG1PH=6Tq, SEG2PH=3Tq, PRSEG=5Tq
// Sample 3 times
// Each bit time is 15Tq

///Interrupt Section of CAN Peripheral

C1INTF = 0; //Reset all The CAN Interrupts
IFS1bits.C1IF = 0; //Reset the Interrupt Flag status register
C1INTE = 0x00FF; //Enable all CAN interrupt sources
IEC1bits.C1IE = 1; //Enable the CAN1 Interrupt

//-----------------------------------------------------------------------------------------------------------------------
// Configure Receive registers, Filters and Masks
//-----------------------------------------------------------------------------------------------------------------------

// We are initializing the Receive Buffer 0 and Receive Buffer 1 for CAN1


C1RX0CON = C1RX1CON = 0x0000; // Receive Buffer1 and 0 Status
//and Control Register for CAN1


// Acceptance Mask Register0SID and Register1SID associated with Recieve Buffer0
// and Receive Buffer1 for CAN1
C1RXM0SID = C1RXM1SID = 0x1FFD;

// Acceptance Mask Register0EIDH and Register1EIDH associated with Recieve Buffer0
// and Receive Buffer1 for CAN1
C1RXM0EIDH = C1RXM1EIDH = 0x0FFF;

// Acceptance Mask Register0EIDL and Register1EIDL associated with Recieve Buffer0
//and Receive Buffer1 for CAN1
C1RXM0EIDL = C1RXM1EIDL = 0xFC00;


//Initializing of Acceptance Filter n Standard Identifier for CAN1

C1RXF0SID = 0x0AA8; //CAN1 Receive Acceptance Filter2 SID
C1RXF2SID = 0x1555; //CAN1 Receive Acceptance Filter2 SID
C1RXF2EIDH = 0x0004; //CAN1 Receive Acceptace Filter2 Extended Identifier high byte
C1RXF2EIDL = 0x8C00; //CAN1 Receive Acceptance Filter2 Extended identifier low byte




//-----------------------------------------------------------------------------------------------------------------------
// Configure Transmit Registers Buffer 0 and Transmit Buffer 1
//-----------------------------------------------------------------------------------------------------------------------

C1TX0CON = 0x0003; // High priority
C1TX0SID = 0x50A8; // SID
C1TX0EID = 0x0000; // EID
C1TX0DLC = 0x01C0; //Select the Data word Length for CAN1 Transmit Buffer0 which is 8 byte

// Data Field 1,Data Field 2, Data Field 3, Data Field 4 // 8 bytes selected by DLC

C1TX0B1 = OutData0[0];
C1TX0B2 = OutData0[1];
C1TX0B3 = OutData0[2];
C1TX0B4 = OutData0[3];

C1TX1CON = 0x0002; // High Intermediate priority
C1TX1SID = 0xA855; // SID
C1TX1EID = 0x0004; // EID
C1TX1DLC = 0x8DA0; //Select the Data word Length for CAN1 Transmit Buffer1 which
// is 4 byte

//Data Field 1, Data Field 2 // 4 bytes selected by DLC

C1TX1B1 = OutData1[0];
C1TX1B2 = OutData1[1];

//Change to NORMAL Operation Mode from Configuration Mode

C1CTRLbits.REQOP = 0;

while(C1CTRLbits.OPMODE != 0);//Wait for CAN1 mode change from Configuration Mode to Normal mode

//Enable transmission

C1TX0CONbits.TXREQ = 1;
C1TX1CONbits.TXREQ = 1;


while(1)
{


//ADD YOUR CODE HERE//

// THE CODE WILL LOOP HERE AND WHEN AN CAN INTERRUPT COMES IT WILL GO TO INTERRUPTS//


} //end while loop


} //end main loop


//--------------------------------------------------------------------------------------------------------------------------
//Interrupt Section for CAN1
//--------------------------------------------------------------------------------------------------------------------------

void __attribute__((interrupt, no_auto_psv)) _C1Interrupt(void)
{

IFS1bits.C1IF = 0; //Clear interrupt flag

if(C1INTFbits.TX0IF)
{

C1INTFbits.TX0IF = 0; //If the Interrupt is due to Transmit0 of CAN1 Clear the Interrupt
LATEbits.LATE0 = 1;
}
else if(C1INTFbits.TX1IF)
{

C1INTFbits.TX1IF = 0; //If the Interrupt is due to Transmit1 of CAN1 Clear the Interrupt
LATEbits.LATE1 = 1;
}

if(C1INTFbits.RX0IF)
{

C1INTFbits.RX0IF = 0; //If the Interrupt is due to Receive0 of CAN1 Clear the Interrupt

InData0[0] = C1RX0B1;
InData0[1] = C1RX0B2; //Move the recieve data from Buffers to InData
InData0[2] = C1RX0B3;
InData0[3] = C1RX0B4;

if ((InData0[0]==OutData0[0]) && (InData0[1]==OutData0[1]) && (InData0[2]==OutData0[2]) && (InData0[3]==OutData0[3]))

LATEbits.LATE0 = 1; // If the data received is same which was transmitted
// turn on the LED
}

else if(C1INTFbits.RX1IF)
{

C1INTFbits.RX1IF = 0; //If the Interrupt is due to Receive1 of CAN1 Clear the Interrupt
InData1[0] = C1RX1B1; //Move the data received to Indata Registers
InData1[1] = C1RX1B2;

if ((InData1[0]==OutData1[0]) && (InData1[1]==OutData1[1]))

LATEbits.LATE1 =1; //If the data received is same which was transmitted
//turn on the LED
}
}








for receiving uC the code is


#include <p30f4012.h>


#define FCY 7370000 // 7.37 MHz
#define BITRATE 1000000 // 1Mbps
#define NTQ 15 // Number of Tq cycles which will make the
//CAN Bit Timing .
#define BRP_VAL ((FCY/(2*NTQ*BITRATE))-1) //Formulae used for C1CFG1bits.BRP

//---------------------------------------------------------------------

// Buffer Registers for CAN data to be send out in the transmit mode.

unsigned int OutData0[4] = {0x5251, 0x5453, 0x5655, 0x5857};
unsigned int OutData1[2] = {0x5A59, 0x5C5B};
unsigned int OutData2[4] = {0x6261, 0x6463, 0x6665, 0x6867};
unsigned int OutData3[2] = {0x6A69, 0x6C6B};

// Intilializing the receive registers to be 0

unsigned int InData0[4] = {0, 0, 0, 0};
unsigned int InData1[2] = {0, 0};
unsigned int InData2[4] = {0, 0, 0, 0};
unsigned int InData3[2] = {0, 0};

//---------------------------------------------------------------------

int main(void)
{
//____________________________________________________________________________________________
// Osc initialization
//____________________________________________________________________________________________

OSCCONbits.COSC=1; //set internal oscillator as clock
OSCCONbits.TUN0=0; //set it to its default frequency
OSCCONbits.TUN1=0;
OSCCONbits.TUN2=0;
OSCCONbits.TUN3=0;
OSCCONbits.LPOSCEN=0;






TRISE = 0x0000; // Initialize the PORTE as output
LATE = 0x0000; // Initially LEDs of Eport are off

//--------------------------------------------------------------------------------------------------------------------
//Initialization of CAN1 Module and Enabling of CAN1 Interrupts
//--------------------------------------------------------------------------------------------------------------------



C1CTRLbits.CANCKS = 1; // Select the CAN Master Clock . It is equal to Fcy here.
// equal to Fcy=7.37MHz)


C1CFG1bits.SJW=00; //Synchronized jump width time is 1 x TQ when SJW is equal to 00
C1CFG1bits.BRP = BRP_VAL; //((FCY/(2*NTQ*BITRATE))-1)

C1CFG2 = 0x03F5; // SEG1PH=6Tq, SEG2PH=3Tq, PRSEG=5Tq
// Sample 3 times
// Each bit time is 15Tq

///Interrupt Section of CAN Peripheral

C1INTF = 0; //Reset all The CAN Interrupts
IFS1bits.C1IF = 0; //Reset the Interrupt Flag status register
C1INTE = 0x00FF; //Enable all CAN interrupt sources
IEC1bits.C1IE = 1; //Enable the CAN1 Interrupt

//-----------------------------------------------------------------------------------------------------------------------
// Configure Receive registers, Filters and Masks
//-----------------------------------------------------------------------------------------------------------------------

// We are initializing the Receive Buffer 0 and Receive Buffer 1 for CAN1


C1RX0CON = C1RX1CON = 0x0000; // Receive Buffer1 and 0 Status
//and Control Register for CAN1


// Acceptance Mask Register0SID and Register1SID associated with Recieve Buffer0
// and Receive Buffer1 for CAN1
C1RXM0SID = C1RXM1SID = 0x1FFD;

// Acceptance Mask Register0EIDH and Register1EIDH associated with Recieve Buffer0
// and Receive Buffer1 for CAN1
C1RXM0EIDH = C1RXM1EIDH = 0x0FFF;

// Acceptance Mask Register0EIDL and Register1EIDL associated with Recieve Buffer0
//and Receive Buffer1 for CAN1
C1RXM0EIDL = C1RXM1EIDL = 0xFC00;


//Initializing of Acceptance Filter n Standard Identifier for CAN1

C1RXF0SID = 0x0AA8; //CAN1 Receive Acceptance Filter2 SID
C1RXF2SID = 0x1555; //CAN1 Receive Acceptance Filter2 SID
C1RXF2EIDH = 0x0004; //CAN1 Receive Acceptace Filter2 Extended Identifier high byte
C1RXF2EIDL = 0x8C00; //CAN1 Receive Acceptance Filter2 Extended identifier low byte




//-----------------------------------------------------------------------------------------------------------------------
// Configure Transmit Registers Buffer 0 and Transmit Buffer 1
//-----------------------------------------------------------------------------------------------------------------------

C1TX0CON = 0x0003; // High priority
C1TX0SID = 0x50A8; // SID
C1TX0EID = 0x0000; // EID
C1TX0DLC = 0x01C0; //Select the Data word Length for CAN1 Transmit Buffer0 which is 8 byte

// Data Field 1,Data Field 2, Data Field 3, Data Field 4 // 8 bytes selected by DLC

C1TX0B1 = OutData0[0];
C1TX0B2 = OutData0[1];
C1TX0B3 = OutData0[2];
C1TX0B4 = OutData0[3];

C1TX1CON = 0x0002; // High Intermediate priority
C1TX1SID = 0xA855; // SID
C1TX1EID = 0x0004; // EID
C1TX1DLC = 0x8DA0; //Select the Data word Length for CAN1 Transmit Buffer1 which
// is 4 byte

//Data Field 1, Data Field 2 // 4 bytes selected by DLC

C1TX1B1 = OutData1[0];
C1TX1B2 = OutData1[1];

//Change to NORMAL Operation Mode from Configuration Mode

C1CTRLbits.REQOP = 0;

while(C1CTRLbits.OPMODE != 0);//Wait for CAN1 mode change from Configuration Mode to Normal mode

/*//Enable transmission

C1TX0CONbits.TXREQ = 1;
C1TX1CONbits.TXREQ = 1;
*/

while(1)
{


//ADD YOUR CODE HERE//

// THE CODE WILL LOOP HERE AND WHEN AN CAN INTERRUPT COMES IT WILL GO TO INTERRUPTS//


} //end while loop


} //end main loop


//--------------------------------------------------------------------------------------------------------------------------
//Interrupt Section for CAN1
//--------------------------------------------------------------------------------------------------------------------------

void __attribute__((interrupt, no_auto_psv)) _C1Interrupt(void)
{

IFS1bits.C1IF = 0; //Clear interrupt flag

/* if(C1INTFbits.TX0IF)
{

C1INTFbits.TX0IF = 0; //If the Interrupt is due to Transmit0 of CAN1 Clear the Interrupt

}
else if(C1INTFbits.TX1IF)
{

C1INTFbits.TX1IF = 0; //If the Interrupt is due to Transmit1 of CAN1 Clear the Interrupt

}

*/

if(C1INTFbits.RX0IF)
{

C1INTFbits.RX0IF = 0; //If the Interrupt is due to Receive0 of CAN1 Clear the Interrupt
LATEbits.LATE0 = 1;
InData0[0] = C1RX0B1;
InData0[1] = C1RX0B2; //Move the recieve data from Buffers to InData
InData0[2] = C1RX0B3;
InData0[3] = C1RX0B4;

if ((InData0[0]==OutData0[0]) && (InData0[1]==OutData0[1]) && (InData0[2]==OutData0[2]) && (InData0[3]==OutData0[3]))

LATEbits.LATE0 = 1; // If the data received is same which was transmitted
// turn on the LED
}

else if(C1INTFbits.RX1IF)
{

C1INTFbits.RX1IF = 0; //If the Interrupt is due to Receive1 of CAN1 Clear the Interrupt
LATEbits.LATE1 =1;

InData1[0] = C1RX1B1; //Move the data received to Indata Registers
InData1[1] = C1RX1B2;

if ((InData1[0]==OutData1[0]) && (InData1[1]==OutData1[1]))

LATEbits.LATE1 =1; //If the data received is same which was transmitted
//turn on the LED
}
}

:cry:
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top