通过上述描述,配置为标准ID模式时,只需要关注CAN_RXMGMASK寄存器的MG[28:18]就可以了。这里要注意的是,MPC574X虽然是大端模式,但是这里的MG31却对应存储的bit0。
CAN_0.MB[8].CS.B.IDE =0;/* MB 8 will look for a standard ID */
CAN_0.MB[8].ID.B.ID_STD =0x500;/* MB 8 will look for ID = 0x555 */
CAN_0.MB[8].CS.B.CODE =4;/* MB 8 set to RX EMPTY */
CAN_0.RXMGMASK.R =0x1C03ffff;/* Global acceptance mask */
在上述代码中,将RXMGMASK寄存器配置为0x1C03FFFF。及对应的MG[28:18]为111 0000 0000。故配置完后,只接收0x5xx类型的ID帧。
CAN配置详细代码
/* General init. No MB IDs iniialized */voidFlexCAN0_Init(void){
uint8_t i;
CAN_0.MCR.B.MDIS =1;/* Disable module before selecting clock source*/
CAN_0.CTRL1.B.CLKSRC=0;/* Clock Source = oscillator Clock (8 MHz) */
CAN_0.MCR.B.MDIS =0;/* Enable module for config. (Sets FRZ, HALT)*/while(!CAN_0.MCR.B.FRZACK){}/* Wait for freeze acknowledge to set *//* CAN bus: 10 MHz clksrc, 500K bps with 16 tq *//* PRESDIV+1 = Fclksrc/Ftq = 8 MHz/8MHz = 1 *//* so PRESDIV = 0 *//* PSEG2 = Phase_Seg2 - 1 = 4 - 1 = 3 *//* PSEG1 = PSEG2 = 3 *//* PROPSEG= Prop_Seg - 1 = 7 - 1 = 6 *//* RJW = Resync Jump Width - 1 = 4 = 1 *//* SMP = 1: use 3 bits per CAN sample *//* CLKSRC=0 (unchanged): Fcanclk= Fxtal= 10 MHz*/
CAN_0.CTRL1.B.PRESDIV =0;
CAN_0.CTRL1.B.PSEG1 =3;
CAN_0.CTRL1.B.PSEG2 =3;
CAN_0.CTRL1.B.PROPSEG =6;
CAN_0.CTRL1.B.RJW =3;
CAN_0.CTRL1.B.SMP =1;for(i=0; i<64; i++){//MPC574xP has 64 buffers
CAN_0.MB[i].CS.B.CODE =0;/* Inactivate all message buffers */}
CAN_0.MB[0].CS.B.CODE =8;/* Message Buffer 0 set to TX INACTIVE */// interrupt mask
CAN_0.IMASK1.R =0x00000100;// mail buffer 8 set to receive
CAN_0.MB[8].CS.B.IDE =0;/* MB 8 will look for a standard ID */
CAN_0.MB[8].ID.B.ID_STD =0x500;/* MB 8 will look for ID = 0x555 */
CAN_0.MB[8].CS.B.CODE =4;/* MB 8 set to RX EMPTY */
CAN_0.RXMGMASK.R =0x1C03ffff;/* Global acceptance mask *//* set mask registers - all ID bits must match */for(i=0;i<64;i++){
CAN_0.RXIMR[i].R =0x00;}/* Configure the CAN0_TX pin to transmit. */
SIUL2.MSCR[PB0].B.SSS =1;//PTB0 is for CAN0_TX. Select signal source select to CAN0_TX
SIUL2.MSCR[PB0].B.OBE =1;//Set pin to output. Enable output buffer
SIUL2.MSCR[PB0].B.SRC =3;//Maximum slew rate/* Configure the CAN0_RX pin. */
SIUL2.MSCR[PB1].B.IBE =1;//PB1 is CAN0_RX pin. Enable input buffer
SIUL2.IMCR[32].B.SSS =0b0010;//Set PB1 as CAN0_RX.// CAN_0.MCR.B.AEN = 1;
CAN_0.MCR.B.MAXMB =0x3f;// CAN_0.MCR.B.IRMQ = 1;
CAN_0.MCR.B.HALT =0;while(CAN_0.MCR.B.FRZACK & CAN_0.MCR.B.NOTRDY){}/* Wait to clear *//* Good practice: wait for FRZACK on freeze mode entry/exit */
INTC_0.PSR[524].R =0x8009;}