We have developed this library to use the i2c bus on our 12f1840 development board.

http://www.xappsoftware.com/wordpress/?p=3523

http://www.xappsoftware.com/wordpress/?p=3428

Naturally it can be used also with our PICcoletta board and with other boards which use Microchip PICs. Leave a message to this post if you find useful this library or if you use this library with other PICs.

This library doesn't use interrupts, but only the polling mode (arghh!!!)

The flow goes out from the polling cycle when the flag has been signaled or when the timeout occurs. The timeout is handled using the tmr1 overflow.


(Refer to the Microchip application note AN1355)

We have used the HI-TECH C compiler LITE edition that can be downloaded freely from the Microchip site.

The following procedure initializes the i2c:

 

As we have already said, all the operations on the i2c bus are controlled using the "polling mode". The procedure that controls the polling is the poll_if_tim1 function:

This function arms a timer and performs a check on the overflow flags and on the SSP1IF flag of the PIR1 register. If the timeout expires the function sets the error variable.

To start or restart the i2c communication we use two equal functions, we use two functions because you can decide to implements differents behaviours in the two cases.

The stop function is very simple, too.

And now the ACK and NACK functions:

Last but not least, the write and read functions:

If you found useful this article, please share it using the social buttons below. Thank you in advance.

Gg1