本帖最后由 power568 于 2016-4-2 13:44 编辑 在使用STM32F405调试CAN的时,使用官方的例程的环回模式时,数据收发都可以,但是将环回模式更改为正常模式,且接上CAN卡想抓数据时,发现发送数据失败(超时), CAN初始化代码如下: void CanComInit(void) { CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; //// CAN register init CAN_DeInit(CAN_ONE_PERIPH_PORT); //// 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_Mode_LoopBack; CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; //// Baudrate (CAN clocked at 42 MHz) //// Baudrate = 42 MHz/((1+BS1+BS2)*CAN_Prescaler) CAN_InitStructure.CAN_BS1 = CAN_BS1_13tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq; CAN_InitStructure.CAN_Prescaler = 2; CAN_Init(CAN_ONE_PERIPH_PORT, &CAN_InitStructure); CAN_FilterInitStructure.CAN_FilterNumber = 0; CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0020; //// StdId = 0x01 CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000; CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x003F; CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF; CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0; CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; CAN_FilterInit(&CAN_FilterInitStructure); test = 1; while(1) { //// transmit TxMessage.StdId = test; TxMessage.RTR = CAN_RTR_DATA; TxMessage.IDE = CAN_ID_STD; TxMessage.DLC = 2; TxMessage.Data[0] = 0xCA; TxMessage.Data[1] = 0xFf; TransmitMailbox = CAN_Transmit(CAN_ONE_PERIPH_PORT, &TxMessage); num = 0; while((CAN_TransmitStatus(CAN_ONE_PERIPH_PORT, TransmitMailbox) != CANTXOK) && (num != 0xFFFFF)) { num++; } //// num = 0; //// while((CAN_MessagePending(CAN_ONE_PERIPH_PORT, CAN_FIFO0) < 1) && (num != 0xFFFF)) //// { //// num++; //// } //// //// receive //// RxMessage.StdId = 0x00; //// RxMessage.IDE = CAN_ID_STD; //// RxMessage.DLC = 0; //// RxMessage.Data[0] = 0x00; //// RxMessage.Data[1] = 0x00; //// CAN_Receive(CAN_ONE_PERIPH_PORT, CAN_FIFO0, &RxMessage); //// if (RxMessage.StdId != 0x11) //// { //// RxMessage.StdId = 0xFF; //// } } } 发送后,num == 0xFFFFF 退出循环... 代码只是将环回模式更改为正常模式,其余没有更改,不知道为什么??? 求指点... |
楼上的说的对,波特率设置错误,应该是:CAN_InitStructure.CAN_BS1 = CAN_BS1_12tq; |
STM32F407 定时器触发DMA 求助大神
【MCU实战经验】基于STM32F407的音频播放器设计
盘古UE-STM32F407工控板原理图
【STM32F429心得\疑问】+STM32F4之FSMC和FMC
STM32F429读取IO口传输的数据速率
STM32F407ZGT6 手摸芯片背部重启
STM32F4 SPI 动作时,软件片选信号被拉高,IO口程序逻辑失控
STM32F401RE NUCLEO求助,串口一直不能进中断
读取STM32F407内部温度传感器值错误
STM32F429多路内部ADC独立采集的办法