使用TMS570Ls12x LaunchPad,根据HalCoGen(V04.06.00)->Help->Help Topics->Examples->example_canCommunication生成代码:
#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
*
*/
/* USER CODE BEGIN (2) */
/* USER CODE END */
void main(void)
{
/* USER CODE BEGIN (3) */
/* 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);
/* … run forever */
while(1);
/* USER CODE END */}
/* USER CODE BEGIN (4) */
/** @fn checkPackets(uint8 *src_packet,uint8 *dst_packet,uint32 psize)
* @brief check two buffers and report error
*
*/
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);
}
canTransmit()返回状态是1成功,为什么在CAN1 Tx、Rx引脚上用示波器测量没有一直是高电平?
gaoyang9992006:
你可以查看该函数原型,返回值是1跟发送接收引脚是不是一直高电平无关。