EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

CAN-settings for PIC18F4585

Started by Mich July 29, 2007
Hi,

I 'm programming a PIC18F4585 with MPLAB. I want to use the CAN option
and I have made the basic C and H files with the aplication maestro of
Microchip.
I 'm working on a clock frequency of 40MHz.

These are the settings I have calculated for a baudrate of 125k.

#define BAUD_RATE_PRESC 16
#define SJW_TIME 1
#define SAMPLES 1
#define PROP_TIME 2
#define PH_SEG_1 4
#define PH_SEG_2 3

Can someone help me to find the right configuration?

Thanks

Mich

Mich wrote:
> Hi, > > I 'm programming a PIC18F4585 with MPLAB. I want to use the CAN option > and I have made the basic C and H files with the aplication maestro of > Microchip. > I 'm working on a clock frequency of 40MHz. > > These are the settings I have calculated for a baudrate of 125k. > > #define BAUD_RATE_PRESC 16 > #define SJW_TIME 1 > #define SAMPLES 1 > #define PROP_TIME 2 > #define PH_SEG_1 4 > #define PH_SEG_2 3 > > Can someone help me to find the right configuration? > > Thanks > > Mich >
Go to http://intrepidcs.com and download "MB Time" http://intrepidcs.com/index.php?name=CmodsDownload&file=index&req=viewdownload&cid=2&orderby=dateD Part of this is like RS-232. You need to set it to match who you are talking to.
"Neil" <NeilKurzm@worldnet.att.net> wrote in message 
news:nbcsi.232$oo5.45@newsfe12.lga...
> Mich wrote: >> Hi, >> >> I 'm programming a PIC18F4585 with MPLAB. I want to use the CAN option >> and I have made the basic C and H files with the aplication maestro of >> Microchip. >> I 'm working on a clock frequency of 40MHz. >> >> These are the settings I have calculated for a baudrate of 125k. >> >> #define BAUD_RATE_PRESC 16 >> #define SJW_TIME 1 >> #define SAMPLES 1 >> #define PROP_TIME 2 >> #define PH_SEG_1 4 >> #define PH_SEG_2 3 >> >> Can someone help me to find the right configuration? >> >> Thanks >> >> Mich >> > > Go to http://intrepidcs.com and download "MB Time" > > http://intrepidcs.com/index.php?name=CmodsDownload&file=index&req=viewdownload&cid=2&orderby=dateD > > Part of this is like RS-232. You need to set it to match who you are > talking to.
Tools are OK for finding settings but it's better to go back and understand what all the numbers mean, the datasheet for the PIC is quite good for this, sec. 23.9. There are some examples in 23.9.2 as well. You don't specify the sample time but this is normally around 80% of the bit time so I'll use that. You say you have a clock frequency of 40MHz, again I will assume that this is the internal frequency and not just the external crystal you are using. (BTW always use a crystal, I had a very very very bad experience with a resonator, they are just not accurate enough over life and temperature range for CAN applications.) The easiest bit first is the SJW_TIME, to make the most robust system make this as big as possible, usually 3. I'm not quite sure why you would normally want to set this lower, if anybody has reasons then that would be useful to know. [SJW_TIME=3] 125k means a bit time of 8us, each bit duration is made up of a number of time quanta (Tq), these are the smallest units of time in the setup, so if we can make the Tq = 0.5us then we need 16 of them in total to get the 8us. It is generally better to have as many time quanta as possible as this improves the control of when things actually happen during the bit time, e.g. the sample point. The length of Tq is determined by the baud rate prescaler, see equ 23-3. At 40 MHz, the oscillator period is 25ns. 0.5us/25ns = 20, but the BRP always has 1 added by the micro and is then doubled as you can see in the equ, so we need to divide by 2 and subtract 1 first, then [BAUD_RATE_PRESC=9] To sample at 80%, then calculate 80%*16 Tq = 12.8. We must have an integer so use 13 which will mean sampling at 81.25%, this should be acceptable (if not then the number of Tq will need to be changed. e.g. if you use 20 then you can sample at exactly 80%). You can see from the bit time partitioning in the data sheet that only Phase Segment 2 is after the sample point so this must be 16-13=3, however again the micro always adds 1, so we must subtract 1 so [PH_SEG_2=2] The remaining 13 Tq are split up between the sync segment, propagation and phase segment 1. The Sync segment is always 1 Tq leaving 12. Both the propagation and phase seg 1 have a maximum value of 8, so lets make phase seg 1 = 8 and than the propagation can be 4. However again similarly to the previous settings the micro adds 1 to the values so we must subtract 1, therefore [PH_SEG_1=7] [PROP_TIME=3] I'm not sure what the benefit of separating the time before the sample point into propagation and phase seg 1 is, micro's I used before only had phase seg 1 and no propagation register, but this was a 16bit part so this may be the reason, i.e. the PIC18 can't go above 7/8! Finally I'd like to add that all my calculations may be wrong!! It's worth spending the time to understand it as it makes life easier if you get a problem later. I would worry about the settings you have given because it looks to me like this gives a baud rate around 90.5 kbit/s and a sample point at 69%. BUT it may be that as these are #defines, then the point in the initialisation at which the settings are actually written to the registers, the various -1's are taken account of, I am assuming this part of the code is generated by the application maestro, is this correct? I would be surprised if it made this kind of error, but you would never know if you don't do your own calculation first! Any comments from anyone else on this would be appreciated as I am also looking at this part for CAN. Dave.

The 2024 Embedded Online Conference