Hi All
Software
I have written the code of i2c reading the mpu-9250 device. The communication is between stm32f030 and mpu9250. I am able to read the other device mounted in the PCB through i2c like RTC. But I am not able to read the mpu-9250 device ...
Hardware configuration::
schematic of the MPU-9250 is as per the reference schematic given in the datasheet for I2C operation.
https://invensense.tdk.com/mems/gyro/documents/PS-MPU-9250A-01.pdf
where AD0 is connected to ground and no connection with respect to RESV pins.
I have pasted my code below too ......The address is 0x68
#include "stm32f0xx.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_usart.h"
#include "stm32f0xx_i2c.h"
#include
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
uint8_t sec = 0;
#define RTC_reg_address 0 //adress of the register within ds1307 or RTC
#define RTC_address (0X68 & 0x7f) << 1 // Device address i.e RTC address
#define mpu6050_FLAG_TIMEOUT ((uint32_t)0x1000)
#define mpu6050_LONG_TIMEOUT ((uint32_t)(500 * mpu6050_FLAG_TIMEOUT))
void USART_send( unsigned char data){
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, data);
}
void USART_print(char* StringPtr){
while(*StringPtr != 0x00){
USART_send(*StringPtr);
StringPtr++;}
}
uint8_t ReadReg(uint8_t RegAdd)
{
uint8_t mpu6050_BufferRX[2] ={0,0};
uint8_t temp;
uint32_t DataNum = 0;
int mpu6050_Timeout = 0;
/* Test on BUSY Flag */
#if 1
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C2, I2C_ISR_BUSY) != RESET)
{
if((mpu6050_Timeout--) == 0) USART_print(" error1 ");
}
#endif
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(I2C2, RTC_address, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
/* Wait until TXIS flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C2, I2C_ISR_TXIS) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print(" error2 ");
}
/* Send Register address */
I2C_SendData(I2C2, (uint8_t)RegAdd);
/* Wait until TC flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C2, I2C_ISR_TC) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print(" error3 ");
}
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(I2C2, RTC_address, 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
/* Reset local variable */
DataNum = 0;
/* Wait until all data are received */
while (DataNum != 1)
{
/* Wait until RXNE flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C2, I2C_ISR_RXNE) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print(" error4 ");
}
/* Read data from RXDR */
mpu6050_BufferRX[DataNum]= I2C_ReceiveData(I2C2);
/* Update number of received data */
DataNum++;
}
/* Wait until STOPF flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C2, I2C_ISR_STOPF) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print(" error5 ");
}
/* Clear STOPF flag */
I2C_ClearFlag(I2C2, I2C_ICR_STOPCF);
// !< Store LM75_I2C received data
//tmp = (uint16_t)(LM75_BufferRX[0] << 8);
//tmp |= LM75_BufferRX[0];
temp = mpu6050_BufferRX[0];
// return a Reg value
return (uint8_t)temp;
}
uint8_t WRiteReg(uint8_t data,uint8_t RegAdd)
{
uint8_t mpu6050_BufferRX[2] ={0,0};
uint8_t temp;
uint32_t DataNum = 0;
int mpu6050_Timeout = 0;
/* Test on BUSY Flag */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C2, I2C_ISR_BUSY) != RESET)
{
if((mpu6050_Timeout--) == 0) USART_print(" error1 ");
}
/* Send Start*/
I2C_GenerateSTART(I2C2, ENABLE);
/* Send Device address address of the DS1307*/
I2C_SendData(I2C1, RTC_address);
/* Send the address of register within device DS1307*/
I2C_SendData(I2C1, (uint8_t)RegAdd);
/* Wait until TC flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C2, I2C_ISR_TC) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print(" error3 ");
}
/* Send the data to write in internal register*/
I2C_SendData(I2C2, (uint8_t)data);
/* Wait until TC flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C2, I2C_ISR_TC) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print(" error3 ");
}
I2C_GenerateSTOP(I2C2, ENABLE);
/*stop bit flag*/
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF));
}
void LED_init(void){
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void USART_init(void){
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
//Configure pins: Rx and Tx
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
void I2C_init(void){
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
/* Configure the I2C clock source. The clock is derived from the HSI */
// RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_1);
//Configure pins: SCL and SDA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStructure.I2C_DigitalFilter = 0x00;
I2C_InitStructure.I2C_OwnAddress1 = 0x00; // MPU6050 7-bit adress = 0x68, 8-bit adress = 0xD0;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
//I2C_InitStructure.I2C_Timing = 0xA0120227;
I2C_InitStructure.I2C_Timing = 0x20310A0D;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C2, &I2C_InitStructure);
I2C_Cmd(I2C2, ENABLE);
}
int main(void)
{
// LED_init();
// USART_init();
I2C_init();
uint8_t data = (0X10 & 0x7f) << 1;
int i=0;
while(1)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty
// WRiteReg(data,RTC);
sec = ReadReg(RTC_reg_address);
GPIO_SetBits(GPIOC, (GPIO_Pin_8 | GPIO_Pin_9));
for(i=0;i<10000000;i++);
/*
int i=0;
if(USART_ReceiveData(USART1)=='x'){
GPIO_SetBits(GPIOC, GPIO_Pin_8);
}
for(i=0;i<1000000;i++);
GPIO_ResetBits(GPIOC, GPIO_Pin_8);
for(i=0;i<1000000;i++);
*/
}
}
Please help!
- Log in to post comments
phpbb Topic ID
16767