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

pic18f46k80 canbus readfail

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



Joined: 25 Apr 2013
Posts: 14

View user's profile Send private message

pic18f46k80 canbus readfail
PostPosted: Wed Sep 17, 2014 11:58 am     Reply with quote

i have made 2 programs with the help of this forum, a transmiter and a receiver for the canbus. i don't receive any data on the Rx line (pin_b3) on the board2_id. i use board1_id just to transmit and the other one just to receive.
can someone tell me if the code is correct?
compiler version is 5.008
can driver mcp2551 - rs conected to gnd (0 ohm)
thank you in advance

board1_id

Code:
#include <18F46K80.h>
#fuses HSH, NOPROTECT, PUT, BROWNOUT, NOWDT, NOPLLEN
#fuses CANB
#use delay(clock=20000000)

#include <can-18F4580.c>  // Use correct driver

#define BOARD1_ID  24 //cu ecran lcd
#define BOARD2_ID  25

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)


int16 ms;

#int_timer2
void isr_timer2(void) {
   ms++; //keep a running timer that increments every milli-second
}

void main() {
   struct rx_stat rxstat;
   int32 rx_id;
   int in_data[8];
   int rx_len;

//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=24;
   int1 tx_rtr=1;
   int1 tx_ext=0;
   int tx_len=8;
   int tx_pri=3;

   int i;

   for (i=0;i<8;i++) {
      out_data[i]=0;
      in_data[i]=0;
   }

   printf("\r\n\r\nCCS CAN EXAMPLE\r\n");

   setup_timer_2(T2_DIV_BY_4,79,16);   //setup up timer2 to interrupt every 1ms if using 20Mhz clock

   can_init();

   enable_interrupts(INT_TIMER2);   //enable timer2 interrupt
   enable_interrupts(GLOBAL);       //enable all interrupts (else timer2 wont happen)

   printf("\r\nRunning...");

   while(TRUE)
   {

      if ( can_kbhit() ) {  //if data is waiting in buffer...
         output_high(pin_e0);
         if(can_getd(rx_id, &in_data[0], rx_len, rxstat)) { //...then get data from buffer
               printf("\r\nGOT: BUFF=%U ID=%LU 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");
               }

      }

      //every two seconds, send new data if transmit buffer is empty
      if ( can_tbe() && (ms > 2000)) {
         ms=0;
         i=can_putd(tx_id, out_data, tx_len,tx_pri,tx_ext,tx_rtr); //put data on transmit buffer
         if (i != 0xFF) { //success, a transmit buffer was open
                     output_toggle(pin_e0);
                     printf("\r\nPUT %U: ID=%LU 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");
                  can_abort();
               }
         }
      else {
            //
            //printf("\r\nFAIL on PUTD\r\n");
         }
   }
}



board2_id:
Code:
#include <18F46K80.h>
#fuses HSH, NOPROTECT, PUT, BROWNOUT, NOWDT, NOPLLEN
#fuses CANB
#use delay(clock=20000000)

#include <can-18F4580.c>  // Use correct driver

#define BOARD1_ID  24
#define BOARD2_ID  25 //cu sursa 7805

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)


int16 ms;

#int_timer2
void isr_timer2(void) {
   ms++; //keep a running timer that increments every milli-second
}

void main() {
   struct rx_stat rxstat;
   int32 rx_id;
   int in_data[8];
   int rx_len;
   int1 transmit=0;
//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=24;
   int1 tx_rtr=1;
   int1 tx_ext=0;
   int tx_len=8;
   int tx_pri=3;

   int i;

   for (i=0;i<8;i++) {
      out_data[i]=0;
      in_data[i]=0;
   }

   printf("\r\n\r\nCCS CAN EXAMPLE\r\n");

   setup_timer_2(T2_DIV_BY_4,79,16);   //setup up timer2 to interrupt every 1ms if using 20Mhz clock

   output_high(pin_d3);
   can_init();

   enable_interrupts(INT_TIMER2);   //enable timer2 interrupt
   enable_interrupts(GLOBAL);       //enable all interrupts (else timer2 wont happen)

   printf("\r\nRunning...");
   delay_ms(100);
   output_low(pin_d3);
   while(TRUE)
   {

      if ( can_kbhit() ) {   //if data is waiting in buffer...
         output_high(pin_d3);
       if(can_getd(rx_id, &in_data[0], rx_len, rxstat)) { //...then get data from buffer
            printf("\r\nGOT: BUFF=%U ID=%LU 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");
         }

      }

      //every two seconds, send new data if transmit buffer is empty
      if ( can_tbe() && (ms > 2000) && (transmit)) {
         ms=0;
         i=can_putd(tx_id, out_data, tx_len,tx_pri,tx_ext,tx_rtr); //put data on transmit buffer
         if (i != 0xFF) { //success, a transmit buffer was open
            printf("\r\nPUT %U: ID=%LU 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");
         }
      }
   }
}
Code:
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 17, 2014 2:58 pm     Reply with quote

My advice is:

1. First make each PIC work in loopback mode. Example:
http://www.ccsinfo.com/forum/viewtopic.php?t=48915&start=3

2. Next, use my two-board test program. I explain how I have it working
with the 18F46K80 near the end of this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=51749&start=4
705150



Joined: 25 Apr 2013
Posts: 14

View user's profile Send private message

PostPosted: Thu Sep 18, 2014 9:35 am     Reply with quote

@PCM programmer i am once again thankful for your support, i have tried your code at the beginning, but i now suspect a defect mcp2551 at that time. It works with your code for 18f46k80.

But what i need now is to know how can i transfer text or variables over the canbus (eg temp reading or calendar time). I will have a i2c lcd connected to one 18f46k80 and receive various text/variables from another 18f46k80 that needs to be displayed on the lcd.

Also, if you could tell me the maximum length of the data one transmission can provide (believe its 8byte but I've heard of as much as 64bytes?)

Thank you once again
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 18, 2014 10:20 am     Reply with quote

CAN FD is something new. For anything we can currently do, it's 8 bytes.

With regard to sending text, read this old thread. I had some doubts,
but then other people contacted Microchip Support and got a work-around:
http://www.ccsinfo.com/forum/viewtopic.php?t=17941

This concerns sending entire sentences (like this one), that are longer
than the 8 bytes in one frame. If all you want to do is send, eg.,
TACH1234 or TEMP0180, etc, then there's no problem. Then you don't
need the above link.
705150



Joined: 25 Apr 2013
Posts: 14

View user's profile Send private message

PostPosted: Thu Sep 18, 2014 10:30 am     Reply with quote

thank you PCM programmer for your response. it will do for now the 8byte length.

have a pleasant day
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