完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
利用查询模式进行CAN的环回模式已经完成,给需要的朋友参考下
int main(void) { static GPIO_InitTypeDef GPIO_InitStruct; static CAN_HandleTypeDef CAN_HandleStruct; static CAN_FilterConfTypeDef CAN_FilterConfStruct; static CanTxMsgTypeDef CanTxMsgStruct; static CanRxMsgTypeDef CanRxMsgStruct; typedef unsigned char u8; typedef unsigned short int u16; typedef unsigned int u32; typedef unsigned __int64 u64; u8 Txbuf[8]; u8 Rxbuf[8]; u8 i; u8 cnt = 0; HAL_Init(); SystemClock_Config(); __HAL_RCC_CAN1_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitStruct.Alternate = GPIO_AF9_CAN; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); CAN_HandleStruct.Init.ABOM = DISABLE; CAN_HandleStruct.Init.AWUM = DISABLE; CAN_HandleStruct.Init.BS1 = CAN_BS1_7TQ; CAN_HandleStruct.Init.BS2 = CAN_BS2_6TQ; CAN_HandleStruct.Init.Mode = CAN_MODE_LOOPBACK; CAN_HandleStruct.Init.NART = ENABLE; CAN_HandleStruct.Init.Prescaler = 6; CAN_HandleStruct.Init.RFLM = DISABLE; CAN_HandleStruct.Init.SJW = DISABLE; CAN_HandleStruct.Init.TTCM = DISABLE; CAN_HandleStruct.Init.TXFP = DISABLE; CAN_HandleStruct.Instance = CAN; CAN_HandleStruct.Lock = HAL_UNLOCKED; CAN_HandleStruct.pRxMsg = &CanRxMsgStruct; CAN_HandleStruct.pTxMsg = &CanTxMsgStruct; HAL_CAN_Init(&CAN_HandleStruct); CAN_FilterConfStruct.BankNumber = 0; CAN_FilterConfStruct.FilterActivation = ENABLE; CAN_FilterConfStruct.FilterFIFOAssignment = CAN_FILTER_FIFO0; CAN_FilterConfStruct.FilterIdHigh = 0x0000; CAN_FilterConfStruct.FilterIdLow = 0x0000; CAN_FilterConfStruct.FilterMaskIdHigh = 0x0000; CAN_FilterConfStruct.FilterMaskIdLow = 0x0000; CAN_FilterConfStruct.FilterMode = CAN_FILTERMODE_IDMASK; CAN_FilterConfStruct.FilterNumber = 0; CAN_FilterConfStruct.FilterScale = CAN_FILTERSCALE_32BIT; HAL_CAN_ConfigFilter(&CAN_HandleStruct, &CAN_FilterConfStruct); while(1) { for(i=0;i<=8;i++) { Txbuf=cnt+i; } CanTxMsgStruct.DLC = 8; CanTxMsgStruct.ExtId = 0x12; CanTxMsgStruct.IDE = 0; CanTxMsgStruct.RTR = 0; CanTxMsgStruct.StdId = 0x12; for(i=0;i<8;i++) CanTxMsgStruct.Data=Txbuf; HAL_CAN_Transmit(&CAN_HandleStruct,0); HAL_CAN_Receive(&CAN_HandleStruct,CAN_FIFO0, 1000); for(i=0;i if(CanRxMsgStruct.DLC) { for(i=0;i<=8;i++) { printf("%dt", Rxbuf); } } HAL_Delay(1000); } } |
|
|
|
程序好大,20160929_Test_CAN20160929_Test_CANProjectsSTM32F334R8-NucleoTemplatesMDK-ARM
|
|
|
|
ke5627712 发表于 2018-11-6 16:37 没传上去 |
|
|
|
can基本一样,如果收不到,说明配置的问题。也可以用STM32CubeMX生成测试工程。
|
|
|
|
post_deleted
|
|
|
|
|
|
|
|
|
|
CUBE 很方便的
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1049 浏览 0 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1017 浏览 2 评论
2123 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1219 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1639 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-24 10:15 , Processed in 0.758953 second(s), Total 87, Slave 70 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号