I'm trying to make a connection between two pic18f458's over CAN bus. I'm using two mcp2551 to connect to the bus system. I'm experiencing difficulty's and I'm not sure what it is that i'm doing wrong.
When the first controller starts sending, i'm seeing the same data over and over on my scope ( on the txd of mcp2551 ). On the receive of my second pic (receiver) i see the same data comming in (so i assume bus is connected correctly). when I look at the tx of my second pic i see that on init it sends data 1 time, right after the first transmit of the initiating pic. (seems like it's answering but nothing further happens). when i look at the rx of my first pic i see roughtly the same data that is send by that same pic. (guessing that it is an error frame or it's trying to re-send over and over the same message because no ack is given by receiver)
If someone know's what i'm doing wrong and is able to help me i'd be very gratefull
tx (first pic) code:
Code: Select all
unsigned short aa, aa1, len, aa2;
unsigned char data[8];
long id;
unsigned short zr;
void main(){
PORTC = 0;
TRISC = 0;
PORTD = 0;
TRISD = 0;
aa = 0;
aa1 = 0;
aa2 = 0;
aa1 = CAN_TX_PRIORITY_0 &
CAN_TX_XTD_FRAME &
CAN_TX_NO_RTR_FRAME;
aa = CAN_CONFIG_SAMPLE_THRICE &
CAN_CONFIG_PHSEG2_PRG_ON &
CAN_CONFIG_STD_MSG &
CAN_CONFIG_DBL_BUFFER_ON &
CAN_CONFIG_VALID_XTD_MSG &
CAN_CONFIG_LINE_FILTER_OFF;
data[0] = 0;
// init CAN
CANInitialize(1,1,3,3,1,aa);
// set can to config mode
CANSetOperationMode(CAN_MODE_CONFIG,0xFF);
id = -1;
Delay_ms(5000);
//set all mask1 bits to ones
CANSetMask(CAN_MASK_B1,ID,CAN_CONFIG_XTD_MSG);
//set all mask2 bits to ones
CANSetMask(CAN_MASK_B2,ID,CAN_CONFIG_XTD_MSG);
// set id of filter B1_F1 to 3
CANSetFilter(CAN_FILTER_B2_F3,3,CAN_CONFIG_XTD_MSG);
// set can to normal mode
CANSetOperationMode(CAN_MODE_NORMAL,0xFF);
//PORTD = 0xFF;
id = 12111;
CANWrite(id,data,1,aa1);
while(1){
zr = CANRead(&id, data , &len, &aa2);
if((id == 3) && zr){
PORTD = 0xAA;
PORTC = data[0];
data[0]++;
delay_ms(18);
CANWrite(id, data, 2 , aa1);
}
}
}
rx ( second pic ) code:
Code: Select all
unsigned short aa, aa1, len, aa2;
unsigned char data[8];
long id;
unsigned short zr, cont, oldstate;
void main(){
PORTC = 0;
TRISC = 0;
PORTD = 0;
TRISD = 0;
aa = 0;
aa1 = 0;
aa2 = 0;
aa1 = CAN_TX_PRIORITY_0 &
CAN_TX_XTD_FRAME &
CAN_TX_NO_RTR_FRAME;
aa = CAN_CONFIG_SAMPLE_THRICE &
CAN_CONFIG_PHSEG2_PRG_ON &
CAN_CONFIG_STD_MSG &
CAN_CONFIG_DBL_BUFFER_ON &
CAN_CONFIG_VALID_XTD_MSG &
CAN_CONFIG_LINE_FILTER_OFF;
data[0] = 0;
// init CAN
CANInitialize(1,1,3,3,1,aa);
// set can to config mode
CANSetOperationMode(CAN_MODE_CONFIG,0xFF);
id = -1;
//set all mask1 bits to ones
CANSetMask(CAN_MASK_B1,ID,CAN_CONFIG_XTD_MSG);
//set all mask2 bits to ones
CANSetMask(CAN_MASK_B2,ID,CAN_CONFIG_XTD_MSG);
// set id of filter B1_F1 to 3
CANSetFilter(CAN_FILTER_B2_F3,12111,CAN_CONFIG_XTD_MSG);
// set can to normal mode
CANSetOperationMode(CAN_MODE_NORMAL,0xFF);
//PORTD = 0xFF;
id = 3;
//CANWrite(id,data,1,aa1);
while(1){
oldstate = 0;
zr = CANRead(&id, data , &len, &aa2);
if((id == 12111) & zr){
PORTD = 0xAA;
PORTC = data[0];
data[0]++;
delay_ms(20);
CANWrite(id, data, 2 , aa1);
}
}
}
Thank you