当前位置:   article > 正文

【驱动程序】MPU6050_CubeMX_HAL库_mpu6050驱动程序

mpu6050驱动程序

MPU6050驱动程序_CubeMX_HAL库_STM32F103C8T6

找了很久没有找到HAL库版本的MPU6050驱动程序,就写了一个,方便大家移植使用;如有错误,欢迎指正

程序中使用了I2C通信,和中值+均值滤波
使用芯片:STM32F103C8T6
工程文件参考:

链接:https://pan.baidu.com/s/1qnkTfxwq6kDg8zV_nqhM_Q 
提取码:c8t6
  • 1
  • 2

接线:

MPU6050:

  • VCC - 3.3v
  • GND - 接地
  • SCl - PB6
  • SDA - PB7
  • 其余悬空

共四个代码文件

  1. MPU6050.c
  2. MPU6050.h
  3. i2c_hal.c
  4. i2c_hal.h

MPU6050.c

#define MPU6050_ADDR 0x68
#define MPU6050_WHO_AM_I 0x75
#define MPU6050_PWR_MGMT_1 0x6B
#define MPU6050_ACCEL_XOUT_H 0x3B
#define MPU6050_GYRO_XOUT_H 0x43
#include "i2c_hal.h"

unsigned int Accel_X, Accel_Y, Accel_Z;
unsigned int Gyro_X, Gyro_Y, Gyro_Z;
unsigned int Temp;

void MPU6050_WriteReg(uint8_t reg, uint8_t data) {
    I2CStart();
    I2CSendByte((MPU6050_ADDR << 1) | 0);  // 写操作
    I2CWaitAck();
    I2CSendByte(reg);
    I2CWaitAck();
    I2CSendByte(data);
    I2CWaitAck();
    I2CStop();
}

uint8_t MPU6050_ReadReg(uint8_t reg) {
    uint8_t data;
    I2CStart();
    I2CSendByte((MPU6050_ADDR << 1) | 0);  // 写操作
    I2CWaitAck();
    I2CSendByte(reg);
    I2CWaitAck();
    I2CStart();
    I2CSendByte((MPU6050_ADDR << 1) | 1);  // 读操作
    I2CWaitAck();
    data = I2CReceiveByte();
    I2CSendNotAck();
    I2CStop();
    return data;
}

void MPU6050_Init(void) {
    uint8_t check = MPU6050_ReadReg(MPU6050_WHO_AM_I);
    if (check == 0x68) { // 检查设备ID
        MPU6050_WriteReg(MPU6050_PWR_MGMT_1, 0x00); // 退出睡眠模式
        // 配置加速度计和陀螺仪
        MPU6050_WriteReg(0x1C, 0x00); // 配置加速度计范围(±2g)
        MPU6050_WriteReg(0x1B, 0x08); // 配置陀螺仪范围(±500°/s)
    }
}
unsigned char Rec_Data[14] = {0};
void MPU6050_Read(void)
{
	for (int i = 0; i < 14; i++) 
	{
		Rec_Data[i] = MPU6050_ReadReg(MPU6050_ACCEL_XOUT_H + i);
	}
	Accel_X = (unsigned int)((255-Rec_Data[0]) << 8 | Rec_Data[1]); // 加速度
	Accel_Y = (unsigned int)((255-Rec_Data[2]) << 8 | Rec_Data[3]);
	Accel_Z = (unsigned int)((255-Rec_Data[4]) << 8 | Rec_Data[5]);
	
	Gyro_X = (unsigned int)((255-Rec_Data[6]) << 8 | Rec_Data[7]); // 角度
	Gyro_Y = (unsigned int)((255-Rec_Data[8]) << 8 | Rec_Data[9]);
	Gyro_Z = (unsigned int)((255-Rec_Data[10]) << 8 | Rec_Data[11]);
	
	Temp = (unsigned int)((255-Rec_Data[12]) << 8 | Rec_Data[13]); // 温度
	
	Mean_Filtering(Accel_X,0); // 滤波处理
	Mean_Filtering(Accel_Y,1);
	Mean_Filtering(Accel_Z,2);
	Mean_Filtering(Gyro_X,3);
	Mean_Filtering(Gyro_Y,4);
	Mean_Filtering(Gyro_Z,5);
	Mean_Filtering(Temp,6);
}

//-中值+均值滤波-------------------------

unsigned int Mean_Value_all[7][VALUE_N] = {0}; // 储存值
unsigned int Mean_Value[7] = {0}; // 滤波后的值
unsigned int Value_i[7] = {0}; // 数组下标

void Mean_Filtering(unsigned int value,unsigned int name)
{
	Mean_Value_all[name][Value_i[name]] = value;
	Value_i[name]++;
	if(Value_i[name] > VALUE_N)
	{
		Value_i[name] = 0;
		unsigned int n = sizeof(&Mean_Value_all[7][0]) / sizeof(Mean_Value_all[7][0]);
		bubbleSort(&Mean_Value_all[name][0],VALUE_N);
		Mean_Value[name] = 0;
		for(int i = 3; i < VALUE_N-3; i++)
		{
			Mean_Value[name] += Mean_Value_all[name][i];
		}
		Mean_Value[name] /= (VALUE_N-6);
	}

}

//冒泡排序
void bubbleSort(unsigned int arr[], unsigned int n) {
	unsigned int i, j, temp;
	for (i = 0; i < n - 1; i++) 
	{
		for (j = 0; j < n - i - 1; j++) 
		{
			if ((int)arr[j] > (int)arr[j + 1]) 
			{
				temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114

MPU6050.h

#ifndef _MPU6050_H_
#define _MPU6050_H_
#include "i2c_hal.h"
#include "main.h"

/*-MPU6050===================================================================*/
extern unsigned int Accel_X, Accel_Y, Accel_Z;
extern unsigned int Gyro_X, Gyro_Y, Gyro_Z;

void MPU6050_WriteReg(uint8_t reg, uint8_t data);
uint8_t MPU6050_ReadReg(uint8_t reg);
void MPU6050_Init(void);
void MPU6050_Read(void);
extern unsigned char Rec_Data[14];

//-均值滤波-------------------------
#define VALUE_N 10  // 每组排序的成员个数
extern unsigned int Mean_Value[7];
void Mean_Filtering(unsigned int value,unsigned int name);
void bubbleSort(unsigned int arr[], unsigned int n);
#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

i2c_hal.c

/**
 * CT117E-M4 / GPIO - I2C
*/

#include "i2c_hal.h"

#define DELAY_TIME 20

void SDA_Input_Mode(void) {
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    GPIO_InitStructure.Pin = GPIO_PIN_7;
    GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
    GPIO_InitStructure.Pull = GPIO_PULLUP;
    GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}

void SDA_Output_Mode(void) {
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    GPIO_InitStructure.Pin = GPIO_PIN_7;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
    GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}

void SDA_Output(uint16_t val) {
    if (val) {
        GPIOB->BSRR |= GPIO_PIN_7;
    } else {
        GPIOB->BRR |= GPIO_PIN_7;
    }
}

void SCL_Output(uint16_t val) {
    if (val) {
        GPIOB->BSRR |= GPIO_PIN_6;
    } else {
        GPIOB->BRR |= GPIO_PIN_6;
    }
}

uint8_t SDA_Input(void) {
    return HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7) == GPIO_PIN_SET ? 1 : 0;
}

static void delay1(unsigned int n) {
    uint32_t i;
    for (i = 0; i < n; ++i);
}

void I2CStart(void) {
    SDA_Output(1);
    delay1(DELAY_TIME);
    SCL_Output(1);
    delay1(DELAY_TIME);
    SDA_Output(0);
    delay1(DELAY_TIME);
    SCL_Output(0);
    delay1(DELAY_TIME);
}

void I2CStop(void) {
    SCL_Output(0);
    delay1(DELAY_TIME);
    SDA_Output(0);
    delay1(DELAY_TIME);
    SCL_Output(1);
    delay1(DELAY_TIME);
    SDA_Output(1);
    delay1(DELAY_TIME);
}

unsigned char I2CWaitAck(void) {
    unsigned short cErrTime = 5;
    SDA_Input_Mode();
    delay1(DELAY_TIME);
    SCL_Output(1);
    delay1(DELAY_TIME);
    while (SDA_Input()) {
        cErrTime--;
        delay1(DELAY_TIME);
        if (0 == cErrTime) {
            SDA_Output_Mode();
            I2CStop();
            return ERROR;
        }
    }
    SCL_Output(0);
    SDA_Output_Mode();
    delay1(DELAY_TIME);
    return SUCCESS;
}

void I2CSendAck(void) {
    SDA_Output(0);
    delay1(DELAY_TIME);
    SCL_Output(1);
    delay1(DELAY_TIME);
    SCL_Output(0);
    delay1(DELAY_TIME);
}

void I2CSendNotAck(void) {
    SDA_Output(1);
    delay1(DELAY_TIME);
    SCL_Output(1);
    delay1(DELAY_TIME);
    SCL_Output(0);
    delay1(DELAY_TIME);
}

void I2CSendByte(unsigned char cSendByte) {
    unsigned char i = 8;
    while (i--) {
        SCL_Output(0);
        delay1(DELAY_TIME);
        SDA_Output(cSendByte & 0x80);
        delay1(DELAY_TIME);
        cSendByte <<= 1;
        SCL_Output(1);
        delay1(DELAY_TIME);
    }
    SCL_Output(0);
    delay1(DELAY_TIME);
}

unsigned char I2CReceiveByte(void) {
    unsigned char i = 8;
    unsigned char cR_Byte = 0;
    SDA_Input_Mode();
    while (i--) {
        cR_Byte <<= 1;
        SCL_Output(0);
        delay1(DELAY_TIME);
        SCL_Output(1);
        delay1(DELAY_TIME);
        cR_Byte |= SDA_Input();
    }
    SCL_Output(0);
    delay1(DELAY_TIME);
    SDA_Output_Mode();
    return cR_Byte;
}

void I2CInit(void) {
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    GPIO_InitStructure.Pin = GPIO_PIN_7 | GPIO_PIN_6;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStructure.Pull = GPIO_PULLUP;
    GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154

i2c_hal.h

#ifndef __I2C_HAL_H
#define __I2C_HAL_H

#include "stm32f1xx_hal.h"

#define SUCCESS 0
#define ERROR 1

void SDA_Input_Mode(void);
void SDA_Output_Mode(void);
void SDA_Output(uint16_t val);
void SCL_Output(uint16_t val);
uint8_t SDA_Input(void);
void I2CStart(void);
void I2CStop(void);
unsigned char I2CWaitAck(void);
void I2CSendAck(void);
void I2CSendNotAck(void);
void I2CSendByte(unsigned char cSendByte);
unsigned char I2CReceiveByte(void);
void I2CInit(void);

#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号