7.1 At the end - from the beginning...
What is always the most difficult thing to do? You have bought the microcontroller, you have learned everything about its circuits and registers, you have a great idea how to apply all your knowledge in practice. But the most difficult thing to do is to start...
How to start working?
A microcontroller is a good-natured “genie in the bottle” and no extra knowledge is required to use it.
In order to create a device controlled by the microcontroller, it is necessary to provide the simplest PC, program for compiling and simple device to transfer the code from PC to the chip itself.
Even though the whole process is quite logical, there are often some queries, not because it is complicated, but for numerous variations. Let’s take a look.
Writing program in assembly language
In order to write a program for the microcontroller, a specialized program in the Windows environment may be used. It may, but it does not have to... When using such a software, there are numerous tools which facilitate the operation (simulator tool comes first), which is an obvious advantage. But there is also another ways to write a program. Basically, text is the only thing that matters. Any program for text processing can be used for this purpose. The point is to write all instructions in such an order they should be executed by the microcontroller, observe the rules of assembly language and write instructions exactly as they are defined. In other words, you just have to follow the program idea. That’s all!
CSEG AT 0
JMP XRESET ; Reset vector
XRESET: ORL WMCON,#PERIOD ; Define Watch-dog period
ORL WMCON,#WDTEN ; Watch-dog timer is enabled
To enable the compiler to operate successfully, it is necessary that a document containing this program has the extension, .asm in its name, for example: Program asm.
When a specialized program (mplab) is used, this extension will be automatically added. If any other program for text processing (Notepad) is used then the document should be saved and renamed. For example: Program.txt -> Program.asm. This procedure is not necessarily performed. The document may be saved in original format while its text may be copied to the programmer for further use.
Compiling a program
The microcontroller “does not undrestand” assembly language as such. That is why it is necessary to compile the program into machine language. It is more than simple when a specialized program (mplab) is used because a compiler is a part of the software. Just one click on the appropriate icon solves the problem and a new document with .hex extension appears. It is actually the same program, only compiled into machine language which the microcontroller perfectly understands. Such documentation is commonly named “hex code” and seemingly represents a meaningless sequence of numbers in hexadecimal number system.
In the event that other software for program writing in assembly language is used, a special software for compiling the program must be installed and used as follows - set up the compiler, open the document with .asm extension and compile. The result is the same- a new document with extension .hex. The only problem now is that it is stored in your PC.
Programming a microcontroller
In order to transfer a “hex code” to the microcontroller, it is necessary to provide a cable for serial communication and a special device, called programmer, with software. There are several ways to do it.
A large number of programs and electronic circuits having this purpose can be found on the Internet. Do as follows: open hex code document, set a few parameters and click the icon for compiling. After a while, a sequence of zeros and ones will be programmed into the microcontroller through the serial connection cable and programmer hardware. What's left is to place the programmed chip into the taget device. In the event that it is necessary to make some changes in the program, the previous procedure may be repeated an unlimited number of times.
The end or...?
This section briefly describes the use of MPLAB and programmer software developed by Mikroelektronika. Everything is very simple...
Start the program Mikroelektronika Asm51 Console
. The window appears...
...Open a new document: File -> New. Write your program or copy text...
... Save and name your document: File -> Save As... (Document name is limited to 8 characters!)
Finally, to compile program into HEX code select: Project -> Build or click the 'play' icon.
If everything works properly, the computer will respond with a short report.
The program is written and successfully compiled. All that's left is to dump the program to the microcontroller. For this purpose it is necessary to have a software that takes the written and compiled program and passes it to the microcontroller.
Start the program 8051 Flash_setup.exe...
...and finally - Finish!
Program installation is performed as usually - just click Next, Accept, Next...
The program has been installed and ready for use. The settings are easily performed so that there is no need for additional explanations (the type of the microcontroller, frequency and clock oscillator etc.).
- Connect the PC and programmer via a USB cable;
- Load the HEX code using command: File -> Load HEX; and
- Click the 'Write' push button and wait...
That’s all! The microcontroller is programmed and everything is ready for operation. If you are not satisfied, make some changes in the program and repeat the procedure. Until when? Until you feel satisfied...
A device which in the testing program phase can simulate any environment is called a development system. Apart from the programmer, the power supply unit and the microcontroller’s socket, the development system contains elements for input pin activation and output pin monitoring. The simplest version has every pin connected to one push button and one LED as well. A high quality version has LED displays, LCD displays, temperature sensors and all other elements which can be supplied with the target device. These peripherals can be connected to the MCU via miniature jumpers. In this way, the whole program may be tested in practice during its development stage, because the microcontroller doesn't know or care whether its input is activated by a push button or a sensor built in a real device.