最近在用107的板子在调试CAN,参考了网上的代码,用CAN1通讯,板子上对应的IO为PD0,PD1,已经remap,中断接收,轮循发送,自发自收没有问题,NORMAL模式下就无法收到CAN盒的数据帧,CAN盒也收不到板子发送的数据帧(追踪到发送状态一直是CAN_TxStatus_Pending),用示波器量过,发送和接收时总线上都是有差分信号出来的,检查了代码,发现好像也没啥问题,求指正! void can_configuration(void) { CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* GPIOB Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD , ENABLE); /* Enable CAN1 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 , ENABLE); NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Configure CAN1 pin(PB5): RX */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOD, &GPIO_InitStructure); /* Configure CAN1 pin(PB6): TX */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); /* Enable the USART2 Pins Software Remapping */ GPIO_PinRemapConfig(GPIO_Remap2_CAN1, ENABLE); /* CAN1 register init */ CAN_DeInit(CAN1); 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 = ENABLE; CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; CAN_InitStructure.CAN_BS1 = CAN_BS1_4tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq; CAN_InitStructure.CAN_Prescaler = 9; //500kHz CAN_Init(CAN1,&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=0x0000; CAN_FilterInitStructure.CAN_FilterIdLow=0x0000; CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000; CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000; CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0; CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; CAN_FilterInit(&CAN_FilterInitStructure); CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE); } 发送: void CanWriteData(void) { CanTxMsg TxMessage; /* transmit */ TxMessage.StdId = 0x55; TxMessage.RTR = CAN_RTR_DATA; TxMessage.IDE = CAN_ID_STD; TxMessage.DLC = 8; TxMessage.Data[0] = 0x55; TxMessage.Data[1] = 0x55; TxMessage.Data[2] = 0x55; TxMessage.Data[3] = 0x55; TxMessage.Data[4] = 0x55; TxMessage.Data[5] = 0x55; TxMessage.Data[6] = 0x55; TxMessage.Data[7] = 0x55; CAN_Transmit(CAN1,&TxMessage); } 接收: void CAN1_RX0_IRQHandler(void) { CanRxMsg RxMessage; CAN_Receive(CAN1,CAN_FIFO0, &RxMessage); recv_frame.id = RxMessage.StdId; recv_frame.data_buff[0] = RxMessage.Data[0]; recv_frame.data_buff[1] = RxMessage.Data[1]; recv_frame.data_buff[2] = RxMessage.Data[2]; recv_frame.data_buff[3] = RxMessage.Data[3]; recv_frame.data_buff[4] = RxMessage.Data[4]; recv_frame.data_buff[5] = RxMessage.Data[5]; recv_frame.data_buff[6] = RxMessage.Data[6]; recv_frame.data_buff[7] = RxMessage.Data[7]; can_recv_flag = ENABLE; } |
【MCU实战经验】基于STM32F103C8T6的hart总线调试器设计
求教STM32F103进入STOP模式后用外部中断唤醒的问题
基于STM32F103RCT6的无源蜂鸣器音乐播放(生日快乐歌)
STM32F103c8t6有没有DAC 功能?
STM32F103x中文数据手册
新手求教,为何在我电脑上找不到STM32F1Xx.h文件
金龙107例程汇总(STM32F107)
万利STM32F107VC 原理图
STM32F103 ADC多通道检测必须要DMA吗?
【官方例程】STM32F107以太网官方例程
RE:STM32F107 CAN收发问题(求助!)
RE:STM32F107 CAN收发问题(求助!)
RE:STM32F107 CAN收发问题(求助!)
RE:STM32F107 CAN收发问题(求助!)
我是107的板子和周立功的USBCAN-II作通讯
波特率在这几句中设置了的
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_4tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;
CAN_InitStructure.CAN_Prescaler = 9; //500kHz
/* CANbps= Fpclk/((BRP+1)*((Tseg1+1)+(Tseg2+1)+1) 此处计算为 CANbps=36000000/(9*(4+3+1))=500kHz */
回复:STM32F107 CAN收发问题(求助!)
接收数据长度是按标准帧来的,应该不用设置吧,现在是接收中断都没有进。。。
我是107的板子和周立功的USBCAN-II作通讯
波特率在这几句中设置了的
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_4tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;
CAN_InitStructure.CAN_Prescaler = 9; //500kHz
/* CANbps= Fpclk/((BRP+1)*((Tseg1+1)+(Tseg2+1)+1) 此处计算为 CANbps=36000000/(9*(4+3+1))=500kHz */
你现在就是中断不能进去吗,那就是中断设置的问题,你看看看中断都使能了,中断入口正确吗
回复:STM32F107 CAN收发问题(求助!)
RE:STM32F107 CAN收发问题(求助!)
请教位填充错误是哪里设置错误?如何解决?
RE:STM32F107 CAN收发问题(求助!)
RE:STM32F107 CAN收发问题(求助!)
有解决吗,我也碰到同样的问题
先LOOP BACK模式验证下基本软件配置,包括中断配置;
NORMAL模式下注意两边波特率、匹配电阻、硬件连接等。还以ST都有官方参考代码,多参考下,事半功倍。