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 support@ccsinfo.com

Another CAN Bus Problem ...! [SOLVED]
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
baklou



Joined: 10 Jan 2014
Posts: 22

View user's profile Send private message

PostPosted: Wed Feb 19, 2014 1:31 am     Reply with quote

Thank you for the over-writing issue, I totally missed it. I changed a lot of things in your "two boards" test program because the communication doesn't work even between board #1 and #2 in the first "way" to go. So I focused on a "simple" communication, so just board #1 to board #2.

About the CAN library, I just modified the can_init() function, and more particularly the "CAN_RX_MODE", and add the I/O settings in this function

Code:

set_tris_c((*0xF94 & 0xBF ) | 0x80);


So finally, here is my can_init() routine, which I didn't change a lot :

Code:

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

   // RXB0CON
   //    filthit0=0
   //    jtoff=0
   //      rxb0dben=1   buffer zero will overflow into buffer one
   //      rxrtrro=0
   //      rxm1:0=0      will receive all valid IDs
   RXB0CON=0;
   RXB0CON.rxm=CAN_RX_ALL;
   RXB0CON.rxb0dben=CAN_USE_RX_DOUBLE_BUFFER;
   RXB1CON=RXB0CON;

   CIOCON.endrhi=CAN_ENABLE_DRIVE_HIGH;
   CIOCON.cancap=CAN_ENABLE_CAN_CAPTURE;
   CIOCON.tx2src=CAN_CANTX2_SOURCE;       //added for PIC18F6585/8585/6680/8680
   CIOCON.tx2en=CAN_ENABLE_CANTX2;        //added for PIC18F6585/8585/6680/8680

   can_set_id(RX0MASK, CAN_MASK_ACCEPT_ALL, CAN_USE_EXTENDED_ID);  //set mask 0
   can_set_id(RXFILTER0, 0, CAN_USE_EXTENDED_ID);  //set filter 0 of mask 0
   can_set_id(RXFILTER1, 0, CAN_USE_EXTENDED_ID);  //set filter 1 of mask 0

   can_set_id(RX1MASK, CAN_MASK_ACCEPT_ALL, CAN_USE_EXTENDED_ID);  //set mask 1
   can_set_id(RXFILTER2, 0, CAN_USE_EXTENDED_ID);  //set filter 0 of mask 1
   can_set_id(RXFILTER3, 0, CAN_USE_EXTENDED_ID);  //set filter 1 of mask 1
   can_set_id(RXFILTER4, 0, CAN_USE_EXTENDED_ID);  //set filter 2 of mask 1
   can_set_id(RXFILTER5, 0, CAN_USE_EXTENDED_ID);  //set filter 3 of mask 1

   // set dynamic filters
   can_set_id(RXFILTER6, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER7, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER8, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER9, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER10, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER11, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER12, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER13, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER14, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER15, 0, CAN_USE_EXTENDED_ID);

   set_tris_c((*0xF94 & 0xBF ) | 0x80); // C7 as input and C6 as output
 
   can_set_mode(CAN_OP_NORMAL);
}


I know exchanging files here is not allowed, so don't ask to you to give me back the original file. But I never modify things I don't understand. Anyway, I don't really know if my problem is software or hardware. I even changed my two PICs, maybe a factory default...but nothing.

I really start to become crazy...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 20, 2014 12:28 am     Reply with quote

I was able to make my two board CAN bus test program work. I only
have one 18F46K80, so I used that on Board #2. I used an 18F2580
on Board #1. I didn't have two 8 MHz crystals so I used two 20 MHz
crystals. I can type into TeraTerm and see the typing OK.

I have posted a link to a photo of my test setup at the bottom. I am using
jumper wires as the "can bus". But I have MCP2551's and 120 ohm
resistors at each end. Note that I have a black ground jumper between
the two boards. This article says the CAN Bus needs one:
http://www.edn.com/electronics-blogs/dev-monkey-blog/4408712/Does-The-CAN-Bus-Need-a-Common-Ground-

I edited the can_init() routine in can-18F4580.c to set the correct TRISC,
the same as you did. That's the only change I made to that file. I left
this line unchanged:
Code:
RXB0CON.rxm=CAN_RX_VALID;


One problem in your early code was that you didn't specify a fuse for
your 8 MHz crystal. So the compiler used the INTRC_HP fuse by default.
You were not running off the crystal but rather the internal oscillator.
Possibly this was the source of your problem (too many errors).
But you fixed that problem by using the HSM fuse in your later code.

Here is the program for Board #2 which has the 18F46K80:
Code:

#include <18F46K80.h>
#fuses HSH, NOPROTECT, PUT, BROWNOUT, NOWDT, NOPLLEN
#fuses CANC  // *** Baklou is using CANC
#use delay(clock=20000000)

#include <can-18F4580.c>  // Use correct driver

#define BOARD1_ID  24
#define BOARD2_ID  25

// Uncomment this line to compile code for Board #1.
// Comment it out to compile for Board #2.
//#define BOARD1   1   

//======================================
void main()
{
struct rx_stat rxstat;
int32 rx_id;
int32 tx_id;
int8 rx_len;               
int8 buffer[8];

can_init();

#ifdef BOARD1   // For Board #1
while(1)
  {
   buffer[0] = getc();   // Wait for a character

   // Transmit it to board #2.
   can_putd(BOARD2_ID, buffer, 1, 1, 1, 0);

   buffer[0] = 0;   // Clear the buffer byte

   // Wait for the char to be echoed back.
   while(!can_kbhit());

   if(can_getd(rx_id, buffer, rx_len, rxstat))
     {
      if(rx_id == BOARD1_ID)  // Is it for this board ?
        {
         putc(buffer[0]);  // If so, display the char
        }
      }
  }
#else  // For Board #2
while(1)
  {
   if(can_kbhit())  // Message available ?
     {
      // If so, get the message.
      if(can_getd(rx_id, buffer, rx_len, rxstat))
        {
         if(rx_id == BOARD2_ID)  // Is it for this board ?
           {
            // If so, echo back the character.
            can_putd(BOARD1_ID, buffer, 1, 1, 1, 0);
           }
         }
      }
  }
#endif
 
}


This is the Board #1 code:
Code:

#include <18F2580.h>
#fuses HS, NOPROTECT, PUT, BROWNOUT, NOWDT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include <can-18F4580.c>  // Use correct driver

#define BOARD1_ID  24
#define BOARD2_ID  25

// Uncomment this line to compile code for Board #1.
// Comment it out to compile for Board #2.
#define BOARD1   1   

//======================================
void main()
{
struct rx_stat rxstat;
int32 rx_id;
int32 tx_id;
int8 rx_len;               
int8 buffer[8];

can_init();

#ifdef BOARD1   // For Board #1
while(1)
  {
   buffer[0] = getc();   // Wait for a character

   // Transmit it to board #2.
   can_putd(BOARD2_ID, buffer, 1, 1, 1, 0);

   buffer[0] = 0;   // Clear the buffer byte

   // Wait for the char to be echoed back.
   while(!can_kbhit());

   if(can_getd(rx_id, buffer, rx_len, rxstat))
     {
      if(rx_id == BOARD1_ID)  // Is it for this board ?
        {
         putc(buffer[0]);  // If so, display the char
        }
      }
  }
#else  // For Board #2
while(1)
  {
   if(can_kbhit())  // Message available ?
     {
      // If so, get the message.
      if(can_getd(rx_id, buffer, rx_len, rxstat))
        {
         if(rx_id == BOARD2_ID)  // Is it for this board ?
           {
            // If so, echo back the character.
            can_putd(BOARD1_ID, buffer, 1, 1, 1, 0);
           }
         }
      }
  }
#endif
 
}

The photo in the link below shows the test setup. The green and yellow
jumper wires are needed because my MCP2551 is hardwired to the CAN
bus pins on PortB. To test the PortC pins (which you are using), I
jumpered them over.
http://s27.postimg.org/itynzzckj/Two_CAN_bus_boards.jpg
baklou



Joined: 10 Jan 2014
Posts: 22

View user's profile Send private message

PostPosted: Thu Feb 20, 2014 9:31 am     Reply with quote

You really miss nothing ! lol effectively I made this mistake, and I saw it leaving the NOFCMEN fuse.
Anyway, after checking all of the stuffs, I think this only can be the oscillator frequency which is the problem...because it's the only thing which differs in my project than yours (and all the others) and as written on the can-18F4680 library, all the defaults settings are defined for this clock frequency...And I effectively still use this defaults settings...
I order a delivery for 20Mhz frequency oscillators, I will tell you this tomorrow.

Thank you a lot
baklou



Joined: 10 Jan 2014
Posts: 22

View user's profile Send private message

PostPosted: Mon Feb 24, 2014 4:00 am     Reply with quote

Hello everybody,

I come back again and for the last time. Instead of waiting for a new oscillator, I calculate again the good values to have a 125 Kbps CAN bus. And nothing has changed.
So I decided to change again my two transceivers, because of the previous issue I met, I was wondering...
And it is. It was my f***ing transceivers...from the beginning. Sorry to be so angry but on six transceivers ordered, I had at least two of them which was wrong....

Anyway, thanks to PCM Programmer to allow me his time and his advices.

Good news for a Monday morning Smile

Thanks Greg
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Mon Feb 24, 2014 10:56 am     Reply with quote

Well, that's a good start to the week !! Very Happy

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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