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 18F2480 & MCP2551 Oscillator Problem
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Mael



Joined: 12 Aug 2009
Posts: 15

View user's profile Send private message

CAN 18F2480 & MCP2551 Oscillator Problem
PostPosted: Wed Aug 12, 2009 1:07 am     Reply with quote

Hi everybody.

I've made a board with a PIC18F2480 with a MCP2551 to connect to a canbus network. I've used a 12 Mhz crystal and configured the can bus timing, here you can see the program:
Code:
#include "main.h"
#define CAN_USE_EXTENDED_ID FALSE
#include <can-18xxx8.c>

void main()
{
int TX_BUFFER[8];
int32 TX_ID=0x511;
int1 SENDED=FALSE;

TX_BUFFER[0]=0x00;
TX_BUFFER[1]=0x01;
TX_BUFFER[2]=0x02;
TX_BUFFER[3]=0x2B;

   can_init();
   can_set_mode(CAN_OP_CONFIG);
   
   BRGCON1.brp=4;
   BRGCON1.sjw=0;
   BRGCON2.prseg=1;
   BRGCON2.seg1ph=2;
   BRGCON2.sam=FALSE;
   BRGCON2.seg2phts=FALSE; 
   BRGCON3.seg2ph=5;
   BRGCON3.wakfil=FALSE;

   can_set_mode(CAN_OP_NORMAL);
 
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_spi(SPI_SS_DISABLED);
   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_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab

   // TODO: USER CODE!!
LOOP:
   SENDED=can_putd(TX_ID, &TX_BUFFER[1],1,1,FALSE,FALSE);
   delay_ms(1000);
   goto LOOP;
}

Like this everything works ok, I can read and write messages without problem, but after that I tried to change the crystal to 8 Mhz, and I did this modifications in the program:

Code:

   BRGCON1.brp=4;
   BRGCON1.sjw=0;
   BRGCON2.prseg=1;
   BRGCON2.seg1ph=1;
   BRGCON2.sam=FALSE;
   BRGCON2.seg2phts=FALSE; 
   BRGCON3.seg2ph=2;
   BRGCON3.wakfil=FALSE; 

Now, I can not connecting with the can network, and the pic can't start the communication. Can somebody say to me what I'm doing wrong? Thank you.
Regards.


Last edited by Mael on Wed Aug 12, 2009 4:05 am; edited 2 times in total
Ttelmah
Guest







PostPosted: Wed Aug 12, 2009 3:54 am     Reply with quote

Er.
Look at what you have posted. Is this really what you have used?. The two sets of settings, are identical - no 'modification'....

Best Wishes
Mael



Joined: 12 Aug 2009
Posts: 15

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 4:05 am     Reply with quote

Sorry, now is corrected in the first post. Confused
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 10:31 am     Reply with quote

Quote:
I've used a 12 Mhz crystal and configured the can bus timing,
but after that I tried to change the crystal to 8 Mhz,
Now, I can not connect with the can network.

See this thread. It explains how to setup the CAN bus timing parameters
for a specific crystal frequency.
http://www.ccsinfo.com/forum/viewtopic.php?t=22138&start=7
Mael



Joined: 12 Aug 2009
Posts: 15

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 10:43 am     Reply with quote

Thank you for the help, now my pic can send messages to the canbus. I have one last question. I have another node for the bus with a PIC18F2550, MCP2510 and MCP2551. The 18F2550 crystal is 16 Mhz and the MCP2510 crystal is 16Mhz also. I set the bit timing using the Microchip can timing tool, but I can not connect to the can bus with this new node. I think it's because the CCS library mcp2510.c is designed to work with a pic clock speed of 25 Mhz. Is this true? In this case, which is the crystal speed for the MCP2510? Thank you very much.
Regards.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 10:56 am     Reply with quote

Read the Bit Timing calculator page:
http://intrepidcs.com/support/mbtime.htm

It says:
Quote:
MB Time:
Size: 1.94 MB

Microchip Controller Area Network (CAN) Bit Timing Calculator

Compatible with MCP2510, MCP2515, and PIC18F with both legacy and ECAN can controllers

So, set the crystal speed to 16 MHz and use the MBTime calculator
to get the correct settings.
Mael



Joined: 12 Aug 2009
Posts: 15

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 11:18 am     Reply with quote

I used the calculator and still don't work, then is correct to use a crystal of 16Mhz. For pic and another one of 16Mhz. Also for the MCP2510?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 11:35 am     Reply with quote

The CAN bus baud rate must be the same for both the CAN bus boards.

Does your MCP2510 board use the CLKOUT signal to drive some other
circuit on the board ? If so, that could cause a problem if you change
the crystal frequency of the MCP2510.
Mael



Joined: 12 Aug 2009
Posts: 15

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 11:58 am     Reply with quote

Sorry, I didn't explain it very well.
In the second board I have a PIC 18F2550, this pic has it's own crystal of 16 Mhz. The MCP have another crystal of the same speed and clkout pin is not connected.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 12:26 pm     Reply with quote

Post your modified can_set_baud() routine for the MCP2510, from the
can-mcp2510.c file. You need to modify it if the MCP2510 uses a
16 MHz crystal. Post the modifications.
Mael



Joined: 12 Aug 2009
Posts: 15

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 12:37 pm     Reply with quote

Here you have the modified can_set_baud. This values are extracted from the bit calculator with 16 Time Quanta.
Code:
void can_set_baud(void) {
   struct struct_CNF1 new_CNF1;
   struct struct_CNF2 new_CNF2;
   struct struct_CNF3 new_CNF3;


/*   new_CNF1.brp=CAN_BRG_PRESCALAR;
   new_CNF1.sjw=CAN_BRG_SYNCH_JUMP_WIDTH;

   new_CNF2.prseg=CAN_BRG_PROPAGATION_TIME;
   new_CNF2.phseg1=CAN_BRG_PHASE_SEGMENT_1;
   new_CNF2.sam=CAN_BRG_SAM;
   new_CNF2.btlmode=CAN_BRG_SEG_2_PHASE_TS;

   new_CNF3.phseg2=CAN_BRG_PHASE_SEGMENT_2;
   new_CNF3.wakfil=CAN_BRG_WAKE_FILTER;
*/
   new_CNF1=0x04;
   new_CNF2=0xB8;
   new_CNF3=0x05;

   mcp2510_write(CNF1, (int)new_CNF1);
   mcp2510_write(CNF2, (int)new_CNF2);
   mcp2510_write(CNF3, (int)new_CNF3);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 12:41 pm     Reply with quote

Post your CAN bus baud rate, so I can check the numbers.
Mael



Joined: 12 Aug 2009
Posts: 15

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 1:31 pm     Reply with quote

The can Baud rate is 100Kbps. I go to put the connection on MCP2510 to make sure that is correct.

1 (Tx) - To MCP2551 Pin 4(Rx)
2 (Rx) - To MCP2551 Pin 1(Tx)
7 (OSC2) - To pin 1 of 16 Mhz crystal with a 30pF capacitor to ground
8 (OSC2) - To pin 2 of 16 Mhz crystal with a 30pF capacitor to ground
9(Vss) - Ground
13 (SCK) - To pic pin C3 (SCK pin in mcp2510.c library)
14 (SI) - To pic pin C1 (SI pin in mcp2510.c library)
15 (SO) - To pic pin PIN C0 (SO pin in mcp2510.c library)
16 (CS) - To pic pin B1 (CS pin mcp2510.c library)
17 (Reset) - to +5V via 10K resistor.
18 (Vdd) - +5V

Thank you and regards.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 12, 2009 1:56 pm     Reply with quote

Quote:
1 (Tx) - To MCP2551 Pin 4(Rx)
2 (Rx) - To MCP2551 Pin 1(Tx)

This is not correct. Pin 1 of the MCP2510 is the TXCAN pin.
The data sheet describes it as:
Quote:
TXCAN - Transmit output pin to CAN bus


The MCP2551 data sheet says pin 1 on it is:
Quote:
Pin 1 - TXD Transmit Data Input

This means that pin 1 on the MCP2510 should connect to pin 1 on the
MCP2551.

The same thing is true for the RX pins.
Pin 2, RXCAN, is the input on the MCP2510. Pin 4, RXD is the output on
the MCP2551. So pin 2 on the MCP2510 must connect to pin 4 on the
MCP2551.

In other words, you have the connections reversed. You need to fix it.
Mael



Joined: 12 Aug 2009
Posts: 15

View user's profile Send private message

PostPosted: Thu Aug 13, 2009 2:08 am     Reply with quote

I've made the change today but the can is still not working. I think that the is problem is that the MCP2510 library was written to work with the CCS Can Development PCB, which uses a different clock speed than my pcb. Can be this? In that case I must change the clock speed for the pic and MCP
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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