本帖最后由 Ghost123456 于 2016-12-8 20:09 编辑
各位大哥大姐,新手求助啊!咋个回事呢,CAN1和CAN2都不通;之前在103上面CAN1是OK的;
先解决CAN1的问题
代码如下:
u8 CAN1_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
#if CAN_RX0_INT_ENABLE
NVIC_InitTypeDef NVIC_InitStructure;
#endif
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//ê1ÄüPORTAê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); //ê1ÄüCAN1ê±Öó
RCC_APB1PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //ê1ÄüAFIOê±Öó
GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE);//¶Ë¿úó3éä
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíì
GPIO_Init(GPIOB, &GPIO_InitStructure); //3õê¼»ˉIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//éÏà-êäèë
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉIO
//CANμ¥ÔaéèÖÃ
CAN_InitStructure.CAN_TTCM=DISABLE; //·Çê±¼ä′¥·¢í¨DÅÄ£ê½ //
CAN_InitStructure.CAN_ABOM=DISABLE; //èí¼t×Ô¶ˉàëÏß1üàí //
CAN_InitStructure.CAN_AWUM=DISABLE; //ËˉÃßÄ£ê½í¨1yèí¼t»½DÑ(Çå3yCAN->MCRμÄSLEEPλ)//
CAN_InitStructure.CAN_NART=ENABLE; //½ûÖ1±¨ÎÄ×Ô¶ˉ′«Ëí //
CAN_InitStructure.CAN_RFLM=DISABLE; //±¨ÎÄ2»Ëø¶¨,DÂμĸ2¸Ç¾éμÄ //
CAN_InitStructure.CAN_TXFP=DISABLE; //óÅÏ輶ó鱨Îıê궷û¾ö¶¨ //
CAN_InitStructure.CAN_Mode= 1; //Ä£ê½éèÖão mode:0,ÆÕí¨Ä£ê½;1,»Ø»·Ä£ê½; //
//éèÖÃ2¨ìØÂê
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq ; //ÖØDÂí¬2½ìøÔ¾¿í¶è(Tsjw)Îatsjw+1¸öê±¼äμ¥λ CAN_SJW_1tq CAN_SJW_2tq CAN_SJW_3tq CAN_SJW_4tq
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq; //Tbs1=tbs1+1¸öê±¼äμ¥λCAN_BS1_1tq ~CAN_BS1_16tq
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;//Tbs2=tbs2+1¸öê±¼äμ¥λCAN_BS2_1tq ~ CAN_BS2_8tq
CAN_InitStructure.CAN_Prescaler=18; //·ÖÆμÏμêy(Fdiv)Îabrp+1 //125kbps
CAN_Init(CAN1, &CAN_InitStructure); // 3õê¼»ˉCAN1
CAN1->FMR|=0X00000E00;//′óμú14ÎaCAN2μÄÂË2¨Æ÷
CAN_FilterInitStructure.CAN_FilterNumber=0; //1yÂËÆ÷0
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //32λ
CAN_FilterInitStructure.CAN_FilterIdHigh=0x060F;////32λID
CAN_FilterInitStructure.CAN_FilterIdLow=0x8000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0FFF;//32λMASK
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0xF000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//1yÂËÆ÷01Øáaμ½FIFO0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //¼¤»î1yÂËÆ÷0
CAN_FilterInit(&CAN_FilterInitStructure);//ÂË2¨Æ÷3õê¼»ˉ
#if CAN_RX0_INT_ENABLE
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);//FIFO0ÏûÏ¢1òoÅÖD¶ÏÔêDí.
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // Ö÷óÅÏ輶Îa1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; // ′ÎóÅÏ輶Îa2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
return 0;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
你好,现在我是用回环模式;不能发送数据,查看了好几个寄存器,都是按照流程走的;可惜就是看不到TX信号上的波形,还显示发送成功……一直是高电平…………
一周热门 更多>