你可以用PE自动生成的代码作参考。以下是我用PE生成的:
void CAN1_Init(void)
{
/* CAN0CTL1: CANE=1,CLKSRC=0,LOOPB=0,LISTEN=0,??=0,WUPM=0,SLPAK=0,INITAK=0 */
CAN0CTL1 = 128; /* Set the control register */
/* CAN0CTL0: RXFRM=0,RXACT=0,CSWAI=0,SYNCH=0,TIME=0,WUPE=0,SLPRQ=0,INITRQ=1 */
CAN0CTL0 = 1; /* Set the control register */
CAN0IDAC_IDAM = 0; /* Set the acceptance mode */
CAN0IDAR0 = 0; /* Set the acceptance code, register CAN0IDAR0 */
CAN0IDAR1 = 0; /* Set the acceptance code, register CAN0IDAR1 */
CAN0IDAR2 = 0; /* Set the acceptance code, register CAN0IDAR2 */
CAN0IDAR3 = 0; /* Set the acceptance code, register CAN0IDAR3 */
CAN0IDAR4 = 0; /* Set the acceptance code, register CAN0IDAR4 */
CAN0IDAR5 = 0; /* Set the acceptance code, register CAN0IDAR5 */
CAN0IDAR6 = 0; /* Set the acceptance code, register CAN0IDAR6 */
CAN0IDAR7 = 0; /* Set the acceptance code, register CAN0IDAR7 */
CAN0IDMR0 = 255; /* Set the acceptance mask, register CAN0IDMR0 */
CAN0IDMR1 = 255; /* Set the acceptance mask, register CAN0IDMR1 */
CAN0IDMR2 = 255; /* Set the acceptance mask, register CAN0IDMR2 */
CAN0IDMR3 = 255; /* Set the acceptance mask, register CAN0IDMR3 */
CAN0IDMR4 = 255; /* Set the acceptance mask, register CAN0IDMR4 */
CAN0IDMR5 = 255; /* Set the acceptance mask, register CAN0IDMR5 */
CAN0IDMR6 = 255; /* Set the acceptance mask, register CAN0IDMR6 */
CAN0IDMR7 = 255; /* Set the acceptance mask, register CAN0IDMR7 */
/* CAN0BTR0: SJW1=0,SJW0=1,BRP5=0,BRP4=0,BRP3=0,BRP2=1,BRP1=0,BRP0=1 */
CAN0BTR0 = 69; /* Set the device timing register */
/* CAN0BTR1: SAMP=0,TSEG22=0,TSEG21=1,TSEG20=1,TSEG13=0,TSEG12=1,TSEG11=1,TSEG10=1 */
CAN0BTR1 = 55; /* Set the device timing register */
CAN0CTL1_CLKSRC = 0; /* Select the clock source from crystal */
CAN0CTL0_INITRQ = 0; /* Start device */
while(CAN0CTL1_INITAK) {} /* Wait for enable */
/* CAN0RFLG: WUPIF=1,CSCIF=1,RSTAT1=1,RSTAT0=1,TSTAT1=1,TSTAT0=1,OVRIF=1 */
CAN0RFLG |= 254; /* Reset error flags */
/* CAN0RIER: WUPIE=0,CSCIE=1,RSTATE1=1,RSTATE0=1,TSTATE1=1,TSTATE0=1,OVRIE=0,RXFIE=1 */
CAN0RIER = 125; /* Enable interrupts */
}
|