Board logo

标题: [求助]哪位大侠用过MC9S12XDT256单片机的 [打印本页]

作者: angel.randy    时间: 2009-8-14 09:46     标题: [求助]哪位大侠用过MC9S12XDT256单片机的

以前没接触过can模块,不知道can的程序如何写,研究过MC9S12XDT256单片机的各位大侠能否给一个can模块的初始化例子参考一下啊,我的邮箱是angelrandy@126.com,小女子先在此谢过了,[em23]
作者: louzhu    时间: 2009-8-14 11:21

没有过 第一次听楼主介绍这个
作者: louzhu    时间: 2009-8-14 11:21

坐沙发了 新知识 潜力贴 得顶 呵呵


作者: chiusir    时间: 2009-8-15 23:12

这是我开发板配套程序中原函数,自己灵活的修改下就可以用了:

void InitCAN0(void)
{
CAN0CTL0 = 0x01; /* Enter Initialization Mode
*
* 0b00000001
* ||||||||__ Enter Initialization Mode
* |||||||___ Sleep Mode Request bit
* ||||||____ Wake-Up disabled
* |||||_____ Time stamping disabled
* ||||______ Synchronized Status
* |||_______ CAN not affected by Wait
* ||________ Receiver Active Status bit
* |_________ Received Frame Flag bit
*/
while(CAN0CTL1_INITAK!=1); // Wait for Initialization Mode acknowledge INITRQ bit = 1
CAN0CTL1 = 0x80; /* Enable MSCAN module and not LoopBack Mode
*
* 0b10100000
* ||||||||__ Initialization Mode Acknowledge
* |||||||___ Sleep Mode Acknowledge
* ||||||____ Wake-up low-pass filter disabled
* |||||_____ Unimplemented
* ||||______ Listen Only Mode disabled
* |||_______ not Loop Back Mode enabled
* ||________ Ext Osc/Xtal as Clock Source
* |_________ MSCAN Module enabled
*/
CAN0BTR0 = 0x43; /* Synch Jump = 2 Tq clock Cycles
*
* 0b01000011
* ||||||||__
* |||||||___\
* ||||||____ |
* |||||_____ |_ CAN Clock Prescaler = 4
* ||||______ |
* |||_______/
* ||________
* |_________>- SJW = 2
*/
CAN0BTR1 = 0x14; /* Set Number of samples per bit, TSEG1 and TSEG2
* bit rate=Fclk/pres v/(1+TSEG1+TSEG2)=16M/4/(1+2+5)=500kbps
* 0b00010100
* ||||||||__
* |||||||___\
* ||||||____ |- TSEG1 = 5
* |||||_____/
* ||||______
* |||_______\_ TSEG2 = 2
* ||________/
* |_________ One sample per bit
*/
CAN0IDAC = 0x00; /* Set two 32-bit Filters
*
* 0b00000000
* ||||||||__
* |||||||___\_ Filter Hit Indicator
* ||||||____/
* |||||_____ Unimplemented
* ||||______
* |||_______>- two 32-bit Acceptance Filters
* ||________
* |_________>- Unimplemented
*/
/* note Acceptance Filters neither Acceptance Filter is accorded with,message in receivebuffer will pass */
CAN0IDAR0 = ACC_CAN0_EX_ID_R0; // |\ 32 bit Filter 0
CAN0IDAR1 = ACC_CAN0_EX_ID_R1; // | \__ Accepts Extended Data Frame Msg
CAN0IDAR2 = ACC_CAN0_EX_ID_R2; // | / with ID: CAN0_ID
CAN0IDAR3 = ACC_CAN0_EX_ID_R3; // |/
CAN0IDAR4 = ACC_CAN0_EX_ID_R0; // |\ 32 bit Filter 0
CAN0IDAR5 = ACC_CAN0_EX_ID_R1; // | \__ Accepts Extended Data Frame Msg
CAN0IDAR6 = ACC_CAN0_EX_ID_R2; // | / with ID: CAN0_ID
CAN0IDAR7 = ACC_CAN0_EX_ID_R3; // |/
CAN0IDMR0 = MASK_CAN0_EX_ID_R0;
CAN0IDMR1 = MASK_CAN0_EX_ID_R1;
CAN0IDMR2 = MASK_CAN0_EX_ID_R2;
CAN0IDMR3 = MASK_CAN0_EX_ID_R3;
CAN0IDMR4 = MASK_CAN0_EX_ID_R0;
CAN0IDMR5 = MASK_CAN0_EX_ID_R1;
CAN0IDMR6 = MASK_CAN0_EX_ID_R2;
CAN0IDMR7 = MASK_CAN0_EX_ID_R3;

CAN0CTL0 = 0x00; /* Exit Initialization Mode Request */
while ((CAN0CTL1&0x01)!=0); // 等待,直到MSCAN正常运行
while(!(CAN0CTL0&0x10)); // 等待,直到MSCAN与CAN总线同步
CAN0RFLG = 0xC3; /* Reset Receiver Flags
*
* 0b11000011
* ||||||||__ Receive Buffer Full Flag
* |||||||___ Overrun Interrupt Flag
* ||||||____
* |||||_____>- Transmitter Status Bits
* ||||______
* |||_______>- Receiver Status Bits
* ||________ CAN Status Change Interrupt Flag
* |_________ Wake-Up Interrupt Flag
*/
CAN0RIER = 0x01; /* Enable Receive Buffer Full Interrupt
*
* 0b00000001
* ||||||||__ Receive Buffer Full Int enabled
* |||||||___ Overrun Int disabled
* ||||||____
* |||||_____>- Tx Status Change disabled
* ||||______
* |||_______>- Rx Status Change disabled
* ||________ Status Change Int disabled
* |_________ Wake-Up Int disabled
*/
}


作者: xmyhome    时间: 2009-8-17 11:07

多谢游侠了!初始化程序已经差不多弄懂了,我用的是CAN4,但现在的问题是程序运行时CANRXFLG寄存器中的TSTAT1位总是置1,即传送错误,看了几天也不知道问题出在哪儿!
作者: chiusir    时间: 2009-9-1 23:38

把相应的0改为4即可,这个程序是经过我们的开发板验证的,完全可以使用!
作者: yumuzi    时间: 2009-9-2 10:26

楼主比较热心




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0