RT,求MC9S12G的初始化和发送接收的源代码!自己写了一个但是不能成功发送出数据!以下是我自己的源代码,包含初始化和发送模块,更希望有个大大帮我找出其中的!!!红色段是复制的别人的,不太懂那两个寄存器!/******************************************************************************
Function Name: CAN_Init()
Engineer: created by Huanglongjie
Date: 9/24/2015
Arguments: none
Return: none
MCU: MC9S12G48
function: CAN Init
caution: baud rate = 125khz;
CANE which is write once in normal,(now I only turn on rreceive interrupt)
******************************************************************************/
void MSCAN_Init(void)
{
CANCTL1_CANE = 1;//enable MSCAN
CANCTL0 = 0b00101100;//Enable internal MSCAN timer,The MSCAN is able to restart,
//The module ceases to be clocked during wait mode
CANCTL0_INITRQ = 1;//gointo initialization mode
while(CANCTL1_INITAK != 1)
{
;//wait for going into CAN initialization mode
}
CANCTL1 = 0b11000100;//clock source is the bus clock
//Automatic bus-off recovery ,MSCAN wakes up only in case of
//a dominant pulse on the CAN bus that has a length of Twup
CANBTR0 = 0b01001111;//Synchronization Jump Width max = 2 Tq clock cycles and Tq = 1us
CANBTR1 = 0b00100011;//Time Segment 2 = 3tq,Time Segment 1 = 4tq;=>baud rate = 125khz
CANRIER = 0b00000001;//enable receive buffer full interrupt
CANIDAC = 0b00010000;//Four 16-bit acceptance filters
[color=Red] ////////////////////////////////////////////////////////////////////////
CANIDAR0 = 0x20; /* Filter 0, ID=0x100 Standard Identifier */
CANIDMR0 = 0x00;
CANIDAR1 = 0x00;
CANIDMR1 = 0x07; /* AM[2:0] = 7 to receive standard identifiers */
CANIDAR2 = 0x00; /* Filter 1, ID=0x0000 */
CANIDMR2 = 0x00;
CANIDAR3 = 0x00;
CANIDMR3 = 0x07; /* AM[2:0] = 7 to receive standard identifiers */
CANIDAR4 = 0x00; /* Filter 2, ID=0x0000 */
CANIDMR4 = 0x00;
CANIDAR5 = 0x00;
CANIDMR5 = 0x07; /* AM[2:0] = 7 to receive standard identifiers */
CANIDAR6 = 0x00; /* Filter 3, ID=0x0000 */
CANIDMR6 = 0x00;
CANIDAR7 = 0x00;
CANIDMR7 = 0x07; /* AM[2:0] = 7 to receive standard identifiers */[/color]
////////////////////////////////////////////////////////////////////////
CANCTL0_INITRQ = 0;//MSCAN restarts and then tries to synchronize to the CAN bus,gointo normol mode
while(CANCTL1_INITAK);//wait gointo normol mode
{
;
}
while(CANCTL0_SYNCH == 0)
{
;//wait that MSCAN is synchronized to the CAN bus
}
CANR标志寄存器 = 0xc3; /*reset some flags */
}
/******************************************************************************
Function Name: CAN_Send_Data_Frame()
Engineer: created by Huanglongjie
Date: 9/24/2015
Arguments: none
Return: none
MCU: MC9S12G48
function: CAN_Send_Data_Frame
caution:
*******************************************************************************/
UINT MSCAN_Send_Data_Frame(UINT id,UCHAR can_send_msg[])
{
UCHAR id_low7bit = 0,id_high4bit = 0,buffer_select_flag = 0,send_success_flag = 0;
UINT overflow_cnt = 0;
/*do
{
buffer_empty_flag = CANT标志寄存器;
}while(buffer_empty_flag == 0x00);//show all tx buffer is full,wait tx buffer is empty*/
if(CANT标志寄存器 != 0x00)//exist empty tx buffer?
{
CANTBSEL = CANT标志寄存器;//selecet corresponding buffer
buffer_select_flag = CANTBSEL;//return value of selected tx buffer id
id_low7bit = id&0x007f;
id_high4bit = (id&0x0780)/64;//calculate id
CANTXDSR0 = can_send_msg[0];
CANTXDSR1 = can_send_msg[1];
CANTXDSR2 = can_send_msg[2];
CANTXDSR3 = can_send_msg[3];
CANTXDSR4 = can_send_msg[4];
CANTXDSR5 = can_send_msg[5];
CANTXDSR6 = can_send_msg[6];
CANTXDSR7 = can_send_msg[7];
CANTXIDR0 = 0x00;
CANTXIDR1 = 0x00;
CANTXIDR2 = id_high4bit;
CANTXIDR3 = (id_low7bit*2)+0;//+0 show is data frame not is Remote frame
CANTXDLR = 0x08;//DLC = 8;
CANTXIDR1_IDE = 0;//frame is Standard format
CANTXTBPR = 0x08;
CANT标志寄存器 = buffer_select_flag;//start transmission send data
do
{
send_success_flag = buffer_select_flag & CANT标志寄存器;//not is 0 show successful
overflow_cnt ++;
}while((!send_success_flag)&&overflow_cnt < 5000);//wait send data successlly
if(overflow_cnt < 5000)
{
return CAN_SEND_SUCCESS_FLAG;
}
else
{
return CAN_SEND_FAIL_FLAG;
}
}
else
{
return CAN_SEND_FAIL_FLAG;
}
}
|