stm32f103c8t6 CAN通信的代码 库函数
时间: 2023-07-29 13:14:58 浏览: 126
以下是STM32F103C8T6芯片的CAN通信代码库函数版本的示例代码,使用的是STM32官方提供的HAL库函数:
```c
#include "stm32f1xx_hal.h"
CAN_HandleTypeDef hcan;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_CAN_Init();
uint8_t canTxData[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
uint32_t canTxMailbox;
while (1)
{
if (HAL_CAN_GetState(&hcan) == HAL_CAN_STATE_READY)
{
CAN_TxHeaderTypeDef canTxHeader;
canTxHeader.StdId = 0x123;
canTxHeader.ExtId = 0;
canTxHeader.RTR = CAN_RTR_DATA;
canTxHeader.IDE = CAN_ID_STD;
canTxHeader.DLC = 8;
if (HAL_CAN_AddTxMessage(&hcan, &canTxHeader, canTxData, &canTxMailbox) == HAL_OK)
{
// 发送成功
}
else
{
// 发送失败
}
}
HAL_Delay(1000);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_CAN_Init(void)
{
hcan.Instance = CAN1;
hcan.Init.Prescaler = 4;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan.Init.TimeSeg1 = CAN_BS1_13TQ;
hcan.Init.TimeSeg2 = CAN_BS2_2TQ;
hcan.Init.TimeTriggeredMode = DISABLE;
hcan.Init.AutoBusOff = DISABLE;
hcan.Init.AutoWakeUp = DISABLE;
hcan.Init.AutoRetransmission = ENABLE;
hcan.Init.ReceiveFifoLocked = DISABLE;
hcan.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void Error_Handler(void)
{
while (1)
{
}
}
void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *hcan)
{
if (hcan->pRxMsg->StdId == 0x456)
{
// 收到标准ID为0x456的CAN数据帧
uint8_t canRxData[8];
memcpy(canRxData, hcan->pRxMsg->Data, 8);
// 处理接收到的数据
}
HAL_CAN_Receive_IT(hcan, CAN_FIFO0);
}
```
以上代码实现了CAN总线的初始化、周期性发送数据和接收数据。其中,CAN的初始化配置在`MX_CAN_Init()`函数中,CAN的发送和接收操作在`main()`函数中。在发送数据时,需要先使用`HAL_CAN_GetState()`函数判断CAN总线是否处于就绪状态,然后使用`HAL_CAN_AddTxMessage()`函数发送数据帧。在接收数据时,需要在初始化之后使用`HAL_CAN_Receive_IT()`函数开启接收中断,并在`HAL_CAN_RxCpltCallback()`回调函数中处理接收到的数据。
需要注意的是,在这个示例代码中,使用的是标准ID为0x123的数据帧进行发送,收到的数据帧标准ID需要为0x456,具体的ID值需要根据实际情况进行配置。
阅读全文