- UID
- 813321
- 性别
- 男
|
本帖最后由 flexraybeginer 于 2010-12-13 19:56 编辑
芯片用的是mc9s12xf512,每隔400毫秒发送次can报文,但总线电平无反应(用canoe和示波器测试),用仿真器观测,程序停止在can发送程序while((CAN0TFLG & u8TxBuffer) != u8TxBuffer)
具体程序如下:
#include "mc9s12xf512.h"
UINT8 u8TxBuffer[] = "S12XF512";
void DelayMS(unsigned int time) //100ms
{
PITCFLMT = 0x00; //关闭计时器
PITCE = 0x01; //使能通道1
PITMUX = 0x00; //使用宏时钟0
PITMTLD0 = 0xFF; //宏时钟为7us
PITLD0 = time*15625-1; //timeus
PITTF = 0x01; // 清时钟标志位
PITCFLMT = 0x80; //使能计时器
while(!(PITTF&0x01));
PITTF = 0x01;
PITCFLMT = 0x00;
}
void vfnClock_Settings(void)
{
/* Bus Clock = OSCCLK * (SYNR+1) / ((REFDV+1)(2*POSTDIV)) */
/* 40MHz = 4MHz * (9+1) / (1) */
PLLCTL_PLLON = 0; /* Disable the PLL */
SYNR_SYNDIV = 0x09; /* Set PLL synthesizer register to 9 */
REFDV_REFDIV = 0x00; /* Set PLL divider register to 0 */
/* Fvco = 2*OSCCLK*(SYNR+1)/(REFDV+1) = 8MHz* 10/1 = 80MHz */
/* -> VCOFRQ = 01b */
SYNR_VCOFRQ1 = 0;
SYNR_VCOFRQ0 = 1;
/* Fref = OSCCLK/(REFDV+1) = 4MHz/1 = 4MHz -> REFRQ= 01b */
REFDV_REFFRQ1 = 0;
REFDV_REFFRQ0 = 1;
PLLCTL_PLLON = 1; /* PLL enabled */
while(!CRGFLG_LOCK)
; /* Wait until the PLL is within the desired frequency */
CLKSEL_PLLSEL = 1; /* Select clock source from PLL */
}
void main(void) {
/* put your own code here */
UINT8 p,k=0;
p = 1;
vfnClock_Settings();
vfnCAN_Init();
EnableInterrupts;
for(;;) {
p = u8CAN_SendFrame(0xD8E00000,0x00,8 , u8TxBuffer);
DelayMS(4);//400毫秒
k = k ^ 1;
if(p == 0) p = 1;
_FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
下面是can发送驱动函数
#include "CAN_driver.h"
/*******************************************************************************/
/**
* \brief MSCAN configuration, baud rate = 500kbps, 16 bit acceptance filters
* \author Jaime Orozco
* \param void
* \return void
*/
void vfnCAN_Init(void)
{
CAN0CTL0 = 0x01; /* MSCAN in initialization mode */
while (!(CAN0CTL1_INITAK))
; /* Wait for initialization mode acknowledge */
CAN0CTL1_CANE = 1; /* Enable MSCAN module */
CAN0CTL1_CLKSRC = 0; /* Clock source is OSCCLK, CANCLK = 4MHz */
CAN0CTL1_LOOPB = 0; /* Set to 1 for LoopBack Mode, 0 otherwise */
CAN0CTL1_LISTEN = 0; /* Not listen only mode */
/* Baud rate = CANCLK/(Prescaler * time quantas) */
CAN0BTR1_TSEG_10 = 0x04; /* Time Segment 1 = 5 */
CAN0BTR1_TSEG_20 = 0x01; /* Time Segment 2 = 2 */
/* TSEG1 + TSEG2 + SYNCH_SEG = 8 time quantas */
/* Prescaler = CANCLK/(Baud rate * time quantas) = 4MHz/(500kHz * 8) = 1 */
CAN0BTR0_BRP = 0x00; /* Baud rate prescaler = 1 */
CAN0BTR0_SJW = 0x01; /* Sinchronization jump width = 2 clock cycles */
CAN0BTR1_SAMP = 0; /* One sample per bit */
CAN0IDAC_IDAM = 0x01; /* Four 16-bit acceptance filters */
CAN0IDAR0 = ACC_CODE_ID100_HIGH; /* 16 bit Filter 0 */
CAN0IDMR0 = MASK_CODE_ST_ID_HIGH;
CAN0IDAR1 = ACC_CODE_ID100_LOW;
CAN0IDMR1 = MASK_CODE_ST_ID_LOW;
CAN0IDAR2 = 0x00; /* 16 bit Filter 1 */
CAN0IDMR2 = MASK_CODE_ST_ID_HIGH;
CAN0IDAR3 = 0x00;
CAN0IDMR3 = MASK_CODE_ST_ID_LOW;
CAN0IDAR4 = 0x00; /* 16 bit Filter 2 */
CAN0IDMR4 = MASK_CODE_ST_ID_HIGH;
CAN0IDAR5 = 0x00;
CAN0IDMR5 = MASK_CODE_ST_ID_LOW;
CAN0IDAR6 = 0x00; /* 16 bit Filter 3 */
CAN0IDMR6 = MASK_CODE_ST_ID_HIGH;
CAN0IDAR7 = 0x00;
CAN0IDMR7 = MASK_CODE_ST_ID_LOW;
CAN0CTL0 = 0x00; /* Exit initialization mode request */
while (CAN0CTL1_INITAK)
; /* Wait for normal mode */
while(!(CAN0CTL0_SYNCH))
; /* Wait for CAN synchronization */
CAN0RFLG_RXF = 1; /* Clear receiver flags */
CAN0RIER_RXFIE = 1; /* Enable Full Receive Buffer interrupt */
}
/*******************************************************************************/
/**
* \brief CAN frame transmission
* \author Jaime Orozco
* \param u32ID: Identifier, u8Prio: Priority, \n
u8Length: Frame size, u8TxData: Data array
* \return void
*/
UINT8 u8CAN_SendFrame(UINT32 u32ID, UINT8 u8Prio, UINT8 u8Length, UINT8 *u8TxData)
{
/* Transmission buffer */
UINT8 u8TxBuffer = {0};
/* Index to data within the transmission buffer */
UINT8 u8Index;
if (!CAN0TFLG) /* Retrun if Transmit Buffer is full */
return ERR_BUFFER_FULL;
CAN0TBSEL = CAN0TFLG; /* Select lowest empty buffer */
u8TxBuffer = CAN0TBSEL; /* Backup selected buffer */
*((UINT32 *) ((UINT32)(&CAN0TXIDR0)))= u32ID; /* Load Id to IDR Registers */
/* Load data to Data Segment Registers */
for (u8Index=0;u8Index<u8Length;u8Index++) {
*(&CAN0TXDSR0 + u8Index) = u8TxData[u8Index];
}
CAN0TXDLR = u8Length; /* Set Data Length Code */
CAN0TXTBPR = u8Prio; /* Set Priority */
CAN0TFLG = u8TxBuffer; /* Start transmission */
while ( (CAN0TFLG & u8TxBuffer) != u8TxBuffer)
; /* Wait for Transmission completion */
return NO_ERR;
} |
|