Microcontroller Programming Hardware and Software

advertisement
Microcontroller Programming
Hardware and Software
To program a low-level chip, you need three pieces
of hardware: a PC, a hardware programmer with
compatible cable, and the target circuit that you’ll
plug the micro into. You’ll also need some software.
1
1. PC: The vast majority of software tools for
programming microcontrollers run on Windows
machines (but see sidebar, page 162). You don’t
need a fast machine; any PC with the proper port
for your hardware programmer cable (serial, USB,
etc.) will do.
2. Hardware Programmer: This is what you plug
your chip into in order to transfer your program
from the PC. Traditionally, you then remove the
chip and place it in your circuit, but some hardware
programmers support in-circuit programming,
which lets you burn the chip in place, within the
circuit, making it easier to debug and re-run the
software.
For our example here, we used Microchip’s
PICkit 1 Flash Starter Kit, a $35 USB programmer
that contains a small demo board and can program
some but not all of Microchip’s 8- and 14-pin micros.
2
3
3. Target Circuit: If you’re just starting out with
microcontroller programming, you can experiment
with a demo board, like the one included with the
PICkit 1. These are printed circuit boards with
a space to plug in your chip and various input
and output devices such as buttons, LEDs, and
potentiometers. Using one of these boards, you can
explore the features of your chip and run different
programs without worrying about wiring.
If you have a standalone circuit idea in mind, the
next step is to build your own. There are plenty
of circuits published online, and you may be able
to find one that’s close to what you need. But
schematics often contain errors, so you need to be
careful. If you’re up to it, you can also design your
own circuit from scratch, as discussed below.
Software: In addition to the hardware, you need to
put together your software development environment.
This will include the text editor where you write
your code, a compiler, the software that drives your
hardware programmer (which probably came
included with the hardware), and microprocessor
simulation and debugging tools.
You can buy most of this software grouped
together into an integrated development environment
(IDE) from companies like Microchip and MicroEngineering Labs. We used Proton Lite, a free trialversion IDE that restricts you to 50 lines of BASIC
code — which is plenty for our simple blink program.
Make:
161
PIC PROGRAMMING
Designing
a Circuit
First, think of what inputs and outputs your circuit
will have: switches, sensors, lights, motors, etc.
Then you can determine the power requirements.
For simplicity, our circuit uses batteries, but a wallwart with a voltage regulator is more reliable.
Next, determine how your inputs will interface
with the microcontroller. Some pins take only digital
inputs, a.k.a. logic inputs, where 5V means 1 and 0V
means 0. The general rule for these is that power
brings the voltage up and ground draws it down. To
make a button that changes an input pin from 0 to
1, for example, connect the pin to ground, through a
resistor, and also connect it to a button that, when
pressed, completes a connection to power.
Some micro pins take analog as well as digital
inputs; you can feed these from analog sensors that
produce a range of electrical values. For example, a
potentiometer’s knob changes its resistance, which
changes a voltage fed through it. Connect a pot to a
pin that works as an analog-digital (ADC) converter,
and the micro will convert the current position of
the knob into a number you can program with.
An LED will light up directly from a micro’s output
pins, but things like motors require more current.
You can supply this by connecting an output pin to
the base of a transistor that has higher current running through it. Motors may generate voltage spikes
that can damage your chip, but a diode running in
reverse across the transistor will protect the circuit.
Once you know which sensors connect to which
types of pins, you need to study your microcontroller’s datasheet. As with most micros, pins on the
PIC12F675 perform multiple tasks, and you set registers in your software to tell the pins how to behave.
In the registers table from the PIC12F675 datasheet
on Microchip’s website, we see that the TRISIO register tells a pin to be input or output, and the ANSEL
register determines which pins connect to the ADC.
These registers have eight bits, one for each pin. So,
to connect a simple binary button to a pin, set its
corresponding bit in the TRISIO register to 1 (input)
and in the ANSEL register to 0 (disconnect). To connect an LED, set the TRISIO to 0 (output). To read a
potentiometer value, set the pin’s TRISIO to input
(1) and set the ANSEL to connect the ADC (0).
162
Make: Volume 04
www.makezine.com/04/primer
WRITING THE CODE
There are many general refererences for
programming in BASIC, but here are two handy
sample code segments for microcontrollers.
Button on pin GPIO.0 lights an LED on
pin GPIO.1:
if GPIO.0 = 1 then
GPIO.1 = 1
else
GPIO.1 = 0
endif
Turn an LED on for 1,000 program cycles:
The main program runs the startlight subroutine
when a button is pressed, turning the LED on,
and calls the endlight subroutine inside a loop,
to turn it back off.
startlight:
GPIO.1 = 1
counterVar = 1000
return
endlight:
counterVar = counterVar -1
if counterVar = 0 then
GPIO.1 = 0
endif
return
Open Source PIC Programming
Contrary to Microchip documentation, PIC development does not require a Windows PC. I use free Unix
tools on Mac OS X, plus a USB-to-serial adapter to
connect my Mac to my hardware programmer. Here’s
all the software you’ll need.
gputils: Package includes gpasm assembler, which
translates compiled source code into the hexified
format suitable for burning onto a PIC.
gpsim: PIC simulator steps through code and indicates pin status, for wiring-free debugging.
picp: Utility for PICSTART Plus and Warp-13 hardware
programmers, writes hex code to the PIC.
Fink: Unix package manager for Mac OS X lets you
install the utilities above, and other software.
X11 for Mac OS X, and Xcode Developer Tools (with X11
SDK): Available from Apple, these let you install Fink. —Mikey Sklar
Download