|
by Dariusz Caban
Start ý The
Bus ý The Protocol ý The
Process ý Conclusions ý Sources
and PDF
THE PROCESS
The I2C protocol does not
have to be implemented in hardware. Software implementation is also
possible, because the protocol is forgiving with regard to timing
accuracy. And, any of the microcontrollerýs general-purpose I/O lines
can be used as I2C lines. This approach is useful when
a system design includes only a single master.
As stated earlier, such designs are most
frequent. I have implemented the I2C protocol for the 8031
microcontroller, using only Standard mode. The source code was written
in C-51. The use of a high-level programming language shortened development
time considerably. It also simplifies changes and adaptation of the
code to microcontrollers with different architectures.
Listing 1 presents functions performing
basic operations of the I2C protocol. The functions given
require 135 bytes of code memory and only a few bytes of internal
data memory, if a compact memory model is set. In order to guarantee
appropriate timing characteristics of signals, NOP instructions are
used. An 8031 microcontroller executes a NOP instruction in one machine
cycle. [2] One cycle takes 12/fosc s, where fosc
is the oscillator frequency. The number of NOP instructions in the
given functions was selected based on an oscillator frequency of 12
MHz.
| Listing
1ýBy
using these functions, data transfer via an I2C bus
can be performed, assuming only a single master exists. |
Now, letýs use the set of functions given
in Listing 1 to implement operations on the Atmel AT24C02 device you
saw in Figure 1. This device supports 256 bytes of EEPROM. The slave
address of the AT24C02 consists of a 4-bit type identifier (1010),
followed by a 3-bit sequence, which corresponds to logic levels on
the A2, A1, and A0 inputs. This way, up to eight EEPROMs can be addressed
on the same I2C bus. The AT24C02 also has a write protect
(WP) pin that provides hardware data protection.
The following operations are allowed:
byte write, page write, acknowledge polling, current address read,
random read, and sequential read. Listings 2 and 3 present example
functions performing byte write and sequential read operations, respectively.
It was assumed that a single EEPROM exists in the system.
| Listing
2ýThis function writes bytes to the EEPROM cell located at
a given address. The function returns the status of operation.
|
| Listing
3 ýThe
bytes are placed in memory, starting from the block address. |
When the microcontroller terminates the
write sequence with a stop condition, the EEPROM enters an internally
timed write cycle that, for the AT24C02, can last up to 10 ms. During
the write cycle, the EEPROM is busy and ignores all communications
on the I2C bus. The ready/busy status of the device is
determined by using an acknowledge polling operation. This operation
involves issuing of a start condition followed by the slave address
byte. If the EEPROM does not acknowledge, the cycle is still in progress.
If it does, the cycle has completed. Listing 4 presents the function
performing the acknowledgement of the polling operation and its possible
use.
| Listing
4 ýThe
first function determines the ready/busy status of the EEPROM.
If you assume that after the write sequence a program execution
may be suspended until the write cycle is complete, the EEPROM_busy()
function can be used. |
The sequential read operation is a convenient
way to get multi-byte values stored in the EEPROM. In the function
presented in Listing 3, this operation is initiated by a write sequence
to load the EEPROMýs internal address counter with the initial value.
Then the master issues a start condition again, sends a slave address
with the data direction bit high, and begins reading. After the master
receives a byte and acknowledges it, the EEPROM increments the address
counter and sends a successive byte. The EEPROM continues sending
until the master does not acknowledge and generates a stop condition.
Listing 5 shows an example of using the EEPROM_sequential_read()
function.
| Listing
5ýThis
example shows how to restore float variables within the contents
of the EEPROM. |
PREVIOUS
NEXT
Circuit Cellar provides up-to-date information for engineers. Visit
www.circuitcellar.com for
more information and additional articles.
For subscription information, call (860) 875-2199, subscribe@circuitcellar.com
or subscribe online.
ýCircuit Cellar, the Magazine for Computer Applications. Posted with
permission. |