Top Banner

of 45

16F84 Edited

Apr 07, 2018

Download

Documents

Shameem Haneefa
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/6/2019 16F84 Edited

    1/45

    EMBEDDED SYSTEM

    Being an electronic engineer, you might have seen PC desktop's motherboard; it's an

    embedded system. It has microprocessor (Pentium or Athlon), memory (DRAM DIMM module

    and onboard SRAM), I/O interface (keyboard, mouse etc.), Peripheral communication interface

    (PCI, USB port, etc). This PC system's architecture is designed for application such as net

    surfing, excel, word, powerpoint. Suppose you want to use the same computer to monitor the

    engine of your bike or car. Can you think of using big PC for that purpose? It's so impractical.

    The input and output are totally different, here comes customizing your own

    microprocessor/microcontroller, memory, display, i/o and peripheral interface and also theoperating system. This field of designing application specific computer systems is called

    embedded systems development. If the response of this computer system need to be real time and

    highly reliable then it's called Real Time Embedded System. The real time means, for example in

    a control system where the speed of motor need to be varied at the moment at which some

    parameter deflects from it's original value, then it's real time; with no waiting or hanging.

    To define in a sentence, Embedded System is a special purpose computer system/board,

    which encapsulates all the devices such as processor, memory, interface and control in single

    package or board to perform only a specific application task.

    Every home has several examples of embedded computers. Any appliance that has a

    digital clock, for instance, has a small embedded microcontroller that performs no other task than

    to display the clock. Modern cars have embedded computers onboard that control such things as

    ignition timing and anti-lock brakes using input from a number of different sensors.

    Embedded computers rarely have a generic interface, however. Even if embeddedsystems have a keypad and an LCD display, they are rarely capable of using many different

    types of input or output. An example of an embedded system with I/O capability is a security

    alarm with an LCD status display, and a keypad for entering a password.

  • 8/6/2019 16F84 Edited

    2/45

    In general, an Embedded System:

    Is a system built to perform its duty, completely or partially independent of human

    intervention.

    Is specially designed to perform a few tasks in the most efficient way.

    Interacts with physical elements in our environment, viz. controlling and driving a motor,

    sensing temperature, etc.

    Figure 1:Sample block diagram of a typical embedded system.

    http://www.eeherald.com/images/ESMOD1BIG.gif
  • 8/6/2019 16F84 Edited

    3/45

    Figure 2: PowerPC based embedded board.

    Most embedded systems are time critical applications, meaning that the embedded system

    is working in an environment where timing is very important: the results of an operation are only

    relevant if they take place in a specific time frame. An autopilot in an aircraft is a time critical

    embedded system. If the autopilot detects that the plane for some reason is going into a stall then

    it should take steps to correct this within milliseconds or there would be catastrophic results.

    What are Embedded Systems used for?

    The uses of embedded systems are virtually limitless, because every day new products are

    introduced to the market that utilizes embedded computers in novel ways. In recent years, hardware such

    as microprocessors, microcontrollers, and FPGA chips have become much cheaper. So when

    implementing a new form of control, it's wiser to just buy the generic chip and write your own custom

    software for it. Producing a custom-made chip to handle a particular task or set of tasks costs far more

    time and money. Many embedded computers even come with extensive libraries, so that "writing your

    own software" becomes a very trivial task indeed.

  • 8/6/2019 16F84 Edited

    4/45

    From an implementation viewpoint, there is a major difference between a computer and

    an embedded system. Embedded systems are often required to provideReal-Time response.

    A Real-Time system is defined as a system whose correctness depends on the timeliness of its

    response. Examples of such systems are flight control systems of an aircraft, sensor systems in

    nuclear reactors and power plants. For these systems, delay in response is a fatal error. A more

    relaxed version ofReal-Time Systems, is the one where timely response with small delays is

    acceptable. Example of such a system would be the Scheduling Display System on the railway

    platforms. In technical terminology,Real-Time Systems can be classified as:

    a.Hard Real-Time Systems - systems with severe constraints on the timeliness of the response.

    b. Soft Real-Time Systems - systems which tolerate small variations in response times.

    c. Hybrid Real-Time Systems - systems which exhibit both hard and soft constraints on its

    performance.

    Why Study Embedded Systems?

    Embedded systems are playing important roles in our lives every day, even though they might

    not necessarily be visible. Some of the embedded systems we use every day control the menu

    system on television, the timer in a microwave oven, a cell phone, an MP3 player or any other

    device with some amount of intelligence built-in. In fact, recent poll data shows that embedded

    computer systems currently outnumber humans in the USA. Embedded systems is a rapidly

    growing industry where growth opportunities are numerous.

    Processors embedded in to a system

    Microprocessor, Microcontroller and System on Chip

    Microprocessor:

    Microprocessor is the Central Processing Unit (CPU) of embedded system. It does arithmetic and

    logic operations of the digital binary data.Very old embedded systems circuit/board was

  • 8/6/2019 16F84 Edited

    5/45

    generally made up of separate microprocessor (8085), I/P interface, O/P interface, memory,

    clock and timing devices, power supply devices, and analog/linear devices. (little more to be

    added)

    Microcontroller:In the early days of embedded systems, engineers have built embedded systems with separate set

    of devices connected on a printed circuit board. The complexity involved in manufacturing and

    re-engineering was very high with many Integrated Circuits and other components on-board.

    Also the advance in technology has enabled processor manufacturers to add one device after one

    into single IC. It started with adding I/O interface and memory, now we see lot more functions

    inside the processor chip. These microprocessors with all the additional support built-in are

    called microcontrollers.

    To define, Microcontroller is an Integrated Circuit device with CPU, memory, I/O interface and

    any other logic and analog function on a single chip. (have to include a circuit and different types

    of micro controllers used)

    Microcontrollers versus Microprocessors

    Microcontroller differs from a microprocessor in many ways. First and the most important is its

    functionality. 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. In short that means that

    microprocessor is the very heart of the computer. 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.

    System On Chip (SOC)

    Even though MCU holds most of the functions, it still lacks in few special analog functions and

    application specific functions. The idea of putting entire system (all the semiconductor IC

    functions) on a single chip is called System on chip. On a printed circuit board, you see a single

    IC accompanied with few discrete and passive components.

  • 8/6/2019 16F84 Edited

    6/45

    If we look at the recent microcontrollers released in the market, most of them are very close to

    System On Chip. The concept of SOC is well ticking in the market.

    SOC is a common sense solution, that means, why we have to go for a complex board when we

    can put everything into a single IC. SOC saves board space, ease manufacturing, and score

    higher in reliability over non-SOC solutions. It's drawback is, the manufacturer profits from this

    product only if it's used in millions. (need a block diagram)

    Embedded hardware and software devices

    Programming: machine language, assembly language, and C programming

    Assembly and machine language

    The microcontroller is the one, which decides what need to be done, what need not be

    done, and how to be done. Basic rule we need to keep in mind while "instructing" the

    microcontroller is - microcontroller is like a very intelligent child. The child (controller) would

    do exactly what was told it to do - nothing more nothing less. If the instruction is ambiguous then

    the behavior of the microcontroller would go haywire.

    Example: In a bread toaster, the sequence of operations is,

    a) Turn on the heater

    b) Check whether the bread is properly roasted or not (by checking the temperature or set time)

    c) If bread is not yet completely toasted properly again go to step (b)

    d) Stop the heater as the bread is toasted properly.

    Now how do you tell this sequence to a microcontroller inside a bread toaster? You should tell it

    (microcontroller) in a way it understands. It is like speaking to a person who knows somelanguage, which you can't speak. The instant option left to you to speak to such person is to catch

    hold of a translator, who knows both the languages and translate/convert your language to other's

    language.

  • 8/6/2019 16F84 Edited

    7/45

    The language what all microcontrollers understand is called machine language. Here is just a few

    lines of machine language for Freescale's 6812 microcontroller.

    CF0C00180B8000024D008018030FA008009600847FB1F033260EFE080009

    7E080026EE4C008020E918030FA008004D008020DE23F000

    Does this jumble of hexadecimal codes discourage you? Obviously you should be! Any way

    don't get disheartened by this magic series of numbers. But make it very clear this (machine

    language) is the native language of all the microcontrollers and you should "instruct" them only

    in their language. Also this machine language is different for each microcontroller families

    (8051, PIC, ARM etc.).

    In the very early stages itself computer scientists/ chip designers noted this problem

    instantaneously and came out with a solution. For each of the operation that microcontroller can

    do (execute) they assigned an "English like" word so that programmer/ designer can easily

    instruct the microcontroller. This is called assembly language.

    Here below is table of assembly languages instructions for popular PIC16xx microcontroller. In

    total it has only 35 instructions.

  • 8/6/2019 16F84 Edited

    8/45

  • 8/6/2019 16F84 Edited

    9/45

    Development/Classification of microcontrollers (Invisible)

    Microcontrollers have gone through a silent evolution (invisible). The evolution can be rightly

    termed as silent as the impact or application of a microcontroller is not well known to a common

    user, although microcontroller technology has undergone significant change since early 1970's.

    Development of some popular microcontrollers is given as follows.

    Intel 4004 4 bit (2300 PMOS trans, 108 kHz) 1971

    Intel 8048 8 bit 1976

    Intel 8031 8 bit (ROM-less) .

    Intel 8051 8 bit (Mask ROM) 1980

    Microchip PIC16C64 8 bit 1985

    Motorola 68HC11 8 bit (on chip ADC) .

    Intel 80C196 16 bit 1982

    Atmel AT89C51 8 bit (Flash memory) .

    Microchip PIC 16F877 8 bit (Flash memory + ADC) .

    We use more number of microcontrollers compared to microprocessors. Microprocessors are

    primarily used for computational purpose, whereas microcontrollers find wide application in

    devices needing real time processing / control. Applications of microcontrollers are numerous.

    Starting from domestic applications such as in washing machines, TVs, airconditioners,

    microcontrollers are used in automobiles, process control industries, cell phones, electrical

    drives, robotics and in space applications.

    Microcontroller Chips

    Broad Classification of different microcontroller chips could be as follows:

    Embedded (Self -Contained) 8 - bit Microcontroller

    16 to 32 Microcontrollers

  • 8/6/2019 16F84 Edited

    10/45

    Digital Signal Processors

    Features of Modern Microcontrollers

    Built-in Monitor Program Built-in Program Memory

    Interrupts

    Analog I/O

    Serial I/O

    Facility to Interface External Memory

    Timers

    Internal Structure of a Microcontroller

    Fig. 2.1 Internal Structure of a Microcontroller

  • 8/6/2019 16F84 Edited

    11/45

    BASIC 8051 ARCHITECTURE

    8051 employs Harvard architecture. It has some peripherals such as 32 bit digital I/O, Timers

    and Serial I/O. The basic architecture of 8051 is given in figure.

    Fig 5.1 : Basic 8051 Architecture (can add another architectural design)

  • 8/6/2019 16F84 Edited

    12/45

    Pin details of 8051

    ALE/PROG: Address Latch Enable output pulse for latching the low byte of the address during

    accesses to external memory. ALE is emitted at a constant rate of 1/6 of the oscillator frequency,for external timing or clocking purposes, even when there are no accesses to external memory.

    (However, one ALE pulse is skipped during each access to external Data Memory.) This pin is

    also the program pulse input (PROG) during EPROM programming.

    PSEN: Program Store Enable is the read strobe to external Program Memory. When the device

    is executing out of external Program Memory, PSEN is activated twice each machine cycle

    (except that two PSEN activations are skipped during accesses to external Data Memory). PSEN

    is not activated when the device is executing out of internal Program Memory.

    EA/VPP: When EA is held high the CPU executes out of internal Program Memory (unless the

    Program Counter exceeds 0FFFH in the 80C51). Holding EA low forces the CPU to execute out

    of external memory regardless of the Program Counter value. In the 80C31, EA must be

  • 8/6/2019 16F84 Edited

    13/45

    externally wired low. In the EPROM devices, this pin also receives the programming supply

    voltage (VPP) during EPROM programming.

    XTAL1: Input to the inverting oscillator amplifier.

    XTAL2: Output from the inverting oscillator amplifier.

    Port 0: Port 0 is an 8-bit open drain bidirectional port.

    Port 1: Port 1 is an 8-bit bidirectional I/O port with internal pullups.

    Port 2: Port 2 is an 8-bit bidirectional I/O port with internal pullups.

    Port 3: Port 3 is an 8-bit bidirectional I/O port with internal pullups.

    Port Pin Alternate Function

    P3.0 RxD (serial input port)

    P3.1 TxD (serial output port)

    P3.2 INT0 (external interrupt 0)

    P3.3 INT1 (external interrupt 1)

    P3.4 T0 (timer 0 external input)

    P3.5 T1 (timer 1 external input)

    P3.6 WR (external data memory write strobe)

    P3.7 RD (external data memory read strobe)

    VCC: Supply voltage

    VSS: Circuit ground potential

    Various features of 8051 microcontroller are given as follows.

    8-bit CPU

    16-bit Program Counter

    8-bit Processor Status Word (PSW)

    8-bit Stack Pointer

    Internal RAM of 128bytes

    Special Function Registers (SFRs) of 128 bytes

    32 I/O pins arranged as four 8-bit ports (P0 - P3)

  • 8/6/2019 16F84 Edited

    14/45

    Two 16-bit timer/counters : T0 and T1

    Two external and three internal vectored interrupts

    One full duplex serial I/O

    8051 Instructions

    8051 has about 111 instructions. These can be grouped into the following categories

    1. Arithmetic Instructions

    2. Logical Instructions

    3. Data Transfer instructions

    4. Boolean Variable Instructions

    5. Program Branching Instructions

  • 8/6/2019 16F84 Edited

    15/45

    PIC MICROCONTROLLERS

    PIC stands for Peripheral Interface Controller given by Microchip Technology to identify its

    single-chip microcontrollers. These devices have been very successful in 8-bit microcontrollers.

    The main reason is that Microchip Technology has continuously upgraded the device

    architecture and added needed peripherals to the microcontroller to suit customers' requirements.

    The development tools such as assembler and simulator are freely available on the internet at

    www.microchip.com .

    The architectures of various PIC microcontrollers can be divided as follows.

    Low - end PIC Architectures :

    Microchip PIC microcontrollers are available in various types. When PIC microcontroller was

    first available from General Instruments in early 1980's, the microcontroller consisted of a

    simple processor executing 12-bit wide instructions with basic I/O functions. These devices are

    known as low-end architectures. They have limited program memory and are meant for

    applications requiring simple interface functions and small program & data memories. Some of

    the low-end device numbers are

    12C5XX

    16C5X

    16C505

    Mid range PIC Architectures

    Mid range PIC architectures are built by upgrading low-end architectures with more number of

    peripherals, more number of registers and more data/program memory. Some of the mid-range

    devices are

    16C6X

    16C7X

    16F87X

    http://www.microchip.com/http://www.microchip.com/http://www.microchip.com/
  • 8/6/2019 16F84 Edited

    16/45

    CPU Architecture: The CPU uses Harvard architecture with separate Program and Variable

    (data) memory interface. This facilitates instruction fetch and the operation on data/accessing of

    variables simultaneously.

    Fig 16.1 CPU Architecture of PIC microcontroller

  • 8/6/2019 16F84 Edited

    17/45

    PIC16F84:

    The PIC16F84A belongs to the mid-range family of the PICmicro microcontroller devices. A

    block diagram of the device is shown in Figure 1-1. The program memory contains 1K words,

    which translates to 1024 instructions, since each 14-bit program memory word is the same width

    as each device instruction. The data memory (RAM) contains 68 bytes. Data EEPROM is 64

    bytes. There are also 13 I/O pins that are user-configured on a pin-to-pin basis. Some pins are

    multiplexed with other device functions. These functions include:

    External interrupt

    Change on PORTB interrupt

    Timer0 clock input

    The pin diagram and pin descriptions are given below.

  • 8/6/2019 16F84 Edited

    18/45

  • 8/6/2019 16F84 Edited

    19/45

    The figure shows the block diagram of PIC 16F84.

    A number of important new additions have appeared. The addition of an EEPROM memory

    gives the valuable capability of being able to store data values even when the chip is powered

    down. There are now two digital input/output ports. These are Port A, with five pins, and Port B,

    with eight. Importantly, there is the addition of an interrupt capability. This can be seen

    externally on pin 6, where bit 0 of Port B is shared with the external interrupt input. We will also

    see that there are three further internal interrupt sources, generated by the peripherals. Overall,

    we have a microcontroller that, while only modestly more complex than the 12F508, has proved

    incredibly diverse and useful in small applications.

  • 8/6/2019 16F84 Edited

    20/45

    The Program Development Process

    The process of writing in assembler needs to be placed in the broader context of project

    development. The possible stages in the development process for the program of a simple

    embedded system project are shown in Fig. 4.3. The programmer writes the program, called the

    source code, in Assembler language.

    This is then assembled by the cross-assembler running on the host computer. If the programmer

    has access to a simulator then he/she may choose to test the program by simulation. This is likely

    to lead to program errors being discovered, which will require alteration to the original source

    code. When satisfied with the program, the developer will then download it to the program

    memory of the microcontroller itself, using either a stand-alone programmer linked to the host

    computer or a programming facility designed into the embedded system itself. He/ she will then

    test the program running in the actual hardware. Again, this may lead to changes being required

    in the source code.Clearly, to develop even a simple project, a selection of different software tools is beneficial.

    These are usually bundled together into what is called an Integrated Development Environment

    (IDE).

  • 8/6/2019 16F84 Edited

    21/45

    4.2 The PIC 16 Series Instruction Set, with a Little More on the ALU

    4.2.1 More on the PIC 16 Series ALU

    Before looking at the 16 Series instruction set, it is worth taking a more detailed look at the ALU

    (Fig. 4.4). Understanding this will aid in understanding the instruction set. Looking at this, we

    see that the ALU can operate on data from two sources. One is the W (or Working) register. The

    other is eithera literalvalue ora value from a data memory (whose memory locations Microchip

    calls register files). A literal value is a byte of data associated with a particular instruction that

    the programmer embeds in the program.

    Thus, we can expect to see some instructions that call on data memory and others that require

    literal data to be specified whenever they are used. Examples of all are coming! The data that the

    instruction operates on, or uses, is called the operand. Operands can be data or addresses. Wewill see that some types of instructions always need an operand to be specified with them, others

    do not.

    Once an instruction has been executed, where is the result stored? For many instructions

    Microchip offer a choice, whereby the result can eitherbe held in the W registerorstored back

  • 8/6/2019 16F84 Edited

    22/45

    in data memory. Which one is used is fixed by certain instructions; in others it is determined by

    the state of a special dbit, which is specified within the instruction.

    4.2.2 The PIC 16 Series Instruction Setan Introduction

    Instruction set given in the table is divided into six columns, and each of the 35 instructions gets

    one line. The first column gives the actual mnemonic, together with the code specifying the type

    of operand it acts on. There are four such operand codes:

    ffor file (i.e., memory location in RAM), a 7-bit number

    b for bit, to be found within a file also specified, a 3-bit number

    d for destination, as described above, a single bit

    kfor literal, an 8-bit number if data or 11-bit if address.

    The second column summarizes what the instruction does. The third column shows how many

    instruction cycles the instruction takes to execute. The fourth column gives the actual 14-bit

    opcode of each instruction. This is the code that the cross-assembler produces, as it converts the

    original program in Assembler language to machine code. It is interesting to see here how the

    operand codes, listed above, become embedded within the opcode. The fifth column shows

    which bits in the Status register (Fig. 2.3) are affected by each instruction.

  • 8/6/2019 16F84 Edited

    23/45

    THE PIC 16 SERIES INSTRUCTION SET

    Table A1.

  • 8/6/2019 16F84 Edited

    24/45

    Let us have a look at five example instructions, to see how the information is presented.

    Assembler programming does not have to be case sensitive.

    clrwthis clears the value in the W register to zero. There are no operands to specify. Column

    5 tells us that the Status register Z bit is affected by the instruction. As the result of this

    instruction is always zero, the bit is always set to 1. No other Status register bits are affected.

    clrf fthis clears the value of a memory location, symbolized as f. It is up to the programmer

    to specify a value forf. Again, because the result is zero, the Status registerZbit is affected.

    addwf f,dthis adds the contents of the W register to the contents of a memory location

    symbolized by f. It is up to the programmer to specify a value for f. There is a choice of where

    the result is placed, as discussed above. This is determined by the value of the operand bit d.

    Because of the different values that the result can take, all three condition code bits, i.e. Z, the

    Carry bit C, and the Digit Carry bit DC are affected by the instruction.

    bcf f,bthis instruction clears a single bit in a memory location. Both the bit and the location

    must be specified by the programmer. The bit numberb will take a value from 0 to 7, to identify

    any one of the 8 bits in a memory location. No Status register flags are affected, even though it is

    possible to imagine that the result of the instruction could be to set a memory location to zero.

    addlw kThis instruction adds the value of a literal, whose value kmust be specified by the

    programmer, to the value held in the W register. The result is stored in the W register; there is no

    choice. Like addwf, all condition code bits can be affected by this instruction.

  • 8/6/2019 16F84 Edited

    25/45

    ASSEMBLY LANGUAGE PROGRAMMING

    Introduction

    The ability to communicate is of great importance in any field. However, it is only possible if

    both communication partners know the same language, i.e follow the same rules during

    communication. Using these principles as a starting point, we can also define communication

    that occurs between microcontrollers and man . Language that microcontroller and man use to

    communicate is called "assembly language". The title itself has no deeper meaning, and is

    analogue to names of other languages , ex. English or French. More precisely, "assembly

    language" is just a passing solution. Programs written in assembly language must be translated

    into a "language of zeros and ones" in order for a microcontroller to understand it. "Assembly

    language" and "assembler" are two different notions. The first represents a set of rules used in

    writing a program for a microcontroller, and the other is a program on the personal computer

    which translates assembly language into a language of zeros and ones. A program that is

    translated into "zeros" and "ones" is also called "machine language".

    The process of communication between a man and a microcontroller

    Physically, "Program" represents a file on the computer disc (or in the memory if it is read in a

    microcontroller), and is written according to the rules of assembler or some other language for

    microcontroller programming. Man can understand assembler language as it consists of alphabet

    signs and words. When writing a program, certain rules must be followed in order to reach a

  • 8/6/2019 16F84 Edited

    26/45

    desired effect. A Translator interprets each instruction written in assembly language as a series

    of zeros and ones which have a meaning for the internal logic of the microcontroller.

    Lets take for instance the instruction "RETURN" that a microcontroller uses to return from a

    sub-program. When the assembler translates it, we get a 14-bit series of zeros and ones which the

    microcontroller knows how to interpret.

    Example:

    RETURN 00 0000 0000 1000

    Similar to the above instance, each assembler instruction is interpreted as corresponding to series

    of zeros and ones. The place where this translation of assembly language is found, is called an

    "execution" file. We will often meet the name "HEX" file. This name comes from a hexadecimalrepresentation of that file, as well as from the suffix "hex" in the title, ex. "test.hex". Once it is

    generated, the execution file is read in a microcontroller through a programmer.

    An Assembly Language program is written in a program for text processing (editor) and is

    capable of producing an ASCII file on the computer disc or in specialized surroundings such as

    MPLAB,which will be explained later.

    3.1 Representing numbers in assembler

    In assembly language MPLAB, numbers can be represented in decimal, hexadecimal or binary

    form. We will illustrate this with a number 240:

    .240 decimal

    0xF0 hexadecimal

    b'11110000' Binary

    Decimal numbers start with a dot, hexadecimal with 0x, and binary start with b with the number

    itself under quotes '.

    3.2 Assembly language elements

    Basic elements of assembly language are:

  • 8/6/2019 16F84 Edited

    27/45

    Labels

    Instructions

    Operands

    Directives

    Comments

    Labels

    A Label is a textual designation (generally an easy-to-read word) for a line in a program, or

    section of a program where the micro can jump to - or even the beginning of set of lines of a

    program. It can also be used to execute program branching (such as Goto .......) and the program

    can even have a condition that must be met for the Goto instruction to be executed. It isimportant for a label to start with a letter of the alphabet or with an underline "_". The length of

    the label can be up to 32 characters. It is also important that a label starts in the first clumn.

    Instructions

    Instructions are already defined by the use of a specific microcontroller, so it only remains for us

    to follow the instructions for their use in assembly language. The way we write an instruction is

    also called instruction "syntax". In the following example, we can recognize a mistake in writing

    because instructions movlp and gotto do not exist for the PIC16F84 microcontroller.

  • 8/6/2019 16F84 Edited

    28/45

    Operands

    Operands are the instruction elements for the instruction is being executed. They are usually

    registers orvariables orconstants.

    Comments

    Comment is a series of words that a programmer writes to make the program more clear and

    legible. It is placed after an instruction, and must start with a semicolon ";".

    Directives

    A directive is similar to an instruction, but unlike an instruction it is independent on the

    microcontroller model, and represents a characteristic of the assembly language itself. Directives

    are usually given purposeful meanings via variables or registers. For example, LEVEL can be a

    designation for a variable in RAM memory at address 0Dh. In this way, the variable at that

  • 8/6/2019 16F84 Edited

    29/45

    address can be accessed via LEVEL designation. This is far easier for a programmer to

    understand than for him to try to remember address 0Dh contains information about LEVEL.

    3.3 Writing a sample program

    The following example illustrates a simple program written in assembly language respecting the

    basic rules.

  • 8/6/2019 16F84 Edited

    30/45

    When writing a program, beside mandatory rules, there are also some rules that are not written

    down but need to be followed. One of them is to write the name of the program at the beginning,

    what the program does, its version, date when it was written, type of microcontroller it was

    written for, and the programmer's name.

    Since this data isn't important for the assembly translator, it is written as comments. It should be

    noted that a comment always begins with a semicolon and it can be placed in a new row or it can

    follow an instruction.After the opening comment has been written, the directive must be

    included. This is shown in the example above.

    In order to function properly, we must define several microcontroller parameters such as: - type

    of oscillator,

    - whether watchdog timer is turned on, and

    - whether internal reset circuit is enabled.

    All this is defined by the following directive:

    _CONFIG _CP_OFF&_WDT_OFF&PWRTE_ON&XT_OSC

    When all the needed elements have been defined, we can start writing a program.

    First, it is necessary to determine an address from which the microcontroller starts, following a

    power supply start-up. This is (org 0x00). The address from which the program starts if an

    interrupt occurs is (org 0x04). Since this is a simple program, it will be enough to direct the

    microcontroller to the beginning of a program with a "goto Main" instruction.

    The instructions found in the Main select memory bank1 (BANK1) in order to access TRISB

    register, so that port B can be declared as an output (movlw 0x00, movwf TRISB).

    The next step is to select memory bank 0 and place status of logic one on port B (movlw 0xFF,

    movwf PORTB), and thus the main program is finished.

    We need to make another loop where the micro will be held so it doesn't "wander" if an error

  • 8/6/2019 16F84 Edited

    31/45

    occurs. For that purpose, one infinite loop is made where the micro is retained while power is

    connected. The necessary "end" at the end of each program informs the assembly translator that

    no more instructions are in the program.

    3.4 Control directives

    3.1 #DEFINE Exchanges one part of text for another

    Syntax:

    #define []

    Description:

    Each time appears in the program , it will be exchanged for .

    Example:

    #define turned_on 1

    #define turned_off 0

    Similar directives: #UNDEFINE, IFDEF,IFNDEF

    3.2 INCLUDE Include an additional file in a program

    Syntax:

    #include

    #include "file_name"

    Description:

    An application of this directive has the effect as though the entire file was copied to a place

    where the "include" directive was found. If the file name is in the square brackets, we are dealing

    with a system file, and if it is inside quotation marks, we are dealing with a user file. The

  • 8/6/2019 16F84 Edited

    32/45

    directive "include" contributes to a better layout of the main program.

    Example:

    #include

    #include "subprog.asm"

    3.3 CONSTANT Gives a constant numeric value to the textual designation

    Syntax:

    Constant =

    Description:

    Each time that appears in program, it will be replaced with .

    Example:

    Constant MAXIMUM=100

    Constant Length=30

    Similar directives: SET, VARIABLE

    3.4 VARIABLE Gives a variable numeric value to textual designation

    Syntax:

    Variable=

    Description:

    By using this directive, textual designation changes with particular value.

    It differs from CONSTANT directive in that after applying the directive, the value of textual

    designation can be changed.

    Example:

  • 8/6/2019 16F84 Edited

    33/45

    variable level=20

    variable time=13

    Similar directives: SET, CONSTANT

    3.5 SET Defining assembler variable

    Syntax:

    set

    Description:

    To the variable is added expression . SET directive is similar to EQU,

    but with SET directive name of the variable can be redefined following a definition.

    Example:

    level set 0

    length set 12

    level set 45

    Similar directives: EQU, VARIABLE

    3.6 EQU Defining assembler constant

    Syntax:

    equ

    Description:

    To the name of a constant is added value

    Example:

    five equ 5

  • 8/6/2019 16F84 Edited

    34/45

    six equ 6

    seven equ 7

    Similar instructions: SET

    3.7 ORG Defines an address from which the program is stored in microcontroller

    memory

    Syntax:

    org

    Description:This is the most frequently used directive. With the help of this directive we define where some

    part of a program will be start in the program memory.

    Example:

    Start org 000

    movlw 0xFF

    movwf PORTB

    The first two instructions following the first 'org' directive are stored from address 00, and the

    other two from address 10.

    3.8 END End of program

    Syntax:

    end

    Description:

    At the end of each program it is necessary to place 'end' directive so that assembly translator

    would know that there are no more instructions in the program.

  • 8/6/2019 16F84 Edited

    35/45

    Example:

    .

    .

    movlw 0xFF

    movwf PORTB

    end

    3.9 IF Conditional program branching

    Syntax:

    if

    Description:

    If condition in was met, part of the program which follows IF directive

    would be executed. And if it wasn't, then the part following ELSE or ENDIF directive would be

    executed.

    Example:

    if level=100

    goto FILL

    else

    goto DISCHARGE

    endif

    Similar directives: #ELSE, ENDIF

    3.10 ELSE The alternative to 'IF' program block with conditional terms

    Syntax:

    Else

    Description:

  • 8/6/2019 16F84 Edited

    36/45

    Used with IF directive as an alternative if conditional term is incorrect.

    Example:

    If time< 50

    goto SPEED UP

    else goto SLOW DOWN

    endif

    Similar instructions: ENDIF, IF

    3.11 ENDIF End of conditional program section

    Syntax:

    endif

    Description:

    Directive is written at the end of a conditional block to inform the assembly translator that it is

    the end of the conditional block

    Example:

    If level=100

    goto LOADS

    else

    goto UNLOADS

    endif

    Similar directives: ELSE, IF

    3.12 WHILE Execution of program section as long as condition is met

  • 8/6/2019 16F84 Edited

    37/45

    Syntax:

    while.

    endw

    Description:

    Program lines between WHILE and ENDW would be executed as long as condition was met. If a

    condition stopped being valid, program would continue executing instructions following ENDW

    line. Number of instructions between WHILE and ENDW can be 100 at the most, and number of

    executions 256.

    Example:

    While i

  • 8/6/2019 16F84 Edited

    38/45

    Syntax:

    ifdef

    Description:

    If designation was previously defined (most commonly by #DEFINE instruction),

    instructions which follow would be executed until ELSE or ENDIF directives are not would be

    reached.

    Example:

    #define test

    .

    ifdef test ;how the test was defined

    ......; instructions from these lines would execute

    endif

    Similar directives: #DEFINE, ELSE, ENDIF, IFNDEF, #UNDEFINE

    3.15 IFNDEF Execution of a part of the program if symbol was defined

    Syntax:

    ifndef

    Description:

    If designation was not previously defined, or if its definition was erased with

    directive #UNDEFINE, instructions which follow would be executed until ELSE or ENDIF

    directives would be reached.

    Example:

    #define test

    ..........

    #undefine test

  • 8/6/2019 16F84 Edited

    39/45

    ..........

    ifndef test ;how the test was undefined

    ..... .; instructions from these lines would execute

    endif

    Similar directives: #DEFINE, ELSE, ENDIF, IFDEF, #UNDEFINE

    3.16 CBLOCK Defining a block for the named constants

    Syntax:

    Cblock [][:], [:]......

    endc

    Description:

    Directive is used to give values to named constants. Each following term receives a value greater

    by one than its precursor. If parameter is also given, then value given in

    parameter is added to the following constant.Value of parameter is the starting value. If it is not given, it is considered to be zero.

    Example:

    Cblock 0x02

    First, second, third ;first=0x02, second=0x03, third=0x04

    endc

    cblock 0x02

    first : 4, second : 2, third ;first=0x06, second=0x08, third=0x09

    endc

    Similar directives: ENDC

    3.17 ENDC End of constant block definition

    Syntax:

    endc

    Description:

    Directive was used at the end of a definition of a block of constants so assembly translatorcould know that there are no more constants.

    Similar directives: CBLOCK

  • 8/6/2019 16F84 Edited

    40/45

    3.18 DB Defining one byte data

    Syntax:

    []db [, ,.....,]

    Description:

    Directive reserves a byte in program memory. When there are more terms which need to beassigned a byte each, they will be assigned one after another.

    Example:db 't', 00f, 'e', 's', 012

    Similar instructions: DE, DT

    3.19 DE Defining the EEPROM memory byte

    Syntax:

    [] de [, ,....., ]

    Description:

    Directive is used for defining EEPROM memory byte. Even though it was first intended onlyfor EEPROM memory, it could be used for any other location in any memory.

    Example:org H'2100'de "Version 1.0" , 0

    Similar instructions: DB, DT

    3.20 DT Defining the data table

    Syntax:

    [] dt [, ,........., ]

    Description:

    Directive generates RETLW series of instructions, one instruction per each term.

    Example:dt "Message", 0

    dt first, second, third

    Similar directives: DB, DE

    3.21 _CONFIG Setting the configurational bits

    Syntax:

    _ _config or_ _config,

    Description:

    Oscillator, watchdog timer application and internal reset circuit are defined. Before usingthis directive, the processor must be defined using PROCESSOR directive.

  • 8/6/2019 16F84 Edited

    41/45

    Example:_CONFIG _CP_OFF&_WDT_OFF&_PWRTE_ON&_XT_OSC

    Similar directives: _IDLOCS, PROCESSOR

    3.22 PROCESSOR Defining microcontroller model

    Syntax:

    Processor

    Description:

    Instruction sets the type of microcontroller where programming is done.

    Example:processor 16F84

    3.5 Files created as a result of program translation

    As a result of the process of translating a program written in assembler language we get files

    like:

    Executing file (Program_Name.HEX)

    Program errors file (Program_Name.ERR)

    List file (Program_Name.LST)

    The first file contains translated program which was read in microcontroller by programming. Its

    contents can not give any information to programmer, so it will not be considered any further.

    The second file contains possible errors that were made in the process of writing, and which

    were noticed by assembly translator during translation process. Errors can be discovered in a

    "list" file as well. This file is more suitable though when program is big and viewing the 'list' file

    takes longer. The third file is the most useful to programmer. Much information is contained in

    it, like information about positioning instructions and variables in memory, or error signalization.

    Example of 'list' file for the program in this chapter follows. At the top of each page is stated

    information about the file name, date when it was translated, and page number. First column

    contains an address in program memory where a instruction from that row is placed. Second

    column contains a value of any variable defined by one of the directives : SET, EQU,

  • 8/6/2019 16F84 Edited

    42/45

    VARIABLE, CONSTANT or CBLOCK. Third column is reserved for the form of a translated

    instruction which PIC is executing. The fourth column contains assembler instructions and

    programmer's comments. Possible errors will appear between rows following a line in which the

    error occurred.

  • 8/6/2019 16F84 Edited

    43/45

  • 8/6/2019 16F84 Edited

    44/45

    At the end of the "list" file there is a table of symbols used in a program. Useful element of 'list'

    file is a graph of memory utilization. At the very end, there is an error statistic as well as the

    amount of remaining program memory.

    DEVELOPMENT SUPPORT

    The PICmicro microcontrollers are supported with a full range of hardware and software

    development tools:

    Integrated Development Environment

    - MPLAB IDE Software

    Assemblers/Compilers/Linkers

    - MPASMTM Assembler

    - MPLAB C17 and MPLAB C18 C Compilers

    - MPLINKTM Object Linker/

    MPLIBTM Object Librarian

    Simulators

    - MPLAB SIM Software Simulator

    Emulators

    - MPLAB ICE 2000 In-Circuit Emulator

    - ICEPIC In-Circuit Emulator

    In-Circuit Debugger

    - MPLAB ICD

  • 8/6/2019 16F84 Edited

    45/45

    Device Programmers

    - PRO MATE II Universal Device Programmer

    - PICSTART Plus Entry-Level Development

    Programmer

    Low Cost Demonstration Boards

    - PICDEMTM 1 Demonstration Board

    - PICDEM 2 Demonstration Board

    - PICDEM 3 Demonstration Board

    - PICDEM 17 Demonstration Board

    - KEELOQ Demonstration Board