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

18F2480 CAN bus and MCP2551

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



Joined: 04 Jun 2009
Posts: 3

View user's profile Send private message

18F2480 CAN bus and MCP2551
PostPosted: Mon Jun 08, 2009 8:25 am     Reply with quote

I'am trying to configure the can bus of the pic 18f2480 with the CAN Transceiver MCP2551.
I'am using a 40Mhz crystal and i would a speed of 250kbits/s.
I'am also using the library functions titled can18xxx8.c and can18xxx8.h
I configured the 3 baud rate registers by using the software that was given by PCM Programmer :MBTime

Code:
#IFNDEF CAN_USE_EXTENDED_ID
  #define CAN_USE_EXTENDED_ID         TRUE
#ENDIF

#IFNDEF CAN_BRG_SYNCH_JUMP_WIDTH
  #define CAN_BRG_SYNCH_JUMP_WIDTH  1  //synchronized jump width (def: 1 x Tq)
#ENDIF

#IFNDEF CAN_BRG_PRESCALAR
  #define CAN_BRG_PRESCALAR  7  //baud rate generator prescalar (def: 4) ( Tq = (2 x (PRE + 1))/Fosc )
#ENDIF

#ifndef CAN_BRG_SEG_2_PHASE_TS
 #define CAN_BRG_SEG_2_PHASE_TS   FALSE //phase segment 2 time select bit (def: freely programmable)
#endif

#ifndef CAN_BRG_SAM
 #define CAN_BRG_SAM 0 //sample of the can bus line (def: bus line is sampled 1 times prior to sample point)
#endif

#ifndef CAN_BRG_PHASE_SEGMENT_1
 #define CAN_BRG_PHASE_SEGMENT_1  4 //phase segment 1 (def: 6 x Tq)
#endif

#ifndef CAN_BRG_PROPAGATION_TIME
 #define CAN_BRG_PROPAGATION_TIME 0 //propagation time select (def: 3 x Tq)
#endif

#ifndef CAN_BRG_WAKE_FILTER
 #define CAN_BRG_WAKE_FILTER FALSE   //selects can bus line filter for wake up bit
#endif

#ifndef CAN_BRG_PHASE_SEGMENT_2
 #define CAN_BRG_PHASE_SEGMENT_2 2 //phase segment 2 time select (def: 6 x Tq)
#endif

#ifndef CAN_USE_RX_DOUBLE_BUFFER
 #define CAN_USE_RX_DOUBLE_BUFFER TRUE   //if buffer 0 overflows, do NOT use buffer 1 to put buffer 0 data
#endif


And my program is :
Code:
#include <18F2480.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT
#use delay(clock=40M,oscillator=10M)
#include <can-18xxx8.c>

#define   put_off      output_low
#define   put_on     output_high

#define   can_error        pin_C1   
#define   can_led          pin_C3   


void main() {

   int out_data[8];
   int32 tx_id=24;
   int1 tx_rtr=1;
   int1 tx_ext=0;
   int tx_len=8;
   int tx_pri=3;

   int i;

   for (i=0;i<8;i++) {
      out_data[i]=0;
   }

   can_init();

   while(1)
   {           
      if ( can_tbe() && (delay_ms(200))   
      {
         i=can_putd(tx_id, out_data, tx_len,tx_pri,tx_ext,tx_rtr);
         if (i != 0xFF) {
         put_on(can_led);
         delay_ms(150);
           put_off(can_led));
         }
         else {          
               put_on(can_error);
      delay_ms(150);
        put_off(can_error);
         }
      }
   }
}


I have the software called NSI527 on my computer in order to check the communication of the CAN bus.
The problem is the pic doesn't send any data. And when i tried to send datas from my pc just for checking the status of the connection, it sends 1 or 2 datas then the software is in mode BUS OFF !!!!

Here is a scheme of my connection:
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 08, 2009 11:02 am     Reply with quote

Quote:

I have the software called NSI527 on my computer in order to check the
communication of the CAN bus.

What CAN bus adapter hardware is used with the NSI527 program ?
There must be some kind of cable or adapter board that goes between
your PC and your CAN bus board. I assume that it is connected to J1
on your board. Explain how you connect the PC to the CAN bus on your
board.
nordin59



Joined: 04 Jun 2009
Posts: 3

View user's profile Send private message

PostPosted: Mon Jun 08, 2009 1:14 pm     Reply with quote

I have on my computer a CAN PCI and the software NSI527.
With Windows Millenium, It allows to send and receive frames on many channels CAN. Both are linked (pic an computer).
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 08, 2009 1:36 pm     Reply with quote

Quote:
I have on my computer a CAN PCI and the software NSI527.

Is your PC program setup to use standard or extended ID's ?
In your posted code, you are using standard IDs. Make sure
the PC program is setup for the same thing.

Quote:
int out_data[8];
int32 tx_id=24;
int1 tx_rtr=1;
int1 tx_ext=0;
int tx_len=8;
int tx_pri=3;

In my test program here, in can_putd(), I set tx_rtr = 0, so I think
you should try that.
http://www.ccsinfo.com/forum/viewtopic.php?t=29627&start=7


Quote:
while(1)
{
if ( can_tbe() && (delay_ms(200))
{
i=can_putd(tx_id, out_data, tx_len,tx_pri,tx_ext,tx_rtr);
if (i != 0xFF) {
put_on(can_led);

According to the CAN bus driver here,
Quote:
c:\program files\picc\drivers\can-18xxx8.c

the data type returned by can_putd() is an 'int1'.
Code:

int1 can_putd(int32 id, int * data, int len, int priority, int1 ext, int1 rtr) {
// Returns:
//       If successful, it will return TRUE
//       If un-successful, will return FALSE


It can only have a boolean value of 1 or 0 (TRUE or FALSE). But in your
code, you are comparing it to 0xFF. You need to fix that.
nordin59



Joined: 04 Jun 2009
Posts: 3

View user's profile Send private message

PostPosted: Tue Jun 09, 2009 6:23 am     Reply with quote

Thanks for your help,

Yes, the PC program is setup for using standard IDs.
And i've modified the condition
Code:
 if ( i==1 ) {
      put_on(can_led);}

But the problem still not solved.
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