David Ashton experiments with I2C communications using his PICAXE MCU development system.
In part 1 of this miniseries, I presented an overview of the PICAXE MCU system and performed some simple BASIC programming with my chip. In part 2, I looked at using the SIMULATE and DEBUG commands, along with the analog-to-digital converter, pulse-width modulated output, and the one-pin display options offered by the PICAXE. In part 3, I interfaced my PICAXE to a DS18B20 temperature sensor and some other one-wire devices.
The next thing I wanted to try was using I2C chips. I remember reading about the new I2C interface in Elektor magazine in the 1980s, but even though I have seen it used in quite a few products, I've never had much to do with it myself. Inter-IC communication (I2C) was devised by Philips (now NXP) as an attempt to standardise a two-wire bus for communication between integrated circuits in order to save pins. I recently stripped a scrapped plasma TV and kept the audio board intact. On checking the datasheets for the chips used, I discovered it had two Class-D 9W amplifiers and a TA1343 audio processor IC, which lets you control the volume, balance, bass, and treble using I2C. There are all sorts of other tasty I2C ICs you can obtain -- for example, for outputting to multi-digit LED and LCD displays. And then there are the usual 24Cxx EEPROMs and various real-time clocks (RTCs). Being able to talk to an assortment of these chips using only two pins on your MCU is very useful.
As usual, the creators of the PICAXE make I2C easy. For example, I found the I2C tutorial fairly informative, though I already knew the basics. This tutorial needs updating (it shows outdated commands and chips), but it does give a good overview of how PICAXE chips talk to other I2C chips. For example, you set up for each chip you are using with the HI2CSETUP command as follows.
This sets the PICAXE up as the I2C master -- there is also an I2CSLAVE version of the command that lets you set up your PICAXE chip as an I2C slave. This could be really useful for creating your own intelligent I2C peripherals (in fact, you can get a kit for making an I2C LCD or OLED display), but let's not get ahead of ourselves. Using the above command, you instruct the PICAXE as to the address length (I2CBYTE for eight bits or I2CWORD for 16 bits) and the mode (I2CSLOW for 100 KHz or I2CFAST for 400 KHz) for each chip. There are variations of the mode command to account for using PICAXE chips at faster speeds; they default to 4 MHz (8 MHz for the larger X2 chips) using the internal resonator, but you can use an external resonator and run them up to 64 MHz. The reference was a bit scanty on what speeds you can tell it to use, though.
Once you've done this, you can write to and read from that chip with the HI2COUT and HI2CIN commands. With certain restrictions, you can mix devices on an I2C bus, which comprises two lines. So you can make, for example, a data logger using a PICAXE, an RTC chip, and an EEPROM in which to store your data, along with a serial link and/or a display to upload or read this data. PICAXE offers such a kit, and the I2C tutorial uses it as an example. One thing that was not too clear in the manual or the tutorial was the fact that the address you set up with the HI2CSETUP command is a base address, and each read or write you perform advances this base address by 1. So if you say, for example:
This will read the I2C device at the base address into variable b1, Base address+1 into b2, etc. That's useful for reading the whole contents of an RTC chip at one go. You can also say:
And it will put those characters into the first 12 locations of an I2C-enabled EEPROM chip.
To Page 2 >