喜悦,CAN比特率比PIC上的大多数通信模块要复杂一些,因为每个比特被分解成单个时间量子(TQ),在这个特定的代码中由ECAN_BRP_VAL控制。40MHz的FoSC和4的BRP值给出了TQ宽度:(2×(BRP+1))/FoCC=2(2×(4 +1))/40=25.比特时间是TQ宽度乘以由代码指定的每比特的时间量子数。这似乎由ECAN_PHSEG1_VAL、ECAN_PHSEG2_VAL和ECAN_PROPSEG_VAL值以及传播段在您的特定代码中指定,它们是相位段1和2。除了那些,还有同步段,它总是1 TQ。因此,由提供的代码给出的比特时间是:1+8+8+8=25TQ给定先前计算的TQ的.25uS时间,它给我们这个CAN模块的6.25uS比特时间。这应该给出一个160 kb/s的CAN频率,假设所有的x定义语句都符合我所相信的。如果这个代码输出一个1Mb/s的CAN信号,那么代码中的其他地方就有别的事情发生。有几种方法可以达到250kb/s的速率,这取决于你的其他需要。从所提供的代码中的当前设置出发,一种可能的选择是保持ECAN_BRP_VAL相同,并改变相位段,使得比特时间为16TQ,使得比特时间为4uS,频率为250kb/s。其他的BRP为4的组合,将给出总共16TQ,还有BRP为3的组合,以及给出总共20TQ的任何组合,以及BRP/Time Quanta值的其他几个组合。ATE在数据表的第24.9节中
以上来自于百度翻译
以下为原文
Joy,
CAN bitrate is a bit more complicated than most communications modules on the PIC, as each bit is broken into individual time quanta (TQ), which are controlled by ECAN_BRP_VAL in this particular code. 40MHz FOSC and a BRP value of 4 gives a TQ width of:
(2*(BRP+1))/FOSC = 2(2*(4+1))/40 = .25uS.
The bit time is then the TQ width multiplied by the number of time quanta per bit specified by code. This appears to be specified in your particular code by the ECAN_PHSEG1_VAL, ECAN_PHSEG2_VAL and ECAN_PROPSEG_VAL values, which are phase segment 1 and 2, and the propagation segment. In addition to those there is the synchronization segment, which is always 1 TQ. Thus, the bit time given by the code provided is:
1+8+8+8=25 TQ
Given the previously calculated .25uS time for TQ it gives us a 6.25uS bit time for this CAN module. This should give a CAN frequency of 160 kb/s assuming that all of the #define statements do what I believe them to do. If this code is outputting a 1Mb/s CAN signal then there is something else going on elsewhere in the code.
There are several ways to achieve the desired 250kb/s rate, depending on your other needs. Going off of the current settings you have in the code provided, one possible option is to keep ECAN_BRP_VAL the same and change the phase segments so that the bit time is 16 TQ, making the bit time 4uS and the frequency 250kb/s. One possibility for this is:
#define ECAN_SJW_VAL 2
#define ECAN_BRP_VAL 4
#define ECAN_PHSEG1_VAL 3
#define ECAN_PSHEG2_VAL 4
#define ECAN_PROPSEG_VAL 8
Giving a bit time of 1+3+4+8=16 TQ.
Other combinations with BRP of 4 that will give a total of 16 TQ will also work, as will a BRP of 3 and any combination that gives a total of 20 TQ, as well as several other combinations of BRP/Time Quanta values.
All equations I used here, as well as further information on setting up the CAN baud rate, are in section 24.9 of the datasheet