本帖最后由 守望者2481632 于 2018-10-12 15:19 编辑
CAN配置:
u8 CAN_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//ê1ÄüPORTAê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//ê1ÄüCAN1ê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíì
GPIO_Init(GPIOA, &GPIO_InitStructure); //3õê¼»ˉIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //éÏà-êäèë
GPIO_Init(GPIOA, &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= mode; //Ä£ê½éèÖão mode:0,ÆÕí¨Ä£ê½;1,»Ø»·Ä£ê½;
//éèÖÃ2¨ìØÂê
CAN_InitStructure.CAN_SJW=tsjw; //ÖØDÂí¬2½ìøÔ¾¿í¶è(Tsjw)Îatsjw+1¸öê±¼äμ¥λ CAN_SJW_1tq
CAN_InitStructure.CAN_BS1=tbs1; //Tbs1=tbs1+1¸öê±¼äμ¥λCAN_BS1_1tq ~CAN_BS1_16tq
CAN_InitStructure.CAN_BS2=tbs2; //Tbs2=tbs2+1¸öê±¼äμ¥λCAN_BS2_1tq ~ CAN_BS2_8tq
CAN_InitStructure.CAN_Prescaler=brp; //·ÖÆμÏμêy(Fdiv)Îabrp+1
CAN_Init(CAN1, &CAN_InitStructure); //3õê¼»ˉCAN1
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=0x0000; //32λID
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//32λMASK
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//1yÂËÆ÷01Øáaμ½FIFO0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//¼¤»î1yÂËÆ÷0
CAN_FilterInit(&CAN_FilterInitStructure); //ÂË2¨Æ÷3õê¼»ˉ
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE); //FIFO0ÏûÏ¢1òoÅÖD¶ÏÔêDí.
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // Ö÷óÅÏ輶Îa1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // ′ÎóÅÏ輶Îa0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
return 0;
}
CAN发送函数:
u8 Can_Send_Msg(u8* msg,u8 len)
{
u8 mbox;
u16 i=0;
CanTxMsg TxMessage;
TxMessage.StdId=0x12; // ±ê×¼±ê궷û
TxMessage.ExtId=0x12; // éèÖÃà©Õ1±êê¾·û
TxMessage.IDE=CAN_Id_Standard; // ±ê×¼Ö¡
TxMessage.RTR=CAN_RTR_Data; // êy¾YÖ¡
TxMessage.DLC=len; // òa·¢ËíμÄêy¾Y3¤¶è
for(i=0;i<len;i++)
TxMessage.Data=msg;
mbox= CAN_Transmit(CAN1, &TxMessage);
i=0;
while((CAN_TransmitStatus(CAN1, mbox)==CAN_TxStatus_Failed)&&(i<0XFFF))i++; //μè′y·¢Ëí½áêø
if(i>=0XFFF)return 1;
return 0;
}
主函数:
int main(void)
{
u8 i,j,t;
u8 ucBack_data;
u8 key;
u8 aucBuffer[8] = {0};
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(9600);
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
LED_Init();
CAN_Mode_Init(CAN_SJW_2tq,CAN_BS2_7tq,CAN_BS1_9tq,8,CAN_Mode_Normal);
printf("Restart
");
while(1)
{
for(j=0;j<8;j++)
{
aucBuffer[j] = 0;
aucBuffer[j] += (j+'0');
printf("%c ",aucBuffer[j]);
}
printf("
");
ucBack_data = Can_Send_Msg(aucBuffer,8);
if(ucBack_data == 0)
{
printf("Success!
");
}
else
{
printf("Failed!
");
}
t++;
delay_ms(1000);
if(t==2)
{
LED0=!LED0; //提示系统正在运行
t=0;
}
}
}
我用USBCAN调试助手查看,并没有看到有数据发出,大神帮分析一下哪里出了问题
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
就是例程上的,除了没有加终端电阻,其他的接线都一样的啊
一周热门 更多>