实验目的:检验开发板的CAN通讯是否正常
实验操作:将开发板中的CAN1的H与L的pin分别连接CAN2的H与L的pin,通过CAN1发送数据,CAN2接受数据
实验预期结果:如果通讯正常,D5指示灯常亮,如果通讯一场则D5指示灯熄灭。
1.首先在CCS建立Hercules_CAN_1工程。
2.在HCG上进行图形化配置。
1)首先配置dirver使能
2)配置HET
3)配置CAN1发送
4)配置CAN2接收
5)配置ESM
以上配置完成后,生成代码。
2.在CCS上进行sys_main.c代码编写
#include “sys_common.h”
/* USER CODE BEGIN (1) /
#include “het.h”
#include “can.h”
#include “gio.h”
#include “system.h”
/ Include ESM header file - types, definitions and function declarations for system driver */
#include “esm.h”
#define D_SIZE 9
uint8 tx_data[D_SIZE] = {‘H’,‘E’,‘R’,‘C’,‘U’,‘L’,‘E’,‘S’,’\0’};
uint8 rx_data[D_SIZE] = {0};
uint32 error = 0;
uint32 checkPackets(uint8 *src_packet,uint8 dst_packet,uint32 psize);
/ USER CODE END */
/** @fn void main(void)
- @brief Application main function
- @note This function is empty by default.
- This function is called after startup.
- The user can use this function to implement the application.
*/
/* USER CODE BEGIN (2) /
/ USER CODE END */
int main(void)
{
/* USER CODE BEGIN (3) */
hetInit();
/* initialize can 1 and 2 */
canInit(); /* can1 -> can2 */
/* transmit on can1 */
canTransmit(canREG1, canMESSAGE_BOX1, tx_data);
/*... wait until message receive on can2 */
while(!canIsRxMessageArrived(canREG2, canMESSAGE_BOX1));
canGetData(canREG2, canMESSAGE_BOX1, rx_data); /* receive on can2 */
/* check received data patterns */
error = checkPackets(&tx_data[0],&rx_data[0],D_SIZE);
if(error==0)
gioSetBit(hetPORT1,0,1);
else
gioSetBit(hetPORT1,0,0);
/* ... run forever */
while(1);
/* USER CODE END */
return 0;
}
/* USER CODE BEGIN (4) */
uint32 checkPackets(uint8 *src_packet,uint8 *dst_packet,uint32 psize)
{
uint32 err=0;
uint32 cnt=psize;
while(cnt–)
{
if((*src_packet++) != (dst_packet++))
{
err++; / data error /
}
}
return (err);
}
/ USER CODE END */
3.不要忘了添加include文件夹中的h文件,完成后进行编译下载
可以看到开发板中的D5指示灯常亮,表示通讯正常。硬件pin的接线图如上图所示。