//============================================================================
// 工程名称: gaozu
// 硬件连接: 无
// 程序描述: 见工程说明
// 时间:2013.12.11
//========================================
//头文件
#include "includes.h"
// 定义全局变量
extern int periph_clk_khz;
uint8 CANReData[8]; //接收到的数据
uint8 CANReDataLen; //接收到的数据长度
uint8 CANReFlag=1; //接收到数据标志
uint32 localMsgID = 0x0B; //本机ID
uint32 serverMsgID= 0x0A; //主机ID
uint32 rxMsgID; //接收到的ID号
uint32 txMsgID; //发送ID号
uint32 IDNodeB = 0x0B; //Node B的ID号
uint32 IDNodeC = 0x0C; //Node C的ID号
//====================全局变量定义========
//==========函数声明======================
//===========(结束)=======================
//主函数
void main()
{
//===========局部变量声明=================
uint16 result=0;
uint8 Data[16] = "CAN_DATA";
uint8 DataNodeB[16] = "IamNodeB";
uint32 CANbaud =1000; //波特率 83KHz
uint32 mCANSendCounter=0;
uint8 CANSendFlag = 1;
uint8 i;
uint8 selfLoop = 0; //1=回环测试模式,0=正常模式
uint8 idMask = 1; //1=ID屏蔽,0=ID不屏蔽
//=========================初始化========================================
DisableInterrupts; //禁止总中断
LED_init();
adc_init(ADC0, DP1); //adc初始化
CANInit(CAN_NUM_TEST,CANbaud,selfLoop,idMask); //CAN模块初始化
hw_uart_init (UART_TEST,TEST_UART_BAUDRATE);//uart初始化
hw_uart_enable_re_int(UART_TEST);
CANEnableRXBuff(CAN_NUM_TEST,FLEXCAN_RX_MB_START,localMsgID);//使能接收缓冲区
EnableCANInterrupt(CAN_NUM_TEST,FLEXCAN_RX_MB_START);//使能CAN的MB中断
EnableInterrupts; //开总中断
//==================主程序初始化===============================
printf("*********K60 CAN_Bus Node B Test******\r\n");
//===========主循环(开始)======================================================
while(1)
{
LED_turn(LED0);
mCANSendCounter++;
if(mCANSendCounter > 20000)
{
mCANSendCounter = 0; //清除计数器
txMsgID = localMsgID;
DisableCANInterrupt(CAN_NUM_TEST,FLEXCAN_RX_MB_START);//关CAN的MB中断
if(CANSendData(CAN_NUM_TEST,FLEXCAN_TX_MB_START, txMsgID, 8, DataNodeB)==0)
{
hw_uart_send_string(UART_TEST,(uint8 *)"\r\n\r\nCAN send data : ");
for(i = 0;i < 8;i++)
hw_uart_send1(UART_TEST,DataNodeB[i]);
hw_uart_send_string(UART_TEST,(uint8 *)"\r\n");
hw_uart_send_string(UART_TEST,(uint8 *)"CAN send ID : 0x");
hw_uart_send_hex_number(UART_TEST,txMsgID);
hw_uart_send_string(UART_TEST,(uint8 *)" Successfully!\r\n\r\n");
}
EnableCANInterrupt(CAN_NUM_TEST,FLEXCAN_RX_MB_START);//开CAN的MB中断
}
//3. CAN接收数据处理
if(0==CANReFlag)
{
DisableCANInterrupt(CAN_NUM_TEST,FLEXCAN_RX_MB_START);//关CAN的MB中断
CANReFlag = 1;
hw_uart_send_string(UART_TEST,(uint8 *)"CAN receive rxMsgID : 0x");
hw_uart_send_hex_number(UART_TEST,rxMsgID);
hw_uart_send_string(UART_TEST,(uint8 *)"\r\n");
hw_uart_send_string(UART_TEST,(uint8 *)"CAN receive length : 0x");
hw_uart_send_hex_number(UART_TEST,CANReDataLen);
hw_uart_send_string(UART_TEST,(uint8 *)"\r\n");
hw_uart_send_string(UART_TEST,(uint8 *)"CAN receive data : ");
for(i = 0;i < CANReDataLen;i++)
{
hw_uart_send1(UART_TEST,CANReData[i]);
}
hw_uart_send_string(UART_TEST,(uint8 *)"\r\n");
hw_uart_send_string(UART_TEST,(uint8 *)"\r\n");
EnableCANInterrupt(CAN_NUM_TEST,FLEXCAN_RX_MB_START);//开CAN的MB中断
}
}// end for
//主循环结束============================================================================
} // end_main
串口调试结果为下图,为啥刚发送的数据又被重新接收回来了?请大神帮忙看看
|