1. Introduction
This is the tutorial for using I²C on the Simplecortex. Software examples can be downloaded from our repository. All of the used code concerning I²C is explained in this tutorial. A basic knowledge of programming is required.
Only the basic functions of I²C will be handled:
- Read
- Write
1.1 Starting with I²C
I²C is a two wire bus that provides communication between a master and a slave. Most of the time the master is a microcontroller and the slave’s are devices like ADC’s but a multi-master setup is also possible. By example: Two LPCxpresso’s as master communicate with each other and an ADC as slave.
The LPCxpresso 1769 has three I²C interfaces; one of those interfaces can be configured on two different pins.
Note that I²C is meant to be as a bus system for PCB use. According to the I²C specification, the maximum length of the I²C traces is 50cm or 20 inch.
Features:
- 2-wire
- Low speed (100khz)
- Up to 127 devices
- A lot of applications: Temperature sensor, ADC, DAC, etc.
1.2 Addressing
Addressing is very important to get I²C working.
There are two kinds of addresses, the static address and the hardware address. It isn’t possible to change the value of the static address, so there is still an opportunity that two different IC’s have the same static address. That is why they made the hardware address.
Most of the I²C IC’s have three ports for the hardware addresses, these three ports must be connected to the ground or the supply voltage.
To write to the IC the r/w bit has to be 0, to read from the IC it has to be 1.
2. Hardware
This schematic is a basic application of I²C. All examples are based on this schematic. Pull-up resistor R1 and R2 are important. Without these resistors, I²C will not work.
3. Software
3.1 Software buffers
After initializing I²C, the Simplecortex automatically creates two buffers for the I²C send / receive data. These two buffers are known as the Masterbuffer and Slavebuffer. Each I²C interface has its own master and a slave buffer.
3.1.1 Master-buffer
outgoing I²C data will be send from the Masterbuffer to the I²C interface. The arrangement of the master buffer is important. First the address data and then the write data. When needed the read command comes last.
Right: An arrangement visualization of the masterbuffer
3.1.2 Slave-buffer
All incoming I²C data will be stored in the slave buffer.
3.2 Program settings
This part of the chapter will explain the program commands to set up I²C. There are a few parameters. Without them, I²C will not work or work buggy.
3.2.1 Import library
Every special function has its own library. It is necessary to import the library into the main program.
Command: #include "i2c.h"
3.2.2 Initialize
On the LPC1769 there are three I²C interfaces. When you want to use one of these, you must set them up as I²C interface. Otherwise they are normal IO’s.
Command: I2C0Init();
//Set PIO-0.27 and PIO-0.28 to SDA and SCL.
Command: I2C1Init (select port);
If select port = 0:
//Set PIO-0.0 and PIO-0.1 to SDA and SCL.
If select port = 1:
//Set PIO-0.19 and PIO-0.20 to SDA and SCL.
Command: I2C2Init ();
//Set PIO-0.10 and PIO-0.11 to SDA and SCL.
3.2.3 Port_used
Every command needs to know which I²C interface you want to control. PORT_USED is the same number as the Initialized I²C interface.
PORT_USED can be declared as variable.
Command: int PORT_USED = I2C interface number;
For example:
I2C2Init ();
int PORT_USED = 2;
3.2.4 Declare address
Command: I2CMasterBuffer [PORT_USED] [0] = 8bit address of device;
Example: the address of the pcf8574p = 0b01001110 = 0x4e
0b means binary.
0x means hexadecimal.
3.2.5 Read/Write length
In a data transmission where read and write are simultaneous, the write length setting is very important. If the write length is wrong, the microcontroller will not send the read instruction.
This is the basic formula to get the right write length: Write length = address + length of the write data
Conclusion: the write length is always at least 2 bytes long, The first for the address and the second for the data.
Command:
I2CWriteLength [PORT_USED] = number;
The read length is the same as the amount of data that needs to be read. Note that master buffer and slave buffer has a max capacity of 64 bytes.
Command:
I2CReadLength [PORT_USED] = times to read;
3.2.6 Write
Command to write data to device.
Command:
I2CMasterBuffer [PORT_USED] [master buffer position] = 8 bit write data;
Position 0 in the master buffer holds the address of the I2C IC.
Therefore the data that has to be send must start on position 1 in the master buffer.
3.2.7 Read
This command will give a slave device permission to send data to the microcontroller (master). The microcontroller will keep the data in the Slave buffer.
Command: I2CMasterBuffer [PORT_USED] [master buffer position] = I2C address + 1;
To read to the IC the r/w bit has to be 1, therefore 1 has to be added to the address.
3.2.8 Read slave-buffer
The microcontroller will keep all incoming I²C data in the Slave buffer. This command can be used to save a value of the slave buffer.
Command: variable = I2CSlaveBuffer [PORT_USED] [slave buffer position];
The first data bit will be stored in position 0.
3.2.9 I²C Engine
The I2CEngine command handles all the I2C communication. If all settings are completed use the I2CEngine command to start the I2C communication.
Command: I2CEngine(PORT_USED);
4. Example
/ #include "LPC17xx.h"
// SETTINGS
I2C1Init(0); // Initialize I2C
int PORT_USED = 1 // Make variable “PORT_USED” the number of the initialized port
I2CMasterBuffer [PORT_USED][0] = 0b01001110; // I2c address
I2CReadLength[PORT_USED] = 4; // 4 byte read length
I2CWriteLength[PORT_USED] = 2; // 2 byte write length
while(1)
{
I2CMasterBuffer [PORT_USED] [1] = 0x01; // Write 0x01
I2CMasterBuffer [PORT_USED] [2] = 0b01001111; // Command to read (read bit is high)
I2CEngine( PORT_USED ); // Communicate with I2c
}