STM32f4发现 - Can-bus传输和接收

时间:2014-05-12 20:09:06

标签: stm32f4discovery can-bus

我的代码如下。 我看着tx和Rx当我观察卡的Tx输出时,我看不到任何信号,Can H,Can L的相似 Can1配置有效可以固定PD0,PD1和2可以激活引脚Pb12和PB6。 我用MCP2551可以收发。我在哪里做错了。

这个maain代码包括can1和can 2 config和can1,can2write数据,和nvıcconfig。

如何检查它是否有效。

 /* Includes ------------------------------------------------------------------*/
    #include "stm32f4xx.h"
    #include <stdlib.h>

    /*----------------------Defines-variables-----------------------------------*/

    CAN_InitTypeDef        CAN_InitStructure;
    CAN_FilterInitTypeDef  CAN_FilterInitStructure;
    CanTxMsg TxMessage;

    /*-----------------------function prototip----------------------------------*/

    void CAN1_Config(void);
    void CAN2_Config(void);
    void NVIC_Config(void);
    void Can1WriteData(uint16_t ID);
    void Can2WriteData(uint16_t ID);

    /*-----------------------main ----------------------------------*/
    int main(void)
    {

      CAN1_Config();
        NVIC_Config();
      Can1WriteData(0x123);

        Can1WriteData(0x124);
    }

    /*-----------------------can1 config----------------------------------*/
    void CAN1_Config(void)
    {
      GPIO_InitTypeDef  GPIO_InitStructure;

      /* CAN GPIOs configuration **************************************************/

      /* Enable GPIO clock */
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

      /* Connect CAN pins */
      GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_CAN1);
      GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_CAN1); 

      /* Configure CAN RX and TX pins */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
      GPIO_Init(GPIOD, &GPIO_InitStructure);

      /* CAN configuration ********************************************************/  
      /* Enable CAN clock */
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

      /* CAN register init */
      CAN_DeInit(CAN1);
      CAN_StructInit(&CAN_InitStructure);

      /* CAN cell init */
      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 Baudrate = 1MBps (CAN clocked at 30 MHz) */
      CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
      CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
      CAN_InitStructure.CAN_Prescaler = 2;
      CAN_Init(CAN1, &CAN_InitStructure);

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

      /* Enable FIFO 0 message pending Interrupt */
      CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
    }
    /*-----------------------can2 config----------------------------------*/
    void CAN2_Config(void)
    {
      GPIO_InitTypeDef  GPIO_InitStructure;
      /* CAN GPIOs configuration **************************************************/

      /* Enable GPIO clock */
      RCC_AHB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);

      /* Connect CAN pins to AF9 */
      GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_CAN2);
      GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_CAN2); 

      /* Configure CAN RX and TX pins */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_6;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
      GPIO_Init(GPIOB, &GPIO_InitStructure);

      /* CAN configuration ********************************************************/  
      /* Enable CAN clock */
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);

      /* CAN register init */
      CAN_DeInit(CAN2);
      CAN_StructInit(&CAN_InitStructure);

      /* CAN cell init */
      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 Baudrate = 1MBps (CAN clocked at 30 MHz) */
      CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
      CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
      CAN_InitStructure.CAN_Prescaler = 2;
      CAN_Init(CAN2, &CAN_InitStructure);

      /* CAN filter init */
      CAN_FilterInitStructure.CAN_FilterNumber = 14;
      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); 
      /* Enable FIFO 0 message pending Interrupt */
      CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);
    }
    /*-----------------------NVIC ayarlamasi----------------------------------*/

    void NVIC_Config(void)
    {
      NVIC_InitTypeDef  NVIC_InitStructure;

      NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x1;
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
      NVIC_Init(&NVIC_InitStructure);

      NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
      NVIC_Init(&NVIC_InitStructure);
    }
    /*******************************************************************************
    * Function Name  : Can1WriteData
    *******************************************************************************/
    void Can1WriteData(uint16_t ID)
    {
       /* transmit */
      TxMessage.StdId = ID;
        TxMessage.ExtId = 0x00;
      TxMessage.RTR = CAN_RTR_DATA;
      TxMessage.IDE = CAN_ID_STD;
      TxMessage.DLC = 8;
        //datalara deger veriyorum txdata degiskeninde degerler görünüyor.
      TxMessage.Data[0] = 0x02;    
      TxMessage.Data[1] = 0x11;    
      TxMessage.Data[2] = 0x11;    
      TxMessage.Data[3] = 0x11;    
      TxMessage.Data[4] = 0x11;    
      TxMessage.Data[5] = 0x11;     
      TxMessage.Data[6] = 0x11;    
      TxMessage.Data[7] = 0x11;      
      CAN_Transmit(CAN1,&TxMessage);

    }

    /*******************************************************************************
    * Function Name  : CanWriteData
*
    *******************************************************************************/
    void Can2WriteData(uint16_t ID)
    {
      CanTxMsg TxMessage;
      /* transmit */
      TxMessage.StdId = ID;
        TxMessage.ExtId = 0x00;
      TxMessage.RTR = CAN_RTR_DATA;
      TxMessage.IDE = CAN_ID_STD;
      TxMessage.DLC = 8;
      TxMessage.Data[0] = 0x23;    
      TxMessage.Data[1] = 0x11;    
      TxMessage.Data[2] = 0x11;    
      TxMessage.Data[3] = 0x11;    
      TxMessage.Data[4] = 0x11;    
      TxMessage.Data[5] = 0x11;     
      TxMessage.Data[6] = 0x11;    
      TxMessage.Data[7] = 0x11;      
      CAN_Transmit(CAN2,&TxMessage);
    }

0 个答案:

没有答案