|
|
View previous topic :: View next topic |
Author |
Message |
campbell.donald
Joined: 06 Apr 2010 Posts: 3
|
CAN Nodes With PIC16F877A and MCP2510 |
Posted: Tue Apr 06, 2010 9:02 pm |
|
|
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
|
|
|
campbell.donald
Joined: 06 Apr 2010 Posts: 3
|
|
Posted: Tue Apr 06, 2010 9:20 pm |
|
|
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
|
|
|
campbell.donald
Joined: 06 Apr 2010 Posts: 3
|
|
Posted: Tue Apr 06, 2010 10:04 pm |
|
|
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;
}
|
|
|
|
|
|
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
|