|
|
View previous topic :: View next topic |
Author |
Message |
ambrojohn
Joined: 27 Apr 2008 Posts: 18
|
CANbus ID filtering...in LOOPBACK mode [resolved] |
Posted: Sun May 25, 2008 5:12 pm |
|
|
Hello everybody
I'm exercising with a pic18F2580 and a MCP2551 to use it on my car to read all the messages (or almost part of them) from its CAN bus.
Now, I'm trying to correctly use the masks and filters of the (E)CAN module in LOOPBACK mode (the datasheet says it is possible), but it seems not working.
I use a very simple code like this:
Code: | #include <18F2580.h>
#fuses NOWDT, INTRC, NOPUT, NOPROTECT
#use delay (clock=4000000) // oscillator frequency for delay_ms()
#include <can-18F4580_mod.c>
#use rs232(baud=9600, xmit=PIN_A0, rcv=PIN_B6)
void main()
{
struct rx_stat rxstat;
int32 rx_id;
int in_data[8];
int rx_len;
int i;
//send a request (tx_rtr=1) for 8 bytes of data (tx_len=8) from id 24 (tx_id=24)
int out_data[8];
int32 tx_id=0x24;
int1 tx_rtr=0;
int1 tx_ext=1;
int tx_len=8;
int tx_pri=3;
for (i=0;i<tx_len;i++)
{
out_data[i]=0xFC;
in_data[i]=0;
}
can_init();
can_set_mode(CAN_OP_LOOPBACK);
printf("Running....in mode: %d\n",CANCON.reqop);
while (TRUE)
{
if ( can_tbe())
{
i=can_putd(tx_id, out_data, tx_len,tx_pri,tx_ext,tx_rtr); //put data on transmit buffer
if (i != 0) { //success, a transmit buffer was open
printf("\r\nPUT %U: ID=%X LEN=%U ", i, tx_id, tx_len);
printf("PRI=%U EXT=%U RTR=%U\r\n DATA = ", tx_pri, tx_ext, tx_rtr);
for (i=0;i<tx_len;i++) {
printf("%X ",out_data[i]);
}
printf("\r\n");
}
else { //fail, no transmit buffer was open
printf("\r\nFAIL on PUTD\r\n");
}
}
output_bit( PIN_B0, 1 );
if ( can_kbhit() )
{
if(can_getd(rx_id, &in_data[0], rx_len, rxstat)) //...then get data from buffer
{
printf("\r\nGOT: BUFF=%U ID=%X LEN=%U OVF=%U ", rxstat.buffer, rx_id, rx_len, rxstat.err_ovfl);
printf("FILT=%U RTR=%U EXT=%U INV=%U", rxstat.filthit, rxstat.rtr, rxstat.ext, rxstat.inv);
printf("\r\n DATA = ");
for ( i = 0; i < rx_len; i++ )
{
printf("%X ",in_data[i]);
}
printf("\r\n");
}
else
{
printf("\r\nFAIL on GETD\r\n");
}
}
delay_ms(500);
output_bit( PIN_B0, 0 );
delay_ms(500);
}
}
|
with
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=0;
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;
can_set_id(RX0MASK, 0x7FFFFFFF, CAN_USE_EXTENDED_ID); //set mask 0
can_set_id(RXFILTER0, 0x23, CAN_USE_EXTENDED_ID); //set filter 0 of mask 0
can_set_id(RXFILTER1, 0x23, CAN_USE_EXTENDED_ID); //set filter 1 of mask 0
can_set_id(RX1MASK, 0x7FFFFFFF, CAN_USE_EXTENDED_ID); //set mask 1
can_set_id(RXFILTER2, 0x23, CAN_USE_EXTENDED_ID); //set filter 0 of mask 1
can_set_id(RXFILTER3, 0x23, CAN_USE_EXTENDED_ID); //set filter 1 of mask 1
can_set_id(RXFILTER4, 0x23, CAN_USE_EXTENDED_ID); //set filter 2 of mask 1
can_set_id(RXFILTER5, 0x23, CAN_USE_EXTENDED_ID); //set filter 3 of mask 1
// set dynamic filters
//...
can_set_mode(CAN_OP_NORMAL);
} |
and
Code: | #define CAN_USE_EXTENDED_ID TRUE |
But I always receive the message I sent with ID = 0x24
Where is my mistake?
Moreover, do you have some hints to correctly set the system for this automotive application? That's because I can read a lot of messages (in NORMAL mode from my car) but only if I configure the pic to accept all the messages thus also the invalid ones.
Thank you for your help
Ambrogio
Rome[/code]
Last edited by ambrojohn on Thu May 29, 2008 1:58 am; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
ambrojohn
Joined: 27 Apr 2008 Posts: 18
|
|
Posted: Mon May 26, 2008 1:38 am |
|
|
Yes, I started reading on the forum before writing my post, in particular that message you are relating to (the title of this post resemble it, this is not a case!).
The difference is that I'm using extended IDs, but I also tried with standard IDs without any successful result. I wonder if it is a problem of the LOOPBACK mode (but I don't think so).
_________________________________
The second problem is that when I connect my circuit to the car I see, by means of a led on the RX line, that I'm receiving data, but I can read it only if I set the pic to accept all incoming messages, that is also the invalid ones. In fact, all the messages I receive have the INV field set to 1 (invalid).
Unfortunately, I'm not still able to understand what to do...
That's why I need help from someone more experienced in connecting to a car CAN bus.
Ambrogio |
|
|
ambrojohn
Joined: 27 Apr 2008 Posts: 18
|
[resolved] |
Posted: Thu May 29, 2008 1:58 am |
|
|
Ok, I'm sorry....
The problem was in the following line
Code: | RXB0CON.rxm=CAN_RX_ALL; |
which I had modify, but I miss to reset to default in this way
Code: | RXB0CON.rxm=CAN_RX_VALID; |
I hope this will help
bye |
|
|
|
|
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
|