Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

I2C Command and DS1304

Status
Not open for further replies.

Hesam Kamalan

New Member
Hey guys,

I have a question about the I2C commands used. The MikroC is rather confusing, and that example isn't very helpful for me. Can someone please explain to me how the I2C commands work? how can i set time and get it for show on lcd?
 
Hesam Kamalan said:
Hey guys,

I have a question about the I2C commands used. The MikroC is rather confusing, and that example isn't very helpful for me. Can someone please explain to me how the I2C commands work? how can i set time and get it for show on lcd?

You can read about I2C protocol in most of PIC data sheet's, what part of the I2C commands in mikroC you find problematic?

Code:
  I2C_Init(100000);         // initialize I2C communication

  I2C_Start();              // issue I2C start signal

  I2C_Wr(0xA2);             // send byte via I2C  (device address + W)

  I2C_Wr(2);                // send byte (address of EEPROM location)

  I2C_Wr(0xF0);             // send data (data to be written)

  I2C_Stop();               // issue I2C stop signal



  Delay_100ms();



  I2C_Start();              // issue I2C start signal

  I2C_Wr(0xA2);             // send byte via I2C  (device address + W)

  I2C_Wr(2);                // send byte (data address)

  I2C_Repeated_Start();     // issue I2C signal repeated start

  I2C_Wr(0xA3);             // send byte (device address + R)

  PORTB = I2C_Rd(0u);       // Read the data (NO acknowledge)

  I2C_Stop();               // issue I2C stop signal

can you pinpoint the part of the code that is not clear and I'll try to explain it to you.
 
arhi said:
You can read about I2C protocol in most of PIC data sheet's, what part of the I2C commands in mikroC you find problematic?

Code:
  I2C_Init(100000);         // initialize I2C communication

  I2C_Start();              // issue I2C start signal

  I2C_Wr(0xA2);             // send byte via I2C  (device address + W)

  I2C_Wr(2);                // send byte (address of EEPROM location)

  I2C_Wr(0xF0);             // send data (data to be written)

  I2C_Stop();               // issue I2C stop signal



  Delay_100ms();            [COLOR="Blue"]//Delay for 100 milliseconds[/COLOR]



  I2C_Start();              // issue I2C start signal

  I2C_Wr(0xA2);             // send byte via I2C  (device address + W)

  I2C_Wr(2);                // send byte (data address)

  I2C_Repeated_Start();     // issue I2C signal repeated start

  I2C_Wr(0xA3);             // send byte (device address + R)

  PORTB = I2C_Rd(0u);       // Read the data (NO acknowledge)

  I2C_Stop();               // issue I2C stop signal

can you pinpoint the part of the code that is not clear and I'll try to explain it to you.

I don't like to be picky, but that is the most ridiculous commenting I have witnessed. Can you see why? There was one comment that was missing and so I added it.

BTW, what does the 100000 mean in the statement I2C_Init(100000);

Mike.
 
Pommie said:
I don't like to be picky, but that is the most ridiculous commenting I have witnessed. Can you see why? There was one comment that was missing and so I added it.

BTW, what does the 100000 mean in the statement I2C_Init(100000);

Mike.

:D :D :D
I agree Mike, too much comments, that was copy/paste from basic example of I2C use from MikroC ... not developed by me :) .. I tend to put totally different comments in my code :)

as for i2c_init() .. here's what the mikroC help say:
Initializes I²C with desired clock (refer to device data sheet for correct values in respect with Fosc). Needs to be called before using other functions of I²C Library.

Library requires MSSP module on PORTB or PORTC.

anyhow, even as I actually disagree with style of commenting in this example done by mikroC developer, it show's how to communicate with the I2C device
- init i2c
- send start signal
- send address of the device + read or write
- send the location from/to where you want to read/write
...
 
It does indeed show how to communicate but I feel it would be more useful if it had comments (and code) like,
Code:
  I2C_Start();
  I2C_Write(0xA2);          // Serial EEPROM address, chip 1
  //instead of
  I2C_Start();              // issue I2C start signal
  I2C_Wr(0xA2);             // send byte via I2C  (device address + W)

Mike.
 
mike, I agree 1000% .. I personally hate the "obvious" comments that show "what statement does" instead of "why is this doing what" .. but apart from that, OP's question was that "how i2c commands work" .. this example show's just that and it is pretty obvious what is done + there is help on each statement included with mikroC. Everything else is "understanding the i2c protocol" and for that one need to read data sheet, these "libraries" are just here to make the code "nicer", they do not offer additional layer over i2c (like lcd* libriaries do for example)

I do not agree with way many things are done in mikroC but it is easy tool to use and generates pretty good code (imho for 12* and 16* the generated code is much better (smaller/faster) then what CSC C generates.. donno about other compilers as I'm using those two for 12* and 16* as I have licence for them, for 18* I use C18 and for 2*, 3* dsPIC* must admit I never even saw them except on picture :( )
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top