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 Problem? How to work 18F2431 +mcp2515

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



Joined: 23 Oct 2006
Posts: 12

View user's profile Send private message

CAN Problem? How to work 18F2431 +mcp2515
PostPosted: Tue Jun 19, 2007 10:36 am     Reply with quote

Hello! I have a work about CAN communication.
The following is my code. If it were used for 16F877A, it are good!
But it arenot work for 18F2431
I want to know how to correct it.

Thanks ,say sorry to my poor english.

osc=20M mcp2515 20M CAN bitrate =500K

2431 -----> 2515

PIN_C6(17) ----->> CS(16)
PIN_C7(18) ----->> SI(14)
PIN_C4(15) ----->> SO(15)
PIN_C5(16)----->> SCK(13)

PIN_C3(14) ----->> INT(12)

transmit code
Code:


#include <18F2431.h>

#fuses HS,NOPROTECT,NOLVP,NOWDT

#use delay(clock=20000000)

#include "MCP2515.c"

//////////////////////////////////////////////////////////////////////////
//                               main code                              //
//////////////////////////////////////////////////////////////////////////
void main()
{
 unsigned char buffer[2]={0x19,0x82};
 output_low(PIN_C1);               
 setup_adc_ports(NO_ANALOGS);       
 setup_adc(ADC_OFF);                 

 setup_spi(FALSE);               
 

 can_init();

 while(TRUE)
      {
      if(can_tbe())
         {
          can_putd(0x201,&buffer[0],2,3,0,0);
         // receive ID 201 is good.
          delay_ms(900);
          output_high(PIN_C1); //led on
          delay_ms(100);
          output_low(PIN_C1);  //led off
         }
      }
}


"MCP2515.c"
Code:


#ifndef EXT_CAN_CS
//18F2341
   #define EXT_CAN_CS   PIN_C6
   #define EXT_CAN_SI   PIN_C7
   #define EXT_CAN_SO   PIN_C4
   #define EXT_CAN_SCK  PIN_C5
      ... ... ... ... ... ... ... ... ... ... ... ...
void can_set_baud(void) {
   struct struct_CNF1 new_CNF1;
   struct struct_CNF2 new_CNF2;
   struct struct_CNF3 new_CNF3;
//use MBtime setup CAN Bitrate  osc=20M bitrate=500k 
   new_CNF1=0x00; 
   new_CNF2=0xBA;
   new_CNF3=0x07;
    ... ... ... ... ... ... ... ... ... ... ... ...
   //other codes are the  same as can-mcp251x.c

MBTIME Download
http://www.intrepidcs.com/index.php?name=CmodsDownload&file=index&req=viewdownload&cid=2&orderby=dateD
longmenok



Joined: 23 Oct 2006
Posts: 12

View user's profile Send private message

PostPosted: Mon Jun 25, 2007 9:30 am     Reply with quote

help
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 25, 2007 11:26 am     Reply with quote

Quote:

The following is my code. If it were used for 16F877A, it are good!
But it are not work for 18F2431.

Does the only difference between the two programs consist
of the #include file line ? Other than that, are the two programs
completely identical ?
Code:

#include <18F2431.h>
or
#include <16F877A.h>


Are the hardware connections between the PIC and the MCP2515
the same, for the 18F2431 and the 16F877A ?

Is the MCP2515 circuit the same, for each PIC ?
Does the MCP2515 have a 20 MHz crystal in each case ?


In other words, you said it works for 16F877A, but I want to get
a confirmation from you that the only thing you changed for the
new project is the PIC, and the #include statement.


Also, post the CCS compiler version that you used with each PIC.
longmenok



Joined: 23 Oct 2006
Posts: 12

View user's profile Send private message

PostPosted: Sun Jul 08, 2007 9:53 pm     Reply with quote

MCP2515 is used. can-mcp2510.c is modified as following.

MCP2515 have a 20 MHz crystal in each case

can-mcp2510.c connect with 16F877A is following
Code:

//877A IO pins connected to MCP2515
#ifndef EXT_CAN_CS
   #define EXT_CAN_CS   PIN_A5
   #define EXT_CAN_SI   PIN_C5
   #define EXT_CAN_SO   PIN_C4
   #define EXT_CAN_SCK  PIN_C3
  // mcp2515's PIN INT connect with 877A's RB0

set_baud

Code:

void can_set_baud(void) {
 
   struct struct_CNF1 new_CNF1;
   struct struct_CNF2 new_CNF2;
   struct struct_CNF3 new_CNF3;

   new_CNF1=0x00;
   new_CNF2=0xBA;
   new_CNF3=0x07;

   MCP2515_write(CNF1, (int)new_CNF1);
   MCP2515_write(CNF2, (int)new_CNF2);
   MCP2515_write(CNF3, (int)new_CNF3);
}


can-mcp2510.c connect with 18F2431 is following
Code:

//18F2431 IO pins connected to MCP2515
#ifndef EXT_CAN_CS
//2341
   #define EXT_CAN_CS   PIN_C6   //SS
   #define EXT_CAN_SI   PIN_C7   //SDO
   #define EXT_CAN_SO   PIN_C4   //SDI//SDA
   #define EXT_CAN_SCK  PIN_C5   //sck/scl
    //mcp2515' PIN INT connect with 2431's RC3

the above is the difference for their connect

18F2431'baud is the same as 16F877A

the following is code for 16F877A and 18F2431 to transmat
Code:


#include <16F877A.h>

#fuses HS,NOPROTECT,NOLVP,NOWDT

#use delay(clock=20000000)

#include "can-mcp2510.c"

//////////////////////////////////////////////////////////////////////////
//                                main code                               //
//////////////////////////////////////////////////////////////////////////
void main()
{
 unsigned char buffer[2]={0x19,0x82};
 output_low(PIN_C1);                 
 setup_adc_ports(NO_ANALOGS);       
 setup_adc(ADC_OFF);                 
 setup_psp(PSP_DISABLED);         
 setup_spi(FALSE);                   
 
 
 can_init();

 while(TRUE)
      {
      if(can_tbe())
         {
          can_putd(0x722,&buffer[0],2,3,0,0);
          delay_ms(900);
          output_high(PIN_C1);
          delay_ms(100);
          output_low(PIN_C1);
         }
      }
}



Code:

#include <18F2431.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT
#use delay(clock=20000000)
#include "can-mcp2510.c"


//////////////////////////////////////////////////////////////////////////
//                                main code                                //
//////////////////////////////////////////////////////////////////////////
void main()
{

 unsigned char buffer[2]={0x19,0x82};
 setup_adc_ports(NO_ANALOGS);         
 setup_adc(ADC_OFF);                 
 setup_spi(FALSE);                   
 

 can_init();

 while(TRUE)
      {
         if(can_tbe())
         {
          can_putd(0x722,&buffer[0],2,3,0,0);

         }
      }
}


the following is code for 18F458 to receive //correct it
Code:


#include <18F458.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "can-18xxx8.c"

// set baud in Ccan-18xxx8.c
//  BRGCON1 = 0x00;
//  BRGCON2 = 0xBA;
//  BRGCON3 = 0x07;

///////////////////////////////////////////////////////////////////////
///               main code                                //////
///////////////////////////////////////////////////////////////////////
void main(void)
{
 struct rx_stat rxstat;
 int32 rx_id;                 
 unsigned char buffer[2];   
 unsigned char rx_len;       
 output_low(PIN_D1);       
 setup_adc_ports(NO_ANALOGS);
 setup_adc(ADC_OFF);         
 setup_psp(PSP_DISABLED);   
 setup_spi(FALSE);           
 
 
 can_init();
 
 while(TRUE)
      {
      if(can_kbhit())
        {
        if(can_getd(rx_id,&buffer[0],rx_len, rxstat))
          {
          if((rx_id==0x722)&&(buffer[0]==0x19)&&(buffer[1]==0x82))
            {
            output_high(PIN_D1);
            delay_ms(100);
            output_low(PIN_D1);
            }
         }
       }
      }
}



Thanks! PCM programmer


Last edited by longmenok on Thu Jul 12, 2007 1:14 am; edited 3 times in total
longmenok



Joined: 23 Oct 2006
Posts: 12

View user's profile Send private message

PostPosted: Tue Jul 10, 2007 7:38 pm     Reply with quote

Can you help me! Thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 11, 2007 11:57 am     Reply with quote

Quote:

The following is code for 16F877A and 18F2431 to receive:

#include <18F458.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "can-18xxx8.c"

// set baud in Ccan-18xxx8.c
// BRGCON1 = 0x00;
// BRGCON2 = 0xBA;
// BRGCON3 = 0x07;

The can-18xxx8.c file only works with 18F chips that have a built-in
CAN module. The 16F877A and 18F2431 do not have one.
This code will not work with those two PICs.
longmenok



Joined: 23 Oct 2006
Posts: 12

View user's profile Send private message

PostPosted: Thu Jul 12, 2007 1:10 am     Reply with quote

I use 18F2431+mcp2515 to transmit motor QEI data, and use 18F458 to receive data. when I use 16F877A+mcp2515, CAN Communication can good work. However I use 18F2431+mcp2515 , 18F458 can't receive any data.

IN transmit code use "can-mcp2510.c" for 18f2431
and in receive code use "can-18xxx8.c" for 18f458



their bauds are same,
Code:

Transmit by 18F2431
void can_set_baud(void) {
 
   struct struct_CNF1 new_CNF1;
   struct struct_CNF2 new_CNF2;
   struct struct_CNF3 new_CNF3;

   new_CNF1=0x00;      //Set baud
   new_CNF2=0xBA;
   new_CNF3=0x07;

   MCP2515_write(CNF1, (int)new_CNF1);
   MCP2515_write(CNF2, (int)new_CNF2);
   MCP2515_write(CNF3, (int)new_CNF3);
}


Receive by 18F458
//  BRGCON1 = 0x00;  set baud
//  BRGCON2 = 0xBA;
//  BRGCON3 = 0x07;



why 18F2431 can't communiate?
Thanks! PCM programmer.
longmenok



Joined: 23 Oct 2006
Posts: 12

View user's profile Send private message

PostPosted: Thu Jul 12, 2007 2:01 am     Reply with quote

I test it, can_init() can't good work.

In can_mcp2510.c

Code:
void can_set_mode(CAN_OP_MODE mode) {
   struct struct_CANCTRL old_CANCTRL;

   old_CANCTRL=MCP2515_read(CANCTRL);

   old_CANCTRL.reqop=mode;

   MCP2515_write(CANCTRL, (int)old_CANCTRL);

   do {
      old_CANCTRL=MCP2515_read(CANCTRL);  // In there,  it is always LOOP
      } while (old_CANCTRL.reqop != mode);
   
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jul 12, 2007 10:23 am     Reply with quote

Quote:
I test it, can_init() can't good work.

See this post. It has links to trouble-shoot problems with can_init():
http://www.ccsinfo.com/forum/viewtopic.php?t=30107&start=5
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