📄 main.c
字号:
/******************** (C) COPYRIGHT 2003 ST Microelectronics *******************
*
* 文件说明:
* Can 通信需要使用2块开发板来做,CAN通信使用波特率为125Kbps
*
*******************************************************************************/
#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<0x20000; i++);
}
/*****************************************************************************/
void __main(void)
{
#ifdef DEBUG
debug();
#endif
RCCU_Div2Config(ENABLE);
RCCU_FCLKConfig(RCCU_DEFAULT);
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
CAN_Init(CAN_CR_INIT, CAN_BITRATE_1M);
// configure the message objects
CAN_InvalidateAllMsgObj();
CAN_SetUnusedMsgObj(CAN_TX_MSGOBJ);
CAN_SetUnusedMsgObj(CAN_RX_MSGOBJ);
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)
{
// receive
if(CAN_ReceiveMessage(CAN_RX_MSGOBJ, FALSE, &RxCanMsg) == 1)
{
// 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_TOGGLE);
}
}
// release the message objects
CAN_ReleaseRxMessage(CAN_RX_MSGOBJ);
}
// transmit
CAN_SendMessage(CAN_TX_MSGOBJ, &TxCanMsg[0]);
// wait until end of transmission
CAN_WaitEndOfTx();
// transmission succeeded
LED_Set(0, LED_TOGGLE);
// release the message objects
CAN_ReleaseTxMessage(CAN_TX_MSGOBJ);
// wait
wait();
}
}
/******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -