cancel
Showing results for 
Search instead for 
Did you mean: 

MPU6050 nucleo-F103RB I2C no connection

Kajetan.B
Associate II
Posted on July 03, 2018 at 17:03

I cannot communicate my mpu6050 with nucleo-F103RB board via I2C interface. Here is my code, maybe someone could explain me what is wrong.

&sharpinclude 'stm32f1xx.h'

I2C_HandleTypeDef hi2c;

UART_HandleTypeDef uart;

void send_char(char c)

{

HAL_UART_Transmit(&uart, (uint8_t*)&c, 1, 1000);

}

int __io_putchar(int ch)

{

if (ch == '\n')

send_char('\r');

send_char(ch);

return ch;

}

int main(void)

{

uint8_t who_i_am=1;

SystemCoreClock = 8000000;

HAL_Init();

__HAL_RCC_GPIOA_CLK_ENABLE();

__HAL_RCC_GPIOB_CLK_ENABLE();

__HAL_RCC_USART2_CLK_ENABLE();

__HAL_RCC_I2C1_CLK_ENABLE();

GPIO_InitTypeDef gpio;

gpio.Mode = GPIO_MODE_AF_OD;

gpio.Pin = GPIO_PIN_6 | GPIO_PIN_7; // SCL, SDA

gpio.Pull = GPIO_PULLUP;

gpio.Speed = GPIO_SPEED_FREQ_HIGH;

HAL_GPIO_Init(GPIOB, &gpio);

gpio.Mode = GPIO_MODE_AF_PP;

gpio.Pin = GPIO_PIN_2;

gpio.Pull = GPIO_NOPULL;

gpio.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(GPIOA, &gpio);

gpio.Mode = GPIO_MODE_AF_INPUT;

gpio.Pin = GPIO_PIN_3;

HAL_GPIO_Init(GPIOA, &gpio);

hi2c.Instance = I2C1;

hi2c.Init.ClockSpeed = 100000;

hi2c.Init.DutyCycle = I2C_DUTYCYCLE_2;

hi2c.Init.OwnAddress1 = 0xff;

hi2c.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

hi2c.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

hi2c.Init.OwnAddress2 = 0xff;

hi2c.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

hi2c.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

HAL_I2C_Init(&hi2c);

uart.Instance = USART2;

uart.Init.BaudRate = 9600;

uart.Init.WordLength = UART_WORDLENGTH_8B;

uart.Init.Parity = UART_PARITY_NONE;

uart.Init.StopBits = UART_STOPBITS_1;

uart.Init.HwFlowCtl = UART_HWCONTROL_NONE;

uart.Init.OverSampling = UART_OVERSAMPLING_16;

uart.Init.Mode = UART_MODE_TX_RX;

HAL_UART_Init(&uart);

HAL_I2C_Mem_Read(&hi2c, 0x68, 0x75, 1, (uint8_t*)&who_i_am, sizeof(who_i_am), 1000);

printf('who i am = %d\n',who_i_am);

while (1) {

}

}

#stm32l-i2c #i2c-isues #nucleo-f103rb #i2c-issue #stm32f103-hal
1 ACCEPTED SOLUTION

Accepted Solutions
David SIORPAES
ST Employee
Posted on July 03, 2018 at 17:49

It seems you are using the 7-bit address. Try with 0xd0 instead of 0x68.

View solution in original post

2 REPLIES 2
David SIORPAES
ST Employee
Posted on July 03, 2018 at 17:49

It seems you are using the 7-bit address. Try with 0xd0 instead of 0x68.

Posted on July 03, 2018 at 18:11

Thank you man, I coudn't figure out for 2 days what was the problem.

:)