2023-09-16 03:48 AM
Hi, i am using STM32F401RE Nucleo board and i want am using I2C channel in standard mode which is upto 100KHz but i due to little long length of communication cable, i want my I2C frequency to be around 20KHz. I am using HSI only as clock source.
This is my function to use I2C channel.
void i2c_init()
{
// I2C3 RCC CLK enable
I2C3_CLK_EN;
// Freq setting 16MHz
I2C3->CR2 |= (16 << 0);
//CCR configuration
I2C3->CCR = 0x50;
// T-Rise configuration
I2C3->TRISE = 0x17;
// Set OAR bit
I2C3->OAR1 |= (1 << 14);
// Peripheral Enable
I2C3->CR1 |= (1 << 0);
}
Solved! Go to Solution.
2023-09-24 11:54 PM
Anyway, when I hear "cable length" as limiting factor, maybe you should switch data transfer to some differential interface.
2023-09-25 03:26 AM
To adapt the I2C to the length of the cable you also have to play on the value of the pull ups.
2023-09-26 01:49 AM
Correct. Earlier i was using 4.7K ohms as pull up and now i am using 1K ohms pull-up, usually it works well but sometimes randomly it corrupts the bus. I have 3 slaves connected on the same bus.
2023-09-26 01:57 AM - edited 2023-09-26 02:01 AM
Thanks for the clarity. Well actually i am new to firmware programming and i have just started with STM32F4 and i just could not understand how to identify the frequency of I2C bus. Also i personally prefer register level programming over HAL just for having a better idea of how the things are working in there that is why it takes me a little more to get things done.