本帖最后由 yuandian01 于 2012-2-24 17:20 编辑
哪位用过PIC24HJ系列的CAN接口?现在遇到一个很奇怪的问题,不管是用外部晶振还是内部晶振调整,发现CAN发出来的BIT宽与自己想要得到的不一致,比如波特率100K,位宽10US,观察到的波形位宽是2US左右,或者其他的值。
程序和计算方法都是参照PDF和官方的例程,看了很久,也没找出问题所在点。各位帮忙给分析下,谢谢~~ 附部分代码,内部晶振
#define FCAN 20000000
#define BITRATE 100000
#define NTQ 20 // 16 Time Quanta in a Bit Time
#define BRP_VAL ((FCAN/(2*NTQ*BITRATE))-1)
_FOSCSEL(FNOSC_FRC);
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF);
void oscConfig(void)
{
OSCTUNbits.TUN=23; //FRC 振荡器调节位,将频率调节到8M;
PLLFBD =38 ; // M = 40
CLKDIVbits.PLLPOST =2; // N2 = 8
CLKDIVbits.PLLPRE = 0; // N1 = 2
__builtin_write_OSCCONH(0x01);
__builtin_write_OSCCONL(0x01);
while (OSCCONbits.COSC != 0b001);
while(OSCCONbits.LOCK != 1) {};
}
void ecan1ClkInit(void){
/* FCAN is selected to be FCY
/* FCAN = FCY = 20M
C1CTRL1bits.CANCKS = 0x1;
/*
Bit Time = (Sync Segment + Propagation Delay + Phase Segment 1 + Phase Segment 2)=20*TQ
Phase Segment 1 = 8TQ
Phase Segment 2 = 6Tq
Propagation Delay = 5Tq
Sync Segment = 1TQ
CiCFG1<BRP> =(FCAN /(2 譔譌BAUD))?1
*/
/* Synchronization Jump Width set to 2 TQ */
C1CFG1bits.SJW = 0x1;
/* Baud Rate Prescaler */
C1CFG1bits.BRP =0;
/* Phase Segment 1 time is 8TQ */
C1CFG2bits.SEG1PH=0x7;
/* Phase Segment 2 time is set to be programmable */
C1CFG2bits.SEG2PHTS = 0x1;
/* Phase Segment 2 time is 6 TQ */
C1CFG2bits.SEG2PH = 0x5;
/* Propagation Segment time is 5 TQ */
C1CFG2bits.PRSEG = 0x4;
/* Bus line is sampled three times at the sample point */
C1CFG2bits.SAM = 0x0;
}
} |