ELEC2117 Chapter 3a: PIC16F886 Instruction set Professor Eliathamby Ambikairajah Head of School of Electrical Engineering and Telecommunications, UNSW, Sydney 06 March 2017
Prof E Ambikairajah Instruction Set For a microcontroller to operate, it needs instructions (i.e. the commands that the microcontroller understands) on what to do in the form of a program. The PIC microcontroller executes instructions from program memory, starting from address zero (0000H), when the PIC is reset upon power-up. The PIC 16F886 is a Reduced Instruction Set Computer (RISC) and there are 35 instructions for it. One of the important characteristics of RISC architecture is that each instruction is contained in a single binary word and in our case, the binary word is 14-bits which contains (a) The instruction code (b) Address or data information. The RISC are well-optimised in terms of operating speed, simplicity in architecture and code compactness. The PIC microcontroller fetches a 14-bit instruction from the program memory in a single instruction cycle. 1
Clock Frequency and Instruction Cycle The overall speed of the microcontroller operation is solely dependent on the clock frequency. Clock speed for the PIC16F886: DC to 20 MHz (external clock) Internal Oscillator for the PIC16F886 is software selectable with the clock frequency ranging from 31 khz to 8 MHz The main oscillator frequency (internal or external) is divided by 4 to obtain an instruction cycle time. Four clock pulses are required to execute one instruction except for any program branches which need 8 clock cycles. Table below provides selected clock frequencies and the resulting instruction cycle durations. Overall power consumption of the microcontroller depends on the clock frequency. The higher the speed of operation, the higher the power dissipation. 2
Instruction Pipeline In many CPUs, the instruction is first fetched from the program memory and then executed (a sequential process) The combination of Harvard Architecture and RISC architecture used in PIC microcontrollers allows the program instructions to be pipelined. That is, while the CPU is executing one instruction, it is also fetching the next instruction. A Schematic diagram representing the pipeline process is shown below and it can be seen that while instruction 1 is being executed, instruction 2 is already being fetched. This simple design provides a doubling in execution speed. 3
Arithmetic Logic Unit (ALU) The ALU (shown below) of PIC16F886 receives data from the working register (W) and from the data memory or from the instruction. For many instructions, the results of the ALU can either be stored in the W register (d-bit =0) or stored in data memory (d-bit =1) For certain instructions, the d-bit is specified within the instructions After an arithmetic/logical operation, the ALU modifies the Z, DC, C bits of the STATUS register. 4
35 Instructions for the PIC16F886 5
Prof E Ambikairajah Instruction Descriptions The first column gives the Mnemonic together with the code, specifying the type of operand The second column gives the description of the instruction The third column provides the number of instruction cycles required to execute the instruction The fourth column provides the 14-bit opcode The fifth column shows the three bits (Z, DC, C) of the STATUS register affected by the instruction Description Add the contents of the W and f registers. If d = 0 the result is stored in the W register. If d = 1 the result is stored back in register f. Legend f - any memory location (7-bit) W - working register d destination bit (single bit) 6
11
13
14
Total 35 Instructions 14
Programming a Microcontroller A programmer can write programs for a computer in the following ways: High Level Language (BASIC, FORTRAN, C, C++ etc.) The programs written in high level programming language can run on a variety of computers. Writing a program in a high level programming language is much easier than an equivalent program written in an assembly language or in a machine language. Programs written in high-level languages are translated into machine code by a compiler. Assembly Language An assembly language is a machine dependent language and is also a low level programming language. The programmers must know the architecture of the processor for which the program is written. Assembly language programs are translated into machine code (ones and zeros) by an assembler. Assembly language is used when speed is essential or if an operation cannot be performed in a high-level language. Machine code These are zeros and ones and directly executed by a computer. This code is the lowest level of computer software and incredibly slow to write and error-prone. Each computer has its own machine code. 15
Prof E Ambikairajah Assembly Language Programming Assembly language programs have a simple format and there are four parts: LABEL A Label, An Instruction Mnemonic (op-code), An operand, A comment. Mnemonic (OP-code) Operand Comments start movlw 0xFF ;Load the W register with a constant The Machine code for movlw H FF would be: 11 0000 1111 1111 Move Literal to W Operand as a constant Hex FF The operands may be a register, a variable, a literal constant, a label, a memory address. Comments are optional but must begin with a semi-colon. 19
Microchip MPASM Assembler The assembler offered by Microchip is known as MPASM. The assembler program has to be processed by the assembler and in order to aid this process assembler directives are required. Assembly directives are commands and do not affect the operation of the microcontroller. A few MPASM examples are shown below: Assembler directive Directive description Processor 16f886 It must be written at the beginning of each program and it defines the type of the microcontroller EQU It used to replace a numeric value by a symbol. eg: Max EQU H 25 or PORTB EQU 06 ORG It specifies a location in program memory where the program starts: e.g ORG H 00 END Each program must end using this directive and the assembler immediately stops compiling. #include During compiling, it enables the assembler to use data contained in another file. e.g #include <p16f886.inc> The number representation in MPASM Assembler is shown below: Radix Examples Decimal D 342 or.342 Hexadecimal H 9D or 0x9D or 09D octal O 467 Binary b 10001101 ASCII A G 19
Prof E Ambikairajah Chapter 3a: Exercise 1 Calculate the clock frequency that will give an instruction cycle time of 1.753 micro seconds Using the information in page 5 (35 Instructions for the PIC16F886) the 14-bit opcode is given as 11 1111 1111 0010. Identify and explain the function/operation of the opcode. What is the duration of PIC16F886 instruction cycle? Briefly explain. Three PIC microcontrollers (A, B & C) have maximum clock speeds 10MHz, 20MHz, 24MHz respectively. Microcontroller A divides its clock by 4 to give one clock cycle, microcontroller B by 8, and microcontroller C by 12. Microcontrollers A & C take 2 clock cycles to perform an instruction, while microcontroller B takes three clock cycles. Which PIC microcontroller performs that instruction faster? What do you understand when we say that the PIC16F886 microcontroller is an 8-bit microcontroller? Describe the Data Memory layout of PIC16F886 (number of banks, SFRs, GPRs, etc). Draw a schematic explaining the layout. Explain in few words the purpose of the fourth address location in the data memory (Bank 0,1,2, & 3) of the PIC16F886 microcontroller. What is the function of the W register in the PIC16F886 microcontroller? 19
Prof E Ambikairajah ELEC2117: References 1. Designing Embedded Systems with PIC Microcontrollers Tim Wilmshurst, Elsevier, 2010 2. PIC Microcontrollers Free online book mikroelektronika ; http://www.mikroe.com/products/view/11/book-pic-microcontrollers/ 3. PIC 16F886 Data Sheet (2007), Microchip Technology; www.microchip.com 20