各位同仁,本人刚刚接触stm32F407,在调试双CAN的时候,CAN1收发正常,CAN2不正常。
具体症状:
1.main函数运行到CAN_Transmit(CAN2, &TxMessage_CAN2)时,总线上显示CAN2一直在发送数据,可我写的程序明明是只执行一次,难道是触发了CAN的重发机制??在哪里触发的??
2.通过PC机给CAN2发数据时,上位机总是显示“发送错误”;同时,下位机的stm32F407 也一直进不去接收中断。
折腾了3天了,还是是搞不定,烦请高手指点一下。
static void CAN_Config_CAN2(void) { GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource5, GPIO_AF_CAN2); GPIO_PinAFConfig(GPIOB,GPIO_PinSource6, GPIO_AF_CAN2);
/* Configure CAN2 RX and TX pins */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ; //CAN2_RX GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType= GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //CAN2_TX GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType= GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB,&GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2,ENABLE); CAN_DeInit(CAN2);
/*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; CAN_InitStructure.CAN_Mode= CAN_Mode_Normal; CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
/* CAN Baudrate = 1MBps (CAN clocked at 42MHz)*/ CAN_InitStructure.CAN_BS1= CAN_BS1_6tq; CAN_InitStructure.CAN_BS2= CAN_BS2_7tq; CAN_InitStructure.CAN_Prescaler= 3;
InitStatus_CAN2=CAN_Init(CAN2,&CAN_InitStructure); /*CAN2 filter init */ CAN_FilterInitStructure_CAN2.CAN_FilterNumber = 14; CAN_FilterInitStructure_CAN2.CAN_FilterMode = CAN_FilterMode_IdMask; CAN_FilterInitStructure_CAN2.CAN_FilterScale = CAN_FilterScale_32bit; CAN_FilterInitStructure_CAN2.CAN_FilterIdHigh = 0x0000; CAN_FilterInitStructure_CAN2.CAN_FilterIdLow = 0x0000; CAN_FilterInitStructure_CAN2.CAN_FilterMaskIdHigh= 0x0000; CAN_FilterInitStructure_CAN2.CAN_FilterMaskIdLow = 0x0000; CAN_FilterInitStructure_CAN2.CAN_FilterFIFOAssignment= 0; CAN_FilterInitStructure_CAN2.CAN_FilterActivation = ENABLE; CAN_FilterInit(&CAN_FilterInitStructure_CAN2);
/*CAN2 Transmit Structure preparation */ TxMessage_CAN2.StdId= ID_Transmit_CAN2;//ID号 TxMessage_CAN1.ExtId= 0x00; TxMessage_CAN2.RTR= CAN_RTR_DATA; //数据帧 TxMessage_CAN2.IDE= CAN_ID_STD; //标准ID TxMessage_CAN2.DLC= 8;
CAN_ITConfig(CAN2,CAN_IT_FMP0, ENABLE); }
static void NVIC_Config_CAN2(void) {
NVIC_InitTypeDef NVIC_InitStructure_CAN2; //CAN2中断初始化 NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitStructure_CAN2.NVIC_IRQChannel = CAN2_RX0_IRQn; NVIC_InitStructure_CAN2.NVIC_IRQChannelPreemptionPriority= 0x0; NVIC_InitStructure_CAN2.NVIC_IRQChannelSubPriority = 0x00; NVIC_InitStructure_CAN2.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure_CAN2); }
int main(void) { inti=2;
Init_RxMes(&RxMessage_1);
Init_RxMes(&RxMessage_2);
NVIC_Config_CAN2();
CAN_Config_CAN2();
TxMessage_CAN2.Data[0]= 0x11; TxMessage_CAN2.Data[1]= 0x22; TxMessage_CAN2.Data[2]= 0x33; TxMessage_CAN2.Data[3]= 0x44; TxMessage_CAN2.Data[4]= 0x55; TxMessage_CAN2.Data[5]= 0x66; TxMessage_CAN2.Data[6]= 0x77; TxMessage_CAN2.Data[7]= 0x88;
CAN2_transmit_mailbox= CAN_Transmit(CAN2, &TxMessage_CAN2);
Status_CAN2_transmit =CAN_TransmitStatus(CAN2,CAN2_transmit_mailbox);
while (1) { TxMessage_CAN2.Data[0]= 0x11; TxMessage_CAN2.Data[1]= 0x22; TxMessage_CAN2.Data[2]= 0x33; TxMessage_CAN2.Data[3]= 0x44; TxMessage_CAN2.Data[4]= 0x55; TxMessage_CAN2.Data[5]= 0x66; TxMessage_CAN2.Data[6]= 0x77; TxMessage_CAN2.Data[7]= 0x88;
status_CAN2_FLAG=CAN_GetFlagStatus(CAN2,CAN_FLAG_FMP0); status_can2_IT =CAN_GetITStatus(CAN2,CAN_IT_FMP0); } }
|