请大神帮看下 CAN 模块初始化 有什么问题? 为什么不起作用/

2019-07-20 15:11发布


   
/*************************************************************************
                          CAN1_Configuration
Ãèêö£o3õê¼»ˉCAN1ÅäÖÃÎa1M2¨ìØÂê
*************************************************************************/
void CAN1_Configuration(void)
{
    CAN_InitTypeDef        can;
    CAN_FilterInitTypeDef  can_filter;
    GPIO_InitTypeDef       gpio;
    NVIC_InitTypeDef       nvic;

    //ê1ÄüÏà1Øê±Öó
          RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1ÄüPORTAê±Öó                                                                                                                    

          RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//ê1ÄüCAN1ê±Öó       
       
    //3õê¼»ˉGPIO
          gpio.GPIO_Pin = GPIO_Pin_11| GPIO_Pin_12;
    gpio.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
    gpio.GPIO_OType = GPIO_OType_PP;//íÆíìêä3ö
    gpio.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
    gpio.GPIO_PuPd = GPIO_PuPd_UP;//éÏà-
    GPIO_Init(GPIOA, &gpio);//3õê¼»ˉPA11,PA12
       
          //òy½Å¸′óÃó3éäÅäÖÃ
          GPIO_PinAFConfig(GPIOD,GPIO_PinSource11,GPIO_AF_CAN1); //GPIOA11¸′óÃÎaCAN1
          GPIO_PinAFConfig(GPIOD,GPIO_PinSource12,GPIO_AF_CAN1); //GPIOA12¸′óÃÎaCAN1

                nvic.NVIC_IRQChannel = CAN1_RX0_IRQn;
    nvic.NVIC_IRQChannelPreemptionPriority = 2;
    nvic.NVIC_IRQChannelSubPriority = 1;
    nvic.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvic);

    nvic.NVIC_IRQChannel = CAN1_TX_IRQn;
    nvic.NVIC_IRQChannelPreemptionPriority = 1;
    nvic.NVIC_IRQChannelSubPriority = 1;
    nvic.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvic);

    CAN_DeInit(CAN1);
    CAN_StructInit(&can);

                can.CAN_TTCM=DISABLE;        //·Çê±¼ä′¥·¢í¨DÅģ꽠  
          can.CAN_ABOM=DISABLE;        //èí¼t×Ô¶ˉàëÏß1üàí          
          can.CAN_AWUM=DISABLE;//ËˉÃßÄ£ê½í¨1yèí¼t»½DÑ(Çå3yCAN->MCRμÄSLEEPλ)
          can.CAN_NART=DISABLE;        //½ûÖ1±¨ÎÄ×Ô¶ˉ′«Ëí
          can.CAN_RFLM=DISABLE;        //±¨ÎÄ2»Ëø¶¨,DÂμĸ2¸Ç¾éμÄ  
          can.CAN_TXFP=ENABLE;        //óÅÏ輶ó鱨Îıê궷û¾ö¶¨
    can.CAN_Mode = CAN_Mode_Normal;//Ä£ê½éèÖÃ
    can.CAN_SJW  = CAN_SJW_1tq;        //ÖØDÂí¬2½ìøÔ¾¿í¶è(Tsjw)Îatsjw+1¸öê±¼äμ¥λ CAN
    can.CAN_BS1 = CAN_BS1_9tq;//Tbs1·¶Î§CAN_BS1_1tq ~CAN_BS1_16tq
    can.CAN_BS2 = CAN_BS2_4tq;//Tbs2·¶Î§CAN_BS2_1tq ~        CAN_BS2_8tq
    can.CAN_Prescaler = 3; //////CAN BaudRate 42/(1+9+4)/3=1Mbps
    CAN_Init(CAN1, &can); // 3õê¼»ˉCAN1

                can_filter.CAN_FilterNumber = 0;//1yÂËÆ÷0
                can_filter.CAN_FilterMode = CAN_FilterMode_IdMask;
                can_filter.CAN_FilterScale = CAN_FilterScale_32bit;//32λ
                can_filter.CAN_FilterIdHigh = 0x0000;////32λID
                can_filter.CAN_FilterIdLow = 0x0000;
                can_filter.CAN_FilterMaskIdHigh = 0x0000;//32λMASK
                can_filter.CAN_FilterMaskIdLow = 0x0000;
                can_filter.CAN_FilterFIFOAssignment = 0;
                can_filter.CAN_FilterActivation=ENABLE;//¼¤»î1yÂËÆ÷0
                CAN_FilterInit(&can_filter);//ÂË2¨Æ÷3õê¼»ˉ

    CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);
    CAN_ITConfig(CAN1,CAN_IT_TME,ENABLE);
}

unsigned char can_tx_success_flag = 0;

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
7条回答
openedv、miss
1楼-- · 2019-07-20 18:18
已解决!!!!!!!!!!时钟频率不对1!!!!
openedv、miss
2楼-- · 2019-07-20 21:23
 精彩回答 2  元偷偷看……
openedv、miss
3楼-- · 2019-07-21 00:06
有人在吗?  可不可可以帮帮忙
openedv、miss
4楼-- · 2019-07-21 03:10
 精彩回答 2  元偷偷看……
openedv、miss
5楼-- · 2019-07-21 04:16
 精彩回答 2  元偷偷看……
lison0103
6楼-- · 2019-07-21 07:35
不起作用是什么,你是怎么测试的

一周热门 更多>