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 Bus PIC18f45k80
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Sterngleiter



Joined: 07 Jan 2013
Posts: 90

View user's profile Send private message

Can Bus PIC18f45k80
PostPosted: Mon Dec 31, 2018 9:43 am     Reply with quote

Hello, I use pic18f45k80 and pcwhd 5.070.
I would like to send and receive only.

Here is my code.

transmitter:
Code:

#include <can-18xxx8.c>

void main ( )
{
   int32 rx_id=0;
   int rx_len,rxstat,buffer[8];
   int1 a,b;
   int buf;
   
   can_init ( );
                     //wait for node c to power-up
   while ( TRUE )
   {
   
   if (input(pin_b4)==1)
   {
   while (input(pin_b4)==1);
      while (!can_putd(0x456, buffer, 2, 1, TRUE, FALSE)) {}
   buffer[0]=0x07;
   buffer[1]=0x70;
   buffer[2]=0;
   buffer[3]=0;
   buffer[4]=0;
   buffer[5]=0;
   buffer[6]=0;
   buffer[7]=0;

   }
     
  if (input(pin_b1)==1)
   {
   while (input(pin_b1)==1);
      while (!can_putd(0x200, buffer, 2, 1, TRUE, FALSE)) {}
   buffer[0]=0x07;
   buffer[1]=0x44;
   buffer[2]=0;
   buffer[3]=0;
   buffer[4]=0;
   buffer[5]=0;
   buffer[6]=0;
   buffer[7]=0;

   }     
     
   }
}

receiver:
Code:

#include <can-18xxx8.c>

void set_my_can_filters(void);
#define xyz 0x456
int32 rx_id=0;
struct rx_stat stat;
   int8 rx_len,buffer[8];

#INT_CANRX0
void  CANRX0_isr(void)
{
   // output_d(1);
   can_getd ( rx_id , &buffer , rx_len , stat );
if( rx_id == xyz&&buffer[0]==0x07)
            {
           
         output_d(buffer[1]);
           
            rx_id=0;
           buffer[1]=0;
           
            }
            if (rx_id==0x200){rx_id=0;}
}


void main ( )
{
   
   enable_interrupts(INT_CANRX0);
   enable_interrupts(INT_CANRX1);
   enable_interrupts(GLOBAL);
   
   can_init ( );
 set_my_can_filters();

     //wait for node c to power-up
   while ( TRUE )
   {
     }
}
void set_my_can_filters(void) {
   can_set_mode(CAN_OP_CONFIG);

  can_set_id(RX0MASK, 0x07FFFFFFF, CAN_USE_EXTENDED_ID);  //set mask 0
   can_set_id(RX0FILTER0, 0x456, 1);  //set filter 0 of mask 0
   can_set_id(RX0FILTER1, 0x456, 1);  //set filter 1 of mask 0

   can_set_id(RX1MASK, 0x07FFFFFFF, CAN_USE_EXTENDED_ID);  //set mask 1
   can_set_id(RX1FILTER2, 0x456, 1);  //set filter 0 of mask 1
   can_set_id(RX1FILTER3, 0x456, 1);  //set filter 1 of mask 1
   can_set_id(RX1FILTER4, 0x456, 1);  //set filter 2 of mask 1
   can_set_id(RX1FILTER5, 0x456, 1);  //set filter 3 of mask 1

   can_set_mode(CAN_OP_NORMAL);
}

But this is the following problem.
If I send with the address 0x456 command is executed.
Then I send the address 0x200 as expected, the receiver does nothing.
But when I ping the 0x456 send the buffer is received from the addresses 0x200 at the first send only at the second send the buffer is overwritten even though I have filter. Why?
Thanks for your help.
Sterngleiter



Joined: 07 Jan 2013
Posts: 90

View user's profile Send private message

Can Bus PIC18f45k80
PostPosted: Mon Dec 31, 2018 9:46 am     Reply with quote

And one more question. It is said that in the CAN bus line each ends with 120R resistance should be switched but it does not work when I do that.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Mon Dec 31, 2018 12:52 pm     Reply with quote

You do understand that you need external CAN transceivers?.
Sterngleiter



Joined: 07 Jan 2013
Posts: 90

View user's profile Send private message

PostPosted: Mon Dec 31, 2018 12:54 pm     Reply with quote

I use mcp2551, this is Can tranceiver.
Sterngleiter



Joined: 07 Jan 2013
Posts: 90

View user's profile Send private message

PostPosted: Mon Dec 31, 2018 12:56 pm     Reply with quote

http://ww1.microchip.com/downloads/en/devicedoc/21667e.pdf
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Mon Dec 31, 2018 2:23 pm     Reply with quote

Not sure what you mean by the resistance being 'switched'?.
The resistors should be 120R, at each end of the bus.
Sterngleiter



Joined: 07 Jan 2013
Posts: 90

View user's profile Send private message

PostPosted: Mon Dec 31, 2018 3:21 pm     Reply with quote

My problem if this resistance is used running bus is not.
Mainly I would like to know something about the first problem something, why I only on the second send, first the buffer is overwritten
newguy



Joined: 24 Jun 2004
Posts: 1900

View user's profile Send private message

PostPosted: Mon Dec 31, 2018 3:27 pm     Reply with quote

Do you have another CAN node on the bus? All CAN buses absolutely require at least two nodes, otherwise no transmissions will occur. All transmissions feature an acknowledgement time slot where any and all active nodes will actively ACK the transmission whether it is intended for the node or not. Without this active ACK, a transmitting node automatically tags the transmission as failed and will automatically retry forever.
Sterngleiter



Joined: 07 Jan 2013
Posts: 90

View user's profile Send private message

PostPosted: Mon Dec 31, 2018 3:31 pm     Reply with quote

Only the 2 first. It should send and receive, as shown above in the code is receiver and transmitter.
newguy



Joined: 24 Jun 2004
Posts: 1900

View user's profile Send private message

PostPosted: Mon Dec 31, 2018 3:44 pm     Reply with quote

So you have 2 nodes (yes/no)?

Each is running the code shown above (one as transmitter, one as receiver)?

You can get one message from transmitter -> receiver, but not if the 120R terminations are in place?
Sterngleiter



Joined: 07 Jan 2013
Posts: 90

View user's profile Send private message

PostPosted: Mon Dec 31, 2018 4:51 pm     Reply with quote

Yes that is right, I don‘t need the resistor 120r. It’s receive. The problem is when i send first time with 0x200 then with 0x456 the buffer have at first from 0x200. It must have at 0x456 . Why?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 31, 2018 4:55 pm     Reply with quote

Do your CAN bus connections look very similar to this schematic ? (Except that both of your PICs are 18F45K80 instead of 18F258).

temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Dec 31, 2018 7:27 pm     Reply with quote

a hardware question.
Are you using a real xtal/2caps for the clock or the internal RC oscillator ? While I don't use CAN (too much overhead...), if it's like USB you NEED to use a real xtal /2 caps for the clock.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Tue Jan 01, 2019 1:32 am     Reply with quote

I think there is a misunderstanding of CAN involved here.

All nodes receive/acknowledge all messages.
The ID and filters then allow the receiver to only actually 'listen' to
the messages it wants. However the message will still have been received
into the buffer.

Also I think you are misunderstanding the message ID. The ID is _not_
an 'address'. It is an identifier to specify the type of message.
Sterngleiter



Joined: 07 Jan 2013
Posts: 90

View user's profile Send private message

PostPosted: Tue Jan 01, 2019 6:20 am     Reply with quote

it may be that I have misunderstood something but I've bought this "CAN Bus Development Kit" at ccsinfo.com and checked there are the resistances not synonymous in the schematics in it.
http://www.ccsinfo.com/product_info.php?products_id=CANbuskit


I use 20mhz external xtal.
I have equipped the boards. It definitely does not work when I make the 120R pure.
Or is it another bus "http://www.ccsinfo.com/product_info.php?products_id=CANbuskit" which I do not believe. Nothing in the Exercise Book.
how can I adjust the bus,
that are the correct data on the first send in the buffer.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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