A WORD ABOUT SOFTWARE
The microcontroller can perform almost any digital function from the simplest logic operation to a complex mathematical calculation. The only problem is that you have to tell it to do what you want. And you do that by programming. A program is the sequence of instructions that detail the order in which you do the various processing steps that lead to your application. The controller chip itself is a dumb piece of hardware waiting for its program. What this means is that today, designing an electronic product is as much about writing programs as it is building electronic hardware.
Software is the name given to all the programs that a processor uses for developing and executing a program. The program that implements your application is the applications program. Other types of software used with the processor follow:
Languages—A programming language is a set of rules or syntax that defines how you tell the processor what to do. A language is the tool that you actually use to create your application program. It is a kind of short- hand code that lets you express the operations in simple terms. Editor—An editor is like a word processing program that lets you type in your program. You then save it as a file that the compiler or assembler uses to create machine code.
Compiler—A compiler is the program that translates your program code into binary code that can be stored in the memory and used by the processor. Processors only understand binary code but humans are not good with such code. So, you write your program in a language you can understand and the compiler converts it to the code that the processor understands.
Operating system—For large complex programs or for applications that require the processor to execute multiple programs at different times, you need a piece of software that manages the multiple programs and opera- tions. The operating system (OS) oversees things like memory usage and access, timing, and I/O operations. Not all embedded controllers need an OS, just the more powerful ones with multiple functions. For example, most cell phones have an OS.
Development environment—This is a complete package of software tools that facilitates your software development. It consists of languages and compilers, as well as programs for testing and debugging your code. Subroutines—These are short sections of code that perform a specific operation. It may be a math operation, some type of I/O process, or other function that is performed multiple times. You only write the subroutine once, and then access it when you need to do that operation. Many development systems have libraries of subroutines that you can tap so you do not have to program common functions yourself. Subroutines shorten your development time and speed execution.
Programming Languages
There are dozens of languages used from programming embedded controllers. The more popular ones are assembly language, C, and BASIC.
Assembly Language
Assembly language uses shorthand mnemonics such as three- or four-letter designations for the instructions that the processor can execute. Instead of programming with binary code, you use the mnemonics. In addition, you use names that you make up to identify memory locations, I/O ports, and other things normally referenced by a binary code. Following is what a simple assembly program looks like:
ORG $0000 LDAA FIRST MUL SECOND STA PRODUCT OUT PORT 1 END This programs loads one number from a memory location given the name FIRST, multiplies it by another number in memory location named SECOND. It then stores the result in another memory location called PRODUCT. It next out- puts the value to an I/O port called PORT 1. The ORG $0000 simply says to start the program whose first instruction is in memory location 0000, a hex address.
The software that converts the assembly language program to binary code is the assembler. The assembler gives actual binary assignments to the memory locations named as well as the instruction op codes.
Language
The C language is referred to as a higher-level language in that the various syntax and terminology of the language usually translates into multiple binary machine instructions. The C language lets you write the program in a more conversational or casual format using English instead of cryptic mnemonics and other terms. The language is a bit of a challenge to learn, but after that programming goes much faster. A compiler converts the C-language program code into binary code.
A simple C program may appear like this:
The first line identifies three integer values x, y, and z; 38 is assigned to x, and 57 to y. Then the difference between the two is designated z. Finally, the printf line accesses a subroutine that causes z to be printed.
BASIC Language
Some embedded controllers use a simplified version of BASIC. BASIC was invented before personal computers and initially became the main language of personal computer programming. Microsoft still sells its Visual BASIC soft- ware for program development. BASIC is very simple higher-level language that is easy to learn and use. The program created is then translated into binary code by a compiler. Alternately, the controller runs a program called an inter- preter that resides in memory and executes the program a command at a time.
The following program is written in PBASIC, a simple version of BASIC used with an embedded controller called the BASIC Stamp made by Parallax. It uses a PIC microprocessor and stores a program in flash memory. The pro- gram causes an LED to flash off and on.
The name of this program is Flash. The first line tells the controller to make pin 7 the output port. The next line tells pin 7 to go high or to binary 1. The next command says pause for 500 milliseconds. Next, pin 7 is made to go low or to binary 0 via the Low command. Pause makes it stay there for another 500 milliseconds. Finally, the Goto command causes the program to look back and repeat itself again and again. The result is an LED that turns off and on at a 1000-millisecond (1-second) rate.