MPU6050 Six-Axis Sensor
The MPU6050 is an integrated 6-axis motion processing component launched by InvenSense. It internally integrates a 3-axis gyroscope and a 3-axis acceleration sensor, and contains an IIC interface that can be used to connect external magnetic sensors. Using its built-in Digital Motion Processor (DMP) hardware acceleration engine, it can output complete 9-axis fusion calculation data to the application side through the main IIC interface.
InvenSense provides a DMP-based motion processing driver library, which can greatly reduce the load on the microcontroller for motion processing calculations, and also greatly reduces programming difficulty. This module is widely used in electronic products such as flight controllers and pedometers.
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-19589090137.10.478736b4NYZQSb&id=609979451344 Materials download link: https://pan.baidu.com/s/1dNDqcp76L9QdM7iSZYfz_A Extraction code: 4euma
Figure 41.1-1 Product physical display
Specifications
Operating voltage: 3-5V (module with LDO) Operating current: 5MA Communication interface: IIC
View Materials
Common register descriptions
Initialization steps:
- Reset MPU6050 to restore all registers inside MPU6050 to their default values (write 0x80 to 0X6B)
- Set the power management register to 0x00 to wake up MPU6050 and enter normal working state (write 0x00 to 0x6B)
- Gyroscope configuration register (0x1B) sets the full-scale range of the MPU6050 gyroscope sensor; here we select plus/minus 2000dps
- Acceleration sensor configuration register (0x1C); here we select plus/minus 2g
- The gyroscope sampling rate is controlled by the sampling rate divider register (0x19); here it is set to 50Hz, meaning output frequency = 1KHz, SMPLRT_DIV = 19
- Set the digital low-pass filter of MPU6050. Since it is configured as 50Hz, find a close value, so configure it as 0x03, 42Hz
- Set PLL. Generally, the X-axis gyroscope PLL is selected as the clock source to obtain higher precision clock. (Write 0x01 to 0X6B)
- Set both accelerometer and gyroscope to work (write 0x00 to 0X6C)
There is also a register that can be used to detect whether the MPU6050 is present (when AD0 is connected to ground, reading data from 0x75 returns 0x68; when AD0 is connected to VCC, reading data from 0x75 returns 0x69)
The above is the initialization part. After initialization is complete, start reading data. Address for reading temperature:
Read gyroscope measurement values (raw values), including X/Y/Z axis data
Read accelerometer measurement values (raw values), including X/Y/Z axis data
Porting Process
Pin Selection
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting. For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here. However, here we change the file names bsp_dht11.c and bsp_dht11.h to bsp_mpu6050.c and bsp_mpu6050.h, and rename the folder to MPU6050.
Write Code
In the file bsp_mpu6050.c, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-18 LCKFB-lp first version
*/
#include "bsp_mpu6050.h"
#include "stdio.h"
static void delay_ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
static void delay_us(unsigned int us)
{
ets_delay_us(us);
}
static void delay_1ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
static void delay_1us(unsigned int us)
{
ets_delay_us(us);
}
/**********************************************
Function Name: MPU_Init
Function: Initialize MPU6050
Function Parameters: None
Function Return Value: 0, initialization successful other, initialization failed
**********************************************/
u8 MPU_Init(void)
{
u8 res;
i2c_master_init(); // Initialize IIC bus
MPU_Write_Byte(MPU_PWR_MGMT1_REG, 0X80); // Reset MPU6050
vTaskDelay(pdMS_TO_TICKS(100));
MPU_Write_Byte(MPU_PWR_MGMT1_REG, 0X00); // Wake up MPU6050
MPU_Set_Gyro_Fsr(3); // Gyroscope sensor, ±2000dps
MPU_Set_Accel_Fsr(0); // Acceleration sensor, ±2g
MPU_Set_Rate(50); // Set sampling rate to 50Hz
MPU_Write_Byte(MPU_INT_EN_REG, 0X00); // Disable all interrupts
MPU_Write_Byte(MPU_USER_CTRL_REG, 0X00); // I2C master mode disabled
MPU_Write_Byte(MPU_FIFO_EN_REG, 0X00); // Disable FIFO
MPU_Write_Byte(MPU_INTBP_CFG_REG, 0X80); // INT pin active low
res = MPU_Read_Byte(MPU_DEVICE_ID_REG);
if (res == MPU_ADDR) // Device ID correct, i.e., res = MPU_ADDR = 0x68
{
MPU_Write_Byte(MPU_PWR_MGMT1_REG, 0X01); // Set CLKSEL, PLL X-axis as reference
MPU_Write_Byte(MPU_PWR_MGMT2_REG, 0X00); // Both accelerometer and gyroscope work
MPU_Set_Rate(50); // Set sampling rate to 50Hz
}
else
return 1; // Address setting error, return 1
return 0; // Address setting correct, return 0
}
/**********************************************
Function Name: MPU_Set_Gyro_Fsr
Function: Set the full-scale range of the MPU6050 gyroscope sensor
Function Parameters: fsr:0,±250dps;1,±500dps;2,±1000dps;3,±2000dps
Function Return Value: 0, setting successful other, setting failed
**********************************************/
u8 MPU_Set_Gyro_Fsr(u8 fsr)
{
return MPU_Write_Byte(MPU_GYRO_CFG_REG, fsr << 3); // Set gyroscope full-scale range
}
/**********************************************
Function Name: MPU_Set_Accel_Fsr
Function: Set the full-scale range of the MPU6050 acceleration sensor
Function Parameters: fsr:0,±2g;1,±4g;2,±8g;3,±16g
Function Return Value: 0, setting successful other, setting failed
**********************************************/
u8 MPU_Set_Accel_Fsr(u8 fsr)
{
return MPU_Write_Byte(MPU_ACCEL_CFG_REG, fsr << 3); // Set acceleration sensor full-scale range
}
/**********************************************
Function Name: MPU_Set_LPF
Function: Set the digital low-pass filter of MPU6050
Function Parameters: lpf: digital low-pass filter frequency (Hz)
Function Return Value: 0, setting successful other, setting failed
**********************************************/
u8 MPU_Set_LPF(u16 lpf)
{
u8 data = 0;
if (lpf >= 188)
data = 1;
else if (lpf >= 98)
data = 2;
else if (lpf >= 42)
data = 3;
else if (lpf >= 20)
data = 4;
else if (lpf >= 10)
data = 5;
else
data = 6;
return MPU_Write_Byte(MPU_CFG_REG, data); // Set digital low-pass filter
}
/**********************************************
Function Name: MPU_Set_Rate
Function: Set the sampling rate of MPU6050 (assuming Fs=1KHz)
Function Parameters: rate:4~1000(Hz) rate takes 50 during initialization
Function Return Value: 0, setting successful other, setting failed
**********************************************/
u8 MPU_Set_Rate(u16 rate)
{
u8 data;
if (rate > 1000)
rate = 1000;
if (rate < 4)
rate = 4;
data = 1000 / rate - 1;
data = MPU_Write_Byte(MPU_SAMPLE_RATE_REG, data); // Set digital low-pass filter
return MPU_Set_LPF(rate / 2); // Automatically set LPF to half the sampling rate
}
/**********************************************
Function Name: MPU_Get_Temperature
Function: Get temperature sensor value
Function Parameters: None
Function Return Value: Temperature value (multiplied by 100)
**********************************************/
short MPU_Get_Temperature(void)
{
u8 buf[2];
short raw;
float temp;
MPU_Read_Len(MPU_ADDR, MPU_TEMP_OUTH_REG, 2, buf);
raw = ((u16)buf[0] << 8) | buf[1];
temp = 36.53 + ((double)raw) / 340;
return temp * 100;
}
/**********************************************
Function Name: MPU_Get_Gyroscope
Function: Get gyroscope values (raw values)
Function Parameters: gx,gy,gz: raw readings of gyroscope x,y,z axes (signed)
Function Return Value: 0, read successful other, read failed
**********************************************/
u8 MPU_Get_Gyroscope(short *gx, short *gy, short *gz)
{
u8 buf[6], res;
res = MPU_Read_Len(MPU_ADDR, MPU_GYRO_XOUTH_REG, 6, buf);
if (res == 0)
{
*gx = ((u16)buf[0] << 8) | buf[1];
*gy = ((u16)buf[2] << 8) | buf[3];
*gz = ((u16)buf[4] << 8) | buf[5];
}
return res;
}
/**********************************************
Function Name: MPU_Get_Accelerometer
Function: Get acceleration values (raw values)
Function Parameters: ax,ay,az: raw readings of acceleration sensor x,y,z axes (signed)
Function Return Value: 0, read successful other, read failed
**********************************************/
u8 MPU_Get_Accelerometer(short *ax, short *ay, short *az)
{
u8 buf[6], res;
res = MPU_Read_Len(MPU_ADDR, MPU_ACCEL_XOUTH_REG, 6, buf);
if (res == 0)
{
*ax = ((u16)buf[0] << 8) | buf[1];
*ay = ((u16)buf[2] << 8) | buf[3];
*az = ((u16)buf[4] << 8) | buf[5];
}
return res;
}
/**********************************************
Function Name: MPU_Write_Len
Function: IIC continuous write (write device address, register address, data)
Function Parameters: addr: device address reg: register address
len: length of data to write buf: data area
Function Return Value: 0, write successful other, write failed
**********************************************/
u8 MPU_Write_Len(u8 addr, u8 reg, u8 len, u8 *buf)
{
u8 i;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
esp_err_t ack = i2c_master_write_byte(cmd, (addr << 1) | 0, 1); // Send device address + write command (0 for write, 1 for read)
if (ack == ESP_ERR_NO_MEM) // Wait for acknowledge
{
i2c_master_stop(cmd);
return 1;
}
i2c_master_write_byte(cmd, reg, 1); // Write register address
for (i = 0; i < len; i++)
{
esp_err_t ack1 = i2c_master_write_byte(cmd, buf[i], 1); // Send data
if (ack1 == ESP_ERR_NO_MEM) // Wait for acknowledge
{
i2c_master_stop(cmd);
return 1;
}
}
i2c_master_stop(cmd);
i2c_master_cmd_begin(MPU_I2C_POER_NUM, cmd, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
return 0;
}
/**********************************************
Function Name: MPU_Read_Len
Function: IIC continuous read (after writing device address, read register address, data)
Function Parameters: addr: device address reg: register address to read
len: length of data to read buf: storage area for data read
Function Return Value: 0, read successful other, read failed
**********************************************/
u8 MPU_Read_Len(u8 addr, u8 reg, u8 len, u8 *buf)
{
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
esp_err_t ack = i2c_master_write_byte(cmd, (addr << 1) | 0, 1); // Send device address + write command
if (ack == ESP_ERR_NO_MEM) // Wait for acknowledge
{
i2c_master_stop(cmd);
return 1;
}
i2c_master_write_byte(cmd, reg, 1); // Write register address
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (addr << 1) | 1, 1); // Send device address + read command
while (len)
{
if (len == 1)
i2c_master_read_byte(cmd, buf, I2C_MASTER_NACK); // Read data, send NACK
else
i2c_master_read_byte(cmd, buf, I2C_MASTER_ACK); // Read data, send ACK
len--;
buf++;
}
i2c_master_stop(cmd); // Generate a stop condition
i2c_master_cmd_begin(MPU_I2C_POER_NUM, cmd, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
return 0;
}
/**********************************************
Function Name: MPU_Write_Byte
Function: IIC write one byte
Function Parameters: data: data to write reg: register address to write
Function Return Value: 0, write successful other, write failed
**********************************************/
u8 MPU_Write_Byte(u8 reg, u8 data)
{
// Start i2c bus
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
esp_err_t ack = i2c_master_write_byte(cmd, (MPU_ADDR << 1) | 0, true); // Send device address + write command
if (ack == ESP_ERR_NO_MEM) // Wait for acknowledge
{
i2c_master_stop(cmd);
return 1;
}
i2c_master_write_byte(cmd, reg, 1); // Write register address
esp_err_t ack1 = i2c_master_write_byte(cmd, data, 1); // Send data
if (ack1 == ESP_ERR_NO_MEM) // Wait for ACK
{
i2c_master_stop(cmd);
return 1;
}
i2c_master_stop(cmd);
i2c_master_cmd_begin(MPU_I2C_POER_NUM, cmd, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
return 0;
}
/**********************************************
Function Name: MPU_Read_Byte
Function: IIC read one byte
Function Parameters: reg: register address to read
Function Return Value: res: data read
**********************************************/
u8 MPU_Read_Byte(u8 reg)
{
u8 res;
// Start i2c bus
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (MPU_ADDR << 1) | 0, 1); // Send device address + write command
i2c_master_write_byte(cmd, reg, 1); // Write register address
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (MPU_ADDR << 1) | 1, 1); // Send device address + read command
i2c_master_read_byte(cmd, &res, I2C_MASTER_NACK);
i2c_master_stop(cmd); // Generate a stop condition
i2c_master_cmd_begin(MPU_I2C_POER_NUM, cmd, pdMS_TO_TICKS(1000)); // Execute I2C command
i2c_cmd_link_delete(cmd);
return res;
}
// i2c driver installation
esp_err_t i2c_master_init(void)
{
int i2c_master_port = MPU_I2C_POER_NUM;
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = MPU_I2C_SDA,
.scl_io_num = MPU_I2C_SCL,
.sda_pullup_en = GPIO_PULLDOWN_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = MPU_I2C_FREQ,
};
i2c_param_config(i2c_master_port, &conf);
return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
}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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
In the file bsp_mpu6050.h, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-18 LCKFB-lp first version
*/
#ifndef _BSP_MPU6050_H_
#define _BSP_MPU6050_H_
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_rom_sys.h"
#include "esp_timer.h"
#include "driver/uart.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gptimer.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include "driver/spi_master.h"
#include "nvs_flash.h"
#include "esp_adc/adc_cali_scheme.h"
#include "esp_adc/adc_cali.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"
#include "driver/i2c.h"
#include "sdkconfig.h"
typedef uint8_t u8;
typedef uint16_t u16;
#define MPU_I2C_SDA 2 // I2C SDA GPIO NUM
#define MPU_I2C_SCL 1 // I2C SCL GPIO NUM
#define MPU_I2C_POER_NUM 0 // I2C driver number used by MPU6050
#define MPU_I2C_FREQ 400000 // I2C clock for OLED
#define OLED_ADDR 0x3c // OLED I2C device address
#define I2C_MASTER_TX_BUF_DISABLE 0
#define I2C_MASTER_RX_BUF_DISABLE 0
#define I2C_MASTER_TIMEOUT_MS 1000
#define MPU_SELF_TESTX_REG 0X0D // Self-test register X
#define MPU_SELF_TESTY_REG 0X0E // Self-test register Y
#define MPU_SELF_TESTZ_REG 0X0F // Self-test register Z
#define MPU_SELF_TESTA_REG 0X10 // Self-test register A
#define MPU_SAMPLE_RATE_REG 0X19 // Sampling rate divider
#define MPU_CFG_REG 0X1A // Configuration register
#define MPU_GYRO_CFG_REG 0X1B // Gyroscope configuration register
#define MPU_ACCEL_CFG_REG 0X1C // Accelerometer configuration register
#define MPU_MOTION_DET_REG 0X1F // Motion detection threshold setting register
#define MPU_FIFO_EN_REG 0X23 // FIFO enable register
#define MPU_I2CMST_CTRL_REG 0X24 // IIC master control register
#define MPU_I2CSLV0_ADDR_REG 0X25 // IIC slave 0 device address register
#define MPU_I2CSLV0_REG 0X26 // IIC slave 0 data address register
#define MPU_I2CSLV0_CTRL_REG 0X27 // IIC slave 0 control register
#define MPU_I2CSLV1_ADDR_REG 0X28 // IIC slave 1 device address register
#define MPU_I2CSLV1_REG 0X29 // IIC slave 1 data address register
#define MPU_I2CSLV1_CTRL_REG 0X2A // IIC slave 1 control register
#define MPU_I2CSLV2_ADDR_REG 0X2B // IIC slave 2 device address register
#define MPU_I2CSLV2_REG 0X2C // IIC slave 2 data address register
#define MPU_I2CSLV2_CTRL_REG 0X2D // IIC slave 2 control register
#define MPU_I2CSLV3_ADDR_REG 0X2E // IIC slave 3 device address register
#define MPU_I2CSLV3_REG 0X2F // IIC slave 3 data address register
#define MPU_I2CSLV3_CTRL_REG 0X30 // IIC slave 3 control register
#define MPU_I2CSLV4_ADDR_REG 0X31 // IIC slave 4 device address register
#define MPU_I2CSLV4_REG 0X32 // IIC slave 4 data address register
#define MPU_I2CSLV4_DO_REG 0X33 // IIC slave 4 write data register
#define MPU_I2CSLV4_CTRL_REG 0X34 // IIC slave 4 control register
#define MPU_I2CSLV4_DI_REG 0X35 // IIC slave 4 read data register
#define MPU_I2CMST_STA_REG 0X36 // IIC master status register
#define MPU_INTBP_CFG_REG 0X37 // Interrupt/bypass setting register
#define MPU_INT_EN_REG 0X38 // Interrupt enable register
#define MPU_INT_STA_REG 0X3A // Interrupt status register
#define MPU_ACCEL_XOUTH_REG 0X3B // Acceleration value, X-axis high 8-bit register
#define MPU_ACCEL_XOUTL_REG 0X3C // Acceleration value, X-axis low 8-bit register
#define MPU_ACCEL_YOUTH_REG 0X3D // Acceleration value, Y-axis high 8-bit register
#define MPU_ACCEL_YOUTL_REG 0X3E // Acceleration value, Y-axis low 8-bit register
#define MPU_ACCEL_ZOUTH_REG 0X3F // Acceleration value, Z-axis high 8-bit register
#define MPU_ACCEL_ZOUTL_REG 0X40 // Acceleration value, Z-axis low 8-bit register
#define MPU_TEMP_OUTH_REG 0X41 // Temperature value high 8-bit register
#define MPU_TEMP_OUTL_REG 0X42 // Temperature value low 8-bit register
#define MPU_GYRO_XOUTH_REG 0X43 // Gyroscope value, X-axis high 8-bit register
#define MPU_GYRO_XOUTL_REG 0X44 // Gyroscope value, X-axis low 8-bit register
#define MPU_GYRO_YOUTH_REG 0X45 // Gyroscope value, Y-axis high 8-bit register
#define MPU_GYRO_YOUTL_REG 0X46 // Gyroscope value, Y-axis low 8-bit register
#define MPU_GYRO_ZOUTH_REG 0X47 // Gyroscope value, Z-axis high 8-bit register
#define MPU_GYRO_ZOUTL_REG 0X48 // Gyroscope value, Z-axis low 8-bit register
#define MPU_I2CSLV0_DO_REG 0X63 // IIC slave 0 data register
#define MPU_I2CSLV1_DO_REG 0X64 // IIC slave 1 data register
#define MPU_I2CSLV2_DO_REG 0X65 // IIC slave 2 data register
#define MPU_I2CSLV3_DO_REG 0X66 // IIC slave 3 data register
#define MPU_I2CMST_DELAY_REG 0X67 // IIC master delay management register
#define MPU_SIGPATH_RST_REG 0X68 // Signal path reset register
#define MPU_MDETECT_CTRL_REG 0X69 // Motion detection control register
#define MPU_USER_CTRL_REG 0X6A // User control register
#define MPU_PWR_MGMT1_REG 0X6B // Power management register 1
#define MPU_PWR_MGMT2_REG 0X6C // Power management register 2
#define MPU_FIFO_CNTH_REG 0X72 // FIFO count register high 8 bits
#define MPU_FIFO_CNTL_REG 0X73 // FIFO count register low 8 bits
#define MPU_FIFO_RW_REG 0X74 // FIFO read/write register
#define MPU_DEVICE_ID_REG 0X75 // Device ID register
#define MPU_ADDR 0X68
u8 MPU_Init(void); // Initialize MPU6050
u8 MPU_Write_Len(u8 addr,u8 reg,u8 len,u8 *buf); // IIC continuous write
u8 MPU_Read_Len(u8 addr,u8 reg,u8 len,u8 *buf); // IIC continuous read
u8 MPU_Write_Byte(u8 reg,u8 data); // IIC write one byte
u8 MPU_Read_Byte(u8 reg); // IIC read one byte
u8 MPU_Set_Gyro_Fsr(u8 fsr);
u8 MPU_Set_Accel_Fsr(u8 fsr);
u8 MPU_Set_LPF(u16 lpf);
u8 MPU_Set_Rate(u16 rate);
u8 MPU_Set_Fifo(u8 sens);
short MPU_Get_Temperature(void);
u8 MPU_Get_Gyroscope(short *gx,short *gy,short *gz);
u8 MPU_Get_Accelerometer(short *ax,short *ay,short *az);
esp_err_t i2c_master_init(void);
#endif2
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
Porting Verification
In the main function of your own project, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-18 LCKFB-lp first version
*/
#include <stdio.h>
#include "bsp_mpu6050.h"
#include "string.h"
#include "esp_private/esp_task_wdt.h"
#include "esp_private/esp_task_wdt_impl.h"
int app_main(void)
{
esp_task_wdt_deinit();
// Gyroscope values
short gx = 0,gy = 0,gz = 0;
// Acceleration values
short ax = 0,ay = 0,az = 0;
if( MPU_Init() == 1 )
{
printf("MPU_Init Error!!!\r\n");
return 1;
}
printf("Start........\r\n");
while(1)
{
MPU_Get_Gyroscope( &gx , &gy , &gz );
MPU_Get_Accelerometer( &ax , &ay , &az );
printf("\r\n Gyroscope[] value = %d \r\n", gx);
printf("\r\n Gyroscope[] value = %d \r\n", gy);
printf("\r\n Gyroscope[] value = %d \r\n", gz);
puts("");
printf("\r\n Acceleration[] value = %d \r\n", ax);
printf("\r\n Acceleration[] value = %d \r\n", ay);
printf("\r\n Acceleration[] value = %d \r\n", az);
puts("");
gx = 0;
gy = 0;
gz = 0;
ax = 0;
ay = 0;
az = 0;
vTaskDelay(100 / portTICK_PERIOD_MS);// According to the set sampling rate, do not set the delay too large
}
}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
Power-on phenomenon:
Driver code:
File Download
📌 Materials Download Center (click to jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.