本帖最后由 清风烈酒 于 2018-1-17 09:46 编辑
使用stm32f107芯片库函数,配置环回模式能够正常发送接收;
修改为正常模式,用单步调试查看到程序运行到这条语句 CANx->sTxMailBox[transmit_mailbox].TIR |= TMIDxR_TXRQ;
用示波器测试Tx引脚没有波形输出,发送三次后三个邮箱都占满了便不会发送数据了。
接收采用中断函数void USB_LP_CAN1_RX0_IRQHandler(void)接收。用CANalyst 发送数据下来,从单片机引脚Rx上能够检测到CANalyst发送过来数据的波形,但是程序中断函数没有触发。
不知道大家有没有遇到过这种情况,查了很久都不知道出现了什么问题。
请各位大神帮忙!!!谢谢!!!
- extern CanTxMsg TxMessage;
- extern CanRxMsg RxMessage;
-
-
- void GPIO_CAN_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);
-
- GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE);
-
- /* Configure CAN pin: RX */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- 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 CAN_Interrupt(void)
- {
- CAN_InitTypeDef CAN_InitStructure;
- CAN_FilterInitTypeDef CAN_FilterInitStructure;
-
- /* CAN register init */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); //使能CAN1时钟
- CAN_DeInit(CAN1);
- CAN_StructInit(&CAN_InitStructure);
-
- /* CAN cell init */
- CAN_InitStructure.CAN_TTCM = DISABLE; // 时间触发通信禁止
- CAN_InitStructure.CAN_ABOM = DISABLE; // 离线退出是在中断置位清0后退出
- CAN_InitStructure.CAN_AWUM = DISABLE; // 自动唤醒模式:清零sleep
-
- 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_Mode = CAN_Mode_LoopBack;
-
- CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; // 1tq、BS1、BS2的值跟波特率有关
- CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq;
- CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
- CAN_InitStructure.CAN_Prescaler=4; // 分频系数为1
-
-
- CAN_Init(CAN1, &CAN_InitStructure); // 初始化CAN1
-
- /* 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 = 0x0000;
- CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
- CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
- CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FilterFIFO0;
- CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
- CAN_FilterInit(&CAN_FilterInitStructure);
-
-
- /* CAN FIFO0 message pending interrupt enable */
- CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE); //中断使能
-
- }
-
-
-
- static void CAN_NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
-
- /* Enable CAN1 RX0 interrupt IRQ channel */
- NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 主优先级为1
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 次优先级为0
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
-
-
- void CAN_Config(void)
- {
- GPIO_CAN_Configuration();
- CAN_NVIC_Configuration();
- CAN_Interrupt();
- }
-
-
- u8 CAN_SetMsg(void)
- {
- u8 mailBox;
- u16 i = 0;
-
- TxMessage.StdId=0x00; //ID标识符
- TxMessage.RTR = CAN_RTR_DATA; //数据帧
- TxMessage.IDE = CAN_ID_STD; //标准帧格式
- TxMessage.DLC=8; //帧长度,单位:字节
-
- TxMessage.Data[0] = 0xF1; //待传数据
- TxMessage.Data[1] = 0xF2;
- TxMessage.Data[2] = 0xF3;
- TxMessage.Data[3] = 0xF4;
- TxMessage.Data[4] = 0xF5;
- TxMessage.Data[5] = 0xF6;
- TxMessage.Data[6] = 0xF7;
- TxMessage.Data[7] = 0xF8;
-
- mailBox = CAN_Transmit(CAN1, &TxMessage);
-
- while((CAN_TransmitStatus(CAN1, mailBox)==CANTXFAILED) && (i != 0XFF))
- {
- i++; //等待发送结束
- }
- }
-
-
- void USB_LP_CAN1_RX0_IRQHandler(void)
- {
- RxMessage.StdId=0x00;
- RxMessage.IDE=CAN_ID_STD;
- RxMessage.DLC=0;
- RxMessage.FMI=0;
- RxMessage.Data[0]=0x00;
- RxMessage.Data[1]=0x00;
- RxMessage.Data[2]=0x00;
- RxMessage.Data[3]=0x00;
- RxMessage.Data[4]=0x00;
- RxMessage.Data[5]=0x00;
- RxMessage.Data[6]=0x00;
- RxMessage.Data[7]=0x00;
-
- CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
-
- //LED灯闪烁
- if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_1))
- {
- GPIO_ResetBits(GPIOA,GPIO_Pin_1);
- }else
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_1);
- }
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
宏定义那里,之前是STM32F10X_HD,后面改成STM32F10X_CL就好了
- NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
复制代码楼主使用F107的话那么上面这个中断就不对了,你使用的是FIFO0,那么中断应该是
- CAN1_RX0_IRQn = 20,
复制代码一周热门 更多>