ADXL355(1).zip

  • da鸡哥
    了解作者
  • C/C++
    开发工具
  • 4KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 10 积分
    下载积分
  • 3
    下载次数
  • 2021-02-04 11:19
    上传日期
ADXL355在STM32上的例程,包含SPI.C ADXL355.C Commend.c
ADXL355(1).zip
  • SPI.c
    1001B
  • ADXL355.h
    3.1KB
  • ADXL355.c
    4KB
  • Communication.h
    2.9KB
  • Communication.c
    3.5KB
内容介绍
#include <stdio.h> #include "stm32f10x.h" #include "stm32f10x_rcc.h" #include "stm32f10x_gpio.h" #include "spi.h" #include "ADXL355.h" #include "Communication.h" #include "delay.h" /****************************** Global Data ***********************************/ int32_t volatile i32SensorX; int32_t volatile i32SensorY; int32_t volatile i32SensorZ; int32_t volatile i32SensorT; uint32_t volatile ui32SensorX; uint32_t volatile ui32SensorY; uint32_t volatile ui32SensorZ; uint32_t volatile ui32SensorT; volatile uint32_t ui32timer_counter = 0; /************************* Global scope functions *****************************/ /** @brief Initialization the accelerometer sensor @return none **/ void ADXL355_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4; //SPI CS GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4); //将这三个引脚设为高电平,取消片选 SPI1_Init(); //初始化SPI SPI1_SetSpeed(SPI_BaudRatePrescaler_16); //设置SPI时钟为4.5MHZ,低速模式 /* Quick verification test for boards */ // uint32_t volatile ui32test = SPI_Read(DEVID_AD, SPI_READ_ONE_REG); /* Read the ID register */ // uint32_t volatile ui32test2 = SPI_Read(DEVID_MST, SPI_READ_ONE_REG); /* Read the ID register */ // uint32_t volatile ui32test3 = SPI_Read(PARTID, SPI_READ_ONE_REG); /* Read the ID register */ // uint32_t volatile ui32test4 = SPI_Read(REVID, SPI_READ_ONE_REG); /* Read the ID register */ } /** @brief Turns on accelerometer measurement mode. @return none **/ void ADXL355_Start_Sensor(void) { uint8_t ui8temp; ui8temp = (uint8_t)ADXL355_SPI_Read(POWER_CTL, SPI_READ_ONE_REG); /* Read POWER_CTL register, before modifying it */ ui8temp = ui8temp & 0xFE; /* Set measurement bit in POWER_CTL register */ ADXL355_SPI_Write(POWER_CTL, ui8temp, 0x00, SPI_WRITE_ONE_REG); /* Write the new value to POWER_CTL register */ } /** @brief Puts the accelerometer into standby mode. @return none **/ void ADXL355_Stop_Sensor(void) { uint8_t ui8temp; ui8temp = (uint8_t)ADXL355_SPI_Read(POWER_CTL, SPI_READ_ONE_REG); /*Read POWER_CTL register, before modifying it */ ui8temp = ui8temp | 0x01; /* Clear measurement bit in POWER_CTL register */ ADXL355_SPI_Write(POWER_CTL, ui8temp, 0x00, SPI_WRITE_ONE_REG); /* Write the new value to POWER_CTL register */ } /** @brief Reads the accelerometer data. @return none **/ void ADXL355_Data_Scan(void) { ui32SensorX = ADXL355_SPI_Read(XDATA3, SPI_READ_THREE_REG); ui32SensorY = ADXL355_SPI_Read(YDATA3, SPI_READ_THREE_REG); ui32SensorZ = ADXL355_SPI_Read(ZDATA3, SPI_READ_THREE_REG); ui32SensorT = ADXL355_SPI_Read(TEMP2, SPI_READ_TWO_REG); i32SensorX = ADXL355_Acceleration_Data_Conversion(ui32SensorX); i32SensorY = ADXL355_Acceleration_Data_Conversion(ui32SensorY); i32SensorZ = ADXL355_Acceleration_Data_Conversion(ui32SensorZ); } /** @brief Convert the two's complement data in X,Y,Z registers to signed integers @param ui32SensorData - raw data from register @return int32_t - signed integer data **/ int32_t ADXL355_Acceleration_Data_Conversion (uint32_t ui32SensorData) { int32_t volatile i32Conversion = 0; ui32SensorData = (ui32SensorData >> 4); ui32SensorData = (ui32SensorData & 0x000FFFFF); if((ui32SensorData & 0x00080000) == 0x00080000){ i32Conversion = (ui32SensorData | 0xFFF00000); } else{ i32Conversion = ui32SensorData; } return i32Conversion; }
评论
    相关推荐