Bác nào đã từng làm chỉ em với, em làm mà ko biết sai chỗ nào code hay phần cứng MPU6050 bị die. Sẵn cho em hỏi là có cần nối trở kéo 2 chân SCL(RC3) và SDA(RC4) của 16f877a để giao tiếp với MPU ko? vì thấy sơ đồ con này hình như đã có điện trở kéo 4k7 rồi thì phải?
sơ đồ đấu chân thì
vcc MPU =3.3V
GND
SCL MPU= RC3
SDA = RC4
sơ đồ đấu chân thì
vcc MPU =3.3V
GND
SCL MPU= RC3
SDA = RC4
Code:
#include <16f877a.h> #fuses nolvp, noprotect, nowdt, put, hs #use delay(clock=20M) #use rs232(baud=9600, xmit=pin_c6, rcv=pin_c7) #use i2c(master, sda=pin_c4, scl=pin_c3) #define LCD_RS_PIN PIN_D0 #define LCD_RW_PIN PIN_D1 #define LCD_ENABLE_PIN PIN_D2 #define LCD_DATA4 PIN_D4 #define LCD_DATA5 PIN_D5 #define LCD_DATA6 PIN_D6 #define LCD_DATA7 PIN_D7 //+++++++++++++++++++++++++DEFINE MPU6050+++++++++++++++++++++++++++ #define SMPLRT_DIV 0x19 //0x07(125Hz) #define CONFIG 0x1A //0x06(5Hz) #define GYRO_CONFIG 0x1B //0x18(2000deg/s) #define ACCEL_CONFIG 0x1C //0x01(2G£¬5Hz) #define ACCEL_XOUT_H 0x3B #define ACCEL_XOUT_L 0x3C #define ACCEL_YOUT_H 0x3D #define ACCEL_YOUT_L 0x3E #define ACCEL_ZOUT_H 0x3F #define ACCEL_ZOUT_L 0x40 #define TEMP_OUT_H 0x41 #define TEMP_OUT_L 0x42 #define GYRO_XOUT_H 0x43 #define GYRO_XOUT_L 0x44 #define GYRO_YOUT_H 0x45 #define GYRO_YOUT_L 0x46 #define GYRO_ZOUT_H 0x47 #define GYRO_ZOUT_L 0x48 #define PWR_MGMT_1 0x6B // #define PWR_MGMT_2 0x6C // #define WHO_AM_I 0x75 // #define MPU6050_ADD 0x68 #define MPU6050_SLAVE_WRT 0xD0 //dia chi cua cam bien mpu6050 #define MPU6050_SLAVE_RD 0xD1 #include "lcd.c" void Mpu6050_init(); int Mpu6050_Read(unsigned char address) { char msb; i2c_start(); i2c_write(MPU6050_SLAVE_WRT); i2c_write(address); i2c_start(); i2c_write(MPU6050_SLAVE_RD); msb = i2c_read(1); i2c_stop(); return msb; } void Mpu6050_Write(unsigned char address,unsigned char Data) { i2c_start(); i2c_write(MPU6050_SLAVE_WRT); i2c_write(address); i2c_write(Data); i2c_stop(); } int GetData(unsigned char address) { char H,L; H=Mpu6050_Read(address); L=Mpu6050_Read(address+1); return (H<<8)+L; } void Mpu6050_init() { Mpu6050_Write(PWR_MGMT_1,0x00); // internal 8MHz, disabled SLEEP mode, disable CYCLE mode Mpu6050_Write(SMPLRT_DIV,0x07); //sample rate: 8khz Mpu6050_Write(CONFIG,0x06); //DLPF disable Mpu6050_Write(GYRO_CONFIG,0x18); //full scale range mode 3 +-2000do/s Mpu6050_Write(ACCEL_CONFIG,0x01); //full scale range mode 1 +-4g } //=================================== void main() { int x_accel,y_accel,z_accel,x_gyro,y_gyro,z_gyro; lcd_init(); lcd_gotoxy(1,1); printf(lcd_putc," demo...."); delay_ms(1000); while(1) { x_accel = GetData(ACCEL_XOUT_H); y_accel = GetData(ACCEL_YOUT_H); z_accel = GetData(ACCEL_ZOUT_H); x_gyro = GetData(GYRO_XOUT_H); y_gyro = GetData(GYRO_YOUT_H); z_gyro = GetData(GYRO_ZOUT_H); lcd_init(); Mpu6050_init(); printf(lcd_putc,"a/g : %d,%d,%d\n %d,%d,%d",x_accel,y_accel,z_accel,x_gyro,y_gyro,z_gyro); printf("\r\n a/g : %d,%d,%d, %d,%d,%d",x_accel,y_accel,z_accel,x_gyro,y_gyro,z_gyro); delay_ms(50); } }
Comment