<acronym id="xonnx"></acronym>
      <td id="xonnx"></td>
    1. <pre id="xonnx"></pre>

      1. 專注電子技術學習與研究
        當前位置:單片機教程網 >> STM32 >> 瀏覽文章

        經過can協議分析儀調試過的stm32的can模塊收發程序

        作者:佚名   來源:本站原創   點擊數:  更新時間:2014年04月15日   【字體:

        不多說了,直接上代碼

        主函數:main.c
        #include "main.h"


        int main(void)
        {
         SystemInit();
         can_init();
         
         while(1)
         {

         }

        }

        can模塊配置的子函數:can.c

        #include "can.h"

        void can_init(void)
        {
         GPIO_InitTypeDef GPIO_InitStructure;
         NVIC_InitTypeDef NVIC_InitStructure;
         CAN_InitTypeDef CAN_InitStructure;
         CAN_FilterInitTypeDef CAN_FilterInitStructure;

         //NVIC_Configuration
         NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;     // 主優先級為0
         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;            // 次優先級為0
         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
         NVIC_Init(&NVIC_InitStructure);

         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);                                  
         RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
         
         //Configure CAN pin: RX
         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
         GPIO_Init(GPIOA, &GPIO_InitStructure);
         
         //Configure CAN pin: TX
         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);

         
         CAN_DeInit(CAN1);
         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;   // 模式: CAN_Mode_Normal / CAN_Mode_LoopBack
         CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;        // 1tq、BS1、BS2的值跟波特率有關
         CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
         CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
         CAN_InitStructure.CAN_Prescaler=9;       // 分頻系數為1
         CAN_Init(CAN1, &CAN_InitStructure);      // 初始化CAN1
         
         
         CAN_FilterInitStructure.CAN_FilterNumber=0;
         CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdList;
         CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
         CAN_FilterInitStructure.CAN_FilterIdHigh=0x014 << 5;
         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);

         CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE); //中斷使能
         
        }

         

        void USB_LP_CAN1_RX0_IRQHandler(void)
        {
         CanRxMsg RxMessage;
         
         RxMessage.StdId=0x00;
         RxMessage.ExtId=0x00;
         RxMessage.IDE=0;
         RxMessage.DLC=0;
         RxMessage.FMI=0;
         RxMessage.Data[0]=0x00;
         RxMessage.Data[1]=0x00;
         
         CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
         
        }

         

        主函數的頭文件:main.h

         


        #ifndef  _MAIN_H
        #define  _MAIN_H

        #include "stm32f10x.h"
        #include "can.h"


        #endif

         

        子函數的頭文件:can.h

        #ifndef _CAN_H
        #define _CAN_H

        #include "main.h"

         

        void can_init(void);

        #endif
        不明白的地方歡迎提問

        關閉窗口

        相關文章

        欧美性色欧美精品视频,99热这里只有精品mp4,日韩高清亚洲日韩精品一区二区,2020国自产拍精品高潮