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

CAN Nodes With PIC16F877A and MCP2510

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



Joined: 06 Apr 2010
Posts: 3

View user's profile Send private message

CAN Nodes With PIC16F877A and MCP2510
PostPosted: Tue Apr 06, 2010 9:02 pm     Reply with quote

Hello!

I am building a simple CAN bus with two identical nodes. Each node is made from a PIC16F877A, MCP2510, and an LT1796 transceiver.

I am using a 20 MHz oscillator on the MCP2510s.

I have set my timing as below:

Code:

#define CAN_BRG_PRESCALAR 1
#define CAN_BRG_PROPAGATION_TIME 3
#define CAN_BRG_PHASE_SEGMENT_1 8
#define CAN_BRG_PHASE_SEGMENT_2 8
#define CAN_BRG_SYNCH_JUMP_WIDTH 1


Before I do anything, I call the following functions:

Code:

can_init();
can_set_mode(CAN_OP_NORMAL);
can_set_baud();


My physical bus consists of a twisted pair of wires about 6 inches long, connecting the two LT1796 chips. Each LT1796 chip has a 120 Ohm resistor bridging the CAN_L and CAN_H signals.

When I run the "producer" node, it "successfully" sends three packets and then hangs. The "consumer" node never sees those packets, and simply does nothing.

Does anyone have any suggestions? I have checked my breadboard several times and all the pin connections appear to match those in can-mcp2510.h.

Thank you for your help!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 06, 2010 9:16 pm     Reply with quote

See this post. It shows a way to test the CAN bus connection between
two boards:
http://www.ccsinfo.com/forum/viewtopic.php?t=29627&start=7
campbell.donald



Joined: 06 Apr 2010
Posts: 3

View user's profile Send private message

PostPosted: Tue Apr 06, 2010 9:20 pm     Reply with quote

My code looks very similar to that. My concern is that I'm using a 20 MHz oscillator on the MCP2510, and therefore I need to fiddle with the bit timing.

If I set my bit timings to (what I think are incorrect) values, my producer successfully sends packets, but my receiver does not receive them.

Any suggestions?

Code:

#define CAN_BRG_PRESCALAR 1
#define CAN_BRG_PROPAGATION_TIME 1
#define CAN_BRG_PHASE_SEGMENT_1 3
#define CAN_BRG_PHASE_SEGMENT_2 3
#define CAN_BRG_SYNCH_JUMP_WIDTH 1
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 06, 2010 9:25 pm     Reply with quote

Read this thread. In it, I demonstrate that it can be done with 20 MHz
crystals on the MCP2510 chips:
http://www.ccsinfo.com/forum/viewtopic.php?t=41683
campbell.donald



Joined: 06 Apr 2010
Posts: 3

View user's profile Send private message

PostPosted: Tue Apr 06, 2010 10:04 pm     Reply with quote

OK, thanks! It appears to work.

I ripped apart my previous code, removed my customized bit timings, and simplified its calls to can_putd and can_getd. Now it seems to work.

For anyone interested, I have posted my working code below:

Code:

#include <16F877A.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP,NOPUT,NOWDT,NODEBUG
#use delay(clock=20000000)

#include <can-mcp2510.c>

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#define PRODUCER 1
#define CONSUMER 0

#define ROLE CONSUMER

int main(void) {
   int32 tx_id;
   int8 tx_data[8];
   int8 tx_len;
   struct rx_stat rxstat;
   int rx_len;
   int8 rx_data[8];
   int32 rx_id;
     
   can_init();
   
   can_set_mode(CAN_OP_NORMAL);
   
   while(1) {
#if ROLE == PRODUCER
      tx_id = 42;
      tx_data[0] = 0x55;
      tx_len = 1;
     
      if (can_putd(tx_id, tx_data, tx_len, 3, TRUE, FALSE)) {
         printf("Transmit OK. Successfully transmitted %x\n\r", tx_data[0]);
      } else {
         printf("Transmit FAILED.\n\r");
      } // else
#else
      if (can_kbhit()) {
         if(can_getd(rx_id, &rx_data[0], rx_len, rxstat)) {
            printf("Received packet from %x: %x\n\r", rx_id, rx_data[0]);
         }
      }

#endif
      delay_ms(500);
   }
   
   return -1;
}
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