Theory of Operation STOP CONDITION

Similar documents
SILICON MICROSTRUCTURES

Microcontroller Systems. ELET 3232 Topic 23: The I 2 C Bus

Standard EEPROM ICs. Interfacing SLx 24Cxx I 2 C-Bus Serial EEPROMs to 8051 Controller Family, especially to the Siemens C500 Controller Family

How to Implement I 2 C Serial Communication Using Intel MCS-51 Microcontrollers

Lesson I2C. I²C (Inter-Integrated Circuit) Lab Assignment: I2C Slave Driver

VORAGO VA108x0 I 2 C programming application note

Introduction to I2C & SPI. Chapter 22

The Cubesat Internal bus: The I2C

Serial Peripheral Interface. What is it? Basic SPI. Capabilities. Protocol. Pros and Cons. Uses

Tutorial for I 2 C Serial Protocol

LPC1300, FM+, Fast-mode Plus, I 2 C, Cortex-M3

BV4531U. I2C or Serial 6 Way Relay

Embedded Systems and Software. Serial Interconnect Buses I 2 C (SMB) and SPI

VBattery 7 VCC V DD IRLED IS31SE5001 SDA SCL INTB SDB. Figure 1 Typical Application Circuit

1.3inch OLED User Manual

Application Note: AZD025 IQ Switch - ProxSense TM Series I2C Example Code for the IQS222

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Implementation of MCU Invariant I2C Slave Driver Using Bit Banging

Interfacing the NM24C16 Serial EEPROM to the microcontroller. Interfacing the NM24C16 Serial EEPROM to the 8031 Microcontroller AN-957

ICN12. I2C to UART Bridge, ADC,DAC and I/O

Interfacing Z8 Encore! XP MCUs with an I 2 C-Based Character LCD

McMaster University Embedded Systems. Computer Engineering 4DS4 Lecture 6 Serial Peripherals Amin Vali Feb. 2016

ATmega640/1280/1281/2560/2561

Hello, and welcome to this presentation of the STM32 I²C interface. It covers the main features of this communication interface, which is widely used

GT24C02. 2-Wire. 2Kb Serial EEPROM (Smart Card application)

TB2669. I²C Slave Mode. Introduction. Author: Christopher Best, Microchip Technology Inc.

Freescale Semiconductor, Inc.

Handson Technology. I2C Specification and Devices. 1

Section 16. Basic Sychronous Serial Port (BSSP)

DT-SENSE. UltraSonic and InfraRed Ranger (USIRR)

HDS Series I2C Application Notes

Fremont Micro Devices, Inc.

21. TWI Two-Wire Interface

Altera DE2 I2C Driver. Daniel Fiske Michael Lam Daniel Tiam

RL78 Serial interfaces

um-fpu Application Note 8 Developing an I 2 C Interface for um-fpu V2

DESIGNING OF INTER INTEGRATED CIRCUIT USING VERILOG

Part 1 Using Serial EEPROMs

White Paper Using the MAX II altufm Megafunction I 2 C Interface

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING. EE Microcontroller Based System Design

Lecture 25 March 23, 2012 Introduction to Serial Communications

DIGITAL HUMIDITY SENSOR HYT-131

Figure 1 Typical Application Circuit

I2C BUS SERIAL EEPROM

Development and research of different architectures of I 2 C bus controller. E. Vasiliev, MIET

GT34C02. 2Kb SPD EEPROM

Microcontrollers and Interfacing

AN-1159 APPLICATION NOTE

BL24C02/BL24C04/BL24C08/BL24C16

ECE 4510/5530 Microcontroller Applications Week 10

RM24C64AF 64-Kbit 1.65V Minimum Non-volatile Fast Write Serial EEPROM I 2 C Bus

Growing Together Globally Serial Communication Design In Embedded System

An Efficient Designing of I2C Bus Controller Using Verilog

PIC16C7X 11.0 SYNCHRONOUS SERIAL PORT (SSP) MODULE SSP Module Overview. Applicable Devices

DS28CM00. I²C/SMBus Silicon Serial Number

Laboratory 5 Communication Interfaces

I2C on the HMC6352 Compass

Introduction the Serial Communications Parallel Communications Parallel Communications with Handshaking Serial Communications

AN-895 APPLICATION NOTE

Basics of UART Communication

Functional block diagram AD53x1 (Analog Devices)

Using the I 2 C Interface on the ATmega328P and MC908JL16

DS1676 Total Elapsed Time Recorder, Erasable

The IIC interface based on ATmega8 realizes the applications of PS/2 keyboard/mouse in the system

Raspberry Pi - I/O Interfaces

ELE492 Embedded System Design

Preliminary Data MOS IC. Type Ordering Code Package SDA Q67100-H5092 P-DIP-8-1

or between microcontrollers)

COMP2121: Microprocessors and Interfacing

HT16K23 RAM Mapping 20 4/16 8 LCD Controller Driver with Keyscan

TB3191. I²C Master Mode. Introduction. Author: Christopher Best, Microchip Technology Inc.

I2C Master-Slave Connection

Embedded Workshop 10/28/15 Rusty Cain

Microtronix Avalon I 2 C

I 2 C Master Control FSM. I 2 C Bus Control FSM. I 2 C Master Controller

Interfacing Techniques in Embedded Systems

An SPI interface for the 65(C)02 family of microprocessors

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

Manual iaq-engine Indoor Air Quality sensor

AN F²MC-16FX Family, I2C. Contents. 1 Introduction. This application note describes how to communicate via I2C with a Serial EEPROM.

I2C interface Tutorial

Serial Communications

DS1845 Dual NV Potentiometer and Memory

FM24CL04 4Kb FRAM Serial Memory

ABLIC Inc., Rev.1.6_00

CprE 488 Embedded Systems Design. Lecture 4 Interfacing Technologies

17. I 2 C communication channel

Debounced 8 8 Key-Scan Controller

M68HC08 Microcontroller The MC68HC908GP32. General Description. MCU Block Diagram CPU08 1

DS 1682 Total Elapsed Time Recorder with Alarm

2-Wire, 5-Bit DAC with Three Digital Outputs

AVR XMEGA Product Line Introduction AVR XMEGA TM. Product Introduction.

Codec. WM8731 Audio Codec

Errata Sheet RV-8803-C7

GT24C WIRE. 1024K Bits. Serial EEPROM

Microcontroller basics

Preliminary Data MOS IC. Type Ordering Code Package SDA Q67100-H5096 P-DIP-8-1

I 2 C Slave Controller. I 2 C Master o_timeout_intr

GT24C256 2-WIRE. 256K Bits. Serial EEPROM

FXL6408 Fully Configurable 8-Bit I 2 C-Controlled GPIO Expander

Transcription:

AVR 300: Software I 2 C Master Interface Features Uses Interrupts Supports rmal And Fast Mode Supports Both 7-Bit and 10-Bit Addressing Supports the Entire AVR Microcontroller Family Introduction The need for a simple and cost effective inter-ic bus for use in consumer, telecommunications and industrial electronics, led to the developing of the I 2 C bus. Today the I 2 C bus is implemented in a large number of peripheral and microcontrollers, making it a good choice in low speed applications. The AVR microcontroller family does not have dedicated hardware for I 2 C operation, but because of the flexible I/O and high processing speed, an efficient software I 2 C single master interface, can easily be implemented. Figure 1. START and STOP conditions Theory of Operation The I 2 C bus is a two-wire synchronous serial interface consisting of one data (SDA) and one clock (SCL) line. By using open drain/collector outputs, the I 2 C bus supports any fabrication process (CMOS, bipolar and more). The I 2 C bus is a multi-master bus where one or more devices, capable of taking control of the bus, can be connected. When there is only one master connected to the bus, this does not need to support handling of bus contentions and inter master access (a master accessing another master). Only master devices can drive both the SCL and SDA lines while a slave device is only allowed to issue data on the SDA line. 8-Bit Microcontroller Application te SDA SCL START CONDITION STOP CONDITION Data transfer is always initiated by a bus master device. A high to low transition on the SDA line while SCL is high is defined to be a START condition (or a repeated start condition). A START condition is always followed by the (unique) 7-bit slave address and then by a data direction bit. The slave device addressed now acknowledges to the master by holding SDA low for one clock cycle. If the master does not receives any acknowledge, the transfer is terminated. Depending of the data direction bit, the master or slave now transmits 8-bit of data on the SDA line. The receiving device then acknowledges the data. Mul- 0954A-A 9/97 1

tiple bytes can be transferred in one direction before a repeated START or a STOP condition is issued by the master. The transfer is terminated when the master issues a STOP condition. A STOP condition is defined by a low to high transition on the SDA line while the SCL is high. If a slave device cannot handle incoming data until it has performed some other function, it can hold SCL low to force the master into a wait-state. Figure 2. Bit transfer on the I 2 C bus SDA SCL DATA VALID CHANGE ALLOWED Change of data on the SDA line is only allowed during the low period of SCL as shown in Figure 2. This is a direct consequence of the definition of the START and STOP conditions. A more detailed description and timing specifications, can be found in [1]. Transferring Data All transfer on the bus is byte sized. Each byte is followed by an acknowledge bit set by the receiver. The slave address byte contains both a 7-bit address and a read/write bit. Figure 3. Byte Transfer Formats Master Write S Slave Address R/W A DATA A DATA A/A P "0" n BYTES Master Read S Slave Address R/W A DATA A DATA A P "1" n BYTES Combined Format S Slave Address R/W A DATA A/A Sr Slave Address R/W A DATA A/A n BYTES n BYTES P FROM MASTER TO SLAVE S START CONDITION A ACKNOWLEDGE (SDA LOW) FROM SLAVE TO MASTER P STOP CONDITION A NOT ACKNOWLEDGE (SDA HIGH) Sr REPEATED START Figure 3 shows the valid data transfer formats. In the combined format, multiple data can be sent in any direction (to the same slave device). A change in data direction is done by using a repeated START condition. te that a master read operation must be terminated by not acknowledging the last byte read. Connection Both I 2 C lines (SDA and SCL) are bi-directional therefore outputs must be of an open-drain or an open-collector type. Each line must be connected to the supply voltage via a pull-up resistor. A line is then logic high when none of the connected devices drives the line, and logic low if one or more is drives the line low. 2

Figure 4. Physical connection to the I 2 C bus V DD R P R P AT90Sxxxx (I 2 C Master) I 2 C Slave(s) SCLK OUT "0" PDx SCL SCLK IN DATA OUT "0" PDy SDA DATA IN SCL SDA Figure 4 shows how to connect the AVR microcontroller to the I 2 C bus. The value of R P depends on V DD and the bus capacitance (typically 4.7k). Implementation The only resources used by the I 2 C master routines presented in this application, is the two pins for SCL and SDA on port D. Since the I 2 C bus is synchronous, the duty cycle and the period time to the serial clock line (SCL) is not critical. Therefore it is not necessary to fine-tune the routines which would cause to an increase of program size. There are two types of delays used in this implementation: quarter period and half period delays. For I 2 C in normal mode (100 khz), these delays must be t quarter > 2.5 µs and t half > 5.0 µs. For I 2 C in fast mode (400 khz) the parameters are t quarter > 0.6 µs and t half > 1.3 µs. There is a large number of possible implementations of the delay loop. All the implementations depends on the MCU clock frequency. It is not possible to make a generalized version which is efficient for all clock speeds. The following steps show how to choose the most efficient implementation of the delays: 1. Calculate the required number of clock cycles both delays: n = t * fosc ; t = t quarter and t half, fosc is MCU clock. 3

2. Use n to chose one of the following methods for both delays. n Delay method < 1 remove all calls to the delay routine 1 < n < 2 replace all calls to the delay routine with one nop instruction 2 < n < 3 replace all calls to the delay routine with an rjmp 1 instruction 2 < n < 7 the delay routine should consist of one ret instruction only > 7 use the following routine : ldi i2cdelay, 1+ (n-7)/3 loop: dec i2cdelay brne loop ret (this routine is used in the program code!) I2C Subroutines i2c_init Initializes SCL and SDA lines. The SCLP and SDAP constants located top of the program code, chooses the pin number on port D. It is possible to use any pins on any port by changing the program code if required. All the port D initialization can be put in this subroutine to reduce code size. i2c_start Generate start condition and sends slave address. All data transfer must start with this subroutine. When a transfer is done the i2c_end must be called. When the bus is free (after i2c _end is called) all registers are free for other usage. Code Size 3 :3 r17 r18 i2cadr - Slave address and transfer direction (global) i2cdata - Transmit buffer 4

i2c_rep_start Generate repeated start condition and sends slave address. A repeated START can only be given after a byte has been read or written. Code Size 5 :3 r17 r18 i2cadr - Slave address and transfer direction (global) i2cdata - Transmit buffer i2c_write Writes data (one byte) to the I 2 C bus. This function is also used for sending the address. Code Size 16 :2 r17 i2cdata - Data to be written i2c_get_ack Get slave acknowledge response. The reason for separate this subroutine from the i2c_write routine is to get a more readable program code. Code Size 11 5

Figure 5. i2c_start, i2c_rep_start, i2c_write and i2c_get_ack Flow Chart 'i2c_rep_start' 'i2c_start' 'i2c_write' 'i2c_get_ack' & SDA HIGH i2cadr = i2cdata set CARRY = '1' set SDA LOW left rotate i2cdata left shift i2cdata Yes Delay t quater Delay t quater ZERO = '1'? SCL HIGH? Yes set CARRY = SDA CARRY = '1'? Yes set SDA HIGH set SDA LOW return Figure 5 shows the flow chart for i2c_start, i2c_rep_start, i2c_write and i2c_get_ack. These subroutines shares program code to reduce size. i2c_read Reads data (one byte) from the I 2 C bus. Code Size 11 :3 6

r17 r19 i2cstat - Store acknowledge bit (global) i2cdata - Received data i2c_put_ack Put an acknowledge bit depending on carry flag is set or not. Separating this subroutine from the i2c_read routine is convenient for the user if a acknowledge is based on the result of the read operation. Code Size 12 :2 r19 i2cstat - Acknowledge bit (global) 7

Figure 6. i2c_read and i2c_put_ack Flow Chart 'i2c_read' 'i2c_put_ack' set i2cdata = 0x01 set SDA = i2cstat SCL HIGH? set CARRY = SDA Yes left rotate i2cdata return CARRY = '1'? Figure 5 shows the flow chart for i2c_read and i2c_put_ack. These subroutines shares program code to reduce size. i2c_stop Generate stop condition. When a transfer is done the i2c_end must be called. When the bus is free (after i2c _end is called) all registers are free for use. Code Size 8 8

Figure 7. i2c_stop Flow Chart 'i2c_stop' & SDA LOW Delay t quater set SDA HIGH return Figure 7 shows the flow chart for i2c_stop. i2c_do_transfer The i2c_do_transfer routine is implemented for convenience only. It uses the direction bit from the last address byte send, to decide whether to call the i2c_read or the i2c_write routine. Tips and Warnings The main loop in the program shows an example of reading and writing data to a 256byte SRAM. This is a simple demonstration of how to use the I 2 C routines. Typically the reading and writing of SRAM will be implemented as functions calls, but since there is a large variety of slave implementations and ways of accessing them, the making this type of function calls is left for the user. Warning! Do not change the order of the I 2 C routines. Most routines expects to be followed by a another specific I 2 C routine to work correctly. Conclusion This application note shows how to implement a master I 2 C interface on any AVR microcontroller device. This by using a minimum of resources. Since no interrupts are used in the implementation, these are free for other applications. It is also possible to use the I 2 C interface inside interrupts. References [1] The I 2 C-bus and how to use it (including specifications), Philips Semiconductors, April 1995. 9