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

18F4580 + 82C251 Simple CANBus Program not working

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



Joined: 29 May 2004
Posts: 41
Location: Barcelona, Spain

View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger

18F4580 + 82C251 Simple CANBus Program not working
PostPosted: Thu Sep 17, 2009 4:26 am     Reply with quote

Hia all;

I've wrote this program (XTAL = 4MHz, CAN Baudrate= 125k). It seems to be a CAN config register mistake but I can't find it. I used "Microchip Bit Time Calculator" to find register values.

Any idea?
Code:
/************************************************************/
/*               P R E P R O C E S S O R                    */
/************************************************************/

#include <18F4580.h>

/************************************************************/
/*                         A D C                            */
/************************************************************/

#device ADC=10

/************************************************************/
/*                      F U S E S                           */
/************************************************************/

#fuses XT, NOPROTECT, PUT, BROWNOUT, NOWDT, NOLVP

/************************************************************/
/*           O S C I L A T O R   &   D E L A Y              */
/************************************************************/

#use delay(clock=4000000)

/************************************************************/
/*                S E T U P   R S - 2 3 2                   */
/************************************************************/

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

/************************************************************/
/*                  S E T U P   I 2 C                       */
/************************************************************/

// NONE

/************************************************************/
/*                   D E F I N I T I O N S                  */
/************************************************************/

// -----------------------------------------------------------------------------
// CAN-Interface
// -----------------------------------------------------------------------------

#define CAN_TX_ID                  0x301

#define CAN_BRG_PRESCALAR          0 // with Fosc=4MHz, #TQ per bit interval=500ns
                                     // -> Nominal Bit Rate=125kHz
#define CAN_BRG_SYNCH_JUMP_WIDTH   1
#define CAN_BRG_PROPAGATION_TIME   1
#define CAN_BRG_PHASE_SEGMENT_1    8
#define CAN_BRG_PHASE_SEGMENT_2    6
                               //-----
                               //  16 = number of TQ per bit interval
#define CAN_BRG_SEG_2_PHASE_TS TRUE    // phase segment 2 time select bit
                                       // (def: freely programmable)

/************************************************************/
/*                     I N C L U D E S                      */
/************************************************************/

#include <can-18xxx8.c>

/************************************************************/
/*           G L O B A L   V A R I A B L E S                */
/************************************************************/

int8 rx_len;               
int8 buffer[8];

char data;
char MODE;

enum state {RX=0,TX=1};

/************************************************************/
/*                 F U N C T I O N S                        */
/************************************************************/

void setup_PWM(void)
{
   setup_timer_3(T3_INTERNAL|T3_DIV_BY_4);
   setup_ccp1 (CCP_PWM);   
   setup_timer_2(T2_DIV_BY_4, 250, 1);
}

void setup_INT(void)
{
   ext_int_edge(0,L_TO_H);
   clear_interrupt(INT_EXT);
   enable_interrupts (int_ext);
   enable_interrupts (global);
}

void init_vars (void)
{
   rx_len = 8;
   data=48;
}

void write_CAN (void)
{
   buffer[0] =data;
   buffer[1] =data;
   buffer[2] =data;
   buffer[3] =data;
   buffer[4] =data;
   buffer[5] =data;
   buffer[6] =data;
   buffer[7] =data;       

   can_putd(CAN_TX_ID, buffer, rx_len, 1, 1, 0);
   putc(buffer[0]);

   set_pwm1_duty(200);
   delay_ms (50);
   set_pwm1_duty(0);
   delay_ms (450);
       
   buffer[0] = 0;   // Clear the buffer byte

   if (data==57)
   {
      data=48;
   }
   else
   {
      data++;
   }
}

/******************************************************************/
/*             I N T E R R U P T   V E C T O R S                  */
/******************************************************************/

#int_ext
void ext_isr()
{
   clear_interrupt(INT_EXT);
}

/******************************************************************/
/*                      M A I N   P R O G R A M                   */
/******************************************************************/

void main()
{

   setup_PWM();
   setup_INT();
   
   output_high (PIN_B0);
   output_high (PIN_B1);
   port_b_pullups (TRUE);
   
   MODE=TX;
   
   can_init();
   init_vars();
   
   while (TRUE)
   { 
     write_CAN();
     output_toggle (PIN_B1);
   }
}


Thanks in advance,

Joan
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 17, 2009 12:28 pm     Reply with quote

Your program is not a simple test program. It has tons of stuff in it
that is not needed to test the CAN bus function. You should re-write it
and cut out about 90% of your existing code.

Test the new program, verify that it still fails, and post it.
Post your compiler version.
Also, tell you are testing for the failure. How do you know it's failing ?

Quote:
#include <18F4580.h>
#include <can-18xxx8.c>

CCS has a CAN bus driver for the 18F4580:
Quote:
c:\program files\picc\drivers\can-18f4580.c



One more thing. The block diagram of the PCA82C251 CAN bus
transceiver chip does not show an internal pull-up resistor on the
TXD input. If not, you may have the problem discussed in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=23825
The solution is to add this #define statement above the #include for
the CAN bus driver:
Code:
#define CAN_ENABLE_DRIVE_HIGH  1
#include <can-18F4580.c>
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