论坛元老
- 积分
- 6369
- 金钱
- 6369
- 注册时间
- 2015-8-25
- 在线时间
- 1004 小时
|
发表于 2019-11-29 12:02:26
|
显示全部楼层
本帖最后由 szczyb1314 于 2019-12-1 17:59 编辑
- #ifndef __CAN_H
- #define __CAN_H
- #include "sys.h"
- //////////////////////////////////////////////////////////////////////////////////
- //本程序只供学习使用,未经作者许可,不得用于其它任何用途
- //ALIENTEK战舰STM32开发板
- //CAN驱动 代码
- //正点原子@ALIENTEK
- //技术论坛:www.openedv.com
- //创建日期:2014/5/7
- //版本:V1.1
- //版权所有,盗版必究。
- //Copyright(C) 广州市星翼电子科技有限公司 2014-2024
- //All rights reserved
- //********************************************************************************
- //V1.1修改说明 20150528
- //修正了CAN初始化函数的相关注释,更正了波特率计算公式
- //////////////////////////////////////////////////////////////////////////////////
-
- //CAN接收RX0中断使能
- #define USE_CAN_INTR 0 //0:不用中断 1:使用中断
- //CAN选择接收FIFO
- #define CAN_RXFIFO_0_OR_1 0 //0:使用FIFO0 1:使用FIFO1
-
- u8 CAN_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode);//CAN初始化
-
- u8 Can_Send_Msg(u8* msg,u8 len); //发送数据
- u8 Can_Receive_Msg(u8 *buf); //接收数据
- #endif
- #include "can.h"
- #include "led.h"
- #include "delay.h"
- #include "usart.h"
- //////////////////////////////////////////////////////////////////////////////////
- //本程序只供学习使用,未经作者许可,不得用于其它任何用途
- //ALIENTEK战舰STM32开发板
- //CAN驱动 代码
- //正点原子@ALIENTEK
- //技术论坛:www.openedv.com
- //创建日期:2014/5/7
- //版本:V1.1
- //版权所有,盗版必究。
- //Copyright(C) 广州市星翼电子科技有限公司 2014-2024
- //All rights reserved
- //********************************************************************************
- //V1.1修改说明 20150528
- //修正了CAN初始化函数的相关注释,更正了波特率计算公式
- //////////////////////////////////////////////////////////////////////////////////
- //CAN初始化
- //tsjw:重新同步跳跃时间单元.范围:CAN_SJW_1tq~ CAN_SJW_4tq
- //tbs2:时间段2的时间单元. 范围:CAN_BS2_1tq~CAN_BS2_8tq;
- //tbs1:时间段1的时间单元. 范围:CAN_BS1_1tq ~CAN_BS1_16tq
- //brp :波特率分频器.范围:1~1024; tq=(brp)*tpclk1
- //波特率=Fpclk1/((tbs1+1+tbs2+1+1)*brp);
- //mode:CAN_Mode_Normal,普通模式;CAN_Mode_LoopBack,回环模式;
- //Fpclk1的时钟在初始化的时候设置为36M,如果设置CAN_Mode_Init(CAN_SJW_1tq,CAN_BS2_8tq,CAN_BS1_9tq,4,CAN_Mode_LoopBack);
- //则波特率为:36M/((8+9+1)*4)=500Kbps
- //返回值:0,初始化OK;
- // 其他,初始化失败;
- u8 CAN_Mode_Init(u8 tsjw, u8 tbs2, u8 tbs1, u16 brp, u8 mode)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- CAN_InitTypeDef CAN_InitStructure;
- CAN_FilterInitTypeDef CAN_FilterInitStructure;
- #if USE_CAN_INTR
- NVIC_InitTypeDef NVIC_InitStructure;
- #endif
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能PORTA时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//使能CAN1时钟
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽
- GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化IO
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
- GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化IO
- //CAN单元设置
- CAN_InitStructure.CAN_TTCM = DISABLE; //非时间触发通信模式
- CAN_InitStructure.CAN_ABOM = DISABLE; //软件自动离线管理
- CAN_InitStructure.CAN_AWUM = DISABLE; //睡眠模式通过软件唤醒(清除CAN->MCR的SLEEP位)
- CAN_InitStructure.CAN_NART = ENABLE; //禁止报文自动传送
- CAN_InitStructure.CAN_RFLM = DISABLE; //报文不锁定,新的覆盖旧的
- CAN_InitStructure.CAN_TXFP = DISABLE; //优先级由报文标识符决定
- CAN_InitStructure.CAN_Mode = mode; //模式设置: mode:0,普通模式;1,回环模式;
- //设置波特率
- CAN_InitStructure.CAN_SJW = tsjw; //重新同步跳跃宽度(Tsjw)为tsjw+1个时间单位 CAN_SJW_1tq CAN_SJW_2tq CAN_SJW_3tq CAN_SJW_4tq
- CAN_InitStructure.CAN_BS1 = tbs1; //Tbs1=tbs1+1个时间单位CAN_BS1_1tq ~CAN_BS1_16tq
- CAN_InitStructure.CAN_BS2 = tbs2; //Tbs2=tbs2+1个时间单位CAN_BS2_1tq ~ CAN_BS2_8tq
- CAN_InitStructure.CAN_Prescaler = brp; //分频系数(Fdiv)为brp+1
- CAN_Init(CAN1, &CAN_InitStructure); //初始化CAN1
- CAN_FilterInitStructure.CAN_FilterNumber = 7; //过滤器0(103ZET6有14个过滤器,现在用过滤器0)
- CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; //屏蔽位模式
- CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; //32位宽
- CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000; //32位期望收到的ID
- CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
- CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000; //32位MASK(屏蔽)的ID
- CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
- /*
- CAN_FilterInitStructure.CAN_FilterIdHigh=0xFFFF; //32位期望收到的ID:0xFFFF0000,即希望收到的STID+EXTID+IDE+RTR最好是0xFFFF0000
- CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
- CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0xFF00;//32位MASK(屏蔽)的ID:OxFF00FF00,设置需要关心的ID,表示[31:24][15:8]是必须和期望的ID一致的,
- CAN_FilterInitStructure.CAN_FilterMaskIdLow=0xFF00; //而另外的16位不关心,也认为是正确的ID,即最终要确定的映像为0xFFXX00XX,才是正确的。
- */
-
- #if CAN_RXFIFO_0_OR_1==0
- CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; //过滤器0关联到FIFO0
- #elif CAN_RXFIFO_0_OR_1==1
- CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO1; //过滤器0关联到FIFO1
- #endif
- CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //激活过滤器0
- CAN_FilterInit(&CAN_FilterInitStructure); //滤波器初始化
- #if USE_CAN_INTR
- #if CAN_RXFIFO_0_OR_1==0
- CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE); //FIFO0消息挂号中断允许.
- NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
- #elif CAN_RXFIFO_0_OR_1==1
- CAN_ITConfig(CAN1, CAN_IT_FMP1, ENABLE); //FIFO1消息挂号中断允许.
- NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX1_IRQn;
- #endif
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 主优先级为1
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 次优先级为0
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- return 0;
- }
- #if USE_CAN_INTR //使能中断
- #if CAN_RXFIFO_0_OR_1==0
- void USB_LP_CAN1_RX0_IRQHandler(void)
- {
- CanRxMsg RxMessage;
- int i = 0;
- CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
- for(i = 0; i < 8; i++)
- printf("rx0buf[%d]:%d\r\n", i, RxMessage.Data[i]);
- printf("%d\r\n",RxMessage.FMI);
- }
- #elif CAN_RXFIFO_0_OR_1==1
- void CAN1_RX1_IRQHandler(void)
- {
- CanRxMsg RxMessage;
- int i = 0;
- CAN_Receive(CAN1, CAN_FIFO1, &RxMessage);
- for(i = 0; i < 8; i++)
- printf("rx1buf[%d]:%d\r\n", i, RxMessage.Data[i]);
- printf("%d\r\n",RxMessage.FMI);
- }
- #endif
- #endif
- //can发送一组数据(固定格式:ID为0X12,标准帧,数据帧)
- //len:数据长度(最大为8)
- //msg:数据指针,最大为8个字节.
- //返回值:0,成功;
- // 其他,失败;
- u8 Can_Send_Msg(u8* msg, u8 len)
- {
- u8 mbox;
- u16 i = 0;
- CanTxMsg TxMessage;
- TxMessage.StdId = 0x12; // 标准标识符
- TxMessage.ExtId = 0x12; // 设置扩展标示符
- TxMessage.IDE = CAN_Id_Standard; // 标准帧
- TxMessage.RTR = CAN_RTR_Data; // 数据帧
- TxMessage.DLC = len; // 要发送的数据长度
- for(i = 0; i < len; i++)
- TxMessage.Data[i] = msg[i]; //szc data0先发送data0先收到
- mbox = CAN_Transmit(CAN1, &TxMessage);
- i = 0;
- while((CAN_TransmitStatus(CAN1, mbox) == CAN_TxStatus_Failed) && (i < 0XFFF))i++; //等待发送结束
- if(i >= 0XFFF)return 1;
- return 0;
- }
- //can口接收数据查询
- //buf:数据缓存区;
- //返回值:0,无数据被收到;
- // 其他,接收的数据长度;
- u8 Can_Receive_Msg(u8 *buf)
- {
- u32 i;
- CanRxMsg RxMessage;
- #if CAN_RXFIFO_0_OR_1==0
- if( CAN_MessagePending(CAN1, CAN_FIFO0) == 0)return 0; //没有接收到数据,直接退出
- CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);//读取数据
- #elif CAN_RXFIFO_0_OR_1==1
- if( CAN_MessagePending(CAN1, CAN_FIFO1) == 0)return 0; //没有接收到数据,直接退出
- CAN_Receive(CAN1, CAN_FIFO1, &RxMessage);//读取数据
- #endif
- for(i = 0; i < 8; i++)
- buf[i] = RxMessage.Data[i];
- return RxMessage.DLC;
- }
复制代码
我在迷你板上测试过的CAN的程序 |
|