赞
踩
找了很久没有找到HAL库版本的MPU6050驱动程序,就写了一个,方便大家移植使用;如有错误,欢迎指正
程序中使用了I2C通信,和中值+均值滤波
使用芯片:STM32F103C8T6
工程文件参考:
链接:https://pan.baidu.com/s/1qnkTfxwq6kDg8zV_nqhM_Q
提取码:c8t6
MPU6050:
共四个代码文件
#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; } } } }
#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
/** * 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); }
#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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。