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 Example Change Frequency
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
ECUWizz
Guest







CAN Example Change Frequency
PostPosted: Wed Mar 09, 2005 2:38 am     Reply with quote

How do I set up the CCS CAN Examle to use a 12MHz crystal and not the 20Mhz as it is set up in the example. I know there is some kind of calculations to be made, but I do not kno what it should be. Any help?
JPA
Guest







CAN baudrate
PostPosted: Wed Mar 09, 2005 2:55 am     Reply with quote

Microchip Datasheet explain the way the CAN bus works. The baud rates is directly related to the crystal frequency and others parameters detailed in BRGCON 1, 2 & 3. Note thats those parameters must be set according to the rules defined in the datasheet and the propagation time on the bus.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 09, 2005 9:33 am     Reply with quote

Bit timing calculators are available on the net. Use the board's
search engine to find previous articles. Here are two of my old
posts that contain links to these CAN bus bit timing calculators:

http://www.ccsinfo.com/forum/viewtopic.php?t=21553&highlight=calculator

http://www.ccsinfo.com/forum/viewtopic.php?t=19916&highlight=calculator
ECUWizz
Guest







CAN bit timing calculator
PostPosted: Thu Mar 10, 2005 3:37 am     Reply with quote

I used the kvaser calculator and got a list of possible values. BUT what do they mean? In CCS example I have: SJW, prescalar, Phase segment 1 and 2, sample of can bus line, propagation time. The list from the calculator has other stuff. Where comes what?

Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 10, 2005 1:37 pm     Reply with quote

I used the values to modify some of the constants at the beginning
of the CCS can-18xxx8.h file. It's been a while. I don't remember
my exact criteria for choosing particular values from the many sets
that are displayed in the bit timing calculator results table.

In the code below, I show the two lines that I changed in the .h file.
I think, the first thing I did was to take the default parameters that are
used with the CCS files (ie., crystal frequency and CAN bus speed) and
and crank them through the bit timing calculators. I wanted to see
if the calculators would produce the same timing values that CCS plugs
into the CAN registers in the 18F458 or MCP2510. If I could get it
to do that, then I had "calibrated" and also understood the bit timing
tools. Then I would have confidence that when I put in a new crystal
frequency and CAN bus speed into the calculator, I could properly pick
the correct values from the many listed in the table.

I assume I made some notes on this, but I can't find them immediately.
It's been a while.

My suggestion is that you should build or buy two CAN bus boards,
and get those communicating together at the default crystal freq and
bus speed for the CCS drivers. This will allow you to prove that
your hardware is working, without having to do any software changes.
It's going to be enough of a problem just to get those two boards working
without even thinking about hooking it up to an automobile CAN bus.

I think these changes were for a 18F458 board with a 20 MHz
crystal and probably 125 or 250 kbs bus speed.
Code:
#ifndef CAN_BRG_PHASE_SEGMENT_1
// #define CAN_BRG_PHASE_SEGMENT_1  5 //phase segment 1 (def: 6 x Tq)
 #define CAN_BRG_PHASE_SEGMENT_1   7   // 8-27-03  Modified
#endif

#ifndef CAN_BRG_PROPAGATION_TIME
// #define CAN_BRG_PROPAGATION_TIME 2 //propagation time select (def: 3 x Tq)
#define CAN_BRG_PROPAGATION_TIME    0  // 8-27-03  Modified
#endif
Guest








RE CAN baudrate
PostPosted: Fri Mar 11, 2005 12:43 am     Reply with quote

I got my CCS demo board and my own device running on 20MHz using the demo code, and can connect to each other. Is there any way you can help me sort out the values for a 12MHz board. I have tried almost all the values and calculations that I can get from the calculators.
With this I still need to to resolve to problems, 1-Target,Sender IDs and the baudrate itself. Is it possible for 2 different baudrate boards to connect to each other?

Thanks for all your help so far, I really appreciate it. Let me know if you need to know something, I might just be able to help you.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Mar 11, 2005 1:15 pm     Reply with quote

Re: the 12 MHz crystal.

1. What CAN controller chip are you using ?
ie, 18F458, or MCP2510, or MCP2515, etc.

2. What baud rate to you want to use ?

If you give me that information, I can work on it.
It might be a few days. If someone else answers
before then, that's OK.
anton



Joined: 10 Mar 2005
Posts: 4

View user's profile Send private message

RE CAN frequency
PostPosted: Mon Mar 14, 2005 5:40 am     Reply with quote

I am using the following:
PIC 18F458 and MCP2551 as a tranceiver.
Using a 12 MHz crystal.
I need to run on both 250kbps and 500kbps

Any help would be great.
Thanks
_________________
Anton
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 15, 2005 1:47 pm     Reply with quote

I looked at the problem and I believe I have a solution for you.

I used the CAN bus bit timing calculator call MBTime, from
http://intrepidcs.com/modules/CmodsDownload/upload/Software/MBTime.zip

That one is the most useful because it provides an interactive bit timing
diagram. You can change the values while watching the box at the
bottom of the window, in which they tell you if you're violating
any specs.

Say you want to get values for a 12 MHz crystal at 250 kbs.

1. Start up the MBTime program and enter those values in the two little
boxes at the upper right.

2. In the top center of the window, where it says "Controls", click
the "right arrow" button. A new window appears.

3. On right hand side, it lists three possible solutions, with 8, 12, or 24
quanta. All you really care is that there is at least one solution.
So click the "right arrow" button again to advance to the next screen.

4. Now you're at the interactive bit timing window. In the drop-down
box, you can select 8, 12, or 24 quanta. The higher you go in
quanta, the more flexibility you have in selecting the length of
the various delays. You have finer granularity. One downside
of picking a larger number of quanta is that the bitfields in the
CAN baudrate registers (in the 18F458) have a limited size.
So, if you choose 24 quanta, you can't increase some of the delays
beyond a certain point. You don't really have the flexibility that
you thought you had.

So for that reason, let's choose 12 quanta from the drop-down box.

We're ready to look at the timing. The "Sync" quanta is always 1.

So next, we look at the Propagation delay time. If you study AN754
http://ww1.microchip.com/downloads/en/AppNotes/00754.pdf
from Microchip, it gives some formulas for calculating the best prop
delay time, based on the length of your cable and the prop delays
through your CAN bus driver and receiver chips. But for a short
cable and normal drivers, I think 500 ns should work fine. But,
the granularity of our quanta is 333 ns. So to get 500 ns, we have
to pick 2 quanta to get at least that much, so this gives us 666 ns
for the Prop time. Do this by incrementing the Prop value in
the spinner box so it's set at 2.

The next issue is the Sample Point. On the Kvaser site, on their
bit timing calculator page, they give a few tips on picking the proper
values. They suggest a Sample Point of about 75%. (Note: I
didn't use the Kvaser calculator because I don't think the values
that it produces can be directly dropped into C source code for
programming the 18F458 BRGCONx registers. The values would
require some adjustment, so it's not as convenient to use.)

So, back to the MBTime program. You can use the "spin" boxes
for Phase Segment 1 and Phase Segment 2, to adjust the length
of those periods, to give a 75% sample point. Make sure that
the total number of quanta remains at 12. Watch the little box
at the bottom of the window. If it says "These settings meet
all requirements", then you're doing it correctly.

Leave SJW set = 1. The reason is, according to Microchip's
AN754, SJW only becomes important if the oscillators on the
different CAN boards have a fairly large difference in frequency.
(Such as 1%). If you're using crystals, that's not going to be a
problem.

So to summarize the settings, we have:
Quote:

Sync = 1 quanta
Prop = 2 quanta
Phase 1 = 6 quanta
Phase 2 = 3 quanta
TOTAL = 12 quanta
Sample point = 75%

Now that you've got a satisfactory timing, click the "Generate Report"
button. This will generate a HTML report with a timing diagram, that's
nicely formatted and you can print it out.

Down at the bottom of the report, it lists the settings for the three
CAN baud rate registers in the 18F458.
For our example of 12 MHz, and 250 kbs, we get this:
Quote:

CNF1/BRGCON1 b'00000001' 0x01
CNF2/BRGCON2 b'10101001' 0xA9
CNF3/BRGCON3 b'00000010' 0x02

The easiest way to use these values with the CCS CAN driver files, is
to edit the can_set_baud() function in the can-18xxx8.c file.
CCS uses bitfields in their driver. But the bit timing generator doesn't
directly generate these bitfield values. The report does tell you the
lengths of Prop, Phase Segment 1 and 2, and SJW, but you have to
subtract 1 from these values before you assign them to the BRGCONx
bitfields, because that's how the bitfields are defined in the 18F458 data
sheet. So I thought an easier way would be to comment out all the
CCS bitfield code, and just assign the 8-bit values directly to the
BRGCONx registers. See below.
Code:
void can_set_baud(void)
{       
BRGCON1 = 0x01;
BRGCON2 = 0xA9;
BRGCON3 = 0x02;

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

   BRGCON2.prseg=CAN_BRG_PROPAGATION_TIME;
   BRGCON2.seg1ph=CAN_BRG_PHASE_SEGMENT_1;
   BRGCON2.sam=CAN_BRG_SAM;
   BRGCON2.seg2phts=CAN_BRG_SEG_2_PHASE_TS;

   BRGCON3.seg2ph=CAN_BRG_PHASE_SEGMENT_2;
   BRGCON3.wakfil=CAN_BRG_WAKE_FILTER;
*/
}

One risk is that CCS might be using the BRGCONx register constants
somewhere else in the program. To assure yourself that this doesn't
happen, edit the can-18xxx8.h file, and comment out all the constants
associated with the BRGCONx registers (and only those). Example:
Code:

/*

#IFNDEF CAN_BRG_SYNCH_JUMP_WIDTH
  #define CAN_BRG_SYNCH_JUMP_WIDTH  0  //synchronized jump width (def: 1 x Tq)
#ENDIF

#IFNDEF CAN_BRG_PRESCALAR
  #define CAN_BRG_PRESCALAR  4  //baud rate generator prescalar (def: 4) ( Tq = (2 x (PRE + 1))/Fosc )
#ENDIF

#ifndef CAN_BRG_SEG_2_PHASE_TS
 #define CAN_BRG_SEG_2_PHASE_TS   TRUE //phase segment 2 time select bit (def: freely programmable)
#endif

#ifndef CAN_BRG_SAM
 #define CAN_BRG_SAM 0 //sample of the can bus line (def: bus line is sampled 1 times prior to sample point)
#endif

#ifndef CAN_BRG_PHASE_SEGMENT_1
 #define CAN_BRG_PHASE_SEGMENT_1  5 //phase segment 1 (def: 6 x Tq)
#endif

#ifndef CAN_BRG_PROPAGATION_TIME
 #define CAN_BRG_PROPAGATION_TIME 2 //propagation time select (def: 3 x Tq)
#endif

#ifndef CAN_BRG_WAKE_FILTER
 #define CAN_BRG_WAKE_FILTER FALSE   //selects can bus line filter for wake up bit
#endif

#ifndef CAN_BRG_PHASE_SEGMENT_2
 #define CAN_BRG_PHASE_SEGMENT_2 5 //phase segment 2 time select (def: 6 x Tq)
#endif
*/

There's still the possibility that CCS might be writing to individual bitfields
in their driver. So using a text search tool like Examine32, I did a search
for BRGCON in all the CAN source files. The only place they ever talk
to those registers is in the can_set_baud() function, and we've got that
covered. So there should be no problem with writing directly to the
registers, instead of using the CCS bitfields. And, it will save us some
time when making changes and be less error prone. There's also
the issue of things like the Wake Filter, SAM, and SEGPHTS fields,
but the MBTime program sets them to the same value as CCS does.
------------
Now let's look at your 2nd requested baud rate of 500 kbs with a 12 MHz
crystal.

I think you have to re-start the MBTime program when you change
the main parameters. I don't see anyway to reset it. So just close
it down and re-start it. Type in 12 MHz and 500 in the upper right boxes.

Click the "Control" right-arrow button once, and you see there's only one
solution offered, with 12 quanta. Click it again to get to the timing screen.

Now, we want to increase the Prop setting to at least 500ns, so increase
it to 3 quanta.

Next, we want the Sample Point to be 75%, so set Phase Segment 1 = 5,
and set Phase Segment 2 = 3. Remember to watch the comments box
at the bottom of the window. It will tell you if you've screwed up.
You can also watch the blue lines and the arrows on the bit timing
diagram. You can see when you've pushed it off the screen to the right,
or if you've reduced it too much and have got a margin on the right.
The sum of all the delays has to be kept at 12 quanta, because that was
the solution that the program calculated. So make sure the little box
says "These settings meet all timing requirements".

Then click on Generate Report. It will display a warning for the
MCP2510. That's irrelevant for the 18F458, or at least I hope it is.
Neither the B4 nor the C0 errata mention that bug. So it's probably
only in the MCP2510. If not, you'll have to increase your crystal
frequency to 16 MHz, according to a chart in the MCP2510 errata.

Anyway, for 12 MHz crystal and 500 kbs, you get these settings
for the baud rate registers:
Quote:

CNF1/BRGCON1 b'00000000' 0x00
CNF2/BRGCON2 b'10100010' 0xA2
CNF3/BRGCON3 b'00000010' 0x02


Just plug those directly into the BRGCONx registers as shown above.
Of course, you can also make them into constants. Then it would
be more properly done:

For 500 KBS:
Code:
#define BRGCON1_12MHZ_500KBS  0x00
#define BRGCON2_12MHZ_500KBS  0xA2
#define BRGCON3_12MHZ_500KBS  0x02


for 250 KBS:
Code:
#define BRGCON1_12MHZ_250KBS  0x01
#define BRGCON2_12MHZ_250KBS  0xA9
#define BRGCON3_12MHZ_250KBS  0x02




1st Edit -- Fixed a few typos.
More Edits -- Fixed the name of the AN754 appnote and added a link to it.
Edit -- Updated the link for the Bit Timing calculator program.
Edit -- Had to update the link again. They keep changing it.


Last edited by PCM programmer on Wed Sep 24, 2008 11:33 am; edited 4 times in total
anton



Joined: 10 Mar 2005
Posts: 4

View user's profile Send private message

PIC 18F458 CAN baudrate settings
PostPosted: Wed Mar 16, 2005 3:43 am     Reply with quote

Hi I tried the calculator, and did everything step by step as you described, everything worked and I got th same calculations as you did, but as soon as I change the crystal to 12Mhz and enter the new values the CAN doesn't work. If Iook at the CAN bus on a scope it creates a loop wave that continues until I reset the board.

I used CCS CAN development board. On a 20Mhz crystal I used these settings(the default ones used for the example)

Code:
#define CAN_USE_EXTENDED_ID         TRUE//TRUE
#define CAN_BRG_SYNCH_JUMP_WIDTH    0  //synchronized jump width (def: 1 x Tq)
#define CAN_BRG_PRESCALAR           4 //baud rate generator prescalar (def: 4) ( Tq = (2 x (PRE + 1))/Fosc )
#define CAN_BRG_SEG_2_PHASE_TS      TRUE//TRUE //phase segment 2 time select bit (def: freely programmable)
#define CAN_BRG_SAM                 0 //sample of the can bus line (def: bus line is sampled 1 times prior to sample point)
#define CAN_BRG_PHASE_SEGMENT_1     5 //phase segment 1 (def: 6 x Tq)
#define CAN_BRG_PROPAGATION_TIME    2 //propagation time select (def: 3 x Tq)
#define CAN_BRG_WAKE_FILTER         FALSE//FALSE   //selects can bus line filter for wake up bit
#define CAN_BRG_PHASE_SEGMENT_2     5 //phase segment 2 time select (def: 6 x Tq)
#define CAN_USE_RX_DOUBLE_BUFFER    TRUE//TRUE   //if buffer 0 overflows, do NOT use buffer 1 to put buffer 0 data
#define CAN_ENABLE_DRIVE_HIGH       0
#define CAN_ENABLE_CAN_CAPTURE      0
#byte BRGCON1_12MHz_250KBs =0xF70
#byte BRGCON2_12MHz_250KBs =0xF71
#byte BRGCON3_12MHz_250KBs =0xF72


Then I changed the register values, as you can see there is a "F" in the value. When I remove that(so that it looks more like a normal byte an that it fits the calculator standard) CAN stops working. As soon as I send a message over the bus it creates the loop wave.

Now: I tried the same using a 12Mhz crystal on the CCS CAN development board, and the settings you described above. I left the "F" out, and I put it back, but CAN still doesn't work. I dont know if I am just doing something wron or what. By the method you gave me above I got these values. (this as for a 250Kbps baudrate)

Code:
#define CAN_USE_EXTENDED_ID         TRUE//TRUE
#define CAN_BRG_SYNCH_JUMP_WIDTH   1  //synchronized jump width (def: 1 x Tq)
#define CAN_BRG_PRESCALAR           1 //baud rate generator prescalar (def: 4) ( Tq = (2 x (PRE + 1))/Fosc )
#define CAN_BRG_SEG_2_PHASE_TS      TRUE//TRUE //phase segment 2 time select bit (def: freely programmable)
#define CAN_BRG_SAM                 0 //sample of the can bus line (def: bus line is sampled 1 times prior to sample point)
#define CAN_BRG_PHASE_SEGMENT_1     6 //phase segment 1 (def: 6 x Tq)
#define CAN_BRG_PROPAGATION_TIME    2 //propagation time select (def: 3 x Tq)
#define CAN_BRG_WAKE_FILTER         FALSE//FALSE   //selects can bus line filter for wake up bit
#define CAN_BRG_PHASE_SEGMENT_2     3//phase segment 2 time select (def: 6 x Tq)
#define CAN_USE_RX_DOUBLE_BUFFER    TRUE//TRUE   //if buffer 0 overflows, do NOT use buffer 1 to put buffer 0 data
#define CAN_ENABLE_DRIVE_HIGH       0
#define CAN_ENABLE_CAN_CAPTURE      0
#byte BRGCON1_12MHz_250KBs =0x01
#byte BRGCON2_12MHz_250KBs =0xA9
#byte BRGCON3_12MHz_250KBs =0x02


Is this values correct? I cant see where I am going wrong. What values did you get?

Thanks for all your help
_________________
Anton
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 11:55 am     Reply with quote

Quote:
#byte BRGCON1_12MHz_250KBs =0x01
#byte BRGCON2_12MHz_250KBs =0xA9
#byte BRGCON3_12MHz_250KBs =0x02

That's a mistake. The #byte statement is used to define the address
of a hardware PIC register or sometimes a RAM memory address.
Remove those lines from your program.

You should use the following code:
Code:

void can_set_baud(void)
{       
BRGCON1 = 0x01;
BRGCON2 = 0xA9;
BRGCON3 = 0x02;

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

   BRGCON2.prseg=CAN_BRG_PROPAGATION_TIME;
   BRGCON2.seg1ph=CAN_BRG_PHASE_SEGMENT_1;
   BRGCON2.sam=CAN_BRG_SAM;
   BRGCON2.seg2phts=CAN_BRG_SEG_2_PHASE_TS;

   BRGCON3.seg2ph=CAN_BRG_PHASE_SEGMENT_2;
   BRGCON3.wakfil=CAN_BRG_WAKE_FILTER;
*/
}
anton



Joined: 10 Mar 2005
Posts: 4

View user's profile Send private message

RE CAN speed
PostPosted: Tue Mar 22, 2005 12:49 am     Reply with quote

Hi
Is there any way that you can post me a example of the can-18xxx8.c and can-18xxx8.h files where you succesfully modified the files to work on a 12 mhz crystal. I cannot, even with the help you gave me above get it to run. I did exactly what you told me and still I cannot get it to work. I think I am missing something.
In the code you posted above, you comment out the whole section on SJW propagation delay etc. As far as I can see you only change the registers. I tried both ways anyway, but without success.

Thankyou
_________________
Anton
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 22, 2005 1:22 am     Reply with quote

I think it should work, but I haven't tested it in hardware.

On Tuesday, I will look for some 12 MHz crystals and see if
we have any in the building somewhere. If so, then I'll test
it with my CAN boards and let you know the results.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 23, 2005 5:56 pm     Reply with quote

I tested it today with a 12 MHz oscillator at 250 kbs and it worked.

The only changes I had to make were in the can_set_baud() function,
in the can-18xxx8.c file. I added the three BRGCONx lines, and
commented out everything else.

I programmed the modified code into my 18F458 demo board.
That's a board that we built here at the company. I used the
Microchip MCP2510 Dev. Kit board to talk our board. It uses
the CANKing software. I was able to send a command and
toggle Port D, bit 0, on and off. (We have our own test program,
which receives a CAN command and writes the data to Port D).

The MCP2510 Dev. Kit board runs at 16 MHz. I configured the
CANKing program for 250 kbs and a 75% sample point. I didn't
get any bus errors, such as "error passive" or anything like that.
So it's working fine.

I don't know why it doesn't work for you. You were doing this
#byte stuff before, and I pointed out that it was incorrect.
Did you fix it so it looks exactly like the code below ?

Code:

void can_set_baud(void) {

// 12 MHz oscillator, 250 kbs
BRGCON1 = 0x01;
BRGCON2 = 0xA9;
BRGCON3 = 0x02;

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

   BRGCON2.prseg=CAN_BRG_PROPAGATION_TIME;
   BRGCON2.seg1ph=CAN_BRG_PHASE_SEGMENT_1;
   BRGCON2.sam=CAN_BRG_SAM;
   BRGCON2.seg2phts=CAN_BRG_SEG_2_PHASE_TS;

   BRGCON3.seg2ph=CAN_BRG_PHASE_SEGMENT_2;
   BRGCON3.wakfil=CAN_BRG_WAKE_FILTER;
*/
}
anton



Joined: 10 Mar 2005
Posts: 4

View user's profile Send private message

RE CAN
PostPosted: Thu Mar 24, 2005 12:16 am     Reply with quote

Hi, I tried everything you said, I commented out the can_set_baud and added the BRGCON registers. I also commented out the header setings (SJW etc part) (is this correct), anyway I tried it without commenting it out as well.
I will play with it for a while, look over your code and maybe I can find out where I am going wrong.
I will let you know if I get it working or not.

Thanks for all your help.
Chat to you soon.
_________________
Anton
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