Вы находитесь на странице: 1из 108

Computer Interfacing

Interfaces and Interfacing


. Definitions of interface from Websters Dictionary: . noun: the place at which independent systems meet and act or communicate with each other Examples:
human - machine interface (analogue-machine interface), terminal network interface (TTL - CMOS interface), parallel or serial interface

. Informal Definition i) The physical, electrical and logical means of exchanging Information with a functional module ii) The process of enabling a computer to communicate with the external world through Software, Hardware and Protocols
2

Interfaces and Interfacing


. An interface is a device and/or set of rules to match the output of one device to send information to the input of another device
. physical connection . the hardware . rules and procedures . the software

. Interfacing is the process of connecting devices together so that they can exchange information . The process of reading input signals and sending output signals is called I/O . I/O conventions
. I/O direction is relative to the MCU . Input is data read by the MCU . Output is data sent out by the MCU
A microcontroller (sometimes abbreviated C, uC or MCU)

Why is computer interfacing important ?


1. The human - machine interface determines the ultimate success or failure of many computer- based systems 2. Digital systems exist within and must successfully interact with an analogue natural environment (Digital analogue interfaces are unavoidable) 3. Rather than designing digital systems from elementary components, computer engineers more typically assemble new systems from existing subsystems

Typical Interfacing Activities


. Selecting software/hardware subsystems that can (at least potentially) interact well with each other
. Appropriate D/A and A/D converters (speed, accuracy, .) . Serial vs. parallel communication

. Providing appropriate hardware connections


. Selecting cabling, connectors, drivers, receivers, correct termination, etc.

. Resolving any hardware incompatibilities


. CMOS with TTL

. Configuring hardware interfaces correctly using low-level software drivers


. LCD, Keypads in embedded systems (Liquid Crystal Display-LCD)

. Interfacing software components correctly


. Selecting compatible software versions . Calling the correct procedures in the correct sequence with the correct parameters

Typical Interfacing Activities


SERIAL COMMUNICATION PARALLEL COMMUNICATION

Connectors

Transducer and receiver

Typical Interfacing Activities


What is Termination? Termination, in its most basic sense, is the connection of a cable to a source or destination device. For low frequency applications, this may involve simply putting the individual wires in a connector and attaching the connector to the device. At higher frequencies, termination can also take a second meaning. To first understand termination you need to understand impedance. In many systems, such as Broadband/CATV or broadcast video, signals are split to go in different directions. Each of the splits (outputs) must "see" the correct impedance. Normally, one would simply attach a cable of the correct impedance to each of the outputs of the device. But it can sometimes happen that there are more splits than desired. In that case, those unused outputs must be "terminated" with the correct impedance. To accomplish this, you attach a connector which contains a resistor inside chosen to mimic the correct impedance. In other words, you fool that output into thinking a cable is attached, when in fact, there is none! If you do not terminate unused outputs, they can radiate, and that signal can cause noise and interference in other nearby equipment.

For RF applications, or for extremely long signal runs, such as telephone circuits, impedance matching is essential. Matched impedances mean that the source, cable and load impedances are all the same.

Typical Interfacing Activities


CMOS with TTL Complementary metaloxidesemiconductor (CMOS) is a technology for constructing integrated circuits Transistortransistor logic (TTL) is a class of digital circuits built from bipolar junction transistors (BJT) and resistors

Typical Interfacing Activities


CMOS with TTL

Typical Interfacing Activities


CMOS with TTL

10

Typical Interfacing Activities


CMOS with TTL

11

System-Level Interfaces

Human-machine interface
. Input devices: keyboard, mouse, microphone, camera . Output devices: CRT, printer, light panel, audio amp

Cathode Ray Tube (CRT)

12

System-Level Interfaces
Digital - Analogue Interface
. Input devices: A/D converters, modems, sensors . Output devices: D/A converters, modems, transducers actuators, stepper motors . Control devices: switches, multiplexers, amplifiers Attenuators
level-shifters This great tiny board will allow to interface slave 3.3V I2C devices like magnetometers and pressure sensors with master 5V devices like AVR/PIC microcontrollers

Digital - Digital Interface


. Connectors: wires, ribbon cable, coax, twisted pair, PCB . I/O devices: buffers, level-shifters, synchronizers
A synchronous circuit is a digital circuit in which the parts are synchronized by a clock signal

An asynchronous circuit, or self-timed circuit, is a digital circuit which is not governed by a clock circuit or global clock signal ribbon cable

Printed Circuit Board-PCB


coax

twisted pair

PCB
13

Hardware Interfaces within a Personal Computer (PC)

14

Components of a Computer System

15

Introduction to Microcontroller
Embedded System . You are used to chips like the Pentium, but in terms of installed machines these are a small portion of total computer use. Think how many computers you have at home? . Digital cameras, video cameras, TVs, mobile phones, calculators, micro-wave ovens etc all contain processors. In order for a microprocessor to be used, other components such as memory, or components for receiving and sending data must be added to it. . On the other hand, microcontroller is designed to be all of that in one. . No other external components are needed for its application because all necessary peripherals are already built into it. . Thus, we save the time and space needed to construct devices. . The PICmicro was originally designed around 1980 by General Instrument as a small, fast, inexpensive embedded microcontroller with strong I/O 16 capabilities.

Characteristics of Embedded System


. Embedded computers have to be very low cost, simple and reliable. . They can not use any moving parts (disk drives) because: 1. power hungry 2. bulky 3. expensive

17

Features of Embedded System


. Program in Read Only Memory ROM . Limited RAM storage variables only not code . Built in I/O devices . Use very little power Embedded System General Block Diagram

18

Microcontroller
. Microcontrollers, as the name suggests, are small controllers . They are like single chip computers that are often embedded into other systems to function as processing/controlling unit
. For example, the remote control you are using probably has microcontrollers inside that do decoding and other controlling functions . They are also used in automobiles, washing machines, microwave ovens, toys ... etc, where automation is needed

. The key features of microcontrollers include: . High Integration of Functionality


. Microcontrollers sometimes are called single-chip computers because they have on-chip memory and I/O circuitry and other circuitries that enable them to function as small standalone computers without other supporting circuitry
19

Microcontroller
.The key features of microcontrollers include: . High Integration of Functionality
. Microcontrollers sometimes are called single-chip computers because they have on-chip memory and I/O circuitry and other circuitries that enable them to function as small standalone computers without other supporting circuitry

. Field Programmability, Flexibility


. Microcontrollers often use EEPROM or EPROM as their storage device to allow field programmability so they are flexible to use . Once the program is tested to be correct then large quantities of microcontrollers can be programmed to be used in embedded systems

. Easy to Use
. Assembly language is often used in microcontrollers and since they usually follow a RISC architecture, the instruction set is small . The development package of microcontrollers often includes an assembler, a simulator, a programmer to "burn" the chip and a demonstration board . Some packages include a high level language compiler such as a C compiler and more sophisticated libraries
20

Microcontroller, Microprocessor and Microcomputer


. A microcontrollers are is like a single chip computers that are often embedded into other systems to function as processing/controlling unit
Microcontrollers are used in a wide number of electronic systems such as:
. Engine management systems in automobiles . Keyboard of a PC . Electronic measurement instruments (such as digital multimeters, frequency synthesisers, and oscilloscopes)

. A microprocessor is a programmable digital electronic component that incorporates the functions of a central processing unit (CPU) into a single IC package
These functions are
. The ability to execute a stored set of instructions to carry out user defined tasks . The ability to be able to access external memory chips to both read and write data from and to the memory
21

Microcontroller, Microprocessor and Microcomputer


. Types of Memory can be obtained as either:
. Read Only Memory (ROM)
. This is memory that can only be read, the data being stored in the memory device during its manufacture . Once data has been written onto ROM memory, it cannot be easily removed and is designed for 'read only' use . ROM is referred to as being non-volatile as it retains its contents even when the power is turned off

. Erasable Programmable Read Only Memory (EPROM)


. This is similar to ROM type memory but the user can program it . The contents of the memory can be erased from the memory by exposing the memory chip to ultraviolet radiation for a short period of time . It can therefore be used many times over

. Electrically Erasable Programmable Read Only Memory (EEPROM)


. Similar to EPROM but has part or all of the memory contents erased by the microprocessor

22

Microcontroller, Microprocessor and Microcomputer


. Both ROM and EPROM memory are used to hold the program code of a microprocessor used in an embedded system, ie. a microprocessor used in an application where the program
code is always the same and is designed to execute every time the system is switched on

. Most development work is done using EPROM or EEPROM type memory, ROM memory being used in the final production version
(when all the program code has been fully tested)

. Random Access Memory (RAM)


. All microprocessor systems need memory that can be both read from and written to - such memory is RAM . RAM got its name because early read-write memories were sequential, and did not allow random access. RAM memory is used to store dynamic data (that will change during the operation of the program)
23

Microcontroller, Microprocessor and Microcomputer


. RAM takes the form of integrated circuits that allow the stored data to be accessed in ANY order . That is, at random and without the physical movement of the storage medium or a physical reading head . The word "random" infers that any piece of data can be returned quickly, and in a constant time, regardless of its actual physical location, in relation to the previous data storage location . The key benefit of RAM is that retrieval times are short and consistent. The disadvantages of RAM are cost, and the loss of data when power is turned off (volatile).

. A typical microprocessor system will contain both ROM (could be EPROM, EEPROM, or ROM) to store the program code, and RAM to store dynamic data
24

Input/Output (I/O)
. I/O (input/output) is the collection of interfaces that different functional devices, of any information processing system, use to communicate with each other . Every information transfer is an output from one device and an input into another . For instance, on a computer, a keyboard and mouse are considered input devices while monitors and printers are considered output devices . Typical devices for communication between computers, such as modems and network cards, operate as both input and output devices . I/O can be:
. A number of digital bits formed into a number of digital inputs or outputs called a port . These are usually eight bits wide and thus referred to as a BYTE wide port. i.e. byte wide input port, byte wide output port
25

Input/Output (I/O)
. A serial line from the microprocessor (Transmit or TX) and a serial line to the microprocessor (Receive or RX) allowing serial data in the form of a bit stream to be transmitted or received via a two wire interface . Other I/O devices such as Analogue to Digital Converters (ADC) and Digital to Analogue Converters (DAC), Timer modules, Interrupt controllers etc. (which will be discussed later in the context of microcontrollers)

. These are relatively complex sub systems that can be obtained as separate ICs . They are connected to the microprocessor in a similar manner to that of the memory devices. . Indeed, they often contain their own memory to support internal operations (i.e. registers)
26

Microcontroller Hardware
. Basically, a microcontroller is a device which integrates a number of the components of a microprocessor system onto a single chip (IC), with the following common features:
. The CPU core - ranging from simple 4-bit processors to sophisticated 32/64-bit processors . Memory (both ROM and RAM) . Some parallel digital I/O

27

Microcontroller Hardware
. Most microcontrollers will also combine other devices such as:
. A Timer module to allow the microcontroller to perform tasks for certain time periods . A serial I/O port to allow data to flow between the microcontroller and other devices such as a PC or another microcontroller . An ADC to allow the microcontroller to accept analogue input data for processing

. The CPU is the processing module of the microcontroller.


28

Microcontroller Hardware
. Microcontrollers are nowadays brimming with added functionality on this single IC and often include:
. Communications Peripherals: SPI, I2C., UART, CAN, USB, Ethernet, IrDA, LIN . Control Peripherals: capture/compare, counters, real-time clock & calendar, motor control & PWM . Integrated Display Drivers: LED, LCD . On-chip internal oscillators and Phase-Locked Loop (closed loop feedback control) . Analog Peripherals: A/D Converters, comparators, op amps, brown-out detection & reset, low voltage detection, temperature sensors, D/A Converters and voltage regulators

29

Memory in a microcontroller
The amount of memory contained within a microcontroller varies between different microcontrollers Some may not even have any integrated memory (eg. Hitachi 6503, now discontinued) However, most modern microcontrollers will have integrated Memory. The memory will be divided up into ROM and RAM, with typically more ROM than RAM

30

Microcontroller ROM
. The ROM of a microcontroller is used to store the application program

31

Microcontroller ROM
. Typically, the amount of ROM type memory will vary between around 512 bytes and 4096 bytes . Some 16 bit microcontrollers such as the Hitachi H8/3048 can have as much as 128 Kbytes of ROM type memory . ROM type memory, as has already been mentioned, is used to store the program code . ROM memory can be either ROM (as in One Time Programmable memory), EPROM, or EEPROM
32

Microcontroller RAM
. The microcontroller RAM is used to store data

33

Introduction to PIC Microcontroller


Common Microcontrollers

34

Microcontroller Manufacturers
. There are lots of microcontroller manufacturers

35

Introduction to PIC Microcontroller


PIC Microcontroller
. Range of low end 8 bit microcontrollers. . Smallest have only 8 pins, largest 40 pins. . Typical chip is an 18 pin one. . Very cheap, you can pick them up at less than 1 each. . Targeted at consumer products, burglar alarms etc.

Advantages of PIC
. It is a RISC (Reduced Instruction Set Computer) design . Only thirty seven instructions to remember . Its code is extremely efficient, allowing the PIC to run with typically less program memory than its larger competitors. . It is low cost, high clock speed
36

Harvard architecture
. Like many micros the PIC is a Harvard not a von-Neumann machine . This is simpler and faster . Separate program bus and data bus: can be different widths! . For example, PICs use:
. Data memory (RAM): a small number of 8bit registers . Program memory (ROM): 12bit, 14bit or 16bit wide (in EPROM, FLASH, or ROM)

37

Comparison Between Architectures

38

Comparison Between Architectures


. Harvard architecture is a newer concept than von-Neumann's. It rose out of the need to speed up the work of a microcontroller. . In Harvard architecture, Data Access and Address Access are separate. Thus a greater flow of data is possible through the central processing unit. . PIC16F877 uses 14 bits for instructions which allows for all instructions to be one word instructions.

Advantages of Harvard model


. An add operation of the form a:=b+c must fetch 2 operands from memory and write 1 operand to memory. In addition it is likely to have to fetch 3 instructions from memory. . With a single memory this will take 6 cycles. . With 2 memories, we can fetch the instructions in parallel with the data and do it in 3 cycles. . We have different word lengths for instructions and data . 8 bit data and perhaps 12 bit instructions.

39

Harvard vs von Neumann Block Architecture

40

Harvard vs von Neumann Block Architecture


Von Neumann Architecture . Used in: 80X86 (PCs), 8051, 68HC11, etc.) . Only one bus between CPU and memory . RAM and program memory share the same bus and the same memory, and so must have the same bit width . Bottleneck: Getting instructions interferes with accessing RAM RISC Architecture . Complex/Reduced Instruction Set Computers . A minimal set of instructions, combined, can do every operation . Usually execute in a single cycle . CPU is smaller . Other hardware can be added to the space: (overlapping register windows)
41

Harvard vs von Neumann Block Architecture


Traditionally, CPUs are .CISC.
. Complex Instruction Set Computer (CISC) . Used in: 80X86, 8051, 68HC11, etc. . Many instructions (usually > 100) . Many, many addressing modes . Usually takes more than 1 internal clock cycle to execute

PICs and most Harvard chips are .RISC.


. Reduced Instruction Set Computer (RISC) . Used in: SPARC, ALPHA, Atmel AVR, etc. . Few instructions (usually < 50) . Only a few addressing modes . Executes 1 instruction in 1 internal clock cycle

42

PIC Microcontroller
The PIC Family: Cores . PICs come with 1 of 4 CPU cores: . 12bit cores with 33 instructions: 12C50x, 16C5x . 14bit cores with 35 instructions: 12C67x,16Cxxx . 16bit cores with 58 instructions: 17C4x,17C7xx . Enhanced 16bit cores with 77 instructions: 18Cxxx The PIC Family: Packages . PICs come in a huge variety of packages: . 8 pin DIPs, SOICs: 12C50x (12bit) and 12C67x (14bit) . 18pin DIPs, SOICs: 16C5X (12bit), 16Cxxx (14bit) . 28pin DIPs, SOICs: 16C5X (12bit), 16Cxxx (14bit) . 40pin DIPs, SOICs: 16Cxxx (14bit), 17C4x (16bit) . 44 - 68pin PLCCs*: 16Cxxx (14bit), 17C4x / 17Cxxx (16bit)
43

PIC Microcontroller
The PIC Family: Speed . PICs require a clock to work. . Can use crystals, clock oscillators, or even an RC circuit. . Some PICs have a built in 4MHz RC clock . Not very accurate, but requires no external components! . Instruction speed = 1/4 clock speed
44

PIC Microcontroller
The PIC Family: Program Memory
. PIC program space is different for each chip. . Some examples are:
12C508 512 12bit instructions 16C71C 1024 (1k) 14bit instructions 16F877 8192 (8k) 14bit instructions 17C766 16384 (16k) 16bit instructions

45

The PIC Family: Program Memory


. PICs have two different types of program storage: 1. EPROM (Erasable Programmable Read Only Memory) . Needs high voltage from a programmer to program (~13V) . Needs windowed chips and UV light to erase . Note: One Time Programmable (OTP) chips are EPROM chips, but with no window! . PIC Examples: Any .C. part: 12C50x, 17C7xx, etc. 2. FLASH . Re-writable (even by chip itself) . Much faster to develop on! . Finite number of writes (~100k Writes) . PIC Examples: Any .F. part: 16F84, 16F87x, 18Fxxx (future)
46

The PIC Family: Data Memory


. PICs use general purpose .file registers. for RAM (each register is 8bits for all PICs) . Some examples are:
12C508 25 Bytes RAM 16C71C 36 Bytes RAM 16F877 368 Bytes (plus 256 Bytes of nonvolatile EEPROM) 17C766 902 Bytes RAM

. Dont forget, programs are stored in program space (not in data space), so low RAM values are OK.

47

The PIC Family: Control Registers


. PICs use a series of special function registers for controlling peripherals and PIC behaviors. . Some examples are:
STATUS Bank select bits, ALU bits (zero, borrow, carry) INTCON Interrupt control: interrupt enables, flags, etc. TRIS Tristate control for digital I/O: which pins are floating TXREG UART transmit register: the next byte to transmit

48

The PIC Family: Peripherals


. Different PICs have different on-board peripherals . Some common peripherals are:
. Tri-state (floatable) digital I/O pins . Analog to Digital Converters (ADC) (8, 10 and 12bit, 50ksps) . Serial communications: UART (RS-232C), SPI, I2C, CAN . Pulse Width Modulation (PWM) (10bit) . Timers and counters (8 and 16bit) . Watchdog timers, Brown out detect, LCD drivers

49

PIC Peripherals: Ports (Digital I/O)


. All PICs have digital I/O pins, called Ports.
. the 8pin 12C508 has 1 Port with 4 digital I/O pins . the 68pin 17C766 has 9 Ports with 66 digital I/O pins

. Ports have 2 control registers


. TRISx sets whether each pin is an input or output . PORTx sets their output bit levels

. Most pins have 25mA source/sink (directly drives LEDs) . WARNING: Other peripherals SHARE pins!
50

PIC Peripherals: ADCs


. Only available in 14bit and 16bit cores . Fs (sample rate) < 54KHz . Most 8bits, newer PICs have 10 or 12bits . All are +/- 1LSB and are monotonic . Theoretically higher accuracy when PIC is in sleep mode (less digital noise) . Can generate an interrupt on ADC conversion done . Multiplexed 3 (12C671) - 12 (17C7xxx) channel input . Must wait Tacq to charge up sampling capacitor

51

PIC Peripherals: USART: UART


. Serial Communications Peripheral: Universal Synchronous/ Asynchronous Receiver/Transmitter . Only available in 14bit and 16bit cores . Interrupt on TX buffer empty and RX buffer full . Asynchronous communication: UART (RS-232C serial) . Can do 300bps - 115kbps . 8 or 9 bits, parity, start and stop bits, etc. . Outputs 5V so you need a RS232 level converter (e.g., MAX232)

52

PIC Peripherals: USART: USRT


. Synchronous communication: i.e., with clock signal . SPI = Serial Peripheral Interface . 3 wire: Data in, Data out, Clock . Master/Slave (can have multiple masters) . Very high speed (1.6Mbps) . Full speed simultaneous send and receive (Full duplex) . I2C = Inter IC . 2 wire: Data and Clock . Master/Slave (Single master only; multiple masters clumsy) . Lots of cheap I2C chips available; typically < 100kbps (For example, 8pin EEPROM chips, ADC, DACs, etc.)
53

PIC Peripherals: Timers


. Available in all PICs . 14+bit cores may generate interrupts on timer overflow . Some 8bits, some 16bits, some have prescalers . Can use external pins as clock in/clock out (ie, for counting events or using a different Fosc) . Warning: some peripherals share Timer resources
54

PIC Peripherals: CCP Modules


. Capture/Compare/PWM (CCP) . 10bit PWM width within 8bit PWM period (frequency) . Enhanced 16bit cores have better bit widths . Frequency/Duty cycle resolution tradeoff . 19.5KHz has 10bit resolution . 40KHz has 8bit resolution . 1MHz has 1bit resolution (makes a 1MHz clock!) . Can use PWM to do DAC - See AN655 . Capture counts external pin changes . Compare will interrupt on when the timer equals the value in a compare register
55

PIC Peripherals: Misc.


. Sleep Mode: PIC shuts down until external interrupt (or internal timer) wakes it up. . Interrupt on pin change: Generate an interrupt when a digital input pin changes state (for example, interrupt on keypress). . Watchdog timer: Resets chip if not cleared before overflow . Brown out detect: Resets chip at a known voltage level . LCD drivers: Drives simple LCD displays . Future: CAN bus, 12bit ADC, better analog functions . VIRTUAL PERIPHERALS: . Peripherals programmed in software. UARTS, timers, and more can be done in software (but it takes most of the resources of the machine) 56

PIC Microcontrollers
INSTRUCTION SET PIC16F887

Harvard Architecture
von-Neumann Architecture

Encoding of instruction
Each instruction is of 14-bit long. These 14bits contain both op-code and the operand. bcf f, b Clear 'b' bit of register 'f Encoding: The instruction is executed in one instruction cycle, i.e., 4 clock cycles.

Encoding of instruction
goto K Encoding: Go to label 'k' instruction

Since this instruction requires modification of program Counter, it takes two instruction cycles for execution

GENERAL FORMAT FOR INSTRUCTIONS

OPCODE FIELD DESCRIPTIONS

Instruction Set PIC16F887

Instruction Set PIC16F887

1)ADDLW - Add literal and W


Syntax: [label] ADDLW

Description: The content of the register W is added to the 8-bit literal k. The result is stored in the W register. Operation: (W) + k -> W Operand: 0 k 255 Status affected: C, DC, Z Number of cycles: 1 movlw 0x0F addlw 0x03 Kt qu: W = 0x12, Z=0 DC=1 C=0

2)ADDWF - Add W and f


Syntax: [label] ADDWF f,

Description: Add the contents of the W and f registers. If d = w or d = 0 the result is stored in the W register. If d = f or d = 1 the result is stored in register f. Operation: (W) + (f) -> d cblock 0x20 Operand: 0 f 127, d [0,1] REG Endc Status affected: C, DC, Z Number of cycles: 1 movlw 0x20
movwf REG movlw 0x17 ADDWF REG,w

Kt qu: W = 0x37,

Z=0

DC=0

C=0

2) ADDWF - Add W and f


Kt qu:

W = 0x17, STAUS = 0x18 FSR = 0xC2 REG = 0x37

cblock 0xC2 REG Endc movlw movwf movlw movwf movlw ADDWF

0xC2 FSR 0x20 INDF 0x17 INDF, f

Indirect addressing

Indirect addressing
A simple program to clear RAM location 20h2Fh using indirect addressing

The INDF register is not a physical register. Reading INDF itself indirectly will produce 00h. Writing to the INDF register indirectly results in a no operation

3) ANDLW - AND literal with W


Syntax: [label] ANDLW

Description: The content of the register W is ANDed with the 8-bit literal k. It means that the result will contain one (1) only if both corresponding bits of operand are ones (1). The result is stored in the W register. Operation: (W) AND k -> W Operand: 0 k 255 Status affected: Z movlw 0xAA Number of cycles: 1
ANDLW 0x55

Kt qu: W = 0x00,

Z=1

4) ANDWF - AND W with f


Syntax: [label] ANDWF f,d Description: AND the W register with register f. If d = w or d = 0, the result is stored in the W register. If d = f or d = 1, the result is stored in register f. cblock 0x20 Operation: (W) AND (f) -> d REG Operand: 0 f 127, d[0,1] Endc Status affected: Z movlw 0xC2 Number of cycles: 1 movwf REG
movlw 0x17 andwf REG, f

Kt qu: W = 0x17, Kt qu: W = 0x02,

REG=0x02 REG=0xC2

andwf REG, w

Z=0 Z=0

5) BCF - Bit Clear f


Syntax: [label] BCF

f, b

Description: Bit b of register f is cleared. Operation: (0) -> f(b) cblock 0xC2 Operand: 0 f 127, 0 b 7 REG Status affected: endc banksel REG Number of cycles: 1
movlw movwf bcf

0xC7 REG REG,7

Kt qu: REG = 0x47

5) BCF - Bit Clear f


Kt qu:
cblock Endc 0xC2 REG

W = 0x17, FSR = 0xC2 STAUS = 0x18 REG = 0x27

movlw movwf movlw movwf movlw bcf

0xC2 FSR 0x2F INDF 0x17 INDF,3

6) BSF - Bit set f


Syntax: [label] BSF

f,b
cblock Endc movlw movwf movlw movwf movlw bsf 0xC2 FSR 0x20 INDF 0x17 INDF, 3 0xC2 REG

Description: Bit b of register f is set. Operation: 1 -> f (b) Operand: 0 f 127, 0 b 7 Status affected: Number of cycles: 1 Kt qu:

W = 0x17, FSR = 0xC2 STAUS = 0x18 REG = 0x28

7) BTFSC - Bit test f, Skip if Clear


Syntax: [label] BTFSC

f, b

Description: If bit b of register f is 0, the next instruction is discarded and a NOP is executed instead, making this a two-cycle instruction. Operation: Discard the next instruction if f(b) = 0 cblock 0x20 Operand: 0 f 127, 0 b 7 Kt qu: REG Status affected: Endc movlw 0x01 Number of cycles: 1 or 2 depending on bit b W = 0x18 movwf REG
movlw movwf movlw btfsc addlw addlw 0x00 REG 0x17 REG,0 0x01 0x00

Kt qu:

movlw btfsc addlw addlw

0x17 REG,0 0x01 0x00

W = 0x17

8) BTFSS - Bit test f, Skip if Set


Syntax: [label] BTFSS

f, b

Description: If bit b of register f is 1, the next instruction is discarded and a NOP is executed instead, making this a two-cycle instruction. Operation: Discard the next instruction if f(b) = 1 cblock 0x20 Operand: 0 f 127, 0 b 7 Kt qu: REG Status affected: Endc movlw 0x01 Number of cycles: 1 or 2 depending on bit b W = 0x17 movwf REG
movlw movwf movlw btfss addlw addlw 0x00 REG 0x17 REG,0 0x01 0x00

Kt qu:

movlw btfss addlw addlw

0x17 REG,0 0x01 0x00

W = 0x18

9) CALL - Calls Subroutine


Syntax:[ label ] CALL k Operands:0 k 2047 Opera on:(PC)+ 1 TOS, k PC<10:0>, (PCLATH<4:3>) PC<12:11> Status Affected: None Description:Call Subroutine. First, return address (PC + 1) is pushed onto the stack. The eleven-bit immediate address is loaded into PC bits <10:0>. The upper bits of the PC are loaded from PCLATH. CALL is a two-cycle instruction.

PCL and PCLATH Registers


The Program Counter (PC) is 13 bits wide. The low byte comes from the PCL register, which is a readable and writable register. The high byte (PC<12:8>) is not directly readable or writable and comes from PCLATH.

PCLATH Program Counter LATch High The size of the program memory of the PIC16F887 is 8K (13 bits address)

PCL and PCLATH Registers


On subroutine call or jump execution (CALL and GOTO), the microcontroller is able to provide only 11-bit addressing. For this reason, similar to RAM which is divided in banks, ROM is divided in four pages in size of 2K each

Call Subroutine
CALL k (2) 10 0kkk kkkk kkkk

Go to address
GOTO k (2) 10 1kkk kkkk kkkk

PCL and PCLATH Registers


Such instructions are executed within these pages without any problems. Simply, since the processor is provided with 11bit address from the program, it is able to address any location within 2KB.
org 0x0000 call wait loop goto loop wait nop nop return

0000 0001 0002 0003 0004

2002 2801 0000 0000 0008

CALL 0x2 GOTO 0x1 NOP NOP RETURN

13: 14: 16: 17: 18:

call wait loop goto loop wait nop nop return

PCL and PCLATH Registers


However, if a subroutine or jump address are not within the same page as the location from where the jump is, two missing- higher bits should be provided by writing to the PCLATH register.
org 0x0000 movlw 0x08 movwf PCLATH call wait loop goto loop org 0x0800 wait nop nop return

10) CLRF - Clear f


Syntax: [label] CLRF

Description: The content of register f is cleared and the Z flag of the STATUS register is set. Operation: 0 -> f banksel TRISB Operand: 0 f 127 movlw 0xff Status affected: Z movwf TRISB Number of cycles: 1 clrf TRISB
Kt qu: TRISB = 0x00, Z=1

11) CLRW - Clear W


Syntax: [label] CLRW Description: Register W is cleared and the Z flag of the STATUS register is set. Operation: 0 -> W Operand: movlw 0xff Status affected: Z clrw Number of cycles: 1

Kt qu: W = 0x00,

Z=1

12) CLRWDT - Clear Watchdog Timer

13) COMF - Complement f


Syntax: [label] COMF

f, d

Description: The content of register f is complemented (logic zeros (0) are replaced by ones (1) and vice versa). If d = w or d = 0 the result is stored in W. If d = f or d = 1 the result is stored in register f. Operation: (f) -> d Operand: 0 f 127, d[0,1] movlw 0x13 Status affected: Z movwf REG Number of cycles: 1
COMF REG,w

W = 0xEC, Z = 0

14) DECF - Decrement f


Syntax: [label] DECF

f, d

Description: Decrement register f by one. If d = w or d = 0, the result is stored in the W register. If d = f or d = 1, the result is stored in register f. Operation: (f) - 1 -> d Operand: 0 f 127, d[0,1] movlw 0x01 movwf REG Status affected: Z DECF REG,f Number of cycles: 1

REG = 0x00, Z = 1

15) DECFSZ - Decrement f, Skip if 0


Syntax: [label] DECFSZ

f, d

Description: Decrement register f by one. If d = w or d = 0, the result is stored in the W register. If d = f or d = 1, the result is stored in register f. If the result is 0, then a NOP is executed instead, making this a two-cycle instruction. .... Operation: (f) - 1 -> d MOVLW .10 Operand: 0 f 127, d[0,1] MOVWF CNT ;10 -> CNT Status affected: Loop ...... ...... ;Instruction block Number of cycles: ...... 1 or 2 depending on the result. DECFSZ CNT,f ; decrement REG by one GOTO Loop ; Skip this line if CNT= 0 LAB_03 ....... ; Jump here if CNT = 0

16) GOTO - Unconditional Branch

17) INCF - Increment f


Syntax: [label] INCF

f, d

Description: Increment register f by one. If d = w or d = 0, the result is stored in register W. If d = f or d = 1, the result is stored in register f. Operation: (f) + 1 -> d Operand: 0 f 127, d[0,1] movlw 0x10 Status affected: Z movwf REG Number of cycles: 1 incf REG,w

REG = 0x10 W = 0x11, Z=0

18) INCFSZ - Increment f, Skip if 0


Syntax: [label] INCFSZ

f, d

Description: Register f is incremented by one. If d = w or d = 0, the result is stored in register W. If d = f or d = 1, the result is stored in register f. If the result is 0, then a NOP is executed instead, making this a two-cycle instruction. Operation: (f) + 1 -> d Operand: 0 f 127, d[0,1] Status affected: Number of cycles: 1 or 2 depending on the result.

19) IORLW - Inclusive OR literal with W


Syntax: [label]

IORLW k

Description: The content of the W register is ORed with the 8-bit literal k. The result is stored in register W. Operation: (W) OR (k) -> W Operand: 0 k 255 movlw 0x9A Status affected: Z iorlw 0x35 Number of cycles: 1

W = 0xBF, Z=0

20) IORWF - Inclusive OR W with f


Syntax: [label] IORWF

f, d

Description: The content of register f is ORed with the content of W register. If d = w or d = 0, the result is stored in the W register. If d = f or d = 1, the result is stored in register f. Operation: (W) OR (f) -> d movlw 0x13 movwf REG Operand: 0 f 127, d -> [0,1] movlw 0x91 Status affected: Z iorwf REG,w Number of cycles: 1

REG = 0x13 W = 0x93, Z=0

21) MOVF - Move f


Syntax: [label] MOVF

f, d

Description: The content of register f is moved to a destination determined by the operand d. If d = w or d = 0, the content is moved to register W. If d = f or d = 1, the content remains in register f. Option d = 1 is used to test the content of register f because this instruction affects the Z flag of the STATUS register. movlw 0xC2 Operation: (f) -> d movwf FSR Operand: 0 f 127, d -> [0,1] clrw movf FSR,w Status affected: Z Number of cycles: 1
Before instruction execution: FSR=0xC2 W=0x00 After instruction: W=0xC2 Z = 0

22) MOVLW - Move literal to W


Syntax: [label] MOVLW

Description: 8-bit literal k is moved to register W. Operation: k -> (W) Operand: 0 k 255 Status affected: Number of cycles: 1 Const equ 0x40
MOVLW Const

W=0x40

23) MOVWF - Move W to f


Syntax: [label] MOVWF

Description: The content of register W is moved to register f. Operation: (W) -> f Operand: 0 f 127 banksel OPTION_REG Status affected: movlw 0x20 Number of cycles: 1 movwf OPTION_REG OPTION_REG=0x20

24) NOP - No Operation


Syntax: [label] NOP Description: No operation. Operation: NOP ; 1us delay (oscillator 4MHz) Operand: Before instruction execution: PC = x Status affected: After instruction: PC = x + 1 Number of cycles: 1

25) RETFIE - Return from Interrupt

26) RETLW - Return with literal in W


Syntax: [label] RETLW

Description: 8-bit literal k is loaded into register W. The value from the top of stack is loaded to the program counter. Operation: (k) -> W; top of stack (TOP) -> PC Operand: Status affected: Number of cycles: 2

27) RETURN - Return from Subroutine


Syntax: [label] RETURN Description: Return from subroutine. The value from the top of stack is loaded to the program counter. This is a two-cycle instruction. Operation: TOS -> program counter PC. Operand: Status affected: Number of cycles: 2

28) RLF - Rotate Left f through Carry


Syntax: [label] RLF

f, d

Description: The content of register f is rotated one bit to the left through the Carry flag. If d = w or d = 0, the result is stored in register W. If d = f or d = 1, the result is stored in register f. Operation: (f(n)) -> d(n+1), f(7) -> C, C -> d(0); Operand: 0 f 127, d[0,1] Status affected: C Number of cycles: 1
banksel STATUS bcf STATUS, 0 movlw 0xE6 movwf REG rlf REG,f banksel STATUS bcf STATUS, 0 movlw 0xE6 movwf REG rlf REG,w

REG = 0xCC

C=1

W = 0xCC

C=1

29) RRF - Rotate Right f through Carry


Syntax: [label] RRF

f, d

Description: The content of register f is rotated one bit right through the Carry flag. If d = w or d = 0, the result is stored in register W. If d = f or d = 1, the result is stored in register f. Operation: (f(n)) -> d(n-1), f(0) -> C, C -> d(7); Operand: 0 f 127, d -> [0,1] Status affected: C Number of cycles: 1 banksel STATUS
banksel STATUS bcf STATUS, 0 movlw 0xE6 movwf REG rrf REG, f bcf STATUS, 0 movlw 0xE6 movwf REG rrf REG,w

W = 0x73 C=0

C=0

REG = 0x73

30) SLEEP - Enter Sleep mode

31) SUBLW - Subtract W from literal


movlw 0x01 sublw 0x03

W = 0x02, Z = 0, DC = 1, C = 1
movlw 0x03 sublw 0x03

W = 0x00, Z = 1, DC = 1, C = 1
movlw 0x04 sublw 0x03

W = 0xFF, Z = 0, DC = 0, C = 0

32) SUBWF - Subtract W from f


movlw 0x03 movwf REG movlw 0x02 subwf REG,f

REG = 0x01, Z = 0, DC = 1, C = 1
movlw 0x02 movwf REG movlw 0x02 subwf REG,f

REG = 0x00, Z = 1, DC = 1, C = 1
movlw 0x01 movwf REG movlw 0x02 subwf REG,f

REG = 0xFF, Z = 0, DC = 0, C = 0

33) SWAPF - Swap Nibbles in f


movlw 0xF3 movwf REG swapf REG, w

REG = 0xF3, W = 0x3F


movlw 0xF3 movwf REG swapf REG, f

REG = 0x3F, W = 0xF3

34)XORLW - Exclusive OR literal with W


movlw 0xB5 XORLW 0xAF

W = 0x1A, Z = 0
Const equ 0x37 movlw 0xAF XORLW Const

W = 0x98, Z = 0

35) XORWF - Exclusive OR W with f


movlw 0xAF movwf REG movlw 0xB5 XORWF REG,f

REG = 0x1A, Z = 0
movlw 0xAF movwf REG movlw 0xB5 XORWF REG,w

W = 0x1A, Z = 0

Вам также может понравиться