>16;//要过滤的ID高位 //CAN_FilterInitStructure.CAN_FilterIdLow=(((u32)0x1314<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;//要过滤的ID低位 //CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0xFFFF;//过滤器高16位每位必须匹配//CAN_FilterInitStructure.CAN_FilterMaskIdLow=0xFFFF;//过滤器低16位每位必须匹配CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;CAN_FilterInitStructure.CAN_FilterIdLow=0x0000; CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//过滤器被关联到FIFO0CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//使能过滤器CAN_FilterInit(&CAN_FilterInitStructure);/*CAN通信中断使能*/CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);}/* *函数名:CAN_Config *描述 :完整配置CAN的功能 *输入 :无 *输出 :无 *调用 :外部调用 */voidCAN_Config(void){CAN_GPIO_Config();CAN_NVIC_Config();CAN_Mode_Config();CAN_Filter_Config();/*设置要通过CAN发送的信息*/CAN_TxMsgInit();//CAN_SetMsg();}/* *函数名:CAN_SetMsg *描述 :CAN通信报文内容设置 *输入 :无 *输出 :无 *调用 :外部调用 */ __IOuint32_tflag=0xff;;//用于标志是否接收到数据,在中断函数中赋值CanTxMsgTxMessage;//发送缓冲区定义发送的CAN数据帧信息CanRxMsgRxMessage;//接收缓冲区定义接受到的CAN数据帧信息//CanTxMsgTxMsg_VCU01;voidCAN_SetMsg(void){  TxMessage.StdId=0x12; TxMessage.ExtId=0x00;//使用的扩展ID//TxMessage.IDE=CAN_ID_EXT;//扩展模式TxMessage.IDE=CAN_Id_Standard;//标准模式TxMessage.RTR=CAN_RTR_DATA;//发送的是数据TxMessage.DLC=2;//数据长度为2字节TxMessage.Data[0]=0xAB;TxMessage.Data[1]=0xCD;}/**************************ENDOFFILE************************************/【文件目录】└─STM32-16-CAN-正常模式YMXB-0402  │ EventRecorderStub.scvd  │ LED.uvguix.Administrator  │ LED.uvoptx  │ LED.uvprojx  │    ├─CMSIS  │ │ core_cm3.c  │ │ core_cm3.h  │ │ startup_stm32f10x_hd.s  │ │ stm32f10x.h  │ │ system_stm32f10x.c  │ │ system_stm32f10x.h  │ │    │ ├─CoreSupport  │ │   core_cm3.c  │ │   core_cm3.h  │ │      │ └─DeviceSupport  │   └─ST  │     └─STM32F10x  │       │ stm32f10x.h  │       │ system_stm32f10x.c  │       │ system_stm32f10x.h  │       │    │       └─startup  │         ├─arm  │         │   startup_stm32f10x_cl.s  │         │   startup_stm32f10x_hd.s  │         │   startup_stm32f10x_hd_vl.s  │         │   startup_stm32f10x_ld.s  │         │   startup_stm32f10x_ld_vl.s  │         │   startup_stm32f10x_md.s  │         │   startup_stm32f10x_md_vl.s  │         │   startup_stm32f10x_xl.s  │         │      │         ├─gcc_ride7  │         │   startup_stm32f10x_cl.s  │         │   startup_stm32f10x_hd.s  │         │   startup_stm32f10x_hd_vl.s  │         │   startup_stm32f10x_ld.s  │         │   startup_stm32f10x_ld_vl.s  │         │   startup_stm32f10x_md.s  │         │   startup_stm32f10x_md_vl.s  │         │   startup_stm32f10x_xl.s  │         │      │         ├─iar  │         │   startup_stm32f10x_cl.s  │         │   startup_stm32f10x_hd.s  │         │   startup_stm32f10x_hd_vl.s  │         │   startup_stm32f10x_ld.s  │         │   startup_stm32f10x_ld_vl.s  │         │   startup_stm32f10x_md.s  │         │   startup_stm32f10x_md_vl.s  │         │   startup_stm32f10x_xl.s  │         │      │         └─TrueSTUDIO  │             startup_stm32f10x_cl.s  │             startup_stm32f10x_hd.s  │             startup_stm32f10x_hd_vl.s  │             startup_stm32f10x_ld.s  │             startup_stm32f10x_ld_vl.s  │             startup_stm32f10x_md.s  │             startup_stm32f10x_md_vl.s  │             startup_stm32f10x_xl.s  │                ├─DebugConfig  │   LED_STM32F103RC_1.0.0.dbgconf  │   STM32_ufun_STM32F103RC_1.0.0.dbgconf  │   Target_1_STM32F103RC_1.0.0.dbgconf  │      ├─FWlib  │ ├─inc  │ │   misc.h  │ │   stdarg.h  │ │   stm32f10x_adc.h  │ │   stm32f10x_bkp.h  │ │   stm32f10x_can.h  │ │   stm32f10x_cec.h  │ │   stm32f10x_crc.h  │ │   stm32f10x_dac.h  │ │   stm32f10x_dbgmcu.h  │ │   stm32f10x_dma.h  │ │   stm32f10x_exti.h  │ │   stm32f10x_flash.h  │ │   stm32f10x_fsmc.h  │ │   stm32f10x_gpio.h  │ │   stm32f10x_i2c.h  │ │   stm32f10x_iwdg.h  │ │   stm32f10x_pwr.h  │ │   stm32f10x_rcc.h  │ │   stm32f10x_rtc.h  │ │   stm32f10x_sdio.h  │ │   stm32f10x_spi.h  │ │   stm32f10x_tim.h  │ │   stm32f10x_usart.h  │ │   stm32f10x_wwdg.h  │ │      │ └─src  │     misc.c  │     stm32f10x_adc.c  │     stm32f10x_bkp.c  │     stm32f10x_can.c  │     stm32f10x_cec.c  │     stm32f10x_crc.c  │     stm32f10x_dac.c  │     stm32f10x_dbgmcu.c  │     stm32f10x_dma.c  │     stm32f10x_exti.c  │     stm32f10x_flash.c  │     stm32f10x_fsmc.c  │     stm32f10x_gpio.c  │     stm32f10x_i2c.c  │     stm32f10x_iwdg.c  │     stm32f10x_pwr.c  │     stm32f10x_rcc.c  │     stm32f10x_rtc.c  │     stm32f10x_sdio.c  │     stm32f10x_spi.c  │     stm32f10x_tim.c  │     stm32f10x_usart.c  │     stm32f10x_wwdg.c  │        ├─Listing  │   LED.map  │   startup_stm32f10x_hd.lst  │      ├─Listings  ├─Objects  ├─Output  │   can.crf  │   can.d  │   can.o  │   canmsg.crf  │   canmsg.d  │   canmsg.o  │   core_cm3.crf  │   core_cm3.d  │   core_cm3.o  │   delay.crf  │   delay.d  │   delay.o  │   ExtDll.iex  │   exti.crf  │   exti.d  │   exti.o  │   LED.axf  │   LED.build_log.htm  │   led.crf  │   led.d  │   LED.hex  │   LED.htm  │   LED.lnp  │   led.o  │   LED.sct  │   LED_STM32_ufun.dep  │   main.crf  │   main.d  │   main.o  │   misc.crf  │   misc.d  │   misc.o  │   startup_stm32f10x_hd.d  │   startup_stm32f10x_hd.o  │   stm32f10x_adc.crf  │   stm32f10x_adc.d  │   stm32f10x_adc.o  │   stm32f10x_bkp.crf  │   stm32f10x_bkp.d  │   stm32f10x_bkp.o  │   stm32f10x_can.crf  │   stm32f10x_can.d  │   stm32f10x_can.o  │   stm32f10x_cec.crf  │   stm32f10x_cec.d  │   stm32f10x_cec.o  │   stm32f10x_crc.crf  │   stm32f10x_crc.d  │   stm32f10x_crc.o  │   stm32f10x_dac.crf  │   stm32f10x_dac.d  │   stm32f10x_dac.o  │   stm32f10x_dbgmcu.crf  │   stm32f10x_dbgmcu.d  │   stm32f10x_dbgmcu.o  │   stm32f10x_dma.crf  │   stm32f10x_dma.d  │   stm32f10x_dma.o  │   stm32f10x_exti.crf  │   stm32f10x_exti.d  │   stm32f10x_exti.o  │   stm32f10x_flash.crf  │   stm32f10x_flash.d  │   stm32f10x_flash.o  │   stm32f10x_fsmc.crf  │   stm32f10x_fsmc.d  │   stm32f10x_fsmc.o  │   stm32f10x_gpio.crf  │   stm32f10x_gpio.d  │   stm32f10x_gpio.o  │   stm32f10x_i2c.crf  │   stm32f10x_i2c.d  │   stm32f10x_i2c.o  │   stm32f10x_it.crf  │   stm32f10x_it.d  │   stm32f10x_it.o  │   stm32f10x_iwdg.crf  │   stm32f10x_iwdg.d  │   stm32f10x_iwdg.o  │   stm32f10x_pwr.crf  │   stm32f10x_pwr.d  │   stm32f10x_pwr.o  │   stm32f10x_rcc.crf  │   stm32f10x_rcc.d  │   stm32f10x_rcc.o  │   stm32f10x_rtc.crf  │   stm32f10x_rtc.d  │   stm32f10x_rtc.o  │   stm32f10x_sdio.crf  │   stm32f10x_sdio.d  │   stm32f10x_sdio.o  │   stm32f10x_spi.crf  │   stm32f10x_spi.d  │   stm32f10x_spi.o  │   stm32f10x_tim.crf  │   stm32f10x_tim.d  │   stm32f10x_tim.o  │   stm32f10x_usart.crf  │   stm32f10x_usart.d  │   stm32f10x_usart.o  │   stm32f10x_wwdg.crf  │   stm32f10x_wwdg.d  │   stm32f10x_wwdg.o  │   system_stm32f10x.crf  │   system_stm32f10x.d  │   system_stm32f10x.o  │   timer.crf  │   timer.d  │   timer.o  │   usart1.crf  │   usart1.d  │   usart1.o  │      └─USER      can.c      can.h      CanMsg.c      CanMsg.h      cantxmsg.c      delay.c      delay.h      exti.c      exti.h      key.c      key.h      led.c      led.h      main.c      stm32f10x_conf.h      stm32f10x_it.c      stm32f10x_it.h      timer.c      timer.h      ufun_conf.h      usart1.c      usart1.h       -IMDN开发者社群-imdn.cn"> >16;//要过滤的ID高位 //CAN_FilterInitStructure.CAN_FilterIdLow=(((u32)0x1314<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;//要过滤的ID低位 //CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0xFFFF;//过滤器高16位每位必须匹配//CAN_FilterInitStructure.CAN_FilterMaskIdLow=0xFFFF;//过滤器低16位每位必须匹配CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;CAN_FilterInitStructure.CAN_FilterIdLow=0x0000; CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//过滤器被关联到FIFO0CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//使能过滤器CAN_FilterInit(&CAN_FilterInitStructure);/*CAN通信中断使能*/CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);}/* *函数名:CAN_Config *描述 :完整配置CAN的功能 *输入 :无 *输出 :无 *调用 :外部调用 */voidCAN_Config(void){CAN_GPIO_Config();CAN_NVIC_Config();CAN_Mode_Config();CAN_Filter_Config();/*设置要通过CAN发送的信息*/CAN_TxMsgInit();//CAN_SetMsg();}/* *函数名:CAN_SetMsg *描述 :CAN通信报文内容设置 *输入 :无 *输出 :无 *调用 :外部调用 */ __IOuint32_tflag=0xff;;//用于标志是否接收到数据,在中断函数中赋值CanTxMsgTxMessage;//发送缓冲区定义发送的CAN数据帧信息CanRxMsgRxMessage;//接收缓冲区定义接受到的CAN数据帧信息//CanTxMsgTxMsg_VCU01;voidCAN_SetMsg(void){  TxMessage.StdId=0x12; TxMessage.ExtId=0x00;//使用的扩展ID//TxMessage.IDE=CAN_ID_EXT;//扩展模式TxMessage.IDE=CAN_Id_Standard;//标准模式TxMessage.RTR=CAN_RTR_DATA;//发送的是数据TxMessage.DLC=2;//数据长度为2字节TxMessage.Data[0]=0xAB;TxMessage.Data[1]=0xCD;}/**************************ENDOFFILE************************************/【文件目录】└─STM32-16-CAN-正常模式YMXB-0402  │ EventRecorderStub.scvd  │ LED.uvguix.Administrator  │ LED.uvoptx  │ LED.uvprojx  │    ├─CMSIS  │ │ core_cm3.c  │ │ core_cm3.h  │ │ startup_stm32f10x_hd.s  │ │ stm32f10x.h  │ │ system_stm32f10x.c  │ │ system_stm32f10x.h  │ │    │ ├─CoreSupport  │ │   core_cm3.c  │ │   core_cm3.h  │ │      │ └─DeviceSupport  │   └─ST  │     └─STM32F10x  │       │ stm32f10x.h  │       │ system_stm32f10x.c  │       │ system_stm32f10x.h  │       │    │       └─startup  │         ├─arm  │         │   startup_stm32f10x_cl.s  │         │   startup_stm32f10x_hd.s  │         │   startup_stm32f10x_hd_vl.s  │         │   startup_stm32f10x_ld.s  │         │   startup_stm32f10x_ld_vl.s  │         │   startup_stm32f10x_md.s  │         │   startup_stm32f10x_md_vl.s  │         │   startup_stm32f10x_xl.s  │         │      │         ├─gcc_ride7  │         │   startup_stm32f10x_cl.s  │         │   startup_stm32f10x_hd.s  │         │   startup_stm32f10x_hd_vl.s  │         │   startup_stm32f10x_ld.s  │         │   startup_stm32f10x_ld_vl.s  │         │   startup_stm32f10x_md.s  │         │   startup_stm32f10x_md_vl.s  │         │   startup_stm32f10x_xl.s  │         │      │         ├─iar  │         │   startup_stm32f10x_cl.s  │         │   startup_stm32f10x_hd.s  │         │   startup_stm32f10x_hd_vl.s  │         │   startup_stm32f10x_ld.s  │         │   startup_stm32f10x_ld_vl.s  │         │   startup_stm32f10x_md.s  │         │   startup_stm32f10x_md_vl.s  │         │   startup_stm32f10x_xl.s  │         │      │         └─TrueSTUDIO  │             startup_stm32f10x_cl.s  │             startup_stm32f10x_hd.s  │             startup_stm32f10x_hd_vl.s  │             startup_stm32f10x_ld.s  │             startup_stm32f10x_ld_vl.s  │             startup_stm32f10x_md.s  │             startup_stm32f10x_md_vl.s  │             startup_stm32f10x_xl.s  │                ├─DebugConfig  │   LED_STM32F103RC_1.0.0.dbgconf  │   STM32_ufun_STM32F103RC_1.0.0.dbgconf  │   Target_1_STM32F103RC_1.0.0.dbgconf  │      ├─FWlib  │ ├─inc  │ │   misc.h  │ │   stdarg.h  │ │   stm32f10x_adc.h  │ │   stm32f10x_bkp.h  │ │   stm32f10x_can.h  │ │   stm32f10x_cec.h  │ │   stm32f10x_crc.h  │ │   stm32f10x_dac.h  │ │   stm32f10x_dbgmcu.h  │ │   stm32f10x_dma.h  │ │   stm32f10x_exti.h  │ │   stm32f10x_flash.h  │ │   stm32f10x_fsmc.h  │ │   stm32f10x_gpio.h  │ │   stm32f10x_i2c.h  │ │   stm32f10x_iwdg.h  │ │   stm32f10x_pwr.h  │ │   stm32f10x_rcc.h  │ │   stm32f10x_rtc.h  │ │   stm32f10x_sdio.h  │ │   stm32f10x_spi.h  │ │   stm32f10x_tim.h  │ │   stm32f10x_usart.h  │ │   stm32f10x_wwdg.h  │ │      │ └─src  │     misc.c  │     stm32f10x_adc.c  │     stm32f10x_bkp.c  │     stm32f10x_can.c  │     stm32f10x_cec.c  │     stm32f10x_crc.c  │     stm32f10x_dac.c  │     stm32f10x_dbgmcu.c  │     stm32f10x_dma.c  │     stm32f10x_exti.c  │     stm32f10x_flash.c  │     stm32f10x_fsmc.c  │     stm32f10x_gpio.c  │     stm32f10x_i2c.c  │     stm32f10x_iwdg.c  │     stm32f10x_pwr.c  │     stm32f10x_rcc.c  │     stm32f10x_rtc.c  │     stm32f10x_sdio.c  │     stm32f10x_spi.c  │     stm32f10x_tim.c  │     stm32f10x_usart.c  │     stm32f10x_wwdg.c  │        ├─Listing  │   LED.map  │   startup_stm32f10x_hd.lst  │      ├─Listings  ├─Objects  ├─Output  │   can.crf  │   can.d  │   can.o  │   canmsg.crf  │   canmsg.d  │   canmsg.o  │   core_cm3.crf  │   core_cm3.d  │   core_cm3.o  │   delay.crf  │   delay.d  │   delay.o  │   ExtDll.iex  │   exti.crf  │   exti.d  │   exti.o  │   LED.axf  │   LED.build_log.htm  │   led.crf  │   led.d  │   LED.hex  │   LED.htm  │   LED.lnp  │   led.o  │   LED.sct  │   LED_STM32_ufun.dep  │   main.crf  │   main.d  │   main.o  │   misc.crf  │   misc.d  │   misc.o  │   startup_stm32f10x_hd.d  │   startup_stm32f10x_hd.o  │   stm32f10x_adc.crf  │   stm32f10x_adc.d  │   stm32f10x_adc.o  │   stm32f10x_bkp.crf  │   stm32f10x_bkp.d  │   stm32f10x_bkp.o  │   stm32f10x_can.crf  │   stm32f10x_can.d  │   stm32f10x_can.o  │   stm32f10x_cec.crf  │   stm32f10x_cec.d  │   stm32f10x_cec.o  │   stm32f10x_crc.crf  │   stm32f10x_crc.d  │   stm32f10x_crc.o  │   stm32f10x_dac.crf  │   stm32f10x_dac.d  │   stm32f10x_dac.o  │   stm32f10x_dbgmcu.crf  │   stm32f10x_dbgmcu.d  │   stm32f10x_dbgmcu.o  │   stm32f10x_dma.crf  │   stm32f10x_dma.d  │   stm32f10x_dma.o  │   stm32f10x_exti.crf  │   stm32f10x_exti.d  │   stm32f10x_exti.o  │   stm32f10x_flash.crf  │   stm32f10x_flash.d  │   stm32f10x_flash.o  │   stm32f10x_fsmc.crf  │   stm32f10x_fsmc.d  │   stm32f10x_fsmc.o  │   stm32f10x_gpio.crf  │   stm32f10x_gpio.d  │   stm32f10x_gpio.o  │   stm32f10x_i2c.crf  │   stm32f10x_i2c.d  │   stm32f10x_i2c.o  │   stm32f10x_it.crf  │   stm32f10x_it.d  │   stm32f10x_it.o  │   stm32f10x_iwdg.crf  │   stm32f10x_iwdg.d  │   stm32f10x_iwdg.o  │   stm32f10x_pwr.crf  │   stm32f10x_pwr.d  │   stm32f10x_pwr.o  │   stm32f10x_rcc.crf  │   stm32f10x_rcc.d  │   stm32f10x_rcc.o  │   stm32f10x_rtc.crf  │   stm32f10x_rtc.d  │   stm32f10x_rtc.o  │   stm32f10x_sdio.crf  │   stm32f10x_sdio.d  │   stm32f10x_sdio.o  │   stm32f10x_spi.crf  │   stm32f10x_spi.d  │   stm32f10x_spi.o  │   stm32f10x_tim.crf  │   stm32f10x_tim.d  │   stm32f10x_tim.o  │   stm32f10x_usart.crf  │   stm32f10x_usart.d  │   stm32f10x_usart.o  │   stm32f10x_wwdg.crf  │   stm32f10x_wwdg.d  │   stm32f10x_wwdg.o  │   system_stm32f10x.crf  │   system_stm32f10x.d  │   system_stm32f10x.o  │   timer.crf  │   timer.d  │   timer.o  │   usart1.crf  │   usart1.d  │   usart1.o  │      └─USER      can.c      can.h      CanMsg.c      CanMsg.h      cantxmsg.c      delay.c      delay.h      exti.c      exti.h      key.c      key.h      led.c      led.h      main.c      stm32f10x_conf.h      stm32f10x_it.c      stm32f10x_it.h      timer.c      timer.h      ufun_conf.h      usart1.c      usart1.h        - IMDN开发者社群-imdn.cn">
登录
首页 » C/C++ » STM32-CAN模块通讯实验(正常工作模式)

STM32-CAN模块通讯实验(正常工作模式)

于 2020-04-10 发布
0 80
下载积分: 1 下载次数: 1

代码说明:

学习STM32 的CAN模块,最核心 的就是系统时钟的配置问题,代码如下: /**************************************************************///程 序 名: RCC_Config()//开 发 者: 火工头陀//入口参数: 无//功能说明: 系统时钟配置//**************************************************************/void RCC_Config(void){ ErrorStatus  HSEStartUpStatus; //定义结构体 /* RCC system reset(for debug purpose)将外设 RCC寄存器重设为缺省值 */ RCC_DeInit(); ///* Enable HSE 设置外部高速晶振(HSE)*/ //RCC_HSEConfig(RCC_HSE_ON); /* Enable HSE 设置外部高速晶振(HSE)*/ //外部晶体振荡器被旁路,参照《数据手册》6.2.1 //这样设置是因为ufun学习板的原理图就是这样设计的 //RCC_HSEConfig(RCC_HSE_Bypass); RCC_HSEConfig(RCC_HSE_ON);//亚明小板设置方法 (最常用的无源晶振设置方法) /* Wait till HSE is ready 等待 HSE 起振*/ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if (HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer 预取指缓存使能*/ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Flash 2 wait state 设置代码延时值*/ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK 设置 AHB 时钟(HCLK)= 72 MHz*/ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK1 = HCLK/2 设置低速 AHB 时钟(PCLK1) = 72/2 = 36 MHz*/ RCC_PCLK1Config(RCC_HCLK_Div2); /* PCLK2 = HCLK 设置高速 AHB 时钟(PCLK2)= 72 MHz*/ RCC_PCLK2Config(RCC_HCLK_Div1); //根据外接晶振设置总线频率、PLL 时钟源、倍频系数 /* PLLCLK = 12MHz * 6 = 72 MHz 设置 PLL 时钟源及倍频系数*/ //RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_6); /* PLLCLK = 8MHz * 9 = 72 MHz 设置 PLL 时钟源及倍频系数*/ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* Enable PLL 使能或者失能 PLL*/ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready 等待指定的 RCC 标志位设置成功 等待PLL初始化成功*/ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } /* Select PLL as system clock source 设置系统时钟(SYSCLK) 设置PLL为系统时钟源*/ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source 等待PLL成功用作于系统时钟的时钟源*/ while(RCC_GetSYSCLKSource() != 0x08) { } } } 其次是具体的CAN模块的相关配置 /******************************************************************************* * 文件名  :can.c * 描述    :将printf函数重定向到USART1。这样就可以用printf函数将单片机的数据 *           打印到PC上的超级终端或串口调试助手。         ********************************************************************************** * 实验平台:ufun10.02开发板 * 硬件连接:--------------------------- *          | PA11-CAN-RX               | *          | PA12-CAN-TX               | *           --------------------------- * 库版本  :ST-V3.5.0 * * 作者    :desk1983(火工头陀) * 论坛    : * 淘宝    :**********************************************************************************/#include "can.h" #include "CanMsg.h" /* * 函数名:CAN_GPIO_Config * 描述  :CAN的GPIO 配置,PB8上拉输入,PB9推挽输出 * 输入  :无 * 输出  : 无 * 调用  :内部调用 */static void CAN_GPIO_Config(void){ GPIO_InitTypeDef GPIO_InitStructure; /*外设时钟设置*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); /* GPIO_重映射 相关 设置*/ GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE); /* Configure CAN pin: RX */                // PA11 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉输入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure CAN pin: TX */                // PA12 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     GPIO_Init(GPIOB, &GPIO_InitStructure); }/* * 函数名:CAN_NVIC_Config * 描述  :CAN的NVIC 配置,第1优先级组,0,0优先级 * 输入  :无 * 输出  : 无 * 调用  :内部调用 */static void CAN_NVIC_Config(void){ NVIC_InitTypeDef NVIC_InitStructure; /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /*中断设置*/ NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;    //CAN1 RX0中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;    //抢占优先级0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;    //子优先级为0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);}/* * 函数名:CAN_Mode_Config * 描述  :CAN的模式 配置 * 输入  :无 * 输出  : 无 * 调用  :内部调用 */static void CAN_Mode_Config(void){ CAN_InitTypeDef CAN_InitStructure; /************************CAN通信参数设置**********************************/ /*CAN寄存器初始化*/ CAN_DeInit(CAN1); CAN_StructInit(&CAN_InitStructure); /*CAN单元初始化*/ CAN_InitStructure.CAN_TTCM = DISABLE;    //MCR-TTCM  关闭时间触发通信模式使能 CAN_InitStructure.CAN_ABOM = DISABLE;    //MCR-ABOM  自动离线管理  CAN_InitStructure.CAN_AWUM = DISABLE;    //MCR-AWUM  使用自动唤醒模式 CAN_InitStructure.CAN_NART = ENABLE;    //MCR-NART  禁止报文自动重传   DISABLE-自动重传 CAN_InitStructure.CAN_RFLM = DISABLE;    //MCR-RFLM  接收FIFO 锁定模式  DISABLE-溢出时新报文会覆盖原有报文   CAN_InitStructure.CAN_TXFP = DISABLE;    //MCR-TXFP  发送FIFO优先级 DISABLE-优先级取决于报文标示符  //CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack; //环回模式 CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; //正常模式 //BTR-SJW 重新同步跳跃宽度 1个时间单元 CAN_InitStructure.CAN_SJW  = CAN_SJW_1tq; //BTR-TS1 时间段1 占用了8个时间单元 CAN_InitStructure.CAN_BS1  = CAN_BS1_3tq; //BTR-TS1 时间段2 占用了7个时间单元 CAN_InitStructure.CAN_BS2  = CAN_BS2_2tq; //BTR-BRP 波特率分频器  定义了时间单元的时间长度 36/((1 3 2)*12)=0.5Mbps=500Kbps //BTR-BRP 波特率分频器  定义了时间单元的时间长度 36/((1 8 7)*9)=0.25Mbps=250Kbps CAN_InitStructure.CAN_Prescaler =12;    CAN_Init(CAN1, &CAN_InitStructure);}/* * 函数名:CAN_Filter_Config * 描述  :CAN的过滤器 配置 * 输入  :无 * 输出  : 无 * 调用  :内部调用 */static void CAN_Filter_Config(void){ CAN_FilterInitTypeDef  CAN_FilterInitStructure; /*CAN过滤器初始化*/ CAN_FilterInitStructure.CAN_FilterNumber=0; //过滤器组0 CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //工作在标识符屏蔽位模式 CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //过滤器位宽为单个32位。 /* 使能报文标示符过滤器按照标示符的内容进行比对过滤,扩展ID不是如下的就抛弃掉,是的话,会存入FIFO0。 */ //CAN_FilterInitStructure.CAN_FilterIdHigh= (((u32)0x131416; //要过滤的ID高位  //CAN_FilterInitStructure.CAN_FilterIdLow= (((u32)0x1314

下载说明:请别用迅雷下载,失败请重下,重下不扣分!

发表评论

0 个回复

  • 696518资源总数
  • 105570会员总数
  • 13今日下载