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 CCS Technical Support

PIC18F4685 CAN receive doesn't work

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ramona1805



Joined: 05 May 2010
Posts: 13

View user's profile Send private message

PIC18F4685 CAN receive doesn't work
PostPosted: Tue Aug 24, 2010 1:04 am     Reply with quote

Hello everybody,
I've got a new problem working on my CAN-Communication.
I managed to send messages to my CANcaseXL without great problems.
But now I want to receive messages from CANalyzer.
I've tested an older PIC-Program for a PIC18F458 where every incoming message is detected by the Interrupt CANRX0 or CANRX1.
This works fine only with enabling these Interrupts and the global int and can_init().
In my program I do really the same. As I've told sending works fine but the ISR doesn't come up on sending a message from CANalyzer.

I'd be glad about some help. If you need more Information to help me I will offer.

I'm happy about every reply.
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

Re: PIC18F4685 CAN receive doesn't work
PostPosted: Tue Aug 24, 2010 5:16 am     Reply with quote

Ramona1805 wrote:
Hello everybody,
I've got a new problem working on my CAN-Communication.
I managed to send messages to my CANcaseXL without great problems.
But now I want to receive messages from CANalyzer.
I've tested an older PIC-Program for a PIC18F458 where every incoming message is detected by the Interrupt CANRX0 or CANRX1.
This works fine only with enabling these Interrupts and the global int and can_init().
In my program I do really the same. As I've told sending works fine but the ISR doesn't come up on sending a message from CANalyzer.

I'd be glad about some help. If you need more Information to help me I will offer.

I'm happy about every reply.


Here is what I'd try:

First, try polling the canbus and see if those routines get packets. That is, do something like:
Code:

   if (can_kbhit()) {
      if (can_getd(rx_id, &buffer, rx_len, rxstat)) {
                     //process the packet here
      }
   }      


And/or try to set the ECAN module of the PIC chip to blindly accept all packets (Sorry, I don't remember exactly what to set the config registers to for that. If you can't find it I can look it up.)

You didn't set any masks/filters did you?
Ramona1805



Joined: 05 May 2010
Posts: 13

View user's profile Send private message

PostPosted: Wed Aug 25, 2010 12:50 am     Reply with quote

Thank you for your fast reply collink.
That's what I've read in other threads too.. about the function can_kbhit().
So first of all I tried to show a combination on my PortD-LEDs.
if can_kbhit() returns true.. but nothing happens.
On my CAN-Case I send a message every 10ms or 20ms and get the green light for sending on the case but my LEDs don't change :(
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

PostPosted: Wed Aug 25, 2010 5:35 am     Reply with quote

Ramona1805 wrote:
Thank you for your fast reply collink.
That's what I've read in other threads too.. about the function can_kbhit().
So first of all I tried to show a combination on my PortD-LEDs.
if can_kbhit() returns true.. but nothing happens.
On my CAN-Case I send a message every 10ms or 20ms and get the green light for sending on the case but my LEDs don't change :(


Hmm... Try this ->

In the can_init routine under:

curfunmode=CAN_FUN_OP_LEGACY;

place these two lines:

Code:

   RXB0CON.rxm = CAN_RX_ALL;
   RXB1CON.rxm = CAN_RX_ALL;   


That will force both receive buffers to accept any frame they see whether it's standard, extended, matches a filter, anything. If they see it they'll record it. I believe it will even accept frames with errors. You almost never want this in a real project but it will narrow down whether the frames are actually getting to you or not.
Ramona1805



Joined: 05 May 2010
Posts: 13

View user's profile Send private message

PostPosted: Sun Aug 29, 2010 11:16 pm     Reply with quote

Ok.. now I get the message I send.. but even if only I send ones, the kbhit()-function gets always true..
do I have to delete a message when I've read it.. or whats wrong now?
I also tried out the other configuration RX_EXT and RX_STD but with that I don't receive anything..
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

PostPosted: Mon Aug 30, 2010 4:58 am     Reply with quote

Ramona1805 wrote:
Ok.. now I get the message I send.. but even if only I send ones, the kbhit()-function gets always true..
do I have to delete a message when I've read it.. or whats wrong now?
I also tried out the other configuration RX_EXT and RX_STD but with that I don't receive anything..


I've seen this happen when setting both receive buffers to CAN_RX_ALL. My guess is that, despite not technically being in LISTEN_ONLY mode, it pretends to be in LISTEN_ONLY mode anyway. This will cause the receiving end to not acknowledge the frames. Thus the transmitting end will resend forever. I don't remember whether it happens if you set just one of the buffers to CAN_RX_ALL.

The good news is that the test shows that the frames *ARE* getting to the other end properly. Now you just have to figure out why they don't pass through the normal pathway. That is, why are the masks/filters not passing the frames through. Have you tried making sure that all masks and filters are set to allow everything through?

Code:

   can_set_mode(CAN_OP_CONFIG);
   can_set_id(RX0MASK, CAN_MASK_ACCEPT_ALL, true);  //set mask 0
   can_set_id(RXFILTER0, 0, true);  //set filter 0 of mask 0
   can_set_id(RXFILTER1, 0, true);  //set filter 1 of mask 0
   can_set_id(RX1MASK, CAN_MASK_ACCEPT_ALL, true);  //set mask 1
   can_set_id(RXFILTER2, 0, true);  //set filter 0 of mask 1
   can_set_id(RXFILTER3, 0, true);  //set filter 1 of mask 1
   can_set_id(RXFILTER4, 0, true);  //set filter 2 of mask 1
   can_set_id(RXFILTER5, 0, true);  //set filter 3 of mask 1

//set these only if you aren't in legacy mode.
   can_set_id(RXFILTER6, 0, true);
   can_set_id(RXFILTER7, 0, true);
   can_set_id(RXFILTER8, 0, true);
   can_set_id(RXFILTER9, 0, true);
   can_set_id(RXFILTER10, 0, true);
   can_set_id(RXFILTER11, 0, true);
   can_set_id(RXFILTER12, 0, true);
   can_set_id(RXFILTER13, 0, true);
   can_set_id(RXFILTER14, 0, true);
   can_set_id(RXFILTER15, 0, true);

   can_set_mode(CAN_OP_NORMAL);
Ramona1805



Joined: 05 May 2010
Posts: 13

View user's profile Send private message

PostPosted: Wed Sep 01, 2010 4:47 am     Reply with quote

I've looked up my code about this and found the same configuration as you posted: (should be the original code from the compiler.
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=CAN_FUN_OP_LEGACY;

   // RXB0CON
   //    filthit0=0
   //    jtoff=0
   //      rxb0dben=1   buffer zero will overflow into buffer one
   //      rxrtrro=0
   //      rxm1:0=0      will recive all valid IDs
   RXB0CON=0;
   //RXB0CON.rxm=CAN_RX_VALID; //aus Original
   RXB0CON.rxm = CAN_RX_ALL; // alle Nachrichten
   //RXB1CON.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;
   CIOCON.tx2src=CAN_CANTX2_SOURCE;       //added for PIC18F6585/8585/6680/8680
   CIOCON.tx2en=CAN_ENABLE_CANTX2;        //added for PIC18F6585/8585/6680/8680

   can_set_id(RX0MASK, CAN_MASK_ACCEPT_ALL, CAN_USE_EXTENDED_ID);  //set mask 0
   can_set_id(RXFILTER0, 0, CAN_USE_EXTENDED_ID);  //set filter 0 of mask 0
   can_set_id(RXFILTER1, 0, CAN_USE_EXTENDED_ID);  //set filter 1 of mask 0

   can_set_id(RX1MASK, CAN_MASK_ACCEPT_ALL, CAN_USE_EXTENDED_ID);  //set mask 1
   can_set_id(RXFILTER2, 0, CAN_USE_EXTENDED_ID);  //set filter 0 of mask 1
   can_set_id(RXFILTER3, 0, CAN_USE_EXTENDED_ID);  //set filter 1 of mask 1
   can_set_id(RXFILTER4, 0, CAN_USE_EXTENDED_ID);  //set filter 2 of mask 1
   can_set_id(RXFILTER5, 0, CAN_USE_EXTENDED_ID);  //set filter 3 of mask 1

   // set dynamic filters
   can_set_id(RXFILTER6, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER7, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER8, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER9, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER10, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER11, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER12, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER13, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER14, 0, CAN_USE_EXTENDED_ID);
   can_set_id(RXFILTER15, 0, CAN_USE_EXTENDED_ID);

   set_tris_b((*0xF93 & 0xFB ) | 0x08);   //b3 is out, b2 is in
 
   can_set_mode(CAN_OP_NORMAL);
}

and in file can-18f4580.h CAN_USE_EXTENDED_ID is defined as "true":
Code:

#IFNDEF CAN_USE_EXTENDED_ID
  #define CAN_USE_EXTENDED_ID         TRUE
#ENDIF

About the problem with sending the same message continuously I also tested with only setting RXB0CON.rxm = CAN_RX_ALL;

Perhaps you could help me better with the code of my can_init() on top.
If it would help I could also post you the other functions or the settings in can-18f4580.h.
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

PostPosted: Thu Sep 02, 2010 5:03 am     Reply with quote

I'm certainly running out of guesses... What is your compiler version?

Can you post the full source code for your test application (minus the canbus routines that come with CCS. If you modified those files then post a diff or something.)
Ramona1805



Joined: 05 May 2010
Posts: 13

View user's profile Send private message

PostPosted: Thu Sep 02, 2010 6:16 am     Reply with quote

I've changed some defines in can-18f4580 so that the communication to the cancase works:
Code:
#ifndef __CCS_CAN18F4580_LIB_DEFINES__
#define __CCS_CAN18F4580_LIB_DEFINES__

#ifndef CAN_DO_DEBUG
 #define CAN_DO_DEBUG FALSE
#endif

#IFNDEF CAN_USE_EXTENDED_ID
  #define CAN_USE_EXTENDED_ID         TRUE
#ENDIF

#IFNDEF CAN_BRG_SYNCH_JUMP_WIDTH
  #define CAN_BRG_SYNCH_JUMP_WIDTH  2  //synchronized jump width (def: 1 x Tq)
#ENDIF

#IFNDEF CAN_BRG_PRESCALAR
  #define CAN_BRG_PRESCALAR  1  //baud rate generator prescalar (def: 4) ( Tq = (2 x (PRE + 1))/Fosc )
#ENDIF

#ifndef CAN_BRG_SEG_2_PHASE_TS
 #define CAN_BRG_SEG_2_PHASE_TS   TRUE //phase segment 2 time select bit (def: freely programmable)
#endif

#ifndef CAN_BRG_SAM
 #define CAN_BRG_SAM 0 //sample of the can bus line (def: bus line is sampled 1 times prior to sample point)
#endif

#ifndef CAN_BRG_PHASE_SEGMENT_1
 #define CAN_BRG_PHASE_SEGMENT_1  6//phase segment 1 (def: 6 x Tq)
#endif

#ifndef CAN_BRG_PROPAGATION_TIME
 #define CAN_BRG_PROPAGATION_TIME 3 //propagation time select (def: 3 x Tq)
#endif

#ifndef CAN_BRG_WAKE_FILTER
 #define CAN_BRG_WAKE_FILTER FALSE   //selects can bus line filter for wake up bit
#endif

#ifndef CAN_BRG_PHASE_SEGMENT_2
 #define CAN_BRG_PHASE_SEGMENT_2 7 //phase segment 2 time select (def: 6 x Tq)
#endif

#ifndef CAN_USE_RX_DOUBLE_BUFFER
 #define CAN_USE_RX_DOUBLE_BUFFER FALSE   //if buffer 0 overflows, do NOT use buffer 1 to put buffer 0 data
#endif

#ifndef CAN_ENABLE_DRIVE_HIGH
 #define CAN_ENABLE_DRIVE_HIGH 1
#endif

#ifndef CAN_ENABLE_CAN_CAPTURE
 #define CAN_ENABLE_CAN_CAPTURE 0
#endif

#ifndef CAN_ENABLE_CANTX2           // added 03/30/09 for PIC18F6585/8585/6680/8680
   #define CAN_ENABLE_CANTX2 0      // 0 CANTX2 disabled, 1 CANTX2 enabled
#endif

#ifndef CAN_CANTX2_SOURCE           // added 03/30/09 for PIC18F6585/8585/6680/8680
   #define CAN_CANTX2_SOURCE 0      // 0 source is invert of CANTX1, 1 source is CAN Clock
#endif

And for testing it I made a separate test-project so that nothing could cause errors.
The files named can-18F4685.* are the renamed copies of the can-18F4580.*. I don't think that this causes the errors or do you?
And that's all of my test-code:
Code:

#include "18F4685.h"
#include "can-18F4685.h"
#include "can-18F4685.c"

#FUSES H4, NOWDT, PUT, BROWNOUT, NOLVP
#USE delay(clock = 40000000)

float n;
int8 can_Offset;
signed int32 can_Factor;
signed int32 n_send;
signed int32 n_receive;
int8 can_ErrCode;
int32 can_ID_send;
int32 can_ID_receive;
int8 can_data_send[5];
int8 can_data_receive[4];
int8 can_length_send;
int8 can_length_receive;
struct rx_stat test;

#INT_CANRX0
void canrx0_isr()
{
   output_d(0b11001100);
}

#INT_CANRX1
void canrx1_isr()
{
   output_d(0b11110000);
}

//Main-Methode
void main()
{
   can_init();
   output_d(0x00);
   can_length_send=5;
   can_length_receive = 4;
   can_ID_send = 0x101;
   can_ID_receive = 0x100;
   can_offset = 0;
   can_factor = 1000;

   n = 1234.98;
   
   n_send = (n-can_offset)*can_factor;
   
   can_data_send[0]=make8(n_send,0);
   can_data_send[1]=make8(n_send,1);
   can_data_send[2]=make8(n_send,2);
   can_data_send[3]=make8(n_send,3);
   can_data_send[4]=0;
   
   
   enable_interrupts(INT_CANRX0);
   enable_interrupts(INT_CANRX1);
   can_putd(0x101,can_data_send,can_length_send,3,0,0);
   while(1)
   {
      if(can_kbhit())
      {
         output_d(11100011);
      }
   }
}
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

PostPosted: Thu Sep 02, 2010 6:25 am     Reply with quote

Your code looks OK to me. You set the canbus to 500k baud and your timing values look good.

If it really is not working then I still have to suspect the compiler or some other oddity. I just do not see anything wrong with your source code. What is your compiler version?
Ramona1805



Joined: 05 May 2010
Posts: 13

View user's profile Send private message

PostPosted: Mon Sep 06, 2010 12:01 am     Reply with quote

My Compiler Version is V4.108
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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