|
|
View previous topic :: View next topic |
Author |
Message |
mattmac24
Joined: 09 Sep 2010 Posts: 7
|
CAN bus Filters problem |
Posted: Wed Sep 29, 2010 11:56 am |
|
|
Hi guys, Ive been reading up a fair bit on this problem and I know there are alot of posts about this but I am still having trouble getting my can bus to work. I am using 2 PIC18F4685, each connected to a MCP2551 and MAX232 chip. I have 120ohm terminating resistors in place on the can bus.
I am trying to accept only messages for 1 can id (for now). Using can standard addressing. When I set it to accept all it seems to work fine.
Here is my code (modified from PCM programmer's test program for can).
Code: |
#include <18F4585.h>
#fuses HS, NOPROTECT, PUT, BROWNOUT, NOWDT, NOLVP
#use delay(clock=20M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <can-18xxx8.c>
int32 BOARD1_ID = 0x224; //548
int32 BOARD2_ID = 0x223; //547
//SET_TRIS_D(0x00); // ---not needed but why does this give errors???--
// 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
set_id_to_accept(BOARD2_ID);
while(1)
{
buffer[0] = getc(); // Wait for a character
// Transmit it to board #2.
can_putd(BOARD2_ID, buffer, 1, 1, 0, 0);
buffer[0] = 0; // Clear the buffer byte
// Wait for the char to be echoed back.
while(!can_kbhit())
{
delay_ms(100);
//output_high(PIN_D0);
output_high(PIN_D1);
delay_ms(100);
//output_low(PIN_D0);
output_low(PIN_D1);
delay_ms(100);
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
output_high(PIN_D0);
delay_ms(500);
output_low(PIN_D0);
delay_ms(500);
}
}
}
}
#else // For Board #2
set_id_to_accept(BOARD1_ID);
while(1)
{
output_high(PIN_D0);
// output_high(PIN_D1);
delay_ms(500);
output_low(PIN_D0);
// output_low(PIN_D1);
delay_ms(500);
if(can_kbhit()) // Message available
{
output_high(PIN_D1);
delay_ms(500);
output_low(PIN_D1);
delay_ms(500);
output_high(PIN_D1);
delay_ms(500);
output_low(PIN_D1);
delay_ms(500);
// 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, 0, 0);
}
}
}
}
#endif
}
|
The can_set_accept_id is one I made. It is inside can-18xxx8.c
Code: |
void set_id_to_accept(int32 id) {
can_set_mode(CAN_OP_CONFIG);
can_set_id(RX0MASK, 0x7FF, 0); //set mask 0..exact match - 0x7FF all 11 bits must match
can_set_id(RX0FILTER0, id, 0); //set filter 0 of mask 0
can_set_id(RX0FILTER1, id, 0); //set filter 1 of mask 0
can_set_id(RX1MASK, 0x7FF, 0); //set mask 1 ..see above
can_set_id(RX1FILTER2, id, 0); //set filter 0 of mask 1
can_set_id(RX1FILTER3, id, 0); //set filter 1 of mask 1
can_set_id(RX1FILTER4, id, 0); //set filter 2 of mask 1
can_set_id(RX1FILTER5, id, 0); //set filter 3 of mask 1
can_set_mode(CAN_OP_NORMAL);
}
|
The led's are only for me testing this as for some reason I can not transmit serial output. I can see my messages being sent on an oscilloscope from the output of the max232 but they never show up on my pc (sending from pc to pic works fine however).
Any help to get this working would be greatly appreciated or any general comments about my code would be good too as I am still new to all this.
Thanks for your help,
Matt
Last edited by mattmac24 on Fri Oct 01, 2010 1:22 am; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 29, 2010 4:18 pm |
|
|
Quote: | //SET_TRIS_D(0x00); // ---not needed but why does this give errors???-- |
What compiler version do you have ? The fact that it's sticking TRIS
statements out in space suggests that it's an old version. The version
is listed at the top of the .LST file, which will be in your project directory. |
|
|
mattmac24
Joined: 09 Sep 2010 Posts: 7
|
|
Posted: Thu Sep 30, 2010 5:32 am |
|
|
Hi, thanks for your reply.
I added that line in manually when I wanted to be sure I could put leds in places. It gave me errors which I couldn't really make much sense of. Where should that line go if I wanted to set the pin inputs/outputs? At the moment it is commented out anyway now and the leds work as expected. I think I'm running version 4.093 of the compiler but there was no .LST file I could see. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 30, 2010 3:13 pm |
|
|
Lines of code go inside functions, not in isolated areas by themselves.
That line is not needed anyway. The CCS compiler sets the TRIS for you
when you call the output_low() and output_high() functions. |
|
|
mattmac24
Joined: 09 Sep 2010 Posts: 7
|
|
Posted: Thu Sep 30, 2010 8:15 pm |
|
|
Yes now I can see that it is pretty obvious why that was giving errors. Good to know that it is all taken care of automatically thou.
Can you offer any tips with the rest of the code as to why my can filters are not working as expected? I am still having the same problems.
Thanks,
Matt |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Oct 03, 2010 3:58 pm |
|
|
Quote: |
#ifdef BOARD1 // For Board #1
set_id_to_accept(BOARD2_ID);
#else // For Board #2
set_id_to_accept(BOARD1_ID);
|
Do you have these swapped ? |
|
|
mattmac24
Joined: 09 Sep 2010 Posts: 7
|
|
Posted: Sat Oct 09, 2010 8:43 pm |
|
|
Yea that was it
Sorry, I guess I just made a stupid mistake and couldn't pick it up. Thanks for all your help! I'm obviously very new to this type of thing. |
|
|
|
|
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
|