cancel
Showing results for 
Search instead for 
Did you mean: 

I2C not working

akex
Associate II
Posted on December 09, 2010 at 18:11

I2C not working

8 REPLIES 8
greg_t
Associate II
Posted on May 17, 2011 at 14:17

You should enable the clock

nanayakkaraan
Associate II
Posted on May 17, 2011 at 14:17

Hello alex,

I would point out the steps I needed to follow in order to make I2C work.

  • Enable the GPIO port in which I2C_SDA and I2C_SDL pins are in.
  • Enable peripheral clock on the particular GPIO port.
  • Configure AFIO (alternative functions) so that the pins will be working as  I2C_SDA and I2C_SDL.
  • Enable peripheral clock for I2C.
  • The remaining configurations (which you have mentioned in the post) were done after this.
Hope this might help you. :)

akex
Associate II
Posted on May 17, 2011 at 14:17

WEll, now the I2C is in operational mode, but still not working quite right.

The clock line doesnt seem to generate a clock

for starters, im trying to send a start signal, then the adress, then repeated start, then adrress again, etc.

this is what im getting on the scope, and my code:0690X00000602exQAA.jpg

 

the code:

GPIOB_CRH = 0x0000DD00;//I2C lines are AF output open drain

   

RCC_APB1ENR = 0x00400000; //enable I2C2 clock

RCC_APB2ENR = 0x0000000D; //enable PA,PB,AF clock

   

I2C2_OAR1 = 0x00004000; //bit 14 should be kept 1 by software

I2C2_CR2 = 0x00000008; //frequency 36 mHZ, //enable event interrupts  

I2C2_CCR = 0x00000028; //clock configure

I2C2_TRISE) = 0x00000009; // Rise time

I2C2_CR1 = 0x00000401; //Enable ACK, Enable I2C

 

while (1)

{

    I2C2_CR1 |=0x00000100; //send START

         

          while(I2C2_SR1<1); //wait till START is complete

          delay(1);

         

          *(uint32*)(I2C2_DR) = 0b10010010;

          //while( (I2C2_SR1 >>1) <1);   <- wait till address has been sent. If I use that line, the code gets stuck, meaning this condition isnt fulfilled

          delay(10);  

      }

thanks again!

Posted on May 17, 2011 at 14:18

WEll, now the I2C is in operational mode, but still not working quite right. The clock line doesnt seem to generate a clock

Check you have the right pins, and there is a pull up on the clock pin.

Better yet, use the library and example code.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
akex
Associate II
Posted on May 17, 2011 at 14:18

These are the right pins, and both have pull up resistors.

I really need to build this on my own, and not use libraries for the purposes of my project.

Any other solutions & suggestions will be appreciated!

gbulmer
Associate II
Posted on May 17, 2011 at 14:18

Are you allowed to use library code and examples to test the hardware?

If you can isolate the probem to either hardware, or software, progress may be easier and quicker.

Posted on May 17, 2011 at 14:18

I really need to build this on my own....

How fantastically ironic.

Ok, what is your clock rate for the APB1? I'd guess from the settings 8 MHz, but your comments suggest 36 MHz, so which is it?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
akex
Associate II
Posted on May 17, 2011 at 14:18

I really need to build this on my own....

 

 

How fantastically ironic.

 

 

hehe, by ''on my own'' I meant ''manually'', not alone, in the dark, without internet :)

As for the clock rate, its 8 MHz, the comments for the I2C2_xxx registers are wrong for some reason.

I took all the clock settings from the reference manual, it has an exapmle for setting the right clock rates for 100 kHz speed