我用的是STM32F103C6T6芯片。用示波器检测接收设备CAN_RX上有波形,但程序一直进不了USB_LP_CAN_RX0_IRQHandler中断,不知道为什么?请教大家啊!我的can部分的配置如下:
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure CAN pin: RX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure CAN pin: TX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
NVIC_InitTypeDef NVIC_InitStructure;
/* Deinitializes the NVIC */
NVIC_DeInit();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 0, 0);
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN_RX0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
unsigned char CAN_MessageSend(void)
{
CAN_InitTypeDef CAN_InitStructure;
CanTxMsg TxMessage;
volatile u8 TransmitMailbox = 0;
u32 i;
/* CAN register init */
CAN_DeInit();
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM=DISABLE; //禁止时间触发通讯模式
CAN_InitStructure.CAN_ABOM=DISABLE; //禁止自动离线模式
CAN_InitStructure.CAN_AWUM=DISABLE; //禁止自动唤醒模式
CAN_InitStructure.CAN_NART=DISABLE; //禁止非自动重传模式
CAN_InitStructure.CAN_RFLM=DISABLE; //禁止接收FIFO锁定模式
CAN_InitStructure.CAN_TXFP=DISABLE; //禁止发送FIFO优先级
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq; //已经折算好了
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler=5;
CAN_Init(&CAN_InitStructure);
/* CAN FIFO0 message pending interrupt enable */
CAN_ITConfig(CAN_IT_FMP0, ENABLE);
/* transmit 1 message */
TxMessage.StdId=0x00;
TxMessage.ExtId=0x1234;
TxMessage.IDE=CAN_ID_EXT;//扩展格式
TxMessage.RTR=CAN_RTR_DATA; //数据帧
TxMessage.DLC=2;
TxMessage.Data[0]=0xDE;
TxMessage.Data[1]=0xCA;
/*数据发送*/
TransmitMailbox=CAN_Transmit(&TxMessage);
i = 0;
while((CAN_TransmitStatus(TransmitMailbox) != CANTXOK) && (i != 0xFF)) //等待CAN总线传输数据成功
i++;
if(i==0xff)//发送超时,但发送不成功
return 0;
return 1;
}
void CAN_Interrupt_MessageReceive(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN register init */
CAN_DeInit();
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM=DISABLE;
CAN_InitStructure.CAN_ABOM=DISABLE;
CAN_InitStructure.CAN_AWUM=DISABLE;
CAN_InitStructure.CAN_NART=DISABLE;
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler=5;
CAN_Init(&CAN_InitStructure);
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber=0;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
// CAN_FilterInitStructure.CAN_FilterIdLow=0x91a4;
// CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0xffff;
// CAN_FilterInitStructure.CAN_FilterMaskIdLow=0xffff;
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=DISABLE;//ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* CAN FIFO0 message pending interrupt enable */
CAN_ITConfig(CAN_IT_FMP0, ENABLE);
}
int main(void)
{
#ifdef DEBUG
debug();
#endif
//配置系统时钟
RCC_Configuration();
//配置 NVIC 和 Vector Table
NVIC_Configuration();
//配置使用的GPIO口
GPIO_Configuration();
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
#ifndef CAN_SEND_VERSION
CAN_Interrupt_MessageReceive();
#endif
//Writeflash(1,0x12345678);
// Readflash();
while (1){
#ifdef CAN_SEND_VERSION
TestTx = CAN_MessageSend();
//if (TestTx == 1)
delay();
}
#endif
}
} |