首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

STM32 CAN总线应用程序,调试通过仅供参考

STM32 CAN总线应用程序,调试通过仅供参考

1、打开STM32 CAN系统工作时钟

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE);
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
2、开放系统中断,根据实际情况选择
   
   NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);
  
//    NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX1_IRQn;
//    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;
//    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
//    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//    NVIC_Init(&NVIC_InitStructure);
   //NVIC_InitStructure.NVIC_IRQChannel = CAN1_SCE_IRQn;
   //NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
   //NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
   //NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   //NVIC_Init(&NVIC_InitStructure);
   
//    NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn;
//    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4;
//    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
//    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//    NVIC_Init(&NVIC_InitStructure);
3、端口配置
//初始化MCU_CAN_RXD---PA11
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
   GPIO_Init(GPIOA, &GPIO_InitStructure);
//初始化MCU_CAN_TXD---PA12
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_Init(GPIOA,&GPIO_InitStructure);  
4、CAN初始化

void CAN_Configuration(void)
{
   CAN_InitTypeDef       CAN_InitStructure;
   CAN_FilterInitTypeDef CAN_FilterInitStructure;
   
   CAN_DeInit(CAN1);
   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_LoopBack;
   CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
   CAN_InitStructure.CAN_SJW =CAN_SJW_1tq;     
   CAN_InitStructure.CAN_BS1 =CAN_BS1_9tq;     
   CAN_InitStructure.CAN_BS2 =CAN_BS2_8tq;     
   CAN_InitStructure.CAN_Prescaler =200;         
   
                    //此处Tseg1+1 = CAN_BS1_8tp
   
    if(CAN_Init(CAN1,&CAN_InitStructure) == CANINITFAILED)  
    {
                     
                                       
   }
继承事业,薪火相传
返回列表