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:52 PM
(1/16 MHz) * 400 = 25 µs which is the high and low time of the SCL pulse, so the SCL period is twice that number Tscl = Thi + Tlo = 50 µs , so fscl = 20 kHz.
You should put a little more effort into understanding the basics and reading the datasheet / ref manual.
In case of doubt, take a pen and paper and draw stuff, that helps me every now and then.
2023-09-16 04:14 AM
so set I2C to 20kHz. problem ?
2023-09-16 04:36 AM - edited 2023-09-16 05:54 AM
Hello @DJ1
Usually, when you start the configuration of your ADC values using CubeMX, you find the default value of the I2C clock speed(Hz) is equal to 100000.
You can change it manually to any value you want(20000) in your case.
If you are programming using direct register acces, you have juste to write 400 to the CCR register.
Best regards.
II
2023-09-21 08:38 PM
My doubt is how to do frequency calculation for I2C to run at 20KHz using HSI as 16MHz at register level programming.
2023-09-21 11:20 PM
What Issamos said, set CCR to 400, but mind that CR2 FREQ must be set to PCLK1 frequency, not HSI value (okay, that might be the same).
Thigh = CCR * Tpclk1
Tlow = CCR * Tpclk1
with Tpclk1 = 1 / fPCLK1
2023-09-22 08:45 PM
Okay, let me get this right. PCLK (same as HSI 16MHz) and CCR value of 400. 1/16MHz = 62.5ns. 62.5 * 400 = 25x10^-6. I dont get how it is working at 20KHz. Can you please elaborate on that. I want to confirm theoretically that my I2C bus is running at what freq.
2023-09-24 09:57 AM
@DJ1 I2C bus does not quite run at certain clock frequency (like SPI for example). Host and devices both monitor and drive the clock wire and this affects the data rate and the form of fronts. This has been discussed in this forum.
2023-09-24 08:21 PM
Okay, but can you atleast tell me what becomes the deciding factor while setting I2C bus to a particular freq. Like in terms of calculation the PCLK1 or HSI is SET inside the CR2 reg. Then how do you determine a particular freq because the Thigh and Tlow are already mentioned in the datasheet. Then how do you set I2C to 20Khz through calculation.
2023-09-24 11:50 PM - edited 2023-09-24 11:51 PM
--deleted--
2023-09-24 11:52 PM
(1/16 MHz) * 400 = 25 µs which is the high and low time of the SCL pulse, so the SCL period is twice that number Tscl = Thi + Tlo = 50 µs , so fscl = 20 kHz.
You should put a little more effort into understanding the basics and reading the datasheet / ref manual.
In case of doubt, take a pen and paper and draw stuff, that helps me every now and then.