美文网首页
stm32---CAN通信

stm32---CAN通信

作者: 飞向深空 | 来源:发表于2019-08-16 10:52 被阅读0次
    捕获.PNG
    2.PNG
    3.PNG

    can.c

    #include "can.h"
    
    /*************************************************************
    函数名:CAN_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
    功能: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+tbs2+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;
    //    其他,初始化失败;
    
    **************************************************************/
    void CAN_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
    {
        GPIO_InitTypeDef GPIO_InitStructure;
        CAN_InitTypeDef CAN_InitStructure;
        CAN_FilterInitTypeDef CAN_FilterStructure;
    #if CAN_RX0_INT_ENABLE 
        NVIC_InitTypeDef        NVIC_InitStructure;
    #endif
        //1*开启CAN时钟和对应引脚时钟
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
        
        //2*引脚初始化配置
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入模式
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
        
        //3*CAN初始化配置
        CAN_InitStructure.CAN_Prescaler = brp; //分频系数为brp+1
        CAN_InitStructure.CAN_Mode=mode; //模式设置
        CAN_InitStructure.CAN_BS1=tbs1; //Tbs1范围
        CAN_InitStructure.CAN_BS2=tbs2; //Tbs2范围
        CAN_InitStructure.CAN_SJW=tsjw; //重新同步跳跃宽度的
        CAN_InitStructure.CAN_ABOM=DISABLE; //软件自动离线管理
        CAN_InitStructure.CAN_AWUM=DISABLE; //睡眠模式通过软件唤醒
        CAN_InitStructure.CAN_NART=ENABLE;  //使用报文自动传输
        CAN_InitStructure.CAN_RFLM=DISABLE;  //报文不锁定,新的覆盖旧的
        CAN_InitStructure.CAN_TTCM=DISABLE; //非时间触发通信模式
        CAN_InitStructure.CAN_TXFP=DISABLE;//优先级由报文标识符决定 
        CAN_Init(CAN1,&CAN_InitStructure);
        
        //4*过滤器初始化配置
        CAN_FilterStructure.CAN_FilterNumber =0; //过滤器0
        CAN_FilterStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //掩码模式,缺点是有误差,报表模式缺点是可识别ID少
        CAN_FilterStructure.CAN_FilterScale=CAN_FilterScale_32bit; //32位
        CAN_FilterStructure.CAN_FilterIdHigh=0x0000; //32位ID
        CAN_FilterStructure.CAN_FilterIdLow = 0x0000;
        CAN_FilterStructure.CAN_FilterMaskIdHigh=0x0000; //32位掩码
        CAN_FilterStructure.CAN_FilterMaskIdLow=0x0000;
        CAN_FilterStructure.CAN_FilterFIFOAssignment=CAN_FilterFIFO0;//过滤器0关联到FIFO0(邮箱0)
        CAN_FilterStructure.CAN_FilterActivation=ENABLE;//激活过滤器0
        CAN_FilterInit(&CAN_FilterStructure);//滤波器初始化
    
        #if CAN_RX0_INT_ENABLE 
        CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);              //FIFO0消息挂号中断允许.            
    
        NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;     // 主优先级为1
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;            // 次优先级为0
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
        #endif
    }
    
        #if CAN_RX0_INT_ENABLE  //使能RX0中断
        //中断服务函数                
        void USB_LP_CAN1_RX0_IRQHandler(void)
        {
            CanRxMsg RxMessage;
            int i=0;
            CAN_Receive(CAN1, 0, &RxMessage);
            for(i=0;i<8;i++)
            printf("rxbuf[%d]:%d\r\n",i,RxMessage.Data[i]);
        }
        #endif
        
    
    //can发送一组数据(固定格式:ID为0X12,标准帧,数据帧)   
    //len:数据长度(最大为8)                     
    //msg:数据指针,最大为8个字节.
    //返回值:0,成功;
    //       其他,失败;
    u8 CAN_Send_Msg(u8 *msg,u8 len)
    {
        u16 i=0;
        u8 mbox;
        
        CanTxMsg TxMessage; 
        TxMessage.StdId=0x12; //标准ID,设置为扩展时自动变0
        TxMessage.ExtId=0x12; //扩展ID
        TxMessage.IDE=0; //决定是标准(1)还是扩展模式(0)
        TxMessage.RTR=0; //表明是数据帧(0)还是遥控帧(1),当是遥控帧时TxMessage.Data为0
        TxMessage.DLC=len; //数据长度(最大为8) 
        for(i=0;i<len;i++)
            TxMessage.Data[i] = msg[i];
        
        mbox = CAN_Transmit(CAN1,&TxMessage); //发送数据,返回所使用邮箱的号码,如果没有空邮箱返回 CAN_NO_MB
        i=0;
        while((CAN_TransmitStatus(CAN1,mbox)==CAN_TxStatus_Failed)&&(i<0xfff)) i++; //发送完退出,未发送完i自加到0xfff时强制退出
        if(i>=0xfff) return 1; //失败
        return 0; //成功
    }
    
    
    //can口接收数据查询
    //buf:数据缓存区;     
    //返回值:0,无数据被收到;
    //       其他,接收的数据长度;
    u8 CAN_Receive_Msg(u8 *buf)
    {
        u32 i;
        CanRxMsg RxMessage; //结构体定义
        if(CAN_MessagePending(CAN1,CAN_FIFO0)==0) return 0; //邮箱为空
        CAN_Receive(CAN1,CAN_FIFO0,&RxMessage);
        for(i=0;i<RxMessage.DLC;i++) //RxMessage.DLC为接收数据长度
            buf[i] = RxMessage.Data[i];
        return RxMessage.DLC;//返回接收的数据长度
    }
    

    can.h

    #ifndef _can_H
    #define _can_H
    
    #include "system.h"
    
    
    void CAN_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode);
    u8 CAN_Send_Msg(u8 *msg,u8 len);
    u8 CAN_Receive_Msg(u8 *buf);
    
    #endif
    

    main.c

    #include "system.h"
    #include "SysTick.h"
    #include "led.h"
    #include "usart.h"
    #include "can.h"
    #include "key.h"
    int main()
    {
        u8 i,j,key;
        u8 mode=0; //默认是正常模式
        u8 res;
        u8 tbuf[8],char_buf[8];
        u8 rbuf[8];
        
        SysTick_Init(72);
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  //中断优先级分组 分2组
        LED_Init();
        USART1_Init(9600);
        KEY_Init();
        
        CAN_Mode_Init(CAN_SJW_1tq,CAN_BS2_8tq,CAN_BS1_9tq,4,CAN_Mode_Normal);
        
        while(1)
        {   
            key =KEY_Scan(0);
            if(key==KEY_UP) //切换模式
            {
                mode = !mode; //模式切换,正常模式0x00,环回模式0x01
                CAN_Mode_Init(CAN_SJW_1tq,CAN_BS2_8tq,CAN_BS1_9tq,4,mode);
                if(mode==0) 
                    printf("Normal Mode\r\n");//正常模式
                else
                    printf("LoopBack Mode\r\n");//环回模式
            }
            else if(key==KEY_DOWN)//发送接收数据
            {
                for(j=0;j<8;j++)
                {
                    tbuf[j] = j;
                    char_buf[j] = tbuf[j]+0x30;//加0x30变ASC码
                }
                res = CAN_Send_Msg(tbuf,8); //返回0成功
                if(res)
                    printf("Send Failed\r\n");
                else
                    printf("发送数据:%s\r\n",char_buf);
            }
            res = CAN_Receive_Msg(rbuf); //返回0失败,非0为接收数据长度
            if(res) //res不为0就可进入
            {
                for(j=0;j<res;j++)
                {
                    char_buf[j] = rbuf[j]+0x30; //加0x30变ASC码
                }
                printf("接收数据:%s\r\n",char_buf);
            }
            i++;
            if(i%20==0)
            {
                led1=!led1;
            }
            
            delay_ms(10);
                
        }
    }
    

    相关文章

      网友评论

          本文标题:stm32---CAN通信

          本文链接:https://www.haomeiwen.com/subject/hekfsctx.html