Previous page

Table of contents

Chapter overview

Next page

 

5.7 MPSIM Simulator

Simulator is part of MPLAB environment which provides a better insight into the workings of a microcontroller. Trough a simulator, we can monitor current variable values, register values and status of port pins. Truthfully, simulator does not have the same value in all programs. If a program is simple (like the one given here as an example), simulation is not of great importance because setting port B pins to logic one is not a difficult task. However, simulator can be of great help with more complicated programs which include timers, different conditions where something happens and other similar requirements (especially with mathematical operations). Simulation, as the name indicates "simulates the work of a microcontroller". As microcontroller executes instructions one by one, simulator is conceived - programmer moves through a program step-by-step (line-by-line) and follows what goes on with data within a microcontroller. When writing is completed, it is a good trait to, programmer's first check his program in a simulator, and then runs it out in a real situation. Unfortunately, as with many other good habits, man overflows this one too, more or less. Reasons for this are partly personality, and partly lack of good simulators. 

First thing we need to do, as  in a real situation, is to reset a microcontroller with DEBUG > RUN > RESET command. This command results in bold line positioned at the beginning of a program, and program counter is positioned at zero which can be seen in status line (pc: 0x00).

Beginning of program simulation, resetting a microcontroller

One of the main characteristics of a simulator is the ability to view register status within a microcontroller. These registers are also called special function registers, or SFR registers. 
We can get a window with SFR registers by clicking on WINDOW->SPECIAL FUNCTION REGISTERS, or on SFR icon.

Beside SFR registers, it is useful to have an insight into file registers. Window with file registers can be opened by clicking on WINDOW->FILE REGISTERS.
If there are variables in the program, it is good to watch them, too. To each variable is assigned one window (Watch Windows) by clicking on WINDOW->WATCH WINDOWS.

Simulator with open windows for SFR registers, file registers and variables.

The next command in a simulator is DEBUG>RUN>STEP which starts our steping through the program. The same command could have been assigned from a keyboard with <F7> key (generally speaking, all significant commands have keys assigned on the keyboard).
By using the F7 key, program is executed step-by-step. When we get to a macro, file containing a macro is opened (Bank.inc), and we proceed to go through a macro. In a SFR registers window we can observe how W register receives value 0xFF and delivers it to port B. By clicking on F7 key again, we don't achieve anything because program has arrived to an "infinite loop". Infinite loop is a term we will meet often. It represents a loop from which a microcontroller can not get out until interrupt occurs (if it is used in a program), or until a microcontroller would be reset.

 

Previous page

Table of contents

Chapter overview

Next page

 

© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.