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

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

        stm32_CAN總線的發送和接收程序

        作者:碧海藍天   來源:來自碧海藍天   點擊數:  更新時間:2014年07月30日   【字體:

        碧海藍天在做CAN模塊的多線路控制系統的程序,現在把簡單的發送和接收的程序貼出來供大家參考,注釋寫的很清楚了,這里我就不廢話了,直接上代碼了——

         

        文件can.c

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

        #define GPIO_CAN                   GPIOA
        #define RCC_APB2Periph_GPIO_CAN    RCC_APB2Periph_GPIOA
        #define GPIO_Pin_RX                GPIO_Pin_11
        #define GPIO_Pin_TX                GPIO_Pin_12
        #define GPIO_Remap_CAN             GPIO_Remap1_CAN1
        #define LED_SET() GPIO_SetBits(GPIOB,GPIO_Pin_15)
        #define LED_RST() GPIO_ResetBits(GPIOB,GPIO_Pin_15)


        void CAN_config_init(void)
        {
          CAN_InitTypeDef        CAN_InitStructure;
          CAN_FilterInitTypeDef  CAN_FilterInitStructure;
         
          CAN_DeInit(CAN1);
          CAN_StructInit(&CAN_InitStructure);
            // 36M 250k速率
          CAN_InitStructure.CAN_TTCM=DISABLE;
          CAN_InitStructure.CAN_ABOM=DISABLE;
          CAN_InitStructure.CAN_AWUM=DISABLE;
          CAN_InitStructure.CAN_NART=DISABLE;
          CAN_InitStructure.CAN_RFLM=DISABLE;
          CAN_InitStructure.CAN_TXFP=DISABLE;
          CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;
          CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
          CAN_InitStructure.CAN_BS1=CAN_BS1_12tq;
          CAN_InitStructure.CAN_BS2=CAN_BS2_3tq;
          CAN_InitStructure.CAN_Prescaler=9;
          CAN_Init(CAN1, &CAN_InitStructure);

         
          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=0;
          CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
          CAN_FilterInit(&CAN_FilterInitStructure); 
         }

         
        void CAN_init(void)
        {
         
            NVIC_InitTypeDef NVIC_InitStructure;
            GPIO_InitTypeDef GPIO_InitStructure;   
              // 首先打開電源及時鐘
           
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIO_CAN, ENABLE);
           
            RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
          
           
            NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
            NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
            NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
            NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
            NVIC_Init(&NVIC_InitStructure);
           
            NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn;
            NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
            NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
            NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
            NVIC_Init(&NVIC_InitStructure);
            // 然后配置pin
           
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_RX;
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
            GPIO_Init(GPIO_CAN, &GPIO_InitStructure);
           
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_TX;
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
            GPIO_Init(GPIO_CAN, &GPIO_InitStructure);
            // 映射下
            GPIO_PinRemapConfig(GPIO_Remap_CAN , ENABLE);
            // 波特率過濾器初始化
            CAN_config_init();
            CAN_ITConfig(CAN1, CAN_IT_FMP0 | CAN_IT_FF0 | CAN_IT_FOV0, ENABLE);  // fifo0中斷
            CAN_ITConfig(CAN1, CAN_IT_FMP1 | CAN_IT_FF1 | CAN_IT_FOV1, ENABLE);  // fifo1中斷
            CAN_ITConfig(CAN1, CAN_IT_TME, DISABLE);                // 發送中斷
        }

         
        int CAN_tx_data(void)
        {
            CanTxMsg TxMessage;
            uint8_t TransmitMailbox = 0;
          u32 i;
           
            TxMessage.StdId=0x001;
            TxMessage.RTR=CAN_RTR_DATA;
            TxMessage.IDE=CAN_ID_STD;
            TxMessage.DLC=4;
            TxMessage.Data[0]=0x04;
            TxMessage.Data[1]=0x08;
            TxMessage.Data[2]=0x0c;
            TxMessage.Data[3]=0x10;
            TransmitMailbox = CAN_Transmit(CAN1, &TxMessage);
          i = 0;
          // 用于檢查消息傳輸是否正常
          while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFF))
          {
           i++;
          }
          i = 0;
          // 檢查返回的掛號的信息數目
          while((CAN_MessagePending(CAN1, CAN_FIFO0) < 1) && (i != 0xFF))
          {
           i++;
          }

        }
         
        void USB_LP_CAN1_RX0_IRQHandler(void)
        {
          //  u32 i;
            CanRxMsg RxMessage;
           if(SET == CAN_GetITStatus(CAN1,CAN_IT_FF0))
           {
                CAN_ClearITPendingBit(CAN1,CAN_IT_FF0);
           }
           else if(SET == CAN_GetITStatus(CAN1,CAN_IT_FOV0))
           {
                CAN_ClearITPendingBit(CAN1,CAN_IT_FOV0);
           }
           else
           {
                CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
                if(RxMessage.StdId==0x001&&RxMessage.DLC==4)
             LED_SET();
             //   printf("CAN_FIFO0 RxMessage.StdId is 0x%x\r\n",RxMessage.StdId);
             //   printf("RxMessage.DLC is 0x%x\r\n",RxMessage.DLC);  
              //  for(i = 0; i < RxMessage.DLC; i++)
             //   {
             //       printf("data[%d] is 0x%x\r\n",i,RxMessage.Data[i]);
              //  }
             //   printf("\r\n");  
           
         
        }

         
        void LED_init(void)
        {
         GPIO_InitTypeDef GPIO_InitStruct;

         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
         GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
         GPIO_Init(GPIOB, &GPIO_InitStruct);
        }

         
        void soft_delayms(u16 t)
        {
         u16 tt;
         while(t--)
         {
          tt = 10000;
          while(tt--);
         }
        }


        文件:can.h

         #ifndef __CAN_H
        #define __CAN_H


        #include "stm32f10x.h"

        void CAN_config_init(void);
        void CAN_init(void);
        int CAN_tx_data(void);
        void LED_init(void);
        void soft_delayms(u16 t);

        #endif

         

        文件:main.c
        #include "stm32f10x.h"
        #include "can.h"


        int main(void)

         SystemInit();
         LED_init();
         CAN_config_init();
         CAN_init();
         CAN_tx_data();
         while(1)
          {
           
          }
        }

        有問題的可以直接提問,大家互相討論著進步

        關閉窗口

        相關文章

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