CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

CAN BUS for DSPIC33

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ChrisK



Joined: 02 Apr 2008
Posts: 12

View user's profile Send private message

CAN BUS for DSPIC33
PostPosted: Wed Apr 02, 2008 1:44 am     Reply with quote

HI,

Has anybody a working code example for standard CAN operation that can be used with the DSPIC33 ?, or even better has anybody a working CCS Librarry for can operation that can be used with the DSPIC33 ?

I'm trying to set it up for almost 14 days , and still it won't work.

I really can use some help from anybody that has experience with CAN BUS. (I have no experince with this bus at all)

If I use the osciloscoop on the can-l and can-h lines (after the MCP2551) I see on both lines the same signal (togling from 2,5V down to 1,2V)

Do I need to connect at least two CAN Nodes to see anything usefull on the bus, or can I monitor the data with only the master unit on the bus ?

I terminated the bus with 120ohm on the master PCB

Thanks
jma_1



Joined: 08 Feb 2005
Posts: 147
Location: Wisconsin

View user's profile Send private message

PostPosted: Wed Apr 02, 2008 6:58 am     Reply with quote

Howdy,

I have not used the DSPIC33 or the MCP2551.

Generally you need two nodes with terminating resistors at both ends of the backbone. I would suggest a PC based CAN bus monitoring tool as well as your oscilloscope. An inexpensive PC USB-CAN or RS232-CAN converter ranges from $80 - 300.

On one test board I was playing with even if I had only one node, I would see data pumped out by the processor. The hardware handles the message acknowledgement. The message appeared to be sent repeatedly due to no other node acknowledging the message.

Is your can_init() function completing?

Have you tried the loopback mode?

Have you verified the basic functioning of the hardware and software as a sanity check? I'm notorious for making silly errors and not checking parts of code are actually being executed.

Cheers,
JMA
ChrisK



Joined: 02 Apr 2008
Posts: 12

View user's profile Send private message

PostPosted: Wed Apr 02, 2008 7:23 am     Reply with quote

Hi,

I ordered today a CAN development board from microchip, i receive it tomorow.

The can_init() routine is completing.

But I try only this
Code:

 // can test
  can_init();  // I changed to handle the DSP33.
  printf ("can normal mode\n\r");
 
  c1tr01con.txen0=true;  // buffer 0 is transmit buffer
  c1txd=0x470f;  //just some meaningless data, just to see if there's  something on the bus
  do
  {
     printf("c1tr01con register value = %2x%2x\n\r",*0x430,*0x431);
     c1tr01con.txreq0=true;  // start transmiting
     printf("c1tr01con register value = %2x%2x\n\r",*0x430,*0x431);
     //do
     //{
     //  printf("c1tr01con register value = %2x%2x\n\r",*0x430,*0x431);
     //}
     while((c1tr01con.txreq0)=true);

     printf ("send ");  // just to see if the message is send
     delay_ms(5000);
   }
   while(1);

The last printf in this code shows that there's bus error.

I didn't try the loopback mode, because I just want to see the transmitting data.

All other hardware and software fuctions are working correct, all AD channels, SPI, I2C, RS232 is implemented in code and are working correct.
jma_1



Joined: 08 Feb 2005
Posts: 147
Location: Wisconsin

View user's profile Send private message

PostPosted: Wed Apr 02, 2008 7:37 am     Reply with quote

Greetings ChrisK,

I'm not familiar enough with the dsPIC33 family to debug the software. I would have to wade through the datasheet to find the correct registers and diagram out the operation for myself.

Another sanity check -> some of the dsPIC33 chips have two CAN ports. Are the connections and software both looking at the same CAN port?

I might give the loopback mode a try. Can you use the debugger to examine the registers while your software is running? Perhaps this will provide a clue to the behavior.

JMA
ChrisK



Joined: 02 Apr 2008
Posts: 12

View user's profile Send private message

PostPosted: Fri Apr 04, 2008 1:47 am     Reply with quote

Hi JMA,

I checked the confoguration part and found something strange, I did the following:

in can-dsp33.h file
Code:

enum CAN_OP_MODE {CAN_OP_CONFIG=4, CAN_OP_LISTEN=3, CAN_OP_LOOPBACK=2, CAN_OP_DISABLE=1, CAN_OP_NORMAL=0};

//can control
struct {
   int1 sfrwin; //0 SFR map Window Select bit
    int1 void1; //1
    int1 void2; //2
   int1 cancap;  //3
   int1 void4;   //4
    CAN_OP_MODE opmode:3;  // 5:7    /status operation mode
   CAN_OP_MODE reqop:3;   //8:10   //request can operation mode bits
    int1 cancks;  //CAN Capture
    int1 abat;  // Abort all pending TX
    int1 csidl;  //Stop in idle mode
} CANCON1;
#WORD CANCON1 = 0x400   // can control located in 0x400 and 0x401


in can-dsp33.c file


void can_init(void) {
      can_set_mode(CAN_OP_CONFIG);   //must be in config mode before params can be set

// rest of code I left out for the time being

}

void can_set_mode(CAN_OP_MODE mode)
{
   printf("cancon1=%x%x\n\r",*0x401,*0x400);
   printf("can_op_mode request=%x\n\r",mode);
   CANCON1.reqop=mode;
   printf("cancon1.reqop=%x\n\r",cancon1.reqop);
   do
   {
      printf("cancon1=%x%x\n\r",*0x401,*0x400);
      delay_ms(100);
   }
   while( (CANCON1.opmode) != mode );
   printf("cancon1=%x%x\n\r",*0x401,*0x400);
   delay_ms(100);
   sleep();
}

following on hyperterminal

cancon1=0840
can_op_mode request=04
cancon1.reqop=00
cancon1=0000
cancon1=0000
--
--
--
endless the same

Why is cancon1.reqop not filled with 4, if I change the code to the following then it's OK
Code:

void can_set_mode(CAN_OP_MODE mode)
{
   printf("cancon1=%x%x\n\r",*0x401,*0x400);
   printf("can_op_mode request=%x\n\r",mode);
   CANCON1.reqop=4;  //   :exclaim: THIS IS THE ONLY CHANGED LINE !!!!!!!
   printf("cancon1.reqop=%x\n\r",cancon1.reqop);
   do
   {
      printf("cancon1=%x%x\n\r",*0x401,*0x400);
      delay_ms(100);
   }
   while( (CANCON1.opmode) != mode );
   printf("cancon1=%x%x\n\r",*0x401,*0x400);
   delay_ms(100);
   sleep();
}

following on hyperterminal

cancon1=0480
can_op_mode request=04
cancon1.reqop=04
cancon1=0480
cancon1=0480

This should be OK, but I don't understand why the variable mode is not inserted correctly, and even why in the first exmple the complete register is reset to 0000.

Any Idea ??????

Chris
jma_1



Joined: 08 Feb 2005
Posts: 147
Location: Wisconsin

View user's profile Send private message

PostPosted: Fri Apr 04, 2008 11:55 am     Reply with quote

Howdy ChrisK,

I do not have a dsPIC33 to play around with so I might no be much help.

I compared the can-18F4580.c source file to the code snippet you listed. I noticed something strange -> different check for configuration mode obtained.

while( (CANSTAT.opmode) != mode );

Perhaps this is a bug in the library function you are calling? I do not have a newer version of the compiler available to look at the library routines you might be accessing.

If you change the source file as you indicated, does the CAN interface work as expected?
jma_1



Joined: 08 Feb 2005
Posts: 147
Location: Wisconsin

View user's profile Send private message

PostPosted: Fri Apr 04, 2008 12:24 pm     Reply with quote

Howdy ChrisK,

I checked one of the datasheets and I do not believe the dsPIC33 has a CANSTAT register (or bit).

One reason for the INITIALIZATION_MODE entrance to fail is if a transmission is taking place. Based on the data sheet I'm assuming this to be transmitting by the dsPIC, but perhaps it also means no data being received at the same time (no other nodes transmitting; not connected to other nodes).

Does the order or enabling interrupts matter prior to initialization? Are global interrupts enabled prior to calling can_init()? As a test perhaps try waiting longer before intializing? Without trying out the hardware these are only guesses.

I do remember one forum member's post where he has having troubling intialization of the can port (same type of 'blocking' situation on a PIC18). Do you have another pic to try out? Perhaps it's a hardware issue?

Cheers,
JMA
ChrisK



Joined: 02 Apr 2008
Posts: 12

View user's profile Send private message

PostPosted: Sat Apr 05, 2008 1:06 am     Reply with quote

Hi JMA,

Sorry to say, but I guess you misunderstand my question.

In the first code sample: I put 4 in the variable mode,
but operation cancon1.reqop=mode, has as result 0x0000

If I change to cancon1.reqop=4 then result is 0x0004.

That I don't understand.

greetings Chris
jma_1



Joined: 08 Feb 2005
Posts: 147
Location: Wisconsin

View user's profile Send private message

PostPosted: Sat Apr 05, 2008 1:22 pm     Reply with quote

Greetings ChrisK,

I cannot explain the different behavior. However, the operational mode you are request still must follow the datasheet which could mean the operation failed to place the CAN module into initialization mode. The information I previously listed might indicate some other issue. Worse yet, the problem might be intermittent.

Cheers,
JMA
hec
Guest







What's new?
PostPosted: Fri May 02, 2008 3:59 pm     Reply with quote

Have you guys been able to create a CAN library for the dsPIC33 off of the can-18F4580?

If so... I would love to have a copy Very Happy
mrdr



Joined: 05 Jun 2009
Posts: 1
Location: Germany / Kaltenkirchen

View user's profile Send private message Visit poster's website

PostPosted: Mon Jun 08, 2009 1:01 am     Reply with quote

Rolling Eyes

Me too.

It would be nice if somebody could post his working version of a dsPIC can libary.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group