历史上的今天
今天是:2024年12月16日(星期一)
2020年12月16日 | STM32F7xx —— CAN通信
2020-12-16 来源:eefocus
一、CAN基础
差分信号:显性电平对应逻辑0,CAN_H和CAN_L差为2.5V;隐形电平对应逻辑1,CAN_H和CAN_L差为0V。
CAN总线的开始和结束都有一个120Ω的终端电阻。
数据帧:标准帧11位, 扩展帧29位。
其他的一些理论知识就不再赘述了,可以参考维基百科对于CAN的描述。
STM32F7xx的bxCAN主要特点:支持CAN2.0A和CAN2.0B,波特率高达1Mbps,支持时间触发,具有3个发送邮箱,2个接收邮箱,可变的过滤器组等。
二、几个重要的CAN函数
HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef* hcan); // CAN初始化
HAL_StatusTypeDef HAL_CAN_Transmit(CAN_HandleTypeDef *hcan, uint32_t Timeout); // CAN发送
HAL_StatusTypeDef HAL_CAN_Receive(CAN_HandleTypeDef *hcan, uint8_t FIFONumber, uint32_t Timeout); // CAN接收
三、几个重要的结构
// CAN操作句柄 包含CAN基地址(CAN1/CAN2/CAN3) 初始化结构 发送接收结构体 其余三个是过程变量
typedef struct
{
CAN_TypeDef *Instance; /*!< Register base address */
CAN_InitTypeDef Init; /*!< CAN required parameters */
CanTxMsgTypeDef* pTxMsg; /*!< Pointer to transmit structure */
CanRxMsgTypeDef* pRxMsg; /*!< Pointer to reception structure */
__IO HAL_CAN_StateTypeDef State; /*!< CAN communication state */
HAL_LockTypeDef Lock; /*!< CAN locking object */
__IO uint32_t ErrorCode; /*!< CAN Error code */
}CAN_HandleTypeDef;
// CAN配置结构体
// 前5个参数来设置 CAN_BTR —— 波特率
// 后6个参数用来设置 CAN_MCR —— 通信相关的控制位
typedef struct
{
uint32_t Prescaler; /*!< Specifies the length of a time quantum.
This parameter must be a number between Min_Data = 1 and Max_Data = 1024 */
uint32_t Mode; /*!< Specifies the CAN operating mode.
This parameter can be a value of @ref CAN_operating_mode */
uint32_t SJW; /*!< Specifies the maximum number of time quanta
the CAN hardware is allowed to lengthen or
shorten a bit to perform resynchronization.
This parameter can be a value of @ref CAN_synchronisation_jump_width */
uint32_t BS1; /*!< Specifies the number of time quanta in Bit Segment 1.
This parameter can be a value of @ref CAN_time_quantum_in_bit_segment_1 */
uint32_t BS2; /*!< Specifies the number of time quanta in Bit Segment 2.
This parameter can be a value of @ref CAN_time_quantum_in_bit_segment_2 */
uint32_t TTCM; /*!< Enable or disable the time triggered communication mode.
This parameter can be set to ENABLE or DISABLE. */
uint32_t ABOM; /*!< Enable or disable the automatic bus-off management.
This parameter can be set to ENABLE or DISABLE */
uint32_t AWUM; /*!< Enable or disable the automatic wake-up mode.
This parameter can be set to ENABLE or DISABLE */
uint32_t NART; /*!< Enable or disable the non-automatic retransmission mode.
This parameter can be set to ENABLE or DISABLE */
uint32_t RFLM; /*!< Enable or disable the receive FIFO Locked mode.
This parameter can be set to ENABLE or DISABLE */
uint32_t TXFP; /*!< Enable or disable the transmit FIFO priority.
This parameter can be set to ENABLE or DISABLE */
}CAN_InitTypeDef;
// 过滤器设置
typedef struct
{
uint32_t FilterIdHigh; /*!< Specifies the filter identification number (MSBs for a 32-bit
configuration, first one for a 16-bit configuration).
This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0xFFFF */
uint32_t FilterIdLow; /*!< Specifies the filter identification number (LSBs for a 32-bit
configuration, second one for a 16-bit configuration).
This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0xFFFF */
uint32_t FilterMaskIdHigh; /*!< Specifies the filter mask number or identification number,
according to the mode (MSBs for a 32-bit configuration,
first one for a 16-bit configuration).
This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0xFFFF */
uint32_t FilterMaskIdLow; /*!< Specifies the filter mask number or identification number,
according to the mode (LSBs for a 32-bit configuration,
second one for a 16-bit configuration).
This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0xFFFF */
uint32_t FilterFIFOAssignment; /*!< Specifies the FIFO (0 or 1) which will be assigned to the filter.
This parameter can be a value of @ref CAN_filter_FIFO */
uint32_t FilterNumber; /*!< Specifies the filter which will be initialized.
This parameter must be a number between Min_Data = 0 and Max_Data = 27 */
uint32_t FilterMode; /*!< Specifies the filter mode to be initialized.
This parameter can be a value of @ref CAN_filter_mode */
uint32_t FilterScale; /*!< Specifies the filter scale.
This parameter can be a value of @ref CAN_filter_scale */
uint32_t FilterActivation; /*!< Enable or disable the filter.
This parameter can be set to ENABLE or DISABLE. */
uint32_t BankNumber; /*!< Select the start slave bank filter.
This parameter must be a number between Min_Data = 0 and Max_Data = 28 */
}CAN_FilterConfTypeDef;
// 模式 我们使用普通模式
#define CAN_MODE_NORMAL ((uint32_t)0x00000000U) /*!< Normal mode */
#define CAN_MODE_LOOPBACK ((uint32_t)CAN_BTR_LBKM) /*!< Loopback mode */
#define CAN_MODE_SILENT ((uint32_t)CAN_BTR_SILM) /*!< Silent mode */
史海拾趣
|
本帖最后由 jameswangsynnex 于 2015-3-3 19:58 编辑 在中国,京东方科技集团(BOE Technology Group)及华星光电技术(Shenzhen China Star Optoelectronics Technology)等本土企业将从2011年下半年开始利用第7.5代(底板尺寸为1950mm×2250mm) ...… 查看全部问答> |
|
VirtualAlloc,分配的空间大小可以超过32MB吗? 我的开发板有128M的内存。 OS分配32MB RAM分配32MB 显存分配64MB 这样可以吗? 显存的空间需要用VirtualAlloc来做,我的bsp里是这么做的,但是它限制大小为小于32M。… 查看全部问答> |
|
大家在开发windows CE应用程序时候. 一般是用SDK还是用MFC? 用MFC是否比SDK生成的代码量大很多. 实时性能也会比SDK的差? … 查看全部问答> |
|
还有我这里有一个 泓格PISO-730 PCI总线开关量输入/输出卡 买的时候有光盘,但怎么也装不上去 发现新硬件“网络控制器”但是安装驱动以后怎么也装不上 估计我装的方法有问题或者有别的什么问题 请教达人指点,小弟在这 ...… 查看全部问答> |
|
用IAR调试一段程序,出现如下状况,请问是什么意思? PS:我没设置断点 [ 本帖最后由 zzbaizhi 于 2012-2-24 14:57 编辑 ]… 查看全部问答> |
|
╮(╯▽╰)╭学了好久的课程,不过已经第四批了,还没有轮上我。好想用用那个触摸板啊。难过之余,发给各位同学一份资料。这是我们培训时,官方给的培训光盘里面的资料。希望对大家的学习有帮助。所以求人品,,,,,,希望下一批的名单里面有我啊 ...… 查看全部问答> |




