• 大小: 1.92MB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2023-08-13
  • 语言: 其他
  • 标签: can通讯  

资源简介

基于stm32f072的can通讯代码。

资源截图

代码片段和文件信息

#include “pbdata.h“
/***************************************************************************
*   函 数 名: CAN_GPIO
*   功能说明: CAN的GPIO管脚初始化
*   形    参:无
*   返 回 值: 无
***************************************************************************/
void CAN_GPIO(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定义一个GPIO_InitTypeDef类型的结构体

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB  ENABLE); //PB端口时钟使能                          

/* CAN1 Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN ENABLE);//CAN端口时钟使能

GPIO_PinAFConfig(GPIOBGPIO_PinSource8GPIO_AF_4); //定义PB8复用功能
    GPIO_PinAFConfig(GPIOBGPIO_PinSource9GPIO_AF_4); //定义PB9复用功能

/* Configure CAN pin: RX */                // PB8

GPIO_InitStructure.GPIO_Pin    = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode   = GPIO_Mode_AF;//复用模式
GPIO_InitStructure.GPIO_PuPd   = GPIO_PuPd_UP;//GPIO_PuPd_UP = 0x01
GPIO_Init(GPIOB &GPIO_InitStructure);

/* Configure CAN pin: TX */                // PB9

GPIO_InitStructure.GPIO_Pin    = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode   = GPIO_Mode_AF; //复用模式
GPIO_InitStructure.GPIO_OType  = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed  = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd   = GPIO_PuPd_NOPULL; //GPIO_PuPd_NOPULL = 0x00
GPIO_Init(GPIOB &GPIO_InitStructure);
}

void CAN_Configation(void)
{
CAN_InitTypeDef        CAN_InitStructure;
CAN_FilterInitTypeDef  CAN_FilterInitStructure;
  NVIC_InitTypeDef       nvic;

CAN_GPIO();
 

 nvic.NVIC_IRQChannel = CEC_CAN_IRQn;   //设置中断优先级
   nvic.NVIC_IRQChannelPriority = 1;
   nvic.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&nvic);

CAN_DeInit(CAN);
CAN_StructInit(&CAN_InitStructure);


CAN_InitStructure.CAN_TTCM=DISABLE;         // 时间触发通信禁止
CAN_InitStructure.CAN_ABOM=DISABLE;         // 离线退出是在中断置位清0后退出
CAN_InitStructure.CAN_AWUM=DISABLE;         // 自动唤醒模式:清零sleep
CAN_InitStructure.CAN_NART=DISABLE;         // 自动重新传送报文,知道发送成功
CAN_InitStructure.CAN_RFLM=DISABLE;         // FIFO没有锁定,新报文覆盖旧报文
CAN_InitStructure.CAN_TXFP=DISABLE;         // 发送报文优先级确定:标志符
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal; // 正常模式

//1M
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_8tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler = 3;

CAN_Init(CAN &CAN_InitStructure); // 初始化CAN1

/* CAN filter init */
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=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-12-27 17:37  08. CAN\
     目录           0  2017-12-30 19:41  08. CAN\BSP\
     文件        5608  2017-12-30 19:41  08. CAN\BSP\can.c
     文件         668  2017-12-28 21:29  08. CAN\BSP\can.h
     文件        1452  2017-12-28 21:25  08. CAN\BSP\led.c
     文件        2486  2017-12-28 21:29  08. CAN\BSP\led.h
     目录           0  2017-12-27 17:37  08. CAN\LIB\
     目录           0  2017-12-27 17:37  08. CAN\LIB\inc\
     文件       21120  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_adc.h
     文件       27298  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_can.h
     文件       12420  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_cec.h
     文件       10714  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_comp.h
     文件        4957  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_crc.h
     文件        7766  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_crs.h
     文件       15562  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_dac.h
     文件        4181  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_dbgmcu.h
     文件       56687  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_dma.h
     文件       10707  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_exti.h
     文件       17899  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_flash.h
     文件       13779  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_gpio.h
     文件       18158  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_i2c.h
     文件        4534  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_iwdg.h
     文件        4416  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_misc.h
     文件        7156  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_pwr.h
     文件       27877  2016-11-06 09:36  08. CAN\LIB\inc\stm32f0xx_rcc.h
     文件       37390  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_rtc.h
     文件       22597  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_spi.h
     文件       30144  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_syscfg.h
     文件       51930  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_tim.h
     文件       27007  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_usart.h
     文件        3540  2014-12-05 00:34  08. CAN\LIB\inc\stm32f0xx_wwdg.h
............此处省略107个文件信息

评论

共有 条评论