找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 767|回復: 0
打印 上一主題 下一主題
收起左側

MA600角度傳感器STM32驅動程序

[復制鏈接]
跳轉到指定樓層
樓主
ID:389519 發(fā)表于 2024-10-23 15:42 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式


/* 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發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        LL_SPI_TransmitData16(SPI1, txData);
        timeOut = 2000;
        while(LL_SPI_IsActiveFlag_BSY(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        LL_SPI_ReceiveData16(SPI1);
        MA600GQ_CS_Disable();
        delay_us(1);
    MA600GQ_CS_Enable();
        txData = 0x0000;
        while(!LL_SPI_IsActiveFlag_TXE(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        LL_SPI_TransmitData16(SPI1, txData);
        timeOut = 2000;
        while(LL_SPI_IsActiveFlag_BSY(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }

        Register_Value = LL_SPI_ReceiveData16(SPI1);

        MA600GQ_CS_Disable();
       
        return Register_Value;                                                                                                                                                        // 獲取16位的角度數(shù)據(jù)
}
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發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        LL_SPI_TransmitData16(SPI1, txData);
        timeOut = 2000;
        while(LL_SPI_IsActiveFlag_BSY(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        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發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        LL_SPI_TransmitData16(SPI1, txData);
        timeOut = 2000;
        while(LL_SPI_IsActiveFlag_BSY(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        MA600GQ_CS_Disable();
        LL_SPI_ReceiveData16(SPI1);
        txData = 0x0000;
        timeOut = 2000;
        MA600GQ_CS_Enable();
        while(!LL_SPI_IsActiveFlag_TXE(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        LL_SPI_TransmitData16(SPI1, txData);
        timeOut = 2000;
        while(LL_SPI_IsActiveFlag_BSY(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        Register_Value =  LL_SPI_ReceiveData16(SPI1)&0x00FF;
        MA600GQ_CS_Disable();
       
        return Register_Value;                                                                                                                                                        // 獲取16位的角度數(shù)據(jù)
}
int16_t MA600GQ_Read_Speed(void)
{
       
    uint16_t txData = 0x0000;
    uint16_t timeOut = 200;
       
    MA600GQ_CS_Enable();

        while(!LL_SPI_IsActiveFlag_TXE(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        LL_SPI_TransmitData16(SPI1, txData);
        timeOut = 2000;
        while(LL_SPI_IsActiveFlag_BSY(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        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發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }

        MA600GQ_speed_Value = LL_SPI_ReceiveData16(SPI1);

    MA600GQ_CS_Disable();
       
//    return speed_Value ;                                                                                                                                                        // 獲取16位的角度數(shù)據(jù)

}
uint16_t MA600GQ_GetRawData(void)
{
    uint16_t txData = 0x0000;
    uint16_t timeOut = 2000;
    MA600GQ_CS_Enable();
        while(!LL_SPI_IsActiveFlag_TXE(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        LL_SPI_TransmitData16(SPI1, txData);
        timeOut = 2000;
        while(LL_SPI_IsActiveFlag_BSY(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        timeOut = 2000;
        while(!LL_SPI_IsActiveFlag_RXNE(SPI1))                                                                                                                                // 檢查SPI發(fā)送緩存取是否為空
    {
        if(timeOut-- == 0)
        {
            return 0;                                                                                                                                                                         // 在超時時直接返回,避免繼續(xù)執(zhí)行后續(xù)代碼
        }
    }
        MA600GQ_angle_Value = LL_SPI_ReceiveData16(SPI1);
        MA600GQ_angle_Value *= POLE_PAIR_NUM;
    MA600GQ_CS_Disable();
}

MA600GQ.pdf

1.49 MB, 下載次數(shù): 0, 下載積分: 黑幣 -5

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表