A Simple PIC Application:Program Execution

Program Execution

Microcontroller circuits will not function without a program in the chip; this is created using the PIC development system software on a PC, and downloaded via a serial data link. This process has already been outlined, and will be described in more detail later, so for now we will assume that the program is already in memory.

Figure 3.4 is a block diagram showing a simplified program execution model for the PIC 16F84A. The main elements are the program memory, decoder, working register and file registers. The binary program, in hexadecimal, is stored in the program memory. The instructions are decoded one at a time by the instruction decoder, and the required operations set up in the registers by the control logic. The file registers are numbered from 00 to 4F, with the first 12 registers (00 to 0B) being reserved for specific purposes. These are called the special function registers (SFRs). The rest may be used for temporary data storage; these are called the general purpose registers (GPRs). Only selected registers are shown in this diagram. All addresses and register contents are in hexadecimal. Appendix A explains hexadecimal numbers.

PIC Microcontrollers-1146

3.2.1. Program Memory

The program memory is a block of flash read-only memory (ROM), which means it is non- volatile, but can be reprogrammed. The program created in the host computer is downloaded via port register pins RB6 and RB7. The methods for doing this will be described in more detail in Chapter 4, as will the assembler programming language required to create the program code.

The 14-bit codes are loaded into memory starting at address 000. When the chip is powered up, the program counter resets automatically to 000, and the first instruction is fetched from this address, copied to the instruction register in the control block, decoded and executed. The file registers are modified accordingly, and the resulting output seen at the ports.

3.2.2. Program Counter, PCL: File Register 02

The program counter keeps track of the program execution by holding the address of the current instruction. It is automatically incremented to point to the next instruction during the execution cycle. If there is a jump in the program, the program counter is modified by the jump instruction (e.g. the last one in this program), so that it then points to the required jump destination address. PCLATH stands for program counter latch high. This stores the most significant two bits of the 10-bit program counter, which also cannot be accessed directly.

3.2.3. Working Register, W

This is the main data register (8 bits), used for holding the data that is currently being worked on. It is separate from the file register set and is therefore referred to as W in the PIC program. Literals (values given in the program) must be loaded into W before being moved to another register or used in a calculation. Most data movements have to be via W, in two stages, since direct moves between file registers are not available in the basic PIC instruction set.

3.2.4. Port B Data Register, PORTB: File Register 06

The 8 bits stored in the port B data register will appear on the LEDs connected to pins RB0eRB7, if the port bits are initialized as outputs. The data direction for each pin is determined by placing a data direction code in the register TRISB. A ‘0’ in TRISB sets the corresponding pin in the port register as an output (0 ¼ output). A ‘1’ sets it to input

(1 ¼ input). In this case, 00000000 (binary) will be placed in TRISB to set all bits as outputs, but any combination of inputs and outputs can be used.

3.2.5. Port A Data Register, PORTA: File Register 05

The least significant five bits of File Register 05 are connected to pins RA0eRA4, the other three being unused. Inputs RA0 and RA1 will be used later to read the push buttons. If not initialized as outputs, the PIC I/O pins automatically become inputs, i.e. TRISA ¼ xxx11111. We will use this default setting for port A, so this port does not have to be explicitly initialized. The state of these inputs will have no effect unless the program actually uses them; the first program BIN1 will not use them.

3.2.6. General Purpose Register 1, GPR1: File Register 0C

The first GPR will be used later in a timing loop. It is the first of a block of 68 such registers, numbered 0C to 4F in the ’84A chip. They may be allocated by the programmer as required for temporary data storage, counting and so on.

3.2.7. Bank 1 Registers

The main registers such as the program counter and port data registers are in a random access memory (RAM) block called register bank 0, while TRISA, TRISB and PCLATH are in a separate block, bank 1. Bank 0 can be directly addressed, meaning that data can be moved into it using a simple ‘move’ instruction.

Unfortunately, this is not the case with bank 1. There are two ways to write to these registers. The first way is a simple method, which we will use initially; it requires the required 8-bit code to be loaded into W first, and then moved into the bank 1 register using the TRIS instruction. Later, we will use the recommended method, using bank selection, but this is a little more complicated. TRIS does not now appear in the main instruction set, but continues to be recognized by the PIC assembler.

Leave a comment

Your email address will not be published. Required fields are marked *