Our resident expert on all things
robotic is merely an email away.
roboto@servomagazine.com
Tap into the sum of allhumanknowledge and get your questions answered here!
From software algorithms to material selection, Mr. Roboto strives to meet you
where you are — and what more would you expect from a complex service droid?
by
Dennis Clark
I have a variety of questions this month to answer
(hopefully!), so let’s get started!
Q. In the March ‘09 issue of SERVO Magazine,
you presented an interface design for the
Dinsmore 1490 compass module. A PIC12C508
microcontroller was used to handle the low level details
of reading the compass and sending the data serially to a
main processor. I have one related question and while
I’m writing, an unrelated question, as well. In Dr. John
Peatman’s Design with PCI Microcontrollers, a series of bit
banging routines are presented which implement an I2C
Master Node. The required open-drain outputs are achieved
by toggling the TRISC bits rather than PORTC bits. It is
implied that this technique would allow any PIC I/O bits to
be used for I2C, not just [hardware] SCL and SDA. Can an
I2C slave node be implemented with similar bit banging
routines? This would allow a cheap PIC to interface sensors
like the 1490 to a Master I2C bus. Also in Peatman’s
discussion, a table shows the values of PORTB, C, D, and E
being undefined at Power On Reset (POR). Can you briefly
address the topic of fail-safe design as to insuring that
no un-commanded outputs occur at POR? Example:
A pneumatic RAM extending unexpectedly due to multiple
PORs caused by a loose battery connection, etc.
I enjoy SERVO and your column very much. My
professional interest is in industrial controls. My hobby
interest is in amateur radio. I like Servo for the multitude of
PIC applications and sensor interfacing articles.
— James M. Lynes
A. We’re pleased that you like SERVO. We do our best
to provide a wide variety of articles to appeal to the
wide variety of interests shown by our readers! I’ll
answer your questions in order.
Yes, you can implement an I2C slave using bit banged
routines and simulating the open-drain by using the TRIS
register. I would use a 12F509, though, because it has
twice the flash of the 12F508 and you might need that
to implement the relatively complex code that I2C requires.
The 12F50X series is pin and source compatible with
the older 12C50X series parts, and comes with the added
advantage of not being OTP (one time programmable).
If you are using a newer version of MPLAB (post 8.0),
then you will have the added advantage of getting free
access to the CCS PCB “C” compiler, which is now
bundled with MPLAB. This is a C compiler that is tuned
specifically to the Microchip 12-bit cores like the PIC12F508.
You can write those I2C routines in C instead of assembly,
if that makes it any easier. I like where you are going
with this idea; I’ve considered a similar direction for such
device projects.
The open-drain SDA line’s value comes from the I2C
network specification that allows for multiple masters.
If more than one device tries to take control of the SDA
line at the same time, they will fight each other. One may
try to pull the signal low while the other is trying to pull
it high. If this happens with active outputs, then one of
those two parts is going to damage its output line. With an
open-drain, an output will try to drive the line low, but it
will only allow the line to be pulled high by the load resistor
on that I/O line. Two outputs won’t fight each other.
If you use the TRIS register rather than the PORT
register to control this output line, then what happens is
you pre-load the output bit to a 0 (low). You have a load
resistor on the SDA bus line that will pull the line high
when no one is driving the SDA line. When you write a 0
to your SDA bit in the TRIS register, you will drive the line
low. When you write a 1 to your SDA bit in the TRIS
register, you are turning that bit into an input which floats
or stops driving the output — at which point the pull-up
resistor on your SDA bus line then pulls the I/O line high.
You are — in effect — simulating the open-drain output.
What is cool about this method is that you just treat the
TRIS register like it is a PORT register; nothing more needs
to be considered. This is so simple in the PIC that it makes
one wonder if the creators had this in mind from the
SERVO 07.2009
13