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

CAN RX isn't working...short code example posted

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



Joined: 08 Sep 2008
Posts: 21

View user's profile Send private message

CAN RX isn't working...short code example posted
PostPosted: Fri Nov 21, 2008 1:50 pm     Reply with quote

I am using the short code below, but the CAN receive interrupts aren't getting triggered (and neither is the can_kbhit). The printf's don't output anything, and the LED on PIN_A4 isn't toggling when I send the PIC a CAN message (although it does toggle every second according to the code). I know that CAN is working because I'm receiving CAN messages FROM the PIC, AND I know that my messages going TO the PIC are getting acknowledged because my PC program doesn't show any errors when I send the PIC the command. Any questions why I'm getting no "action" when the PIC is receiving a CAN packet?

Here's the code... (I'm using compiler version 4.081)

Code:
#include <18F4685.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <can-18xxx8.c>

#FUSES H4
#use delay (clock=40M, oscillator=10M)
#USE RS232(Baud=19200, XMIT=PIN_B6, RCV=PIN_B7, BITS=8, PARITY=N, ERRORS)

#int_CANRX1
CANRX1_isr()
{
   output_bit(PIN_A4, !input(PIN_A4));
   printf("1");
}

#int_CANRX0
CANRX0_isr()
{
   output_bit(PIN_A4, !input(PIN_A4)); 
   printf("0");
}

void canSetBaud(void)
{
   can_set_mode(CAN_OP_CONFIG);   
   BRGCON1.brp       = 7;
   BRGCON1.sjw       = 0;
   BRGCON2.prseg    = 2;
   BRGCON2.seg1ph   = 7;
   BRGCON2.sam      = 0;
   BRGCON2.seg2phts = TRUE;
   BRGCON3.seg2ph   = 7;
   BRGCON3.wakfil   = FALSE;
   RXB0CON.rxb0dben=FALSE;   
   can_set_mode(CAN_OP_NORMAL);
}


void main()
{
   int buffer[8], rx_len;
   int32 id = 0xAFAFAFAF;

   setup_adc_ports(AN0|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   enable_interrupts(INT_CANRX1);
   enable_interrupts(INT_CANRX0);

   enable_interrupts(GLOBAL);

   buffer[0] = 0x00;
   buffer[1] = 0x11;
   buffer[2] = 0x22;
   buffer[3] = 0x33;
   buffer[4] = 0x44;
   buffer[5] = 0x55;
   buffer[6] = 0x66;
   buffer[7] = 0x77;

   can_init();
   canSetBaud();
   //set_adc_channel(0);

   while (TRUE)
   {
      delay_ms(1000);
      output_bit(PIN_A4, !input(PIN_A4));
      if (can_kbhit())
      {
         output_bit(PIN_A4, !input(PIN_A4));
      }
      can_putd(id,buffer,8,3,TRUE,FALSE);

   }

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 21, 2008 3:50 pm     Reply with quote

Read both pages of this thread.
http://www.ccsinfo.com/forum/viewtopic.php?t=26637
seidleroni



Joined: 08 Sep 2008
Posts: 21

View user's profile Send private message

PostPosted: Mon Nov 24, 2008 8:14 am     Reply with quote

I found the problem. my canSetBaud function needs to have one more line in, and then the final function will look like this:
Code:

void canSetBaud(void)
{
   can_set_mode(CAN_OP_CONFIG);   
   BRGCON1.brp       = 7;
   BRGCON1.sjw       = 0;
   BRGCON2.prseg    = 2;
   BRGCON2.seg1ph   = 7;
   BRGCON2.sam      = 0;
   BRGCON2.seg2phts = TRUE;
   BRGCON3.seg2ph   = 7;
   BRGCON3.wakfil   = FALSE;
   RXB0CON.rxb0dben=FALSE;   
   
   
   RXB0CON.rxm      = CAN_RX_ALL;  //added line to make receive interrupts work

   can_set_mode(CAN_OP_NORMAL);
}
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