本帖最后由 陆仲文 于 2018-11-1 20:16 编辑
最近一直在做CAN的相关程序,可是我写的程序一直进不了中断,相关配置和正点原子的对照过,感觉没什么问题,但是就是进不了中断,请大家帮我看下。谢谢大家,另外我还想问下大家配置过STM32F7657IG的CAN3没有?
CAN_HandleTypeDef CAN1_Handler; //CAN1¾ä±ú
CAN_TxHeaderTypeDef TxMessage; //·¢ËíÏûÏ¢
CAN_RxHeaderTypeDef RxMessage; //½óêÕÏûÏ¢
extern u8 mydata1[8];
extern u8 mydata2[8];
extern u8 mydata3[8];
extern u8 mydata4[8];
extern u8 mydata[8];
////CAN3õê¼»ˉ
//tsjw:ÖØDÂí¬2½ìøԾ걼äμ¥Ôa.·¶Î§:CAN_SJW_1TQ~CAN_SJW_4TQ
//tbs2:ê±¼ä¶Î2μÄê±¼äμ¥Ôa. ·¶Î§:CAN_BS2_1TQ~CAN_BS2_8TQ;
//tbs1:ê±¼ä¶Î1μÄê±¼äμ¥Ôa. ·¶Î§:CAN_BS1_1TQ~CAN_BS1_16TQ
//brp :2¨ìØÂê·ÖÆμÆ÷.·¶Î§:1~1024; tq=(brp)*tpclk1
//2¨ìØÂê=Fpclk1/((tbs1+1+tbs2+1+1)*brp);
//mode:CAN_MODE_NORMAL,ÆÕí¨Ä£ê½;CAN_MODE_LOOPBACK,»Ø»·Ä£ê½;
//Fpclk1μÄê±ÖóÔú3õê¼»ˉμÄê±oòéèÖÃÎa54M,èç1ûéèÖÃCAN1_Mode_Init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_11tq,6,CAN_MODE_LOOPBACK);
//Ôò2¨ìØÂêÎa:54M/((6+11+1)*6)=500Kbps
//·μ»ØÖμ:0,3õê¼»ˉOK;
// ÆäËû,3õê¼»ˉ꧰ü;
u8 CAN1_Mode_Init(u32 tsjw,u32 tbs2,u32 tbs1,u16 brp,u32 mode)
{
CAN_FilterTypeDef CAN1_FilerConf;
CAN1_Handler.Instance=CAN1;
CAN1_Handler.pTxMsg=&TxMessage; //·¢ËíÏûÏ¢
CAN1_Handler.pRxMsg=&RxMessage; //½óêÕÏûÏ¢
CAN1_Handler.Init.Prescaler=brp; //·ÖÆμÏμêy(Fdiv)Îabrp+1
CAN1_Handler.Init.Mode=mode; //Ä£ê½éèÖÃ
CAN1_Handler.Init.SyncJumpWidth=tsjw; //ÖØDÂí¬2½ìøÔ¾¿í¶è(Tsjw)Îatsjw+1¸öê±¼äμ¥λ CAN_SJW_1TQ~CAN_SJW_4TQ
CAN1_Handler.Init.TimeSeg1=tbs1; //tbs1·¶Î§CAN_BS1_1TQ~CAN_BS1_16TQ
CAN1_Handler.Init.TimeSeg2=tbs2; //tbs2·¶Î§CAN_BS2_1TQ~CAN_BS2_8TQ
CAN1_Handler.Init.TimeTriggeredMode=DISABLE; //·Çê±¼ä′¥·¢í¨DÅÄ£ê½
CAN1_Handler.Init.AutoBusOff=DISABLE; //èí¼t×Ô¶ˉàëÏß1üàí
CAN1_Handler.Init.AutoWakeUp=DISABLE; //ËˉÃßÄ£ê½í¨1yèí¼t»½DÑ(Çå3yCAN->MCRμÄSLEEPλ)
CAN1_Handler.Init.AutoRetransmission=ENABLE; //½ûÖ1±¨ÎÄ×Ô¶ˉ′«Ëí
CAN1_Handler.Init.ReceiveFifoLocked=DISABLE; //±¨ÎÄ2»Ëø¶¨,DÂμĸ2¸Ç¾éμÄ
CAN1_Handler.Init.TransmitFifoPriority=DISABLE; //óÅÏ輶ó鱨Îıê궷û¾ö¶¨
if(HAL_CAN_Init(&CAN1_Handler)!=HAL_OK) return 1; //3õê¼»ˉ
CAN1_FilerConf.FilterIdHigh=0X0000; //32λID
CAN1_FilerConf.FilterIdLow=0X0000;
CAN1_FilerConf.FilterMaskIdHigh=0X0000; //32λMASK
CAN1_FilerConf.FilterMaskIdLow=0X0000;
CAN1_FilerConf.FilterFIFOAssignment=CAN_FILTER_FIFO0;//1yÂËÆ÷01Øáaμ½FIFO0
CAN1_FilerConf.FilterBank=0; //1yÂËÆ÷0
CAN1_FilerConf.FilterMode=CAN_FILTERMODE_IDMASK;
CAN1_FilerConf.FilterScale=CAN_FILTERSCALE_32BIT;
CAN1_FilerConf.FilterActivation=ENABLE; //¼¤»îÂË2¨Æ÷0
CAN1_FilerConf.SlaveStartFilterBank=14;
if(HAL_CAN_ConfigFilter(&CAN1_Handler,&CAN1_FilerConf)!=HAL_OK) return 2;//ÂË2¨Æ÷3õê¼»ˉ
return 0;
}
//CANμ×2ãÇy¶ˉ£¬òy½ÅÅäÖã¬ê±ÖóÅäÖã¬ÖD¶ÏÅäÖÃ
//′Ëoˉêy»á±»HAL_CAN_Init()μ÷óÃ
//hcan:CAN¾ä±ú
void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_CAN1_CLK_ENABLE(); //ê1ÄüCAN1ê±Öó
__HAL_RCC_GPIOA_CLK_ENABLE(); //¿aÆôGPIOAê±Öó
GPIO_Initure.Pin=GPIO_PIN_11|GPIO_PIN_12; //PA11,12
GPIO_Initure.Mode=GPIO_MODE_AF_PP; //íÆíì¸′óÃ
GPIO_Initure.Pull=GPIO_PULLUP; //éÏà-
GPIO_Initure.Speed=GPIO_SPEED_FAST; //¿ìËù
GPIO_Initure.Alternate=GPIO_AF9_CAN1;
HAL_GPIO_Init(GPIOA,&GPIO_Initure); //3õê¼»ˉ
#if CAN1_RX0_INT_ENABLE
__HAL_CAN_ENABLE_IT(&CAN1_Handler,CAN_IT_RX_FIFO0_MSG_PENDING);//FIFO0ÏûÏ¢1òoÅÖD¶ÏÔêDí.
HAL_NVIC_SetPriority(CAN1_RX0_IRQn,1,2); //ÇàÕ¼óÅÏ輶1£¬×óóÅÏ輶2
HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn); //ê1ÄüÖD¶Ï
#endif
}
#if CAN1_RX0_INT_ENABLE //ê1ÄüRX0ÖD¶Ï
//CANÖD¶Ï·tÎñoˉêy
void CAN1_RX0_IRQHandler(void)
{
HAL_CAN_IRQHandler(&CAN1_Handler);//′Ëoˉêy»áμ÷óÃCAN_Receive_IT()½óêÕêy¾Y
}
//CANÖD¶Ï′|àí1y3ì
//′Ëoˉêy»á±»CAN_Receive_IT()μ÷óÃ
//hcan:CAN¾ä±ú
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef* hcan)
{
int b=0;
int a=0;
//CAN_Receive_IT()oˉêy»á1رÕFIFO0ÏûÏ¢1òoÅÖD¶Ï£¬òò′ËÎòÃÇDèòaÖØDÂ′ò¿a
__HAL_CAN_ENABLE_IT(&CAN1_Handler,CAN_IT_RX_FIFO0_MSG_PENDING);//ÖØD¿aÆôFIF00ÏûÏ¢1òoÅÖD¶Ï
switch(CAN1_Handler.pRxMsg->StdId)
{
case 0x190:
for(b=0;b<8;b++)
{
mydata1=CAN1_Handler.pRxMsg->aData;
}
a=0;
for(b=0;b<4;b++)
{
mydata[a++]=((int)mydata1[2*b]<<8)+mydata1[2*b+1];
}
break;
case 0x191:
for(b=0;b<8;b++)
{
mydata2=CAN1_Handler.pRxMsg->aData;
}
a=4;
for(b=0;b<4;b++)
{
mydata[a++]=((int)mydata2[2*b]<<8)+mydata2[2*b+1];
}
break;
case 0x170:
for(b=0;b<8;b++)
{
mydata3=CAN1_Handler.pRxMsg->aData;
}
a=8;
for(b=0;b<4;b++)
{
mydata[a++]=((int)mydata3[2*b]<<8)+mydata3[2*b+1];
}
break;
case 0x171:
for(b=0;b<8;b++)
{
mydata4=CAN1_Handler.pRxMsg->aData;
}
a=12;
for(b=0;b<4;b++)
{
mydata[a++]=((int)mydata4[2*b]<<8)+mydata4[2*b+1];
}
break;
default:
//printf("id:%d
",CAN1_Handler.pRxMsg->StdId);
break;
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>