MANAGING YOUR MOBILE MONKEY
+ 5.0 volt rail as its onboard + 3. 3 volt regulator is always
engaged. The PIC18F2620 is the major player in a minimal
PIC microcontroller configuration. There are just enough
supporting components to allow us to clock, program, and
debug the PIC18F2620. We could make the uDrive design a
bit leaner by using the PIC18F2620’s on-chip internal CPU
clock instead of the 20 MHz crystal (Y1) and its associated
capacitors. The PIC18F2620 can run at speeds between 31
kHz and 32 MHz by utilizing its internal oscillator and companion
PLL (Phase Locked Loop) clock multiplier. I incorporated the
optional activity LED into the hardware you see in Photo 2.
The activity LED is triggered by the Timer3 interrupt and
blinks at 1 Hz when the PIC is percolating as it should. If you
think the activity LED is just eye candy, the inactivity of the
activity LED helped me pinpoint and repair a cold solder joint
in my circuitry during the hardware build phase of this project.
The uDrive attaches directly to the PIC18F2620’s
EUSART (Enhanced Universal Synchronous/Asynchronous
Receiver Transmitter) I/O pins. Using the uDrive’s RESET pin is
optional. I chose to connect the RESET pin as I like the host
PIC to have full control of the peripherals within its domain.
I assembled my uDrive project on a custom plated-through breadboard, which I had manufactured by
ExpressPCB. As I alluded to earlier with my reference to a
cold solder joint, the hardware you see in Photo 2 was
constructed using point-to-point wiring techniques. You
can use your connector of choice for the Microchip
ICSP programming/debugging interface. I used a six-pin
configuration here as I have a RJ- 12 dongle that I use as an
adapter for my Microchip REAL ICE in-circuit emulator. I’ll
provide the ExpressPCB file for the dongle just in case you
want to use it, as well. You can get the ExpressPCB dongle
file from the SERVO website ( www.servomagazine.com).
Coding the uDrive-uSD-G1 API
Since the uDrive communicates serially with the
PIC18F2620’s EUSART, we’ll need to assemble a serial driver
for the PIC18F2620. Our PIC18F2620 serial driver is
interrupt driven and consists of a pair of 256-byte transmit
and receive buffers. The buffers are holding points for
incoming and outgoing data. Data transfers to and
from the buffers are triggered by the EUSART interrupt
mechanism. A single-statement function called
CharInQueue is the EUSART interrupt trigger. Incoming
serial characters from the uDrive are processed by the
EUSART_RxBuf buffer and recvchar function while the
sendchar function and EUSART_TxBuf handle outgoing
serial traffic. You can see all of the buffer definitions and
the actual EUSART code by downloading the API source
code from the SERVO website.
The EUSART code is universal and you can apply it in
your other PIC projects. When you get your copy of the
uDrive API source code, you’ll see that I defined the
PIC18F2620 TX pin that is normally under total control of the
PIC18F2620’s EUSART. We need to control the logic level of
this TX I/O pin when initializing the uDrive. The device
needs some time alone before we attempt to talk to it. That
“alone” time is 400 ms immediately following power-up. We
need to make sure that the uDrive’s RX line is pulled logically
high and the PIC18F2620’s RX line is inactive within 100 ms
after power-up as the uDrive will be mumbling incoherently
over its TX line during this time. The pullup resistor makes
certain that the RX line is pulled high at power-up. We must
disable the PIC18F2620’s EUSART to take control of its pins.
So, at uDrive power-up, the PIC18F2620 EUSART is not
enabled and the PIC18F2620’s TX line is commanded
logically high by the API code. Pullup resistor R5 is logical
high insurance as it maintains a logically high level on the
uDrive RX pin during the entire power-up sequence.
The PIC18F2620 timing is also interrupt-driven. Timer1
and Timer3 are set up as real time clocks with each timer
ticking away milliseconds, seconds, minutes, and hours.
Timer1 is utilized by the second and millisecond delay
functions sdelay1 and mdelay1, respectively. With the
exception of blinking the activity LED, the Timer3 resource
is free and can be applied at your discretion.
The uDrive API is written in C using the HI-TECH PICC- 18
C compiler. The first function called from the main function
is the init function. Following the invocation of the init
function, the PIC18F2620’s I/O pins are initialized, the
analog-to-digital converter is disabled, the timers are
configured, and the interrupts for Timer1 and the EUSART
are activated. The activation of the PIC18F2620 interrupt
mechanism allows us to use the EUSART and timer
resources, as they are interrupt-driven. Following a one
second delay, the EUSART is initialized and enabled. The
one second delay gives the uDrive the “alone” time it needs
to properly set itself up and prepare to perform the autobaud detection sequence. Once the PIC18F2620’s EUSART
is operational, an ASCII “U” (0x55) is transmitted to the
uDrive. It uses this character to determine the PIC18F2620’s
EUSART baud rate. If all goes as planned, the uDrive returns
an ACK (0x06) character to the PIC18F2620.
The Timer3 interrupt is activated if an ACK is returned
for the autobaud operation and a five second delay period
is invoked. The blinking of the activity LED indicates that the
autobaud operation was a success. No blinky LED after five
seconds means that the autobaud detection process failed.
The activity LED will remain illuminated to indicate an
autobaud error has occurred. The activity LED on my uDrive
driver hardware is blinking. So, we have a GO to move on
and initialize the disk drive memory card.
The uDrive-uSD-G1 Initialize Disk Drive
Memory Card API Function
If a memory card is mounted when the uDrive powers
up, the disk drive memory card is automatically initialized.
If the memory card is inserted after power-up, the initialize
Disk Drive Memory Card command must be invoked. For
the purposes of demonstration, our API code always
initializes the memory card programmatically. Here’s the
source code for the initialize Disk Drive Memory Card function:
SERVO 05.2008 53