can1能正常工作,can2不通,希望大神们看看设置的问题
#include "can.h"
#include "led.h"
#include "delay.h"
#include "usart.h"
//CAN3õê¼»ˉ
//tsjw:ÖØDÂí¬2½ìøԾ걼äμ¥Ôa. @ref CAN_synchronisation_jump_width ·¶Î§: ; CAN_SJW_1tq~ CAN_SJW_4tq
//tbs2:ê±¼ä¶Î2μÄê±¼äμ¥Ôa. @ref CAN_time_quantum_in_bit_segment_2 ·¶Î§:CAN_BS2_1tq~CAN_BS2_8tq;
//tbs1:ê±¼ä¶Î1μÄê±¼äμ¥Ôa. @refCAN_time_quantum_in_bit_segment_1 ·¶Î§: ; CAN_BS1_1tq ~CAN_BS1_16tq
//brp :2¨ìØÂê·ÖÆμÆ÷.·¶Î§:1~1024;(êμ¼êòa¼ó1,ò2¾íêÇ1~1024) tq=(brp)*tpclk1
//2¨ìØÂê=Fpclk1/((tsjw+tbs1+tbs2+3)*brp);
//mode: @ref CAN_operating_mode ·¶Î§£oCAN_Mode_Normal,ÆÕí¨Ä£ê½;CAN_Mode_LoopBack,»Ø»·Ä£ê½;
//Fpclk1μÄê±ÖóÔú3õê¼»ˉμÄê±oòéèÖÃÎa36M,èç1ûéèÖÃCAN_Normal_Init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq,6,CAN_Mode_LoopBack);
//Ôò2¨ìØÂêÎa:42M/((1+6+7)*6)=500Kbps
//·μ»ØÖμ:0,3õê¼»ˉOK;
// ÆäËû,3õê¼»ˉ꧰ü;
u16 clen=0;
u8 CanRxBuffer[CAN_REC_LEN];
u8 canflag=0;
u16 TXID=0x0000;
u8 res;
u8 CAN1_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
#if CAN1_RX0_INT_ENABLE
NVIC_InitTypeDef NVIC_InitStructure;
#endif
//ê1ÄüÏà1Øê±Öó
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1ÄüPORTAê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//ê1ÄüCAN1ê±Öó
//3õê¼»ˉGPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11| GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//éÏà-
GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉPA11,PA12
//òy½Å¸′óÃó3éäÅäÖÃ
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_CAN1); //GPIOA11¸′óÃÎaCAN1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_CAN1); //GPIOA12¸′óÃÎaCAN1
CAN_DeInit(CAN1);
//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; //Ä£ê½éèÖÃ
CAN_InitStructure.CAN_SJW=tsjw; //ÖØDÂí¬2½ìøÔ¾¿í¶è(Tsjw)Îatsjw+1¸öê±¼äμ¥λ CAN_SJW_1tq~CAN_SJW_4tq
CAN_InitStructure.CAN_BS1=tbs1; //Tbs1·¶Î§CAN_BS1_1tq ~CAN_BS1_16tq
CAN_InitStructure.CAN_BS2=tbs2;//Tbs2·¶Î§CAN_BS2_1tq ~ CAN_BS2_8tq
CAN_InitStructure.CAN_Prescaler=brp; //·ÖÆμÏμêy(Fdiv)Îabrp+1
CAN_Init(CAN1, &CAN_InitStructure); // 3õê¼»ˉCAN1
//ÅäÖÃ1yÂËÆ÷
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õê¼»ˉ
#if CAN1_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; // Ö÷óÅÏ輶Îa3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // ′ÎóÅÏ輶Îa3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
return 0;
}
#if CAN1_RX0_INT_ENABLE //ê1ÄüRX0ÖD¶Ï
//ÖD¶Ï·tÎñoˉêy
void CAN1_RX0_IRQHandler(void)
{
u8 i;
CanRxMsg RxMessage;
CAN_Receive(CAN1,CAN_FIFO0, &RxMessage); //
switch(RxMessage.StdId)
{
case 0x601:
if((RxMessage.Data[0]==0x2F)&&(RxMessage.Data[1]==0x00)&&(RxMessage.Data[2]==0x21)&&(RxMessage.Data[3]==0x02)&&(RxMessage.Data[4]==0x01))canflag=1;
break;
case 0x201:
TXID=0x111;
res=CAN1_Send_Msg(abc,5);
break;
default:break;
}
}
#endif
//can·¢Ëíò»×éêy¾Y(1춨¸ñê½:IDÎa0X12,±ê×¼Ö¡,êy¾YÖ¡)
//len:êy¾Y3¤¶è(×î′óÎa8)
//msg:êy¾YÖ¸Õë,×î′óÎa8¸ö×Ö½ú.
//·μ»ØÖμ:0,3é1|;
// ÆäËû,꧰ü;
u8 CAN1_Send_Msg(u8* msg,u8 len)
{
u8 mbox;
u16 i=0;
CanTxMsg TxMessage;
TxMessage.StdId=TXID; // ±ê×¼±ê궷ûÎa0
TxMessage.ExtId=0x12; // éèÖÃà©Õ1±êê¾·û£¨29룩
TxMessage.IDE=0; // ê1óÃà©Õ1±ê궷û
TxMessage.RTR=0; // ÏûÏ¢ààDíÎaêy¾YÖ¡£¬ò»Ö¡8λ
TxMessage.DLC=len; // ·¢Ëíá½Ö¡DÅÏ¢
for(i=0;i<len;i++)
TxMessage.Data[i]=msg[i]; // μúò»Ö¡DÅÏ¢
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;
}
//can¿ú½óêÕêy¾Y2éÑˉ
//buf:êy¾Y»o′æÇø;
//·μ»ØÖμ:0,ÎTêy¾Y±»êÕμ½;
// ÆäËû,½óêÕμÄêy¾Y3¤¶è;
u8 CAN1_Receive_Msg(u8 *buf)
{
u32 i;
CanRxMsg RxMessage;
if( CAN_MessagePending(CAN1,CAN_FIFO0)==0)return 0; //ûóD½óêÕμ½êy¾Y,Ö±½óíË3ö
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);//¶áè¡êy¾Y
for(i=0;i<RxMessage.DLC;i++)
buf[i]=RxMessage.Data[i];
return RxMessage.DLC;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
u8 CAN2_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;
//ê1ÄüÏà1Øê±Öó
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//ê1ÄüPORTBê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);//ê1ÄüCAN2ê±Öó
//3õê¼»ˉGPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8| GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//éÏà-
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉPB8,PB9???????????????????????????????
//òy½Å¸′óÃó3éäÅäÖÃ
GPIO_PinAFConfig(GPIOB,GPIO_PinSource8,GPIO_AF_CAN2); //GPIOB8¸′óÃÎaCAN2
GPIO_PinAFConfig(GPIOB,GPIO_PinSource9,GPIO_AF_CAN2); //GPIOB9¸′óÃÎaCAN2
CAN_DeInit(CAN2);
//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; //Ä£ê½éèÖÃ
CAN_InitStructure.CAN_SJW=tsjw; //ÖØDÂí¬2½ìøÔ¾¿í¶è(Tsjw)Îatsjw+1¸öê±¼äμ¥λ CAN_SJW_1tq~CAN_SJW_4tq
CAN_InitStructure.CAN_BS1=tbs1; //Tbs1·¶Î§CAN_BS1_1tq ~CAN_BS1_16tq
CAN_InitStructure.CAN_BS2=tbs2;//Tbs2·¶Î§CAN_BS2_1tq ~ CAN_BS2_8tq
CAN_InitStructure.CAN_Prescaler=brp; //·ÖÆμÏμêy(Fdiv)Îabrp+1
CAN_Init(CAN2, &CAN_InitStructure); // 3õê¼»ˉCAN2
//ÅäÖÃ1yÂËÆ÷
CAN_FilterInitStructure.CAN_FilterNumber=14; //1yÂËÆ÷14??????????????
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ÂËÆ÷141Øáaμ½FIFO1???????????????
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //¼¤»î1yÂËÆ÷0
CAN_FilterInit(&CAN_FilterInitStructure);//ÂË2¨Æ÷3õê¼»ˉ
CAN_ITConfig(CAN2,CAN_IT_FMP0,ENABLE);//FIFO1ÏûÏ¢1òoÅÖD¶ÏÔêDí. ??????????????
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // Ö÷óÅÏ輶Îa3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // ′ÎóÅÏ輶Îa3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
return 0;
}
#if CAN2_RX0_INT_ENABLE //ê1ÄüRX0ÖD¶Ï
//ÖD¶Ï·tÎñoˉêy
void CAN2_RX0_IRQHandler(void)
{
u8 i;
CanRxMsg RxMessage2;
CAN_Receive(CAN2,CAN_FIFO0, &RxMessage2); //???????????????
switch(RxMessage2.StdId)
{
case 0x601:
i=0;
// else if((RxMessage.Data[0]==0x11)&&(RxMessage.Data[1]==0x11)&&(RxMessage.Data[2]==0x11)&&(RxMessage.Data[3]==0x11)&&(RxMessage.Data[4]==0x11))canflag=12;//test
break;
case 0x201:
//if((RxMessage.Data[0]==0x7E)&&(RxMessage.Data[1]==0x7E)&&(RxMessage.Data[2]==0xCD)&&(RxMessage.Data[3]==0x02))
break;
default:break;
}
}
#endif
//can·¢Ëíò»×éêy¾Y(1춨¸ñê½:IDÎa0X12,±ê×¼Ö¡,êy¾YÖ¡)
//len:êy¾Y3¤¶è(×î′óÎa8)
//msg:êy¾YÖ¸Õë,×î′óÎa8¸ö×Ö½ú.
//·μ»ØÖμ:0,3é1|;
// ÆäËû,꧰ü;
u8 CAN2_Send_Msg(u8* msg,u8 len)
{
u8 mbox;
u16 i=0;
CanTxMsg TxMessage2;
TxMessage2.StdId=TXID; // ±ê×¼±ê궷ûÎa0
//TxMessage2.StdId=0x111;//?????????????????????
TxMessage2.ExtId=0x12; // éèÖÃà©Õ1±êê¾·û£¨29룩
TxMessage2.IDE=0; // ê1óÃà©Õ1±ê궷û
TxMessage2.RTR=0; // ÏûÏ¢ààDíÎaêy¾YÖ¡£¬ò»Ö¡8λ
TxMessage2.DLC=len; // ·¢Ëíá½Ö¡DÅÏ¢
for(i=0;i<len;i++)
TxMessage2.Data[i]=msg[i]; // μúò»Ö¡DÅÏ¢
mbox= CAN_Transmit(CAN2, &TxMessage2);
i=0;
while((CAN_TransmitStatus(CAN2, mbox)==CAN_TxStatus_Failed)&&(i<0XFFF))i++; //μè′y·¢Ëí½áêø
if(i>=0XFFF)return 1;
return 0;
}
//can¿ú½óêÕêy¾Y2éÑˉ
//buf:êy¾Y»o′æÇø;
//·μ»ØÖμ:0,ÎTêy¾Y±»êÕμ½;
// ÆäËû,½óêÕμÄêy¾Y3¤¶è;
u8 CAN2_Receive_Msg(u8 *buf)
{
u32 i;
CanRxMsg RxMessage2;
if( CAN_MessagePending(CAN2,CAN_FIFO0)==0)return 0; //ûóD½óêÕμ½êy¾Y,Ö±½óíË3ö ????????????????????
CAN_Receive(CAN2, CAN_FIFO0, &RxMessage2);//¶áè¡êy¾Y
for(i=0;i<RxMessage2.DLC;i++)
buf[i]=RxMessage2.Data[i];
return RxMessage2.DLC;
}
一周热门 更多>