5. 实验
先将模块静止在桌面,然后晃动模块,最后用体温对芯片进行热传递,观察OLED显示的加速度值、角速度值和温度值变化。
主函数内容如下:
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "oled.h"
#include "MPU6050.h"
#include "stdio.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8_t accelX_buff[16]={0},accelY_buff[16]={0},accelZ_buff[16]={0};
uint8_t gyroX_buff[16]={0},gyroY_buff[16]={0},gyroZ_buff[16]={0};
uint8_t temp_buff[16]={0};
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
// 定义一个MPU6050结构体
MPU6050_t mpu6050
= {
.hi2c=&hi2c2,
.devAddr = 0xD0, // 设备地址
.initialized = 0 // 设备是否已初始化
};
float ax, ay, az, gx,gy,gz,temp;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_I2C2_Init();
/* USER CODE BEGIN 2 */
OLED_Init();
OLED_Clear();
MPU6050_Init(&mpu6050, &hi2c2);
if (mpu6050.initialized)
{
OLED_ShowString(0,0,"MPU6050 initialized successfully!",16);
}
else
{
OLED_ShowString(0,0,"MPU6050 initialization failed.",16);
return -1;
}
OLED_Clear();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
/* 读取加速度计数据 */
MPU6050_ReadAccel(&mpu6050, &ax, &ay, &az);
/* 读取加速度计数据 */
MPU6050_ReadGyro(&mpu6050, &gx, &gy, &gz);
/* 读取温度数据 */
temp = MPU6050_ReadTemp(&mpu6050);
/* 打印数据到串口 */
sprintf((char*)accelX_buff,"AccelX:%-.3f ",ax);
OLED_ShowString(0,0,accelX_buff,8);
sprintf((char*)accelY_buff,"AccelY:%-.3f ",ay);
OLED_ShowString(0,1,accelY_buff,8);
sprintf((char*)accelZ_buff,"AccelZ:%-.3f ",az);
OLED_ShowString(0,2,accelZ_buff,8);
sprintf((char*)gyroX_buff,"Gyro X:%-.3f ",gx);
OLED_ShowString(0,3,gyroX_buff,8);
sprintf((char*)gyroY_buff,"Gyro Y:%-.3f ",gy);
OLED_ShowString(0,4,gyroY_buff,8);
sprintf((char*)gyroZ_buff,"Gyro Z:%-.3f ",gz);
OLED_ShowString(0,5,gyroZ_buff,8);
sprintf((char*)temp_buff,"Temp :%-.3f ",temp);
OLED_ShowString(0,6,temp_buff,8);
}
/* USER CODE END 3 */
}
|