📄 main.c
字号:
/******************** (C) COPYRIGHT 2003 ST Microelectronics *******************
*
* 文件说明:
* CAN自循环模式
*
*******************************************************************************/
#include "71x_lib.h"
#include "Main.h"
#include "LED.h"
canmsg RxCanMsg;
canmsg TxCanMsg[2] = {
{ CAN_STD_ID, 0x0ff, 4, { 0x22, 0x33, 0x44, 0x00,0x55 } },
{ CAN_EXT_ID, 0x12345678, 8, { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 } }
};
/*****************************************************************************/
void wait(void)
{
u32 i;
for(i = 0; i < 0x200000; i++);
}
/*****************************************************************************/
void __main(void)
{
#ifdef DEBUG
debug();
#endif
LED_Init();
// initialize the I/O ports for the CAN pins
// setup GPIO 1.11 (CAN RX pin) as input Tristate CMOS
GPIO_Config(GPIO1, 0x0800, GPIO_IN_TRI_CMOS);
// setup GPIO 1.12 (CAN TX pin) as output alternate Push-pull
GPIO_Config(GPIO1, 0x1000, GPIO_AF_PP);
// initialize the CAN at a standard bitrate, interrupts disabled
CAN_Init(0, CAN_BITRATE_125K);
// switch into Loopback+Silent mode (self-test)
CAN_EnterTestMode(CAN_TESTR_LBACK | CAN_TESTR_SILENT);
// configure the message objects
CAN_InvalidateAllMsgObj();
CAN_SetTxMsgObj(CAN_TX_MSGOBJ, CAN_STD_ID);
CAN_SetRxMsgObj(CAN_RX_MSGOBJ, CAN_STD_ID, 0, CAN_LAST_STD_ID, TRUE);
// ready for communication
LED_Set(0, LED_ON);
wait();
LED_Set(0, LED_OFF);
LED_Set(1, LED_ON);
wait();
LED_Set(1, LED_OFF);
LED_Set(2, LED_ON);
wait();
LED_Set(2, LED_OFF);
LED_Set(1, LED_ON);
wait();
LED_Set(1, LED_OFF);
LED_Set(0, LED_ON);
wait();
LED_Set(0, LED_OFF);
while(1)
{
wait();
// send a pre-defined data frame
CAN_SendMessage(CAN_TX_MSGOBJ, &TxCanMsg[0]);
// wait until end of transmission
CAN_WaitEndOfTx();
LED_Set(0, LED_ON);
// wait for reception of a data frame
while (!CAN_ReceiveMessage(CAN_RX_MSGOBJ, FALSE, &RxCanMsg))
{
// add a time-out handling here, if necessary
}
// verify
if(RxCanMsg.Dlc == TxCanMsg[0].Dlc)
{
int i;
for(i = 0; i < RxCanMsg.Dlc && RxCanMsg.Data[i] == TxCanMsg[0].Data[i]; i++);
if(i >= RxCanMsg.Dlc)
{
// receiving succeeded
LED_Set(1, LED_ON);
}
}
wait();
// release the message objects
CAN_ReleaseTxMessage(CAN_TX_MSGOBJ);
CAN_ReleaseRxMessage(CAN_RX_MSGOBJ);
LED_Set(0, LED_OFF);
LED_Set(1, LED_OFF);
}
// switch back into Normal mode
CAN_LeaveTestMode();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -