初级会员
积分 134
金钱 134
注册时间 2018-3-26
在线时间 286 小时
10 金钱
请教一下大家与原子哥,关于CAN通信的问题,我的程序参考了原子哥的HAL例程,进行了简单的修改,发送没有问题,用示波器检测到了波形,在接收端,CANH、CANL端也有波形,但是与STM32连接的PA11、PA12却没有波形,这是什么情况?
这是CAN配置程序
#include "can.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
CAN_HandleTypeDef hcan; //CAN1¾ä±ú
CAN_TxHeaderTypeDef TxHeader; //·¢Ëí
CAN_RxHeaderTypeDef RxHeader; //½óêÕ
/* CAN init function */
uint8_t CAN1_Mode_Init(uint32_t tsjw,uint32_t tbs2,uint32_t tbs1,uint16_t brp,uint32_t mode)
{
CAN_InitTypeDef CAN1_InitConf;
hcan.Instance=CAN1;
hcan.Init = CAN1_InitConf;
hcan.Init.Prescaler=brp; //·ÖÆμÏμêy(Fdiv)Îabrp+1
hcan.Init.Mode=mode; //Ä£ê½éèÖÃ
hcan.Init.SyncJumpWidth=tsjw; //ÖØDÂí¬2½ìøÔ¾¿í¶è(Tsjw)Îatsjw+1¸öê±¼äμ¥λ CAN_SJW_1TQ~CAN_SJW_4TQ
hcan.Init.TimeSeg1=tbs1; //tbs1·¶Î§CAN_BS1_1TQ~CAN_BS1_16TQ
hcan.Init.TimeSeg2=tbs2; //tbs2·¶Î§CAN_BS2_1TQ~CAN_BS2_8TQ
hcan.Init.TimeTriggeredMode=DISABLE; //·Çê±¼ä′¥·¢í¨DÅÄ£ê½
hcan.Init.AutoBusOff=DISABLE; //èí¼t×Ô¶ˉàëÏß1üàí
hcan.Init.AutoWakeUp=DISABLE; //ËˉÃßÄ£ê½í¨1yèí¼t»½DÑ(Çå3yCAN->MCRμÄSLEEPλ)
hcan.Init.AutoRetransmission=ENABLE; //½ûÖ1±¨ÎÄ×Ô¶ˉ′«Ëí
hcan.Init.ReceiveFifoLocked=DISABLE; //±¨ÎÄ2»Ëø¶¨,DÂμĸ2¸Ç¾éμÄ
hcan.Init.TransmitFifoPriority=DISABLE; //óÅÏ輶ó鱨Îıê궷û¾ö¶¨
if(HAL_CAN_Init(&hcan)!=HAL_OK) //3õê¼»ˉ
return 1;
return 0;
}
void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(canHandle->Instance==CAN1)
{
/* USER CODE BEGIN CAN1_MspInit 0 */
/* USER CODE END CAN1_MspInit 0 */
/* CAN1 clock enable */
__HAL_RCC_CAN1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**CAN GPIO Configuration
PA11 ------> CAN_RX
PA12 ------> CAN_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* CAN1 interrupt Init */
HAL_NVIC_SetPriority(USB_HP_CAN1_TX_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USB_HP_CAN1_TX_IRQn);
HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
/* USER CODE BEGIN CAN1_MspInit 1 */
/* USER CODE END CAN1_MspInit 1 */
}
}
void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
{
if(canHandle->Instance==CAN1)
{
/* USER CODE BEGIN CAN1_MspDeInit 0 */
/* USER CODE END CAN1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_CAN1_CLK_DISABLE();
/**CAN GPIO Configuration
PA11 ------> CAN_RX
PA12 ------> CAN_TX
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
/* CAN1 interrupt Deinit */
HAL_NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn);
HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
/* USER CODE BEGIN CAN1_MspDeInit 1 */
/* USER CODE END CAN1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
void CAN_Config(void)
{
CAN_FilterTypeDef sFilterConfig;
/*##-2- Configure the CAN Filter ###########################################*/
sFilterConfig.FilterBank = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;
if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
while(1)
{
}
}
/*##-3- Start the CAN peripheral ###########################################*/
if (HAL_CAN_Start(&hcan) != HAL_OK)
{
/* Start Error */
while(1)
{
}
}
/*##-4- Activate CAN RX notification #######################################*/
if (HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
{
/* Notification Error */
while(1)
{
}
}
/*##-5- Configure Transmission process #####################################*/
TxHeader.StdId = 0x321;
TxHeader.ExtId = 0x01;
TxHeader.RTR = CAN_RTR_DATA;
TxHeader.IDE = CAN_ID_STD;
TxHeader.DLC = 2;
TxHeader.TransmitGlobalTime = DISABLE;
}
uint8_t CAN1_Send_Msg(uint8_t* msg,uint8_t len)
{
uint8_t i=0;
uint32_t TxMailbox;
uint8_t message[8];
TxHeader.StdId=0X12; //±ê×¼±ê궷û
TxHeader.ExtId=0x12; //à©Õ1±ê궷û(29λ)
TxHeader.IDE=CAN_ID_STD; //ê1óñê×¼Ö¡
TxHeader.RTR=CAN_RTR_DATA; //êy¾YÖ¡
TxHeader.DLC=len;
for(i=0;i<len;i++)
{
message[i]=msg[i];
}
if(HAL_CAN_AddTxMessage(&hcan, &TxHeader, message, &TxMailbox) != HAL_OK)//·¢Ëí
{
return 1;
}
while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan) != 3) {}
return 0;
}
//can¿ú½óêÕêy¾Y2éÑˉ
//buf:êy¾Y»o′æÇø;
//·μ»ØÖμ:0,ÎTêy¾Y±»êÕμ½;
// ÆäËû,½óêÕμÄêy¾Y3¤¶è;
uint8_t CAN1_Receive_Msg(uint8_t *buf)
{
uint32_t i;
uint8_t RxData[8];
if(HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0) != 1)
{
return 0xF1;
}
if(HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
{
return 0xF2;
}
for(i=0;i<RxHeader.DLC;i++)
buf[i]=RxData[i];
return RxHeader.DLC;
}
复制代码 这是主程序int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
CAN1_Mode_Init(CAN_SJW_1TQ,CAN_BS2_6TQ,CAN_BS1_7TQ,6,CAN_MODE_NORMAL);
CAN_Config();
MX_TIM2_Init();
MX_TIM3_Init();
MX_TIM4_Init();
MX_USART1_UART_Init();
while (1)
{
/* USER CODE END WHILE */
canbuf[0]=0x01;canbuf[1]=0x01;canbuf[2]=0x01;canbuf[3]=0x01;
canbuf[4]=0x01;canbuf[5]=0x01;canbuf[6]=0x01;canbuf[7]=0x01;
CAN1_Send_Msg(canbuf,8);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
} 复制代码
我来回答