标题: MA600角度传感器STM32驱动程序 [打印本页]

作者: HLzhou    时间: 2024-10-23 15:42
标题: MA600角度传感器STM32驱动程序


/* SPI1 init function */
void MX_SPI1_Init(void)
{


  /* USER CODE BEGIN SPI1_Init 0 */

  /* USER CODE END SPI1_Init 0 */

  LL_SPI_InitTypeDef SPI_InitStruct = {0};

  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* Peripheral clock enable */
  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI1);

  LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  /**SPI1 GPIO Configuration
  PA5   ------> SPI1_SCK
  PA6   ------> SPI1_MISO
  PA7   ------> SPI1_MOSI
  */
  GPIO_InitStruct.Pin = LL_GPIO_PIN_5;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_5;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = LL_GPIO_PIN_6;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_5;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = LL_GPIO_PIN_7;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_5;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

//  /* SPI1 interrupt Init */
//  NVIC_SetPriority(SPI1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),1, 1));
//  NVIC_EnableIRQ(SPI1_IRQn);

  /* USER CODE BEGIN SPI1_Init 1 */

  /* USER CODE END SPI1_Init 1 */
  SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
  SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;
  SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_16BIT;
  SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW;
  SPI_InitStruct.ClockPhase = LL_SPI_PHASE_1EDGE;
  SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
  SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV16;
  SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
  SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
  SPI_InitStruct.CRCPoly = 7;
  LL_SPI_Init(SPI1, &SPI_InitStruct);
  LL_SPI_SetStandard(SPI1, LL_SPI_PROTOCOL_MOTOROLA);
  LL_SPI_DisableNSSPulseMgt(SPI1);
  /* USER CODE BEGIN SPI1_Init 2 */

  /* USER CODE END SPI1_Init 2 */
       

  /* USER CODE BEGIN SPI1_Init 2 */

  /* USER CODE END SPI1_Init 2 */

}


uint16_t MA600GQ_Read_Register(uint8_t Address)
{
        uint16_t Register_Value;
        uint16_t txData = 0xd200 + Address;                                                                                                                                                        // 0xd2 + 8-bit Reg. Address
        uint16_t timeOut = 200;
       
        MA600GQ_CS_Enable();

        while(!LL_SPI_IsActiveFlag_TXE(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        LL_SPI_TransmitData16(SPI1, txData);
        timeOut = 2000;
        while(LL_SPI_IsActiveFlag_BSY(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        LL_SPI_ReceiveData16(SPI1);
        MA600GQ_CS_Disable();
        delay_us(1);
    MA600GQ_CS_Enable();
        txData = 0x0000;
        while(!LL_SPI_IsActiveFlag_TXE(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        LL_SPI_TransmitData16(SPI1, txData);
        timeOut = 2000;
        while(LL_SPI_IsActiveFlag_BSY(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }

        Register_Value = LL_SPI_ReceiveData16(SPI1);

        MA600GQ_CS_Disable();
       
        return Register_Value;                                                                                                                                                        // 获取16位的角度数据
}
uint16_t MA600GQ_Write_Register(uint8_t Address,uint8_t Data)
{
        uint16_t Register_Value;
        uint16_t txData = 0xEA54;                                                                                                                                                        // 0xea54
        uint16_t timeOut = 200;

        MA600GQ_CS_Enable();
        while(!LL_SPI_IsActiveFlag_TXE(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        LL_SPI_TransmitData16(SPI1, txData);
        timeOut = 2000;
        while(LL_SPI_IsActiveFlag_BSY(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        MA600GQ_CS_Disable();
        LL_SPI_ReceiveData16(SPI1);
        txData = (((uint16_t)Address)<<8) + Data;
        timeOut = 2000;
        MA600GQ_CS_Enable();
        while(!LL_SPI_IsActiveFlag_TXE(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        LL_SPI_TransmitData16(SPI1, txData);
        timeOut = 2000;
        while(LL_SPI_IsActiveFlag_BSY(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        MA600GQ_CS_Disable();
        LL_SPI_ReceiveData16(SPI1);
        txData = 0x0000;
        timeOut = 2000;
        MA600GQ_CS_Enable();
        while(!LL_SPI_IsActiveFlag_TXE(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        LL_SPI_TransmitData16(SPI1, txData);
        timeOut = 2000;
        while(LL_SPI_IsActiveFlag_BSY(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        Register_Value =  LL_SPI_ReceiveData16(SPI1)&0x00FF;
        MA600GQ_CS_Disable();
       
        return Register_Value;                                                                                                                                                        // 获取16位的角度数据
}
int16_t MA600GQ_Read_Speed(void)
{
       
    uint16_t txData = 0x0000;
    uint16_t timeOut = 200;
       
    MA600GQ_CS_Enable();

        while(!LL_SPI_IsActiveFlag_TXE(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        LL_SPI_TransmitData16(SPI1, txData);
        timeOut = 2000;
        while(LL_SPI_IsActiveFlag_BSY(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        MA600GQ_angle_Value = LL_SPI_ReceiveData16(SPI1);
        MA600GQ_angle_Value *= POLE_PAIR_NUM;
        LL_SPI_TransmitData16(SPI1, txData);

        timeOut = 2000;

        while(LL_SPI_IsActiveFlag_BSY(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }

        MA600GQ_speed_Value = LL_SPI_ReceiveData16(SPI1);

    MA600GQ_CS_Disable();
       
//    return speed_Value ;                                                                                                                                                        // 获取16位的角度数据

}
uint16_t MA600GQ_GetRawData(void)
{
    uint16_t txData = 0x0000;
    uint16_t timeOut = 2000;
    MA600GQ_CS_Enable();
        while(!LL_SPI_IsActiveFlag_TXE(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        LL_SPI_TransmitData16(SPI1, txData);
        timeOut = 2000;
        while(LL_SPI_IsActiveFlag_BSY(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 检查SPI发送缓存取是否为空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超时时直接返回,避免继续执行后续代码
        }
    }
        MA600GQ_angle_Value = LL_SPI_ReceiveData16(SPI1);
        MA600GQ_angle_Value *= POLE_PAIR_NUM;
    MA600GQ_CS_Disable();
}

MA600GQ.pdf

1.49 MB, 下载次数: 0, 下载积分: 黑币 -5






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1