完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
各位大神
我是STM8的新手,最近买了两套STM8A-DISCOVERY开发板,基于STM8AF52芯片开发,原本想着将两块开发板的CAN口对接,进行一些通信测试,但发现无法实现CAN通信,一下是我的代码,请各位大神指点指点: 主循环中的实现: main() { .... CLK_Config(); CAN_Config(); .... while(1) { ... /* Transmit Parameters*/ Tx_Id = 0; Tx_IDE = CAN_Id_Standard; Tx_RTR = CAN_RTR_Data; Tx_DLC = 1; Tx_Data[0] = i; GPIO_WriteLow(GPIOA, (GPIO_Pin_TypeDef)GPIO_PIN_3); /* Sender send Frame */ status = CAN_Transmit(Tx_Id,Tx_IDE,Tx_RTR,Tx_DLC,Tx_Data); ... } } void CLK_Config(void) { /* Initialization of the clock */ /* Clock divider to HSI/1 */ CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); } void CAN_Config(void) { CAN_InitStatus_TypeDef status = CAN_InitStatus_Failed; /* Filter Parameters */ CAN_FilterNumber_TypeDef CAN_FilterNumber; FunctionalState CAN_FilterActivation; CAN_FilterMode_TypeDef CAN_FilterMode; CAN_FilterScale_TypeDef CAN_FilterScale; UINT8 CAN_FilterID1; UINT8 CAN_FilterID2; UINT8 CAN_FilterID3; UINT8 CAN_FilterID4; UINT8 CAN_FilterIDMask1; UINT8 CAN_FilterIDMask2; UINT8 CAN_FilterIDMask3; UINT8 CAN_FilterIDMask4; /* Init Parameters*/ CAN_MasterCtrl_TypeDef CAN_MasterCtrl; CAN_Mode_TypeDef CAN_Mode; CAN_SynJumpWidth_TypeDef CAN_SynJumpWidth; CAN_BitSeg1_TypeDef CAN_BitSeg1; CAN_BitSeg2_TypeDef CAN_BitSeg2; UINT8 CAN_Prescaler; /* CAN register init */ CAN_DeInit(); /* CAN init */ CAN_MasterCtrl=CAN_MasterCtrl_AllDisabled; CAN_Mode = CAN_Mode_Normal; CAN_SynJumpWidth = CAN_SynJumpWidth_1TimeQuantum; CAN_BitSeg1 = CAN_BitSeg1_8TimeQuantum; CAN_BitSeg2 = CAN_BitSeg2_7TimeQuantum; CAN_Prescaler = 1; status = CAN_Init(CAN_MasterCtrl, CAN_Mode, CAN_SynJumpWidth, CAN_BitSeg1, CAN_BitSeg2, CAN_Prescaler); /* CAN filter init */ CAN_FilterNumber = CAN_FilterNumber_0; CAN_FilterActivation = ENABLE; CAN_FilterMode = CAN_FilterMode_IdMask; CAN_FilterScale = CAN_FilterScale_32Bit; CAN_FilterID1=0; CAN_FilterID2=0; CAN_FilterID3=0; CAN_FilterID4=0; CAN_FilterIDMask1=0; CAN_FilterIDMask2=0; CAN_FilterIDMask3=0; CAN_FilterIDMask4=0; CAN_FilterInit(CAN_FilterNumber, CAN_FilterActivation, CAN_FilterMode, CAN_FilterScale,CAN_FilterID1, CAN_FilterID2, CAN_FilterID3, CAN_FilterID4,CAN_FilterIDMask1, CAN_FilterIDMask2, CAN_FilterIDMask3, CAN_FilterIDMask4); /* Enable Fifo message pending interrupt*/ /* Message reception is done by CAN_RX ISR*/ CAN_ITConfig(CAN_IT_FMP, ENABLE); } 中断配置如下: /** * @brief CAN RX Interrupt routine. * @param None * @retval None */ INTERRUPT_HANDLER(CAN_RX_IRQHandler, 8) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ /* Receiver Receives Frame */ CAN_Receive(); } 同时在stm8_interrupt_vector.c文件中的中断向量表里修改如下: {0x82, (interrupt_handler_t)CAN_RX_IRQHandler}, /* irq8 */ 将两块开发板CAN口对接,然后我通过调试器将断点设置在中断响应函数的CAN_Receive()一行,无任何中断响应。请问,这个CAN通信的问题有可能出在哪里? |
|
相关推荐
1个回答
|
|
首先,我们需要确保您的STM8AF开发板的CAN通信配置是正确的。以下是一些建议和步骤,以帮助您解决无法实现CAN通信的问题:
1. 检查硬件连接:确保两块开发板的CAN口正确连接,并且没有短路或断路的情况。 2. 检查CAN配置:确保您的CAN配置正确,包括波特率、模式(基本或扩展帧)、过滤器等。以下是一些常见的CAN配置参数: a. CAN波特率:确保两个开发板的波特率设置相同。 b. CAN模式:选择基本帧或扩展帧,并确保两个开发板的设置相同。 c. CAN过滤器:如果使用过滤器,请确保它们配置正确,以便正确接收和发送消息。 3. 初始化CAN:在主函数中,确保您已经正确初始化了CAN模块。以下是初始化CAN模块的示例代码: ```c void CAN_Config(void) { CAN_InitTypeDef CAN_InitStructure; /* CAN clock enable */ CAN_CLK_ENABLE(); /* CAN GPIO configuration */ // ... /* CAN peripheral reset */ CAN_DeInit(CAN1); /* CAN configuration */ 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_Init(CAN1, &CAN_InitStructure); /* CAN filters configuration */ // ... /* CAN interrupt enable */ CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE); /* CAN start */ CAN_Cmd(CAN1, ENABLE); } ``` 4. 发送和接收消息:确保您的发送和接收消息的代码是正确的。以下是发送和接收消息的示例代码: ```c void CAN_Send_Message(CAN_TypeDef* CANx, CAN_TxMsg_TypeDef* TxMessage) { CAN_TxStatus_TypeDef TxStatus; TxStatus = CAN_TransmitStatus(CANx, CAN_TxMailbox_0); if (TxStatus == CAN_TxStatus_Failed) { TxStatus = CAN_TransmitStatus(CANx, CAN_TxMailbox_1); } if (TxStatus == CAN_TxStatus_Failed) { TxStatus = CAN_TransmitStatus(CANx, CAN_TxMailbox_2); } if (TxStatus == CAN_TxStatus_Empty) { CAN_Transmit(CANx, TxMessage); } } void CAN_Receive_Message(CAN_TypeDef* CANx, CAN_RxMsg_TypeDef* RxMessage) { CAN_FIFO_TypeDef CAN_FIFONumber = CAN_FIFO0; if (CAN_MessagePending(CANx, CAN_FIFO0) != 0) { CAN_Receive(CANx, CAN_FIFO0, RxMessage); } else if (CAN_MessagePending(CANx, CAN_FIFO1) != 0) { CAN_Receive(CANx, CAN_FIFO1, RxMessage); } } ``` 5. 检查中断:确保您的CAN中断配置正确,并且在中断服务例程中正确处理了发送和接收的消息。 6. 使用示波器或逻辑分析仪:如果以上步骤都无法解决问题,您可以尝试使用示波器或逻辑分析仪来检查CAN总线上的信号,以确定问题是否出在硬件层面。 7. 更新固件库:确保您使用的STM8固件库是最新版本,以避免可能的已知问题。 |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1771 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1619 浏览 1 评论
1070 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
724 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1673 浏览 2 评论
1936浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
729浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
569浏览 3评论
594浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
552浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-22 19:38 , Processed in 0.967687 second(s), Total 77, Slave 60 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号