本帖最后由 技术工程师 于 2024-4-23 11:52 编辑
#申请原创# @21小跑堂 首先感谢国民技术提供的开发板, 也感谢二姨组织这次开工活动,给了我这次机会能评测N32G430开发板,废话不多说,我们开始评测
前言
CAN是控制器局域网络(Controller Area Network)的简称,它是由研发和生产汽车电子产品著称的德国BOSCH公司开发的,并最终成为国际标准(ISO11519),是国际上应用最广泛的现场总线之一。
首先我们阅读手册可以知道,N32G430C8L7拥有一路CAN接口,支持CAN协议2.0A及2.0B,波特率最高支持1Mbit/s,有CAN core管理can和512字节的sram之间通讯。
下面是CAN初始化代码
void BSP_InitCan(void)
{
CAN_InitType CAN_InitStructure;
CAN_FilterInitType CAN_FilterInitStructure;
RCC_APB1_Peripheral_Clock_Enable(RCC_APB1_PERIPH_CAN);
/* CAN初始化 */
CAN_NVIC_Configuration();
CAN_GPIO_Configuration();
CAN_Reset(CAN);
CAN_Structure_Initializes(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.TTCM = DISABLE;
CAN_InitStructure.ABOM = DISABLE;
CAN_InitStructure.AWKUM = DISABLE;
CAN_InitStructure.NART = DISABLE;
CAN_InitStructure.RFLM = DISABLE;
CAN_InitStructure.TXFP = ENABLE;
CAN_InitStructure.OperatingMode = CAN_NORMAL_MODE;
/* Baud rate calculation = CAN clock/(BaudRatePrescaler*(TBS1+TBS2+1)) */
/* CAN模块挂在APB1总线上,最大速率为32Mhz,所以波特率计算方式为32 / ((1+8+7) * 4) */
CAN_InitStructure.RSJW = CAN_RSJW_1TQ;
CAN_InitStructure.TBS1 = CAN_TBS1_8TQ;
CAN_InitStructure.TBS2 = CAN_TBS2_7TQ;
CAN_InitStructure.BaudRatePrescaler = 4; //500kbit/s
/*Initializes the CAN */
CAN_Initializes(CAN, &CAN_InitStructure);
/* CAN filter init */
CAN_FilterInitStructure.Filter_Num = 0;
CAN_FilterInitStructure.Filter_Mode = CAN_FILTER_IDLISTMODE;
CAN_FilterInitStructure.Filter_Scale = CAN_FILTER_32BITSCALE;
CAN_FilterInitStructure.Filter_HighId = CAN_STD_ID_LIST_32BIT_H(0x00000400);
CAN_FilterInitStructure.Filter_LowId = CAN_STD_ID_LIST_32BIT_L(0x00000400);
CAN_FilterInitStructure.FilterMask_HighId = CAN_STD_ID_LIST_32BIT_H(0x00000200);
CAN_FilterInitStructure.FilterMask_LowId = CAN_STD_ID_LIST_32BIT_L(0x00000200);
CAN_FilterInitStructure.Filter_FIFOAssignment = CAN_FIFO0;
CAN_FilterInitStructure.Filter_Act = ENABLE;
CAN_Filter_Initializes(&CAN_FilterInitStructure);
/* IT Configuration for CAN */
CAN_Config_Interrupt_Enable(CAN, CAN_INT_FMP0);
}
初始化这块的难点主要是波特率需要计算can总线波特率计算不正确,很容易导致接受或者发送失败。
通过官方文档的手册可以知道,can模块挂在APB1总线上,而APB1总线最大速率为32Mhz
所以我们can BaudRate = PCLK / ((tq+tbs1+tbs2) * Prescaler)//分频系数。
接下来是数据发送部分
int main(void)
{
bsp_Init();
/* Transmit assign */
CAN_TxMessage.StdId = 0x0400;
CAN_TxMessage.ExtId = 0x00; /* 如不使用,则不用配置 */
CAN_TxMessage.IDE = CAN_STANDARD_ID; /* CAN_ID_STD 标准帧 / CAN_ID_EXT 扩展帧 */
CAN_TxMessage.RTR = CAN_RTRQ_DATA; /* CAN_RTR_DATA / CAN_RTR_REMOTE */
CAN_TxMessage.DLC = 8; /* 0 to 8 */
CAN_TxMessage.Data[0] = 0x00;
CAN_TxMessage.Data[1] = 0x01;
CAN_TxMessage.Data[2] = 0x02;
CAN_TxMessage.Data[3] = 0x03;
CAN_TxMessage.Data[4] = 0x04;
CAN_TxMessage.Data[5] = 0x05;
CAN_TxMessage.Data[6] = 0x06;
CAN_TxMessage.Data[7] = 0x07;
while(1)
{
if (sendflag)
{
sendflag = DISABLE;
CAN_Transmit_Message_initializes(CAN,&CAN_TxMessage);
}
}
}
这里用了板上的key1作为发送控制,这样我们按一下按键,即可发送一次数据。
下面是实际测试:
这里我外接了一个TJA1050接收器,使用了CAN分析仪作为终端来接受CAN数据
接线如图所示
测试效果
附带源码
N32G430.zip
(6.79 MB)
|
@zxbzsm :参考官方例程改的
GPIO_InitStructure.Pin = GPIO_PIN_8; GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP; GPIO_InitStructure.GPIO_Alternate = GPIO_AF6_CAN; GPIO_Peripheral_Initialize(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.Pin = GPIO_PIN_9; GPIO_InitStructure.GPIO_Alternate = GPIO_AF6_CAN; GPIO_Peripheral_Initialize(GPIOB, &GPIO_InitStructure); PB8是rx脚 为啥要推挽输出尼 百思不得其解