Application note I²C application examples Introduction The I 2 C peripheral is very flexible, supporting standard interrupts in both 10-bit and 7-bit addressing modes. As a result, generated events are easier to check and manage. The I 2 C peripheral can also be interfaced with an EEPROM driver which allows greater and more complex communication. This application note is intended to provide five practical application examples of how to use the I 2 C peripheral in devices. The examples are: Aardvark TM I²C - I²C communication using interrupts in 7-bit addressing mode I 2 C - I 2 C communication in 10-bit addressing mode Three examples of I 2 C and MC24C64 EEPROM communication For further information on the family features, pinout, electrical characteristics, mechanical data and ordering information, please refer to the F51xx, F61xx datasheet. For further details on the MC24C64 EEPROM, please consult M24128, M24C64, M24C32 datasheet. This document, its associated firmware, and other such application notes are written to accompany the STM8 firmware library. Please contact STMicroelectronics for information on the STM8 firmware library. June 2008 Rev 2 1/7 www.st.com
Contents AN2672 Contents 1 Aardvark I²C - I²C communication using interrupts in 7-bit addressing mode............................................ 3 1.1 Overview.................................................. 3 1.2 Hardware description......................................... 3 1.3 Firmware description......................................... 3 2 I 2 C - I 2 C communication in 10-bit addressing mode........ 4 2.1 Overview.................................................. 4 2.2 Hardware description......................................... 4 2.3 Firmware description......................................... 4 3 I 2 C and MC24C64 EEPROM communication............... 5 3.1 Overview.................................................. 5 3.2 Hardware description......................................... 5 3.3 Firmware description......................................... 5 4 Revision history............................................ 6 2/7
Aardvark I²C - I²C communication using interrupts in 7-bit addressing mode 1 Aardvark I²C - I²C communication using interrupts in 7-bit addressing mode 1.1 Overview This section describes how to set an I²C - I²C communication from a master transmitter to a slave receiver, using interrupts in 7-bit addressing mode and a clock speed of 100 khz. In the current example, the master I 2 C is an Aardvark external module which emulates the I 2 C protocol. The slave receiver is an device. 1.2 Hardware description Figure 1 shows a typical connection between an Aardvark module used as an I²C interface and an I²C. The Ardvark I²C and device I²C data pins (SDA) and clock pins (SCL) are connected together. A pull-up resistor is connected to each SDA and SCL line. Figure 1. Ardvark I 2 C - I 2 C communication Master Slave Aardvark module ai15022b 1.3 Firmware description The STM8 firmware library includes the I²C driver that supports all I²C communication through a set of functions. The current example is provided as I²C example 4 in the STM8 firmware library. Before enabling the I 2 C peripheral, it has to be configured in 7 bit addressing mode, with a clock speed of 100 khz. The event and buffer interrupts must then be configured to manage the communication. Acting as master, the Aardvark module generates a start condition, and sends the slave address. For data transfer to begin, the slave must acknowledge this address. The Aardvark I 2 C Tx_Buffer is transmitted to the slave and stored in the I 2 C Rx_Buffer. All comminication events are managed in the I 2 C interrupt routine. When data transfer ends, the master generates a stop condition. 3/7
I 2 C - I 2 C communication in 10-bit addressing mode AN2672 2 I 2 C - I 2 C communication in 10-bit addressing mode 2.1 Overview This section describes how to set an I 2 C - I 2 C communication in 10-bit addressing mode with a clock speed of 200 khz. 2.2 Hardware description Figure 2 shows a typical connection between two I 2 Cs. The I 2 C data pins (SDA) and clock pins (SCL) of the master and slave are connected together. A pull-up resistor is connected to each SDA and SCL line. Figure 2. I 2 C - I 2 C communication Master Slave ai15023b 2.3 Firmware description The current example is provided as I²C example 5 in the STM8 firmware library. Before enabling both I 2 C peripherals (which represent master and slave), they have to be configured in 10-bit addressing mode, with a clock speed of 200 khz. The event and buffer interrupts must then be enabled to manage the communication. The master I 2 C generates a start conditon, and after slave address acknowledgement, data transfer in 10-bit addressing mode begins. The Tx_Buffer is transmitted from the master to the slave and stored in the Rx_Buffer. Each master or slave event is managed in the master or slave I 2 C interrupt routine respectively. Transmitted and received buffers are compared to check that data have been correctly transferred. 4/7
I 2 C and MC24C64 EEPROM communication 3 I 2 C and MC24C64 EEPROM communication 3.1 Overview This section describes how to use the I 2 C to communicate with an M24C64 EEPROM. The given example uses most of the I 2 C EEPROM functionality. 3.2 Hardware description Figure 3 shows a typical connection between an I 2 C and an MC24C64 EEPROM. The data pins (SDA) and clock pins (SCA) of the I 2 C and the M24C64 EEPROM are connected together. A pull-up resistor is connected to each SDA and SCL line. The E2, E1, E0, and V SS EEPROM pins are tied to ground and the V CC EEPROM pin is connected to 5 V. Figure 3. I 2 C and M24C64 EEPROM communication SDA SCL V CC E0 EI E2 V SS MC24C64 ai15024b 3.3 Firmware description The STM8 firmware library includes three examples that support all read and write operations through a set of commands. Each example is performed in 7-bit addressing mode with a clock speed of 400 khz. The examples are provided as I²C examples 1, 2, and 3 in the STM8 firmware library. Example 1 shows how to perform a sequential read in the EEPROM Example 2 shows how to perform a random read in the MC24C64 Example 3 shows how to: Write the content of Tx_Buffer Read the content of Tx_Buffer and store it in Rx_Buffer Compare the content of Tx_Buffer and Rx_ Buffer 5/7
Revision history AN2672 4 Revision history Table 1. Document revision history Date Revision Changes 19-Mar-2008 1 Initial release 11-Jun-2008 2 Removed all references to the standard and light versions of the STM8 6/7
Please Read Carefully: Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries ( ST ) reserve the right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any time, without notice. All ST products are sold pursuant to ST s terms and conditions of sale. Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no liability whatsoever relating to the choice, selection or use of the ST products and services described herein. No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such third party products or services or any intellectual property contained therein. UNLESS OTHERWISE SET FORTH IN ST S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT. UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY, DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER S OWN RISK. Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any liability of ST. ST and the ST logo are trademarks or registered trademarks of ST in various countries. Information in this document supersedes and replaces all information previously supplied. The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners. 2008 STMicroelectronics - All rights reserved STMicroelectronics group of companies Australia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan - Malaysia - Malta - Morocco - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of America www.st.com 7/7