Attempt at making MPU6050 work via i2c
This commit is contained in:
parent
bdb94eb75b
commit
fd45ec264a
5 changed files with 105 additions and 2 deletions
49
MPU6050.c
49
MPU6050.c
|
@ -1 +1,50 @@
|
|||
#include <stdint.h>
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
#include "MPU6050.h"
|
||||
#include "i2c.h"
|
||||
|
||||
// Function to initialize MPU-6050
|
||||
void MPU6050_Init() {
|
||||
// Wake up MPU-6050 by writing to PWR_MGMT_1 register
|
||||
I2C_start(MPU6050_ADDR << 1 | TW_WRITE);
|
||||
I2C_write(MPU6050_REG_PWR_MGMT_1);
|
||||
I2C_write(0x00); // Clear sleep mode bit
|
||||
I2C_stop();
|
||||
}
|
||||
|
||||
// Function to read accelerometer data from MPU-6050
|
||||
void MPU6050_Read_Accel(int16_t* accel_data) {
|
||||
uint8_t buffer[6]; // Buffer to store accelerometer data
|
||||
// Read accelerometer data starting from ACCEL_XOUT_H register
|
||||
I2C_start(MPU6050_ADDR << 1 | TW_WRITE);
|
||||
I2C_write(MPU6050_REG_ACCEL_XOUT_H);
|
||||
I2C_start(MPU6050_ADDR << 1 | TW_READ);
|
||||
for (int i = 0; i < 6; i++) {
|
||||
buffer[i] = I2C_read(i < 5); // Read 6 bytes with ACK for all except last byte
|
||||
}
|
||||
I2C_stop();
|
||||
|
||||
// Combine high and low bytes for each axis
|
||||
accel_data[0] = (buffer[0] << 8) | buffer[1]; // X-axis
|
||||
accel_data[1] = (buffer[2] << 8) | buffer[3]; // Y-axis
|
||||
accel_data[2] = (buffer[4] << 8) | buffer[5]; // Z-axis
|
||||
}
|
||||
|
||||
// Function to read gyroscope data from MPU-6050
|
||||
void MPU6050_Read_Gyro(int16_t* gyro_data) {
|
||||
uint8_t buffer[6]; // Buffer to store gyroscope data
|
||||
// Read gyroscope data starting from GYRO_XOUT_H register
|
||||
I2C_start(MPU6050_ADDR << 1 | TW_WRITE);
|
||||
I2C_write(MPU6050_REG_GYRO_XOUT_H);
|
||||
I2C_start(MPU6050_ADDR << 1 | TW_READ);
|
||||
for (int i = 0; i < 6; i++) {
|
||||
buffer[i] = I2C_read(i < 5); // Read 6 bytes with ACK for all except last byte
|
||||
}
|
||||
I2C_stop();
|
||||
|
||||
// Combine high and low bytes for each axis
|
||||
gyro_data[0] = (buffer[0] << 8) | buffer[1]; // X-axis
|
||||
gyro_data[1] = (buffer[2] << 8) | buffer[3]; // Y-axis
|
||||
gyro_data[2] = (buffer[4] << 8) | buffer[5]; // Z-axis
|
||||
}
|
||||
|
|
19
MPU6050.h
19
MPU6050.h
|
@ -1,3 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
// I2c address of MPU6050
|
||||
#define MPU6050_ADDR 0x68 // With AD0 pin grounded/floating, otherwise 0x69
|
||||
|
||||
// MPU-6050 register addresses
|
||||
#define MPU6050_REG_ACCEL_XOUT_H 0x3B
|
||||
#define MPU6050_REG_GYRO_XOUT_H 0x43
|
||||
#define MPU6050_REG_PWR_MGMT_1 0x6B
|
||||
|
||||
#include <stdint.h>
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
// Function to initialize MPU-6050
|
||||
void MPU6050_Init();
|
||||
|
||||
// Function to read accelerometer data from MPU-6050
|
||||
void MPU6050_Read_Accel(int16_t* accel_data);
|
||||
|
||||
// Function to read gyroscope data from MPU-6050
|
||||
void MPU6050_Read_Gyro(int16_t* gyro_data);
|
||||
|
|
3
i2c.h
3
i2c.h
|
@ -1,6 +1,9 @@
|
|||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
#define TW_WRITE 0
|
||||
#define TW_READ 1
|
||||
|
||||
void I2C_init();
|
||||
uint8_t I2C_start(uint8_t addr);
|
||||
void I2C_stop();
|
||||
|
|
28
main.c
28
main.c
|
@ -34,6 +34,10 @@ void blink() {
|
|||
|
||||
int main(void) {
|
||||
initUART();
|
||||
UART_println("ATmega328P Initialized!");
|
||||
I2C_init();
|
||||
UART_println("I2C Initialized!");
|
||||
MPU6050_Init();
|
||||
|
||||
while(1) {
|
||||
UART_println("Hello, World!");
|
||||
|
@ -42,3 +46,27 @@ int main(void) {
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// int main() {
|
||||
// int16_t accel_data[3]; // Array to store accelerometer data (X, Y, Z)
|
||||
// int16_t gyro_data[3]; // Array to store gyroscope data (X, Y, Z)
|
||||
|
||||
// // Initialize MPU-6050
|
||||
// i2c_init();
|
||||
// MPU6050_Init();
|
||||
|
||||
// while (1) {
|
||||
// // Read accelerometer data
|
||||
// MPU6050_Read_Accel(accel_data);
|
||||
// // Read gyroscope data
|
||||
// MPU6050_Read_Gyro(gyro_data);
|
||||
|
||||
// // Print accelerometer and gyroscope data
|
||||
// printf("Accelerometer (mg): X=%d, Y=%d, Z=%d\n", accel_data[0], accel_data[1], accel_data[2]);
|
||||
// printf("Gyroscope (°/s): X=%d, Y=%d, Z=%d\n", gyro_data[0], gyro_data[1], gyro_data[2]);
|
||||
|
||||
// _delay_ms(1000); // Delay for 1 second
|
||||
// }
|
||||
|
||||
// return 0;
|
||||
// }
|
4
makefile
4
makefile
|
@ -64,3 +64,7 @@ clean:
|
|||
|
||||
size: $(TARGET).hex
|
||||
avr-size --mcu=atmega328p $(TARGET).hex
|
||||
|
||||
# Reset target
|
||||
reset:
|
||||
avrdude -p $(MCU) -c $(PROGRAMMER) -E reset
|
Loading…
Reference in a new issue