注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
lugl4313820的个人空间 http://home.eeworld.com.cn/space-uid-1269709.html [收藏] [复制] [分享] [RSS]
日志

【国民技术N32G430】7、CAN测试

已有 858 次阅读2022-9-30 16:29

【目的】熟悉N32G430-CAN的初始化,验证通讯可行。

1、打开例程Nations.N32G430_Library.1.0.0\projects\n32g430_EVAL\examples\CAN\CAN_Tx_Rx。

  2、打开工程后查看can初始化函数,第一是先初使能GPIOB的时钟,APB2时钟。

void CAN_GPIO_Configuration(void)
{
    GPIO_InitType GPIO_InitStructure;

    /* Initializes the GPIO */
    GPIO_Structure_Initialize(&GPIO_InitStructure);

    /* configure CAN pin */
	RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOB);
	RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_AFIO);

    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);
}

3、配置CAN配置,配置为500K标准ID

void CAN_CONFIG(void)
{
    RCC_APB1_Peripheral_Clock_Enable(RCC_APB1_PERIPH_CAN);
    
    /* Initializes 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;
    CAN_InitStructure.RSJW          = CAN_RSJW_1TQ;
    CAN_InitStructure.TBS1          = CAN_TBS1_8TQ;
    CAN_InitStructure.TBS2          = CAN_TBS2_7TQ;   
    CAN_InitStructure.BaudRatePrescaler = 4;
    
    /*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         = 0x8000;
    CAN_FilterInitStructure.Filter_LowId          = 0x0000;
    CAN_FilterInitStructure.FilterMask_HighId     = 0x4000;
    CAN_FilterInitStructure.FilterMask_LowId      = 0x0000;
    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);
}

4、主函数主要是调用了CAN初始化IO然后修改为定时发送,方便用CAN分析仪验证:

int main(void)
{
    /* CAN configure */
    CAN_CONFIG();
    
    /* 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)
    {
        CAN_Transmit_Message_initializes(CAN,&CAN_TxMessage);
      SysTick_Delay_Ms(500);
//        while(Rx_Flag == DISABLE)
//        {
//            
//        }
//        Rx_Flag = DISABLE;
    }
}

5、准备好CAN分析仪。PB8接CAN—TTL的RX,PB9接CAN_TTL的TX。CAN转接板电源需要接5V。接上CAN分析仪。

 6、下载好程序后,打开分析仪软件,使用自动识别波特率,发现500K的速度,并收到了数据。

 【总结】N32G430的can配置按照例程还是比较顺利的。

本文来自论坛,点击查看完整帖子内容。

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章