4、发送数据:
/*使用can总线发送数据*/
uint8_t CANx_SendNormalData(CAN_TxHeaderTypeDef TxHeader,uint32_t std_id ,uint8_t *TxData ,uint16_t lengh)
{
uint32_t TxMailbox;
uint8_t FreeTxNum=0;//用来表示邮箱是否空闲
TxHeader.RTR = CAN_RTR_DATA;
TxHeader.IDE = CAN_ID_STD;
TxHeader.StdId = std_id;
TxHeader.TransmitGlobalTime = DISABLE;
TxHeader.DLC = lengh;
FreeTxNum = HAL_CAN_GetTxMailboxesFreeLevel(&hcan);
while(0==FreeTxNum)
{
FreeTxNum=HAL_CAN_GetTxMailboxesFreeLevel(&hcan);
}
if (HAL_CAN_AddTxMessage(&hcan, &TxHeader, TxData, &TxMailbox) != HAL_OK)
{
/* Transmission request Error */
Error_Handler();
}
return 1;
}
|