I had a fossick (Australian for "rummage" or "search") around my spares box and found a 24C02 (256-byte) EEPROM and a PCF8574 eight-bit port I/O extender. I'd also ordered a DS1307 RTC chip, as I can see a lot of use for RTCs in the stuff I'd like to build, and I knew they would do for an initial foray into I2C land.
I thought that I'd start with the PCF8574. It looked an easy chip to use, and it could drive a seven-segment display, which would provide an immediate output to verify a simple program. Alas, after an hour of messing around, I was almost certain it was a nonworker (so much for my spares box), so I switched over to my DS1307 RTC chip, which I knew was new and ought to work. And the PICAXE manual had some example code for it, so I could be reasonably sure I wasn't coding anything wrong. Happily, this device did work immediately. I programmed in a time and read it back in a loop, and I could see the seconds and minutes advancing. I then commented out the write line in my program and reprogrammed the PICAXE. I was now able to switch the whole circuit off and read the correct time when I powered it up again. I know coding for I2C from scratch is tricky, but as usual, PICAXE makes it very easy. You don't have to worry about any nitty-gritty details like timing considerations.
I was reading and displaying the contents of my RTC around three times a second with a loop and a PAUSE command. The DS1307 RTC has a pin that generates an output once a second (this can be changed), and this can be used as an interrupt. The PICAXE is pretty versatile with regard to interrupts. You can use almost any combination of pins, conditions, and (on the larger chips) ports you want. Being only a small chip, my PICAXE 08M2 has only six I/O pins, so I only wanted to use pin 3 as an interrupt. To set up an interrupt, you use the SETINT command.
Here, "input" is the condition you want, and "mask" contains 1s for the bits in which you are interested. Since I wanted to use input 3, I used the following command:
In this case, the % characters indicate binary values. The second (rightmost) %00001000 -- the mask -- tells the PICAXE that I want to look only at input 3. There's one bit per input, starting with input 0 in the least significant, rightmost bit position. The first %00001000 tells the PICAXE that I want a 1 in that position (%00000000 would tell it I want a 0). You can use OR and NOT or AND (which is the default) after SETINT and widen the mask to tell it to look for a pattern of a number of bits -- or a pattern that is NOT what you specify. You could monitor a number of alarm lines like that, for instance. Compared to the limited number of interrupt pins on the older microprocessors, this is very versatile.
You have to have a subroutine called INTERRUPT at the end of your program, where this subroutine is executed when an interrupt occurs. The SETINT condition is disabled when an interrupt occurs, thereby allowing your interrupt subroutine to be executed without further interrupts. I called my read RTC and display routine, paused for 500 mS to allow the DS1307's interrupt line to go low again, and then put in another SETINT statement to re-enable the interrupts. Interrupts are checked during PAUSE commands, so my main program loop had a two-second pause and just looped back to itself. I could see the time updating regularly once a second. With my previous loop and pause, it was a bit irregular.
I had a big vacuum fluorescent display (VFD) that I wanted to try, and I had also ordered a second display driver board. How I got all this going is perhaps a tale for another time -- suffice it to say for the moment that this did not work like an LCD. But once I did manage to get it going -- along with the RTC and one of the temperature sensors I used in part 2 -- I obtained a nice result shown below (with apologies to Crocodile Dundee).
I'm sorry about the date, which is presented in British/Aussie format. This picture does not do the display justice, but these displays are very difficult to photograph. Who would have thought you could do all this with a tiny eight-pin MCU? The small board seen in front of the display holds the PICAXE one-wire serial display driver board and interfaces the power and the signal connections to the (nonstandard) VFD display connectors. These VFD displays are made by IEE, though mine is an obsolete type I picked up cheap from a supplier. These are great displays.
To Page 3 >