完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
大家好,我用的是STM32F042F6,TSSOP20芯片,CAN引脚为PA11,PA12(与PA9,PA10复用)
当设置为CAN_Mode_LoopBack时,可以在用示波器在TX引脚看到波形,并且程序里可以接收到数据; 当设置为CAN_Mode_Normal时,TX引脚看不到波形,并且也不能接受发送CAN数据。 代码如下,请大家帮忙看看,谢谢!! void CAN1_Init(u16 Baudrate,u16 Receive_MsgId) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; /* CAN1 Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); SYSCFG->CFGR1 |= (uint32_t)SYSCFG_CFGR1_PA11_PA12_RMP; GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_4); GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_4); /* Configure CAN pin: RX */ // PA11 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, GPIO_InitStructure); /* Configure CAN pin: TX */ // PA12 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, GPIO_InitStructure); /* enabling interrupt */ NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn;; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( NVIC_InitStructure); //CAN_INIT();//CA???N?? /* CAN register init */ // CAN_DeInit(CAN); // 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 = DISABLE; CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;//CAN_Mode_LoopBack; // 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=6; CAN_Init(CAN, CAN_InitStructure); CAN_FilterInitStructure.CAN_FilterNumber = 1; CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList; CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; CAN_FilterInitStructure.CAN_FilterIdHigh= (((u32)Receive_MsgId<<21) 0xFFFF0000)>>16; // CAN_FilterInitStructure.CAN_FilterIdHigh=0X00; //????ID?? CAN_FilterInitStructure.CAN_FilterIdLow= (((u32)Receive_MsgId<<21)|CAN_ID_STD|CAN_RTR_DATA) 0xFFFF; //????ID?? CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF; CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF; CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; CAN_FilterInit( CAN_FilterInitStructure); /* CAN FIFO0 message pending interrupt enable */ CAN_ITConfig(CAN,CAN_IT_FMP0, ENABLE); } |
|
相关推荐
1个回答
|
|
正常模式下,CAN是带纠错的,需要你两端的设备都正常连接才行。
否则你对面设备没接,单片机CAN外设就认为通信出错了,也就不会再正常发送了。 所以你把另一边的CAN设备接上再测测。 |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1614 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1541 浏览 1 评论
970 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
682 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1592 浏览 2 评论
1863浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
644浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
515浏览 3评论
531浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
504浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 00:55 , Processed in 0.728137 second(s), Total 79, Slave 62 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号