Microcontroller Operation:Program Control

Program Control

As we have already seen, the microcontroller program is a list of binary codes in the program memory, which are executed in sequence. The sequence is controlled by the program counter, (PC). Most of the time, PC is simply incremented by one to proceed to the next instruction. However, if a program jump (branch) is needed, PC must be modified, that is, the address of the next instruction required loaded into PC, replacing the existing value.

The PC is cleared to zero when the chip is reset or powered up for the first time, so program execution starts at address 0000. The clock signal then drives the execution sequence forward. During the execution cycle of the first instruction, the PC is incremented to 0001, so that the processor is ready to execute the next instruction. This process is repeated unless there is a jump instruction.

The jump instructions must have a destination address as the operand. This can be given as a numerical address, but this would mean that the instructions would have to be counted up by the programmer to work out this address. So, as we can see in the program examples, a destination address is usually specified in the program source code by using a recognizable label, such as ‘again’, ‘start’ or ‘wait’. The assembler program then replaces the label with the actual address when the assembler code is converted to machine code.

Program sequence control operations are illustrated in Figures 2.2e2.4. The diagrams show the program memory from address zero, with different types of jump instruction at address 0002.

Jump

The unconditional jump (Figure 2.2) forces a jump to another point in the program every time it is executed. This is carried out by replacing the contents of the program counter with the address of the destination instruction, in this case, 005. Execution then continues from the new address. Note that the code for GOTO is 28 combined with the destination address 05, giving the instruction code 2805h.

PIC Microcontrollers-1135

The label ‘start’ is placed in the first column of the program code, to differentiate it from the instruction mnemonics, which must be placed in the second column, as we will see. The spelling of the label and its reference must match exactly, including upper and lower case letters. The label is replaced by the corresponding address by the assembler when creating the machine code for the GOTO instruction.

An example of an unconditional jump in the keypad program can be seen at line 50, GOTO Next, where Next is the label assigned to line 37.

Leave a comment

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