Author Topic: MikroC I2C problem  (Read 1443 times)

0 Members and 1 Guest are viewing this topic.

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 146
  • Country: gl
MikroC I2C problem
« on: September 25, 2018, 01:00:41 pm »
Hello guys i can't understand some line in mikroC i2c manual.

Quote
Note : Calculation of the I²C clock value is carried out by the compiler, as it would produce a relatively large code if performed on the library level. Therefore, compiler needs to know the value of the parameter in the compile time. That is why this parameter needs to be a constant, and not a variable.



Can anyone explain the above line??

 

Online JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: MikroC I2C problem
« Reply #1 on: September 25, 2018, 02:22:07 pm »
it should be self-explanatory but sure..
as also noted in the manual the I2C baud rate depends mostly on 2 variables
- MCU clock frequency (Fosc)
- Content of the baud rate generator register (BRG)
so basically baud rate = Fosc/BRG

Since in most MCU projects Fosc is fixed (or maybe one frequency during operation and one in sleep mode) the compiler can calculate the value of BRG at compile time, instead letting the MCU performing divisions -which takes very long to perform if you don't have a hardware division accelerator-
so, the function expects a constant and the compiler will throw an error if a variable is passed to the function.
 

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 146
  • Country: gl
Re: MikroC I2C problem
« Reply #2 on: September 25, 2018, 02:30:49 pm »
Thanks
 

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 146
  • Country: gl
Re: MikroC I2C problem
« Reply #3 on: September 25, 2018, 02:34:16 pm »

The value inside the function I2C1_Init(100000); should always be 100000?? Can i use different value??
 

Online JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: MikroC I2C problem
« Reply #4 on: September 25, 2018, 03:36:08 pm »
Of course you can :) passing it 100000 will set up the peripheral for 100kHz operation, 400000 for example would set it for 400khz :)
 

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 146
  • Country: gl
Re: MikroC I2C problem
« Reply #5 on: September 25, 2018, 03:58:29 pm »


In the above picture it says if Both SCL and SDA are connected  to a positive power supply voltage via resistors. This means that when the bus is free, both lines are high.

My question is
1. if i connect a slave device to SDA and SCL line does the slave pulled the lines(SDA & SCL) to low??
2. The I2C1_Start() command in mikroc compiler determines if I²C bus is free or not what does it mean by free???


3. What is the use of I2C1_Repeated_Start() routine?? I can't understand what it mean by repeated START signal?? why it is required ??
« Last Edit: September 25, 2018, 04:06:57 pm by khatus »
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11234
  • Country: us
    • Personal site
Re: MikroC I2C problem
« Reply #6 on: September 25, 2018, 04:21:59 pm »
All your questions are answered in I2C specification.
1. To communicate 0 or 1 all devices on the bus can either drive the bus low, or let go of the bus, which will make it go high.
2. I2C protocol has a state machine that. When a master successfully issues a start condition, it owns the bus until it releases it though a stop condition. Between the start and stop conditions the bus is busy an no other master can issue a start condition.
3. Repeated start is a start without prior stop. This may be necessary when you are reading a register and need to change write to read direction after you wrote the register address and want to read the data. Some devices don't tolerate a stop and a new start in this case.
Alex
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf