赞
踩
以下是一个基于STM32F103芯片的CAN总线例程示例,仅供参考:
首先需要在MCU上配置CAN硬件和时钟,可以使用STM32CubeMX工具自动生成代码。
接下来是CAN总线初始化和发送CAN消息的程序
- #include "stm32f10x.h"
- #include "stm32f10x_can.h"
-
- void CAN_Configuration(void)
- {
- CAN_InitTypeDef CAN_InitStructure;
- CAN_FilterInitTypeDef CAN_FilterInitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
-
- CAN_StructInit(&CAN_InitStructure);
- CAN_InitStructure.CAN_TTCM = DISABLE;
- CAN_InitStructure.CAN_ABOM = DISABLE;
- CAN_InitStructure.CAN_AWUM = DISABLE;
- CAN_InitStructure.CAN_NART = ENABLE;
- CAN_InitStructure.CAN_RFLM = DISABLE;
- CAN_InitStructure.CAN_TXFP = ENABLE;
- CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
-
- // CAN bus speed is set to 500 kbps in this example
- CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
- CAN_InitStructure.CAN_BS1 = CAN_BS1_12tq;
- CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;
- CAN_InitStructu
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。