Answer the question
In order to leave comments, you need to log in
How to read data from MPU-6050 gyroscope?
Hello! I have a mega16 microcontroller and an MPU-6050 gyroscope module. I'm trying to communicate with him on the TWI bus.
Communication was built according to the protocol from the datasheet and it seems that the module sends me an ACK confirmation bit, but how it comes to reading data from the TWDR register there are only zeros! Tell me what's wrong.
Here is the communication protocol:
unsigned char MPU_I2C_R(unsigned char w,unsigned char x, unsigned char r)
{
char a;
I2C_start();//отправляем условие СТАРТ
I2C_tranciv_byte(w);//отправляем адрес модуля на запись
I2C_tranciv_byte(x);//отправляем адрес регистра откуда будем читать данные
I2C_start();//отправляем повторный СТАРТ
I2C_tranciv_byte(r);//отправляем адрес модуля на чтение
a = I2C_receiver_last_byte();//читаем данные из регистра
I2C_stop();//отправляем условие СТОП
return a;
}
Вот функция чтения чтения данных
unsigned char I2C_receiver_last_byte()
{
TWCR = (1<<TWINT)|(1<<TWEN);// включили прием данных <--вот после этого в отладке регистр TWDR обнуляется
while(!(TWCR&(1<<TWINT))); //ждем установку бита TWIN
return TWDR;
}
Answer the question
In order to leave comments, you need to log in
Are you writing code without using libraries? Could this be of any help? https://www.cl.cam.ac.uk/teaching/1011/P31/lib/i2c.c
Here are the details - www.gaw.ru/html.cgi/txt/doc/micros/avr/arh128/18_5. htm
Thanks everyone for the replies, it worked for me! the point was that I did not initialize the PWR_MGMT_1 register in any way, which, as I understand it, if you do not write ZERO into it, constantly resets the values of all measuring registers. Therefore, zeros were issued ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question