大家好,最近一直在研究C32的CAN 程序,也看了大家的一些资料,写了一个简单的发送程序,但是没有CAN输出,请帮忙看看.另外,好像bean 产生波特率有点问题,如500k,time_seg1=7,time_seg2=6, prescaler=1,如果8M, 则波特率=8M/1*(1+7+6),结果不是500k. 不知是何原因?感觉freescale 的例程少,所以大家有什么好的心得可以共享,共同提高。
#include <hidef.h> /* common defines and macros */ #include <mc9s12c32.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12c32"
unsigned char a[8]={0x00,0x00,0x33,0x44};
void MSCAN0Init(void) {
DDRB=0xff; PORTB=0x0f; //see the led on
CANCTL0_INITRQ=1; /* enter the init mode */ while(!CANCTL1_INITAK ) { };
//CANCTL1_CANE = 1; /* msCan enable */ CANCTL1 = 0x87; CANCTL1_CLKSRC = 0x00; /* Select the clock source from crystal */ CANBTR0 = 0x40; /* set SJW=1, Prescaler value=1*/ CANBTR1 = 0x76; /* set TSEG2=7,TSEG1=8 , samle point 75% Bit Rate=500k*/
CANIDMR0 = 0xFF; /*no filter,define to accept all coming message*/ CANIDMR1 = 0xFF; CANIDMR2 = 0xFF; CANIDMR3 = 0xFF; //CAN0IDMR4 = 0xFF; //CAN0IDMR5 = 0xFF; //CAN0IDMR6 = 0xFF; //CAN0IDMR7 = 0xFF;
CANCTL0_INITRQ = 0; /*enable normal operation mode*/
while(CANCTL1_INITAK != 0) { };
CANCTL0_TIME = 1; /*enable internal time stampe*/ CANCTL0_WUPE = 1; /*WAKE UP enable*/
}
/*************************************************************************************** Function Name : MSCAN0Trans ****************************************************************************************/
void main(void) { /* put your own code here */
MSCAN0Init();
while(1) { unsigned char cansel; DDRB=0xFF; CANTBSEL = CANTFLG; /*select one available transmit buffer*/ cansel = CANTBSEL; CANTXIDR0 = 0x72; /*C32: */ CANTXIDR1 = 0x20; /*standard ID*/ CANTXDSR0 =0x55 ; CANTXDSR1 =0x55 ; CANTXDSR2 =0x44; CANTXDSR3 =0x44; CANTXDSR4 = 0x55 ; CANTXDSR5 =0x55; CANTXDSR6 =0x55 ; CANTXDSR7 =0x55 ; CANTXDLR = 8; CANTXTBPR = 0;
 ORTB=~PORTB; } EnableInterrupts; for(;;) {} /* wait forever */ }
|