Microcontroller Operation:Program Memory and Program Counter

Program Memory

Microcontrollers used for prototyping and short production runs use flash memory to store the program. The program can be downloaded while the chip is in the application circuit (in-circuit programming). Alternatively, the chip is placed in a programming unit attached to the host computer for program downloading, before fitting it in the application board. For longer production runs, preprogrammed chips can be ordered from the manufacturer, which use mask programmed ROM, where the program is incorporated during chip fabrication.

The PIC 16 program consists of a list of 14-bit binary codes, each containing the instruction and operand (data) in one operation code. The program starts at address zero and the

instructions are executed in turn unless a branch instruction or an external ‘interrupt’ occurs. Usually, the last instruction causes a loop back to repeat the control sequence. The capacity of the program memory block is one of the most significant features of each PIC, varying from 1024 to 8096 instructions in the 16 series. The higher power PIC 18, 24, 32 and dsPIC ranges offer more memory, input/output (I/O) and peripheral features.

Program Counter

The Program Counter (PC) is a register that keeps track of the program sequence, by storing the address of the instruction currently being executed. It is automatically loaded with zero when the chip is powered up or reset. The program counter is file register number 2 in the SFR set. As each instruction is executed, PC is incremented (increased by one) to point to the next instruction. Program jumps are achieved by reloading PC to point to an instruction other than the next in sequence. This new address is provided in the instruction.

Often, it is necessary to jump from address zero to the start of the actual program at a higher address, because special control words must be stored in particular low addresses. Specifically, PIC 16 devices use address 004 to store the ‘interrupt vector’ (start address of the interrupt routine). In this case, the main program must not be located at address zero; instead, a jump to a higher address should be placed there. An assembler directive is then needed to place the start of the program at a higher address. This problem can be ignored for programs that do not use interrupts, and such simple programs will be located by default at address zero. Interrupts will be explained in more detail later.

Associated with the program counter is the ‘stack’. This is a temporary program counter store. When a subroutine is executed (see Chapter 5, Section 5.2.4), the stack register temporarily stores the current address, so that it can be recovered at a later point in the program. It is called a stack because the addresses are restored to the PC in the reverse order to which they were stored, that is, ‘last in, first out’ (LIFO), like a stack of plates.

Leave a comment

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