admin管理员组

文章数量:1401673

I wanted to create a simple library for i2c, but I stucked in start generation. my code is this:

void i2c_init()
{
    gpio_init(GPIOB, 6, GPIO_AFOUT_OPENDRAIN); // Initialize PortB-Pin6 for OpenDrain Alternate Function
    gpio_init(GPIOB, 7, GPIO_AFOUT_OPENDRAIN); // Initialize PortB-Pin7 for OpenDrain Alternate Function
    RCC -> APB2ENR |= (1<<0); // Enable Clock for AFIO
    RCC -> APB1ENR |= (1<<21); // Enable Clock for I2C1
    I2C1 -> CR1 |=  (1<<15); // Reset I2C1
    delay_ms(1);
    I2C1 -> CR1 &= ~(1<<15); // Undo Reset I2C1
    I2C1 -> CR2 = 8; // Set Freq of I2C1 (8)
    I2C1 -> CCR = 40; // Set CCR Value of I2C1 (40)
    I2C1 -> TRISE = 9; // Set Max Rise Time for I2C1 (9)
    //I2C1 -> OAR1 = 0x4000;
    I2C1 -> CR1 |= (1<<0); // Enable I2C1
}

unsigned char i2c_write(unsigned char SlaveAdr, unsigned int RegAdr, unsigned char Data)
{
    unsigned int RegAdrH = ((RegAdr & 0xFF00) >> 8);
    unsigned int RegAdrL = (RegAdr  & 0x00FF);
    // Start COM
    I2C1 -> CR1 |= (1<8); // Start Conversation
    while(!(I2C1 -> SR1 & (1<<0))); // Wait for SB (Start Bit) to set (takes Forever!)
    ... 
}

But this loop never ends and SB bit never sets. I'm working on this for a week but failed every time! Does anybody know how to resolve the issue?

I wanted to create a simple library for i2c, but I stucked in start generation. my code is this:

void i2c_init()
{
    gpio_init(GPIOB, 6, GPIO_AFOUT_OPENDRAIN); // Initialize PortB-Pin6 for OpenDrain Alternate Function
    gpio_init(GPIOB, 7, GPIO_AFOUT_OPENDRAIN); // Initialize PortB-Pin7 for OpenDrain Alternate Function
    RCC -> APB2ENR |= (1<<0); // Enable Clock for AFIO
    RCC -> APB1ENR |= (1<<21); // Enable Clock for I2C1
    I2C1 -> CR1 |=  (1<<15); // Reset I2C1
    delay_ms(1);
    I2C1 -> CR1 &= ~(1<<15); // Undo Reset I2C1
    I2C1 -> CR2 = 8; // Set Freq of I2C1 (8)
    I2C1 -> CCR = 40; // Set CCR Value of I2C1 (40)
    I2C1 -> TRISE = 9; // Set Max Rise Time for I2C1 (9)
    //I2C1 -> OAR1 = 0x4000;
    I2C1 -> CR1 |= (1<<0); // Enable I2C1
}

unsigned char i2c_write(unsigned char SlaveAdr, unsigned int RegAdr, unsigned char Data)
{
    unsigned int RegAdrH = ((RegAdr & 0xFF00) >> 8);
    unsigned int RegAdrL = (RegAdr  & 0x00FF);
    // Start COM
    I2C1 -> CR1 |= (1<8); // Start Conversation
    while(!(I2C1 -> SR1 & (1<<0))); // Wait for SB (Start Bit) to set (takes Forever!)
    ... 
}

But this loop never ends and SB bit never sets. I'm working on this for a week but failed every time! Does anybody know how to resolve the issue?

Share Improve this question edited Mar 22 at 22:06 0andriy 4,7292 gold badges25 silver badges39 bronze badges asked Mar 22 at 20:44 ALIALI 12 bronze badges 4
  • What do you have connected to the I2C pins? You need pull-up resistors at the very least - without them, the bus appears to be busy, and it is impossible to generate a start condition. – jasonharper Commented Mar 22 at 22:21
  • i used 1K resistor for both lines. before using this resistors BERR bit was setting and after that problem solved but never started. – ALI Commented Mar 22 at 23:01
  • There is a comparison, not a shift (1<8); // Start Conversation. Is it a typo in the question or actual code? – ReAl Commented Mar 24 at 9:32
  • it was actually a typo! i cant believe it. thanks. – ALI Commented Mar 24 at 10:25
Add a comment  | 

1 Answer 1

Reset to default 0
i found the problem thanks to real.
I2C1 -> CR1 |= (1<8) // Missed < sign
I2C1 -> CR1 |= (1<<8) This one do the work.

本文标签: stm32f1STM32F103C8T6 i2c never generates start conditionStack Overflow