本篇文章源码:STM32L431_RT_Thread_PM_mpu6050_wakeup: 使用MPU6050产生中断,唤醒休眠中的STM32L4
书接上回【笔记】STM32L4系列使用RT-Thread Studio电源管理组件(PM框架)实现低功耗-CSDN博客
上一篇文章使用PA0外接一个按键实现唤醒单片机,因为公司项目需求,需要设备在MPU6050产生位移时唤醒单片机,话不多说直接开始
一.使用MPU6XXX软件包
去掉磁力计,打开示例工程
保存,编译
二.配置RTT工程
打开模拟I2C
打开I2C1宏定义
将packages\mpu6xxx-v1.1.1\samples\mpu6xxx_sample.c里边的i2c2改为i2c1
编译下载,然后终端输入mpu6xxx_test,现象:
MPU6050驱动成功
三.修改MPU6050驱动
因为MPU6050要经过配置,才能产生正确的中断信号,我们直接去修改源码
在packages\mpu6xxx-v1.1.1\src\mpu6xxx.c里找到struct mpu6xxx_device *mpu6xxx_init(const char *dev_name, rt_uint8_t param)
在源码的配置下边加入我们自己的配置
// 复位设备mpu6xxx_write_reg(dev, MPU6XXX_RA_PWR_MGMT_1, 0x80); // 复位HAL_Delay(100);// 清除复位并配置电源管理mpu6xxx_write_reg(dev, MPU6XXX_RA_PWR_MGMT_1, 0x01); // 设置时钟源为 PLL with X axis gyro referencempu6xxx_write_reg(dev, MPU6XXX_RA_PWR_MGMT_2, 0x00); // 启用所有传感器// 配置加速度计mpu6xxx_write_reg(dev, MPU6XXX_RA_ACCEL_CONFIG, 0x00); // ±2g// 配置陀螺仪mpu6xxx_write_reg(dev, MPU6XXX_RA_GYRO_CONFIG, 0x08); // ±500°/s// 配置数字低通滤波器mpu6xxx_write_reg(dev, MPU6XXX_RA_CONFIG, 0x00); // 禁用 DLPF// 配置采样率分频器mpu6xxx_write_reg(dev, MPU6XXX_RA_SMPLRT_DIV, 0x00); // 不分频// 配置运动检测mpu6xxx_write_reg(dev, MPU6XXX_RA_MOT_THR, 0x20); // 设置运动检测阈值mpu6xxx_write_reg(dev, MPU6XXX_RA_MOT_DUR, 0x01); // 设置运动检测持续时间// 配置中断引脚mpu6xxx_write_reg(dev, MPU6XXX_RA_INT_PIN_CFG, 0x10); // 高电平有效,推挽输出// 启用运动检测中断mpu6xxx_write_reg(dev, MPU6XXX_RA_INT_ENABLE, 0x40); // 启用运动检测中断
修改packages\mpu6xxx-v1.1.1\samples\mpu6xxx_sample.c整个文件
/** Copyright (c) 2006-2022, RT-Thread Development Team** SPDX-License-Identifier: Apache-2.0** Change Logs:* Date Author Notes* 2018-10-23 flybreak the first version*/#include <rtthread.h>
#include "mpu6xxx.h"/* Default configuration, please change according to the actual situation, support i2c and spi device name */
#define MPU6XXX_DEVICE_NAME "i2c1"static void mpu6xxx_thread_entry(void *parameter)
{struct mpu6xxx_device *dev;struct mpu6xxx_3axes accel, gyro;/* Initialize mpu6xxx, The parameter is RT_NULL, means auto probing for i2c*/dev = mpu6xxx_init(MPU6XXX_DEVICE_NAME, RT_NULL);if (dev == RT_NULL){rt_kprintf("mpu6xxx init failed\n");return;}rt_kprintf("mpu6xxx init succeed\n");while (1){mpu6xxx_get_accel(dev, &accel);mpu6xxx_get_gyro(dev, &gyro);rt_kprintf("accel.x = %3d, accel.y = %3d, accel.z = %3d ", accel.x, accel.y, accel.z);rt_kprintf("gyro.x = %3d gyro.y = %3d, gyro.z = %3d\r\n", gyro.x, gyro.y, gyro.z);rt_thread_mdelay(500);}mpu6xxx_deinit(dev);
}static int mpu6xxx_test()
{rt_thread_t tid;/* Create a thread to run the mpu6xxx sensor test */tid = rt_thread_create("mpu6xxx_test", mpu6xxx_thread_entry, RT_NULL, 1024, 25, 10);if (tid != RT_NULL){rt_thread_startup(tid);rt_kprintf("mpu6xxx test thread created successfully\n");}else{rt_kprintf("Failed to create mpu6xxx test thread\n");return -1;}return 0;
}MSH_CMD_EXPORT(mpu6xxx_test, mpu6xxx sensor test function);
编译下载,将MPU6050的INT引脚接在板子的PA0,在终端输入mpu6xxx_test,每次晃动传感器,都会触发中断