|An Introduction To Microcontrollers|
Definition of a Microcontroller
Why use a Microcontroller?
What a Microcontroller must have
The BASIC Stamp
BASIC Stamp Software
Definition of a Microcontroller:
First up, just so we're all clear on at least one thing, what is a microcontroller? Technical definitions can be as vague as "Computer Chips" and personal opinion is bound to vary. I like to think of microcontrollers as programmable logic; integrated circuits which can be programmed to do a number of (hopefully logical) things with their pins.
The applications for micros are endless. It's possible to control just about anything electronic with software written by the user and downloaded/programmed into the microcontroller.
Why use a Microcontroller:
Two words; miniaturization, flexibility. Some of the more powerful microcontrollers can easily replace a few hundred logic IC's and achieve a more reliable result. Micros are best suited to situations where some degree of intelligence is required. (ie. decision making) They are by no means restricted to this.
I'm sure everyone will agree, it's easier to edit a text file than rewire a PCB to fix an error. Life can be made a lot easier by dictating the operation of a circuit with software, rather than component layout.
What a Microcontroller must have:
Most general purpose microcontrollers have the following:
Some micros will have extras:
Because microcontrollers execute software they are essentially digital devices. This doesn't mean that they can't work with analog electronics, but it does mean that any work with analog electronics is done in a digital form.
Now I'll introduce some real world examples.
1. PIC18F452: One of the more powerful PICmicro's.
2. PIC16F628: Mid range PICmicro. Less I/O pins, less program space... etc.
3. BASIC Stamp 2p40: The most advanced of the BASIC Stamp series.
4. BASIC Stamp 2: Joe Bloggs BASIC Stamp, actually a PICmicro at heart.
Another microcontroller I'm told can be quite useful is the PICAXE. My understanding is that these devices are actually PICmicro's with an interpreter firmware which executes BASIC tokens, (just like the BASIC Stamp) the difference being that they store the user code in the chip itself rather than an external EEPROM. The result is a single chip (rather than a small circuit board) and considerably reduced prices. The cheapest being under NZ$10. PICAXE micros and starter kits can be purchased from the South Island Component Centre. The PICAXE homepage can be found here.
Many, many, many, many more different microcontrollers exist. The PICmicro family has several hundred members alone, only two of which are above. Because I only have experience with the BASIC Stamp and PICmicro I won't be going into detail on other systems, but a lot of the information in this article will apply to most micros.
The BASIC Stamp:
The BASIC Stamp is a good entry point, in my opinion. They are as fast as the average hobbyist needs for most applications, are extremely easy to write software for and require only a serial cable for programming. One of the biggest advantages of using a BASIC Stamp is that all the functions (PWM, serial I/O... etc) can be performed on any I/O pin, rather than the pin dedicated for that function.
As you might have guessed from the name, BASIC Stamps are programmed in yet another variation of BASIC, (Beginners All-Purpose Symbolic Instruction Code) called Parallax BASIC or just PBASIC. It's very easy to learn and quite powerful with some of the more advanced BASIC Stamp modules.
The BASIC Stamp is somewhat different to most microcontrollers in construction. Rather than being a self-contained chip they are constructed on a small PCB which usually fits into a standard IC footprint. They use another microcontroller to run a PBASIC interpreter which fetches "tokens" out of an external EEPROM and executes them. This EEPROM can also hold user data, in addition to the internal RAM.
The most annoying thing about the BASIC Stamp is the cost. The lower end modules are in the range of NZ$100 to NZ$150, and things like the BS2p40 are close to NZ$300. Not exactly the in the hobbyists budget, but it doesn't stop most people. As there's only a few different modules, I'll go over each on briefly.
Overall, the BASIC Stamp is worth looking at, especially if you are just starting with microcontrollers and don't know a lot about assembly language programming.
The PIC differs from the BASIC Stamp in many, many ways. Firstly, they are completely self-contained processors with the CPU, memory and I/O all in a single package. Some of the more recent PIC's can run on an internal RC oscillator and therefore require no external resonator or crystal.
Being a RISC (Reduced Instruction Set Computer) processor, the instructions executed by the PIC do very little by themselves meaning simple arithmetic and I/O operations require the execution of several instructions. Thus using a PIC is a little harder than a BASIC Stamp. Your can write your code in higher level languages like C and BASIC, but you sacrifice the speed and efficiency of assembly language programming. Compilers for higher level languages translate each statement into multiple instructions, some of which are not always needed but cannot be omitted.
Unlike the BASIC Stamp, PIC's support interrupts. An interrupt is triggered by an event such as a timer overflowing, an I/O pin changing state or an analog to digital conversion completing. When an interrupt occurs the micro automatically executes code (written by the user) to "service" the interrupt, which involves finding out what caused the interrupt and taking appropriate action. Interrupts allow the micro to perform other tasks while still being able to react to certain events without having to poll for those events. (Polling involves constantly checking whether the event has occurred or not)
Getting your code into a PIC is more difficult than for a BASIC Stamp. It must be programmed by an external device called a "programmer" (how imaginative) which interfaces to a PC through either a serial or parallel port. There's two ways to do this, high voltage (HVP) and low voltage programming (LVP).
High voltage programming requires the programmer to apply 13 volts to the PICmicro's /MCLR (reset) pin in order to enable the PIC to receive the program. As the PIC itself runs on a 5 volt supply the use of HVP programming ensures the micro cannot falsely program itself during normal operation. Low voltage programming provides a means of programming a PIC with 5 volts, but requires a dedicated I/O pin to initiate programming mode. (by driving it high) This I/O pin cannot be used by the user as normal operation could easily corrupt the devices programming.
Many DIY programmers are available for free over the internet and can be built for as little as NZ$20. Almost all of these will be "prototype" programmers, meaning they simply write and verify the program code. "Production" programmers must write and verify the code over the entire tolerable Vcc range, usually 4.5-5.5 volts. This is not necessary for most hobbyists. I strongly recommend the use of a HVP programmer, as LVP programming can only be enabled by a HVP programmer. (not to mention that less PIC's support LVP) I would also avoid programmers which derive their power (and 13V signal) from the RS-232 line, as some modern COM port do not provide significant voltage swings on their COM ports. If you want to use a laptop to write your software this is even more important.
Of the many designs out there, I would recommend the Wisp628 programmer from Technische Informatica. It uses a charge pump to get 13.5 volts (for HVP) from a 5 volt supply allowing it to be powered by the target circuit, and uses a MAX232 line driver to ensure compatibility with all computers.
I have recently posted some details on my own implementation of the Wisp628 PIC Programmer and the upcoming Wisp628 IDE software. Click here to check it out.
Below is a brief summary of a few micros I have used, the PIC16F628 and PIC18F452.
BASIC Stamp Software:
Now that we're vaguely familiar with my two favourite microcontrollers I'll shed some light on writing programs for them. This is not a full blown tutorial, just a few bits of information to give an impression of what's required.
As mentioned earlier the BASIC Stamp is programmed in a variant of BASIC called PBASIC. Most PBASIC statement have the layout shown below.
STATEMENT Pin, Data, Variable
STATEMENT is the instruction name, like HIGH or LOW which set the voltage state on any I/O pin. Pin tells the BASIC Stamp what I/O pin to perform the instruction on, (this can usually be any I/O pin) Data is any number of comma delimited constants or variables which provide the Stamp with information on how to execute the instruction. Variable is sometimes needed to store a result.
This is of course a general format, not all statements follow it. Here's a few actual examples of PBASIC statements for the BASIC Stamp 1:
HIGH 4 - Set I/O pin 4 to logical 1 (5V)
LET b0 = $20 - Load the hexadecimal constant '20' into RAM location b0 (byte 0)
SEROUT 1, N2400, ("Hello World") - Transmit the 11 characters "HELLO WORLD" on I/O pin 1 at 2400 baud (inverted)
The important thing to notice is that the mnemonics and acronyms in PBASIC read like english. It's easy to write software for the BASIC Stamp because the language allows you to write your program like a series of events, or like a story.
It's critical for a microcontroller to be able make decisions, ("choose you own adventure" novels anyone?) and PBASIC has a few statements which allow this by conditionally jumping from one point in your program to another.
IF Variable = Variable/Constant THEN Line Label
This is a simplified IF THEN statement, (simplified meaning there's no ELSE) which allows the BASIC Stamp to test a condition and jump to another point in the program if that condition is true. For example:
IF IN1 = 1 THEN Pin1_Is_High
IN1 is a PBASIC reserved variable which represents the logic voltage state on I/O pin 1. If this is high (5V) then the BASIC Stamp will jump to the point in the program immediately following the label Pin1_Is_High.
Of course there's much more to PBASIC than this, but this is only meant to touch the surface. For more information on PBASIC, refer to the BASIC Stamp Manual which can be downloaded in PDF format from Parallax's website.
Although PIC's can be programmed in a range of higher level languages like BASIC, C, Java and Jal, I prefer assembly language. Assembly language requires you to tell the microcontroller exactly what to do one instruction at a time, unlike PBASIC where you ask it to perform a task and it will execute any number of instructions.
Single instructions perform simple and somewhat useless operations by themselves, and therefore must be grouped together to perform useful tasks. The code below performs the same task as the PBASIC statement HIGH 4:
BSF STATUS, RP0
BCF TRISB, 4
BCF STATUS, RP0
BSF PORTB, 4
BSF stands for Bit Set File register. BCF stands for Bit Clear File register. The first and third lines are required to give the CPU access to the required file registers TRISB and PORTB. By clearing bit 4 in TRISB I/O pin 4 of port B is put into output mode, (tristate mode is disabled) and by setting bit 4 in PORTB I/O pin 4 of port B is driven high. Confused? If you're having difficulty following, it may be a good idea to start with a BASIC Stamp. (I did)
PIC's have a "working" register (W register) which holds one operand for most instructions, allowing stored instructions to take up no more than 16bits. (depending on the PIC) The following instructions demonstrate this:
MOVLW Constant - Move Literal to Working register
MOVWF File Register - Move Working register to File register
ADDWF File Register - Add Working register to File register
INCF File Register - Increment File register
By using the MOVLW instruction the operand in W can be predefined. If the PIC had to remember two file registers or constants to use as operands the instruction size would too large, at least 22 bits.
Flow control (decision making) is not so straight forward with the PIC. A few instructions have variations which will skip the next instruction if a certain condition is met. Some of these are:
BTFSS File Register, Bit - Bit Test File register, Skip if Set
BTFSC File Register, Bit - Bit Test File register, Skip if Clear
INCFSZ File Register - Increment File register, Skip if Zero
DECFSZ File Register - Decrement File register, Skip if Zero
The first two instructions will skip the following instruction if the bit of the file register being tested is set, (or clear) and the next two instructions will increment or decrement a file register and skip the next instruction if the result is zero. (incrementing past 255 rolls over to zero) If the next instruction is an unconditional branch, (GOTO) then flow control has been achieved.
As you can see, writing software for the PICmicro is somewhat more involved. If you're just looking to get started with microcontrollers I'd recommend you get yourself a BASIC Stamp, (even a BS1) and download the BASIC Stamp Manual from Parallax. In time you will probably move on to PIC's or another assembly language micro.
That's about as much as I can write in one night, I hope it is of use. What I've tried to do here is simply to give an insight to the world of microcontrollers for those who have little or no knowledge of them. If you choose to start working with a microcontroller there's always plenty of information and tutorials online, not to mention manufacturers documentation.
If you have any comments or questions please don't hesitate to contact me.
|Return To Top||Last Updated: 18/02/2007||Home Page|