Beginners Guide to Microcontrollers
By : Sujit K. Nayak
Beginning
with microcontrollers might look a bit intimidating at first thought. The
reason is that there are lots of microcontrollers to choose from, each with its
own instruction set, architecture and programming tools. But believe me it
isn’t that hard at all.
My
suggestion is that you should learn to program as many microcontrollers as you
can because that would give you the greatest degree of freedom in the embedded
systems world and besides that it will add a few extra lines on your CV :). So
don’t listen to people saying that 8051 is better than the PIC or the AVR beats
the PIC. You can perform any task on a microcontroller that can be done on any
other microcontroller. The only difference will be in the “way” it is done
because all microcontrollers have different instruction sets and architectures.
The
following is a brief introduction into the world of microcontrollers. I might
leave out some points so please forgive me if you find this article
insufficient. And I might also sound a bit biased towards PICs simply because I
like them. But this doesn’t mean that only PICs are the ultimate
microcontrollers. It’s because that’s that only thing that I have learnt
thoroughly so far :)
8051
(and its derivatives)
The 8051
was introduced by Intel Corporation in 1981 and is apparently the most famous
8-bit microcontroller. There are mainly two reasons for its popularity (1)
Intel made the 8051 design open source, which means that any semiconductor manufacturer
can license the 8051 design from Intel and start making their own derivative of
the 8051 and (2) because the 8051 has been around for so long.
The 8051
IP core is available freely on the internet which means that even you can make
your own 8051 microcontroller on an FPGA board! The second reason that I stated
has led to the availability of literally hundreds of thousands of derivatives
of the 8051 from not less than a dozen of manufacturers.
The 8051
has 128 bytes of RAM, 4K Bytes of on-chip ROM, two timers, one serial port, and
four eight-bit I/O ports. The 8052 is an improvement to the 8051 and it has
twice the RAM (256 bytes), twice the on-chip ROM (8K Bytes) and 3 timers.
Whereas the 8031 is a ROM-less 8051 as it has all the features of the 8051
except for the on-chip ROM. A great place to start learning about the 8051
microcontroller is http://www.8052.com/
There are
lots of development tools available for the 8051 microcontroller. An easy to
build programmer worthwhile considering is Asim
Khan’s programmer. The assembler
for 8051 can be downloaded from Atmel’s site. If you prefer to program in C
then you should try Keil’s 8051 C compiler.
Or if you want to program in Basic then try out BASCOM51. Following is a short
intro to the various derivatives of the 8051 family available from different
manufacturers.
Silicon
Labs (formerly known as Cygnal)
The
product line of 8051 microcontrollers features the highest performance 8-bit
microcontrollers. Of all the 8-bit microcontrollers the 8051s from Silicon Labs
are the fastest with speeds of up to 100 MIPS! (million instructions per
second) with a fine blend of various analog and digital peripherals embedded
into the chip. Have a look yourself at the various products available from
Silicon Labs at http://www.silabs.com
Atmel
Atmel
first produced the AT89C51 microcontroller which was an 8051 with programmable
flash memory. This is ideal for fast development because flash memory can be
erased and written easily and lesser time as compared to other types of memory.
Currently Atmel has a huge product line based on the 8051 core. It produces
Flash, One time programmable (OTP) and Mask ROM 8051s thereby addressing the
needs of a wide variety of developers. From a student/hobbyist point of view
the AT89S5X microcontrollers are the most ideal because of their In System
Programmable capability. Surf around Atmel’s website http://www.atmel.com/products/8051/
and you will find alot to start with.
Intel
Although
the 8051 was originally designed by Intel, I haven’t seen any Intel 8051 as of
yet. But they still do have a valuable collection of documents related to the
8051 at http://developer.intel.com/design/mcs51/docs_mcs51.htm
Maxim’s
8051
Maxim
(Dallas Semiconductor) also has a small line of products featuring the 8051
core. They claim upto 33 times the performance of the original 8051. That was
made possible by a redesigned processor core that executes every 8051
instruction upto 12 times faster than the original 8051 giving it speeds of up
to a maximum of 33 MIPS at 33MHz. Have a look at the 8051-compatible products
from Maxim at http://www.maxim-ic.com/products/microcontrollers/high_speed.cfm
Analog
Microcontroller
Don’t be
deceived by the name as a microcontroller cannot be analog in its operation. The
ADuC800 series of analog microcontrollers is a product of Analog Devices which
features high speed ADCs and DACs, a voltage reference and a temperature sensor
with a 8052 core. Have a look at it http://www.analog.com/en/index.html
Phillips
Phillips,
in addition to producing the ARM based LPC2000, LPC900 and LPC700 series of
microcontrollers, also manufactures 8-bit 8051 derivatives. Go to http://www.standardics.philips.com/products/microcontrollers/
to see the various products available from Phillips.
Microchip
PIC
Microchip,
the second largest 8-bit microcontroller supplier in the world, (Motorola is
ranked No: 1) is the manufacturer of the PIC microcontroller and a number of
other embedded control solutions. Check out the following links for an overview
of the history of Microchip and PIC microcontrollers.
Microchip
offers four families of PIC microcontrollers, each designed to address the
needs of different designers.
- Base-Line: 12-bit Instruction Word length
- Mid-Range: 14-bit Instruction Word length
- High-End: 16-bit Instruction Word length
- Enhanced: 16-bit Instruction Word length
You might
be asking that how can an 8-bit microcontroller have a 12, 14 or 16 bit
instruction word length? The reason lies within the fact that PIC
microcontrollers are based on Harvard architecture. Harvard architecture has
the program memory and data memory as separate memories which are accessed from
separate buses. This improves bandwidth over traditional von Neumann
architecture in which program and data are fetched from the same memory using
the same bus. PIC microcontrollers have a data memory bus of 8-bit and a
program memory bus of 12, 14 or 16 bit length depending on the family. All PIC
microcontrollers have a mix of different on-chip peripherals like A/D
converters, Comparators, weak pull-ups, PWM modules, UARTs, Timers, SPI, I2C,
USB, LCD, CAN etc.
PIC
microcontrollers come in various sizes, from the 6-pin
smallest microcontroller in the world to the high
pin count, high memory devices. But from a student’s/hobbyist’s perspective
the 16F series of PICs are the most ideal to start with and out of them the
PIC16F84 seems to be the most popular microcontroller. Although the PIC16F84
and the PIC16F84A (later silicon revision of the PIC16F84) have been replaced
by the higher spec, lower priced PIC16F628 and the PIC16F628A, there are still
lots of tutorials on the internet and books that use the PIC16F84 as a starting
point. My suggestion is that you should start with the PIC16F628 or the
PIC16F628A. Then you can move on the high end PICs like the PIC16F876 or the
PIC16F877. If you don’t want to program in assembly and you prefer C then you
should start with the 18F series because they have a C friendly architecture.
In that case I would suggest you to start with the PIC18F452.
By now
your mind would have filled with numerous questions like where to start? what
would I need to program a PIC? what hardware should I use? what software should
I use? Well, I will try to answer all those questions.
First of
all I would suggest that you go to Nigel
Goodwin’s site which has all the answers to your questions. There you would
find schematics for the programmer, the programming software and a great series
of tutorials based on the PIC16F628 plus a support forum! The most popular,
fastest and the easiest to build programmer is the Parallel David Tait PIC
programmer which is commonly know as the father of all PIC programmers.
Although David Tait no longer provides support for his design there are a
number of variants of his design available on the internet. The P16PRO40 by
Bojan Dobaj or the PLMS OziPICer
and others are all variants of the original David Tait Design. Some people use
another very popular programmer known as the JDM programmer because it
works on the serial port and requires no external power supply whereas all
variants of the David Tait programmer work connect to the parallel port of a PC
and require an external power supply. But one thing I would want to say here is
that the simplicity of the JDM programmer comes at a cost of unreliability. The
JDM programmer draws power from the serial port and most serial ports are not
designed for such a task (especially laptop serial ports). There are some
versions of the JDM programmer which use an external supply. So, if someone
wants to build a JDM programmer he should look for versions with an external
power supply. But even then I wouldn’t recommend a serial port programmer like
the JDM. If you want to build or buy a serial port programmer then go for a “proper”
serial port programmer. By “proper” I mean one that has an embedded
microcontroller that does the programming.
Okay that
was a little intro to the hardware tools, now for the software tools. The first
thing you would need is an assembler. The assembler for PICs is known as MPASM
and it comes with MPLAB. The MPLAB integrated development environment (IDE) is
a free, integrated toolset for the development of embedded applications
employing Microchip's PICmicro and dsPIC microcontrollers. MPLAB also has a
simulator for PIC microcontrollers known as MPSIM. Check Microchip’s website for the latest version
of MPLAB. MPLAB will assemble your code into a “.HEX” file. Then next step is
copying the contents of the “.HEX” file to the program memory of your PIC
microcontroller. If you are using a David Tait programmer then either Nigel
Goodwin’s WinPicProg or ICProg will work. If you are using the JDM
programmer ICProg will be your first choice. For those of you who want to
develop code in C there are a number of compilers available for PIC
microcontrollers. Even if you plan to develop code in C I would recommend a
basic knowledge of assembly as it gives you a closer insight into the
architecture of the microcontroller you are programming. Microchip has only
made a compiler for the 18-series of PIC microcontrollers and another one for
dsPIC microcontrollers. There is a student version of the C18 compiler
available at Microchip’s website. If you want to program the 12, and 16 series
of PICs with C, you would have to look for third party tools like the ones from
IAR (very very expensive), HiTech, CCS
and mikroElektronika.
The compilers from these companies are very expensive but luckily all of them
offer free evaluation versions. Check their websites for more info.
In
addition to the assembler and C compilers available for PIC microcontrollers
there are also Basic compilers like the PIC Basic Compiler from
MELABS or Proton Plus Compiler
from Crownhill or mikroBasic
from mikroElektronika. There is even a Pascal
compiler for PIC microcontrollers from mikroElektronika. If that’s not
enough, there is a compiler for JAL
(just another language) for PICs as well. Third party simulation software for
PIC microcontrollers are also available like the Proteus VSM.
As you
will move on with developing applications using the PIC microcontroller you
will find yourself debugging your code most of the time. For that you would
need an In Circuit Debugger (ICD). The ICD in addition to being a normal
programmer, debugs the code you have developed “in-circuit”. The latest ICD2
from Microchip has a hefty price tag of $299 excluding shipping and taxes. An
alternative is to make one yourself. The ICD2 clone by Lothar Stolz is the simplest of all the ICD2
clones I have seen on the internet. PIC microcontrollers with ICD capability
can be programmed and debugged directly from within MPLAB using an ICD so that
there is no need for a multitude of programming tools.
Once you
get familiar with programming PIC microcontrollers you will feel the need to
view code developed by other programmers. For that go to the PICList which is the biggest archive of PIC
code on the internet. You should also download the Midrange Reference Manual
and the 18C Family Reference Manual from Microchip’s website. And in addition
to that you should have the datasheet (and its Errata) of whichever PIC
microcontroller you are working on.
Atmel
AVR
The AVR
microcontroller is the biggest contender against the PIC microcontroller for
the rank of THE best microcontroller. Some fans of PIC microcontrollers even
say that Atmel used a three-lettered-acronym (TLA) to name the AVR so as to
compete with the PIC. According to Atmel the acronym AVR stands for nothing,
its just a name and it isn’t supposed to mean anything. Have a look at the page
http://partsandkits.com/avr-faq.htm
if you are curious about what AVR stands for.
The AVR is
also based on the Harvard architecture and it too features on-chip peripherals
such as internal oscillators, timers, UART, SPI, pull-up resistors, pulse width
modulation, ADC and analog comparators etc. The AVR microcontrollers have a
RISC core running single cycle instructions as opposed to PICs which require
four cycles per instruction. That usually doesn’t imply that the AVR is four
times faster than the PIC because they have completely different architectures.
But at times you will see people arguing on discussion forums that AVRs are
faster than PICs. In embedded control applications speed isn’t the only
requirement. For most of the time the microcontroller is waiting for something
to happen or wasting cycles. The main concern here is the efficient
architecture, ease of programming, available programming tools and on-chip
peripherals. In that regard both PICs and AVRs go side by side. There are three
families of AVR microcontrollers that Atmel offers;
- tinyAVR
- Classic AVR
- MegaAVR
All three
families of microcontrollers have the same instruction set and memory
organization. The only difference is in the available features. The tinyAVR is
the one that has the least of features and comes in lower pin-count packages
while the MegaAVR comes with the most features and the highest pin-count
packages. Whereas the Classic AVR is somewhere between these two families.
Another feature of the AVR microcontrollers is that the instruction set is
highly optimized for C compilers. And as all of the AVRs have the same core you
only need one compiler for all the AVR devices.
As with
PICs, to start developing applications based on the AVR microcontroller you
would need a couple of hardware and software tools. The AVR studio is a
complete development suite that contains an editor and a simulator and it comes
free of cost. You can download the AVR Studio from Atmel’s Website. The most famous development
board for AVRs is the STK500. You can also make your own home made programmer
like the PonyProg but such
programmers have been known to be quite unreliable. The STK500 development
board is the most recommended programmer for AVRs. Just as with other
microcontrollers you will also need to download the instruction set manual and
the datasheet of the particular microcontroller you are working on.
The best
part of AVR microcontrollers is that there is a completely free C compiler
available. WinAVR
(pronounced "whenever") is a suite of executable, open source
software development tools for the Atmel AVR series of RISC microprocessors
hosted on the Windows platform. It includes the GNU GCC compiler for C and C++.
In addition to the AVR GCC other C compilers are also available for AVR
microcontrollers. These include IAR, Codevision AVR and ImageCraft. For fans of Basic there is
the BASCOM AVR compiler for
AVR microcontrollers.
The most
common AVRs are the AT90S2313 and the AT90S8515. But if you want to go for big
projects where you would need lots of program memory and on-chip peripherals
you should consider using the ATMega16 (with 16K Flash) or the ATMega32 (with
32K of Flash) or higher.
Texas
Instruments MSP430
The MSP430
is an ultra low power, 16-bit mixed-signal family of microcontrollers from
Texas Instruments. The low current features of the MSP430 make it ideal for
battery powered applications. In addition to a 16-bit RISC core the MSP430
features ADCs, DACs, analog comparators, supply voltage supervisor and LCD
drivers.
The MSP430
is available in two flavors; LCD based and Non-LCD based. The LCD based MSP430s
are further divided into the following two types
§
MSP430 4xx Flash based MCUs offer 1.8V-3.6V, up to 60kB/
Flash/ ROM 8MIPs with FLL + SVS
§
MSP430 3xx ROM/OTP family MCU offer 2.5V-5.5V operation,
up to 32kB, 4MIPS and with FLL
and the
Non-LCD based MSP430s are also further divided into the following two types;
§
MSP430 1xx Flash/ ROM based MCUs offer 1.8V to 3.6V
operation, up to 60kB, 8MIPs with Basic Clock
§
MSP430
2xx Flash based MCUs
offer 1.8V to 3.6V operation, brown-out reset, and 16MIPs with Basic Clock
All the
MSP430s except for the MSP430 3xx run on supply voltages lower than 5 volt. So
if you have designed a system with a 5V supply the you would have to use a
lower supply voltage and level converters between the MSP430 and the other
components of the system. Furthermore, none of the MSP430s are available in
through hole packages. Usually students and hobbyists don’t have the tools for
soldering/de-soldering surface mount parts. The solution is to either learn how
to work with surface mount parts or buy a development kit which has the
microcontroller already soldered on a PCB. You can make your own prototyping board
for the MSP430 in your home.
There are
a number of hardware and software tools available for the MSP430. Check TI’s website for more info. Texas Instruments has
also started a University Program for the MSP430 which features discounted
tools for members of the program, books based on the MSP430, course outlines
based on the MSP430 and a newsletter. There are lots of code examples for the
MSP430 to get you started in programming. Although the MSP430 is quite new, it
has the potential to become famous in a short time.
Maxim’s
MAXQ
The MAXQ
microcontroller from Maxim-Dallas Semiconductor is another newly developed
product aimed at acquiring a share of the market dominated by the 8051, PIC and
AVR. The MAXQ is also a 16-bit RISC microcontroller based on the Harvard
architecture featuring high performance at low power consumption figures.
There is
an Integrated Development Environment available at Maxim’s website for
developing applications using the MAXQ microcontrollers. It features an
integrated JTAG port for easy in-circuit debugging. Evaluation kits for the
MAXQ have became available only recently. The MAXQ microcontroller is a new
product and there aren’t much people using it. So I wouldn’t recommend a
student or a hobbyist to start with the MAXQ.
Maxim has
performed some benchmarks
and compared the MAXQ with other competitors in the market. The hilarious part
of these benchmarks is that they are actually comparing 16-bit microcontrollers
with 8-bit microcontrollers. That’s like comparing apples with oranges! But
nevertheless the MAXQ microcontrollers have some serious amount of potential
and we might start seeing it in consumer products soon.
Most
debated topics
8051
versus PIC versus AVR
Perhaps
the most debated topic on discussion forums is the comparison of 8051, PIC and
AVR in terms of speed, ease of programming and integrated peripherals. A lot of
beginners are confused about which microcontroller they should start with. As I
have said it doesn’t matter what microcontroller you begin with. You can do any
task with a microcontroller that can be done on any other microcontroller.
Nevertheless
there are some factors that may help you decide in choosing the right
microcontroller. Ease of programming is one issue; both the 8051 and the AVR
have a big instruction set whereas the 16-series of PICs have an instruction
set of 35 instructions (the 18-series has 77 instructions) and even out of them
some are rarely used. But to some people this is one of the downside of PICs
because a lower number of instructions might mean that you would have to use
more cycles to do the same thing whereas a microcontroller with a bigger
instruction set might be able to do the same thing in a single cycle. But
that’s not always the case. The second major concern is speed. Here I would
like to say that speed isn’t always the deciding factor in embedded control
applications because most of the time the microcontroller is waiting for
something to happen or is wasting machine cycles. The PIC takes four clock
cycle for a single instruction whereas the AVR takes one clock cycle for a
single instruction. But the 18-series PICs have internal hardware multipliers
of upto 4X which means that a PIC can have upto 10MIPS at 10MHz. Another factor
is the cost. This varies greatly from country to country. In some regions 8051s
are cheap and in some regions the AVR or PIC are cheap.
As far as
the support and the amount of code available on the internet is concerned the
PIC stands out from all the three contestants. There are huge amounts of
projects, tutorials, guides related to PICs on the internet. Microchip provides
the best datasheets and applications notes making it easy for the developer to produce
great applications. The reason for this is simply because PICs have been here
for quite a long time.
Assembly
versus C versus Basic
Another
question that is usually asked by beginners is which language should we use to
program the microcontroller. Well, my suggestion in this regard is that
whatever high-level language (like C or Basic) you use; you must have a good
understanding of the assembly language of your microcontroller. This helps to
optimize your code because microcontrollers usually have small amounts of
memory and the hex code produced by high level language compilers is bigger
than that produced by an assembler for the same task. But compilers are getting
better everyday and more and more organizations are now turning to high level
languages.
For high
level languages I suggest you go for C because it is the most flexible language
ever developed. If you want a career in embedded systems you must be good in C.
But there is a saying that technology is good if it works right for you, and
Basic is good technology! So if you like to write code in Basic and if it
serves your needs then you should use Basic.
Conclusion
Microcontrollers
are termed as “computers on a chip” because unlike microprocessors they have
integrated memory and peripherals. This reduces the chip count to a great
level. Most of the time only a few more components (in addition to the power
supply) are needed for a microcontroller based design. If you have been
building projects based on 74-series logic ICs then you seriously need to
consider switching to microcontrollers. A project that might consume tens of
logic ICs can be replaced with a single microcontroller. The best part is that
you can write your own program into microcontrollers and make it do any
function. Nowadays the distance between digital signal processors and
microcontrollers is greatly reducing with devices coming in the market that can
do the job of both.
A survey
states that the microprocessors used in PCs are only 2% of all the
microprocessors that are made in a year. The rest 98% goes into embedded
systems in the form of microcontrollers and FPGAs etc. So you can well imagine
the number of microcontrollers that are developed each year by literally
hundreds of manufacturers. Microcontrollers are here to stay! So, don’t get
left behind!
I am a regular reader of this blog very good site to understand electrical engineering. I am a student of civil engineering. If someone wants to know about engineering materials course of civil engineering then you could use this site- www.engg-material.blogspot.com
ReplyDelete