Curso Assembler Pic 892

download Curso Assembler Pic 892

of 154

Transcript of Curso Assembler Pic 892

  • 7/22/2019 Curso Assembler Pic 892

    1/154

    My First PIC Projects

    An introduction to the PIC processor.

    Bubble Software 2000

  • 7/22/2019 Curso Assembler Pic 892

    2/154

    My First PIC Projects- Page 1

    mICros First Projects

    Introduction

    Flash That LED

    Define The Problem

    Writing The Software

    Mnemonics

    The Assembler

    Labels

    Using a text assembler

    The Flowchart

    MicroPlan Assembler

    The Simulator

    The Real World

    7 segment LED display

    The Counter

    Using Switches

  • 7/22/2019 Curso Assembler Pic 892

    3/154

    My First PIC Projects- Page 2

    Introduction

    Welcome to MICROs. I know you are itching to get started with your new

    software and begin programming PICs as soon as possible, so this introduction

    will get you familiar with the MICROssuite of software and during the processyou will also start writing some small programs and hopefully get them working in

    the real world.

    If you have the MICROsExperimenters Kit, then going through the projects will

    be quite easy. If you do not have the Experimenters Kit, then you may like to geta PIC programmer so that you can program the software into a 16F84 chip tocomplete the projects.

    Before we get going, you have to understand that a PIC, or any othermicrocontroller chip for that matter, is just a piece of silicon wrapped in plasticwith pins sticking out to connect it to the outside world. It does not have anybrains, nor can it think for itself, so anything the chip does is the direct result of

    our intelligence and imagination. Sometimes you may get the feeling that thesethings are alive and are put here to torment your every waking minute, but this is

    usually due to bugs in your software, not a personality inside the chip. So pleaseremember:

    The PIC will always do what you tell it to, not necessarily what you want it to.

    One other thing that can cause problems is in the way you handle the chip itself.Your body is more than likely charged with Static Electricity and is usually the

    zap you feel when you touch a metal object after walking on nylon carpet orsimilar. The PICs most definitely do not like this high voltage discharging intothem. It can destroy the functionality of the chip either totally or partially, so

    always try to avoid touching the pins with your fingers.

    The PIC 16F84 data sheet is available in PDF format on the CD ROM in theAcrobat directory.

  • 7/22/2019 Curso Assembler Pic 892

    4/154

    My First PIC Projects- Page 3

    Flash That LED

    This would have to be the universal number one project for new PIC

    programmers. If you have had anything to do with writing software for PCs, then

    it would be the equivalent of writing hello world on the monitor for the first time.

    You might be thinking at this stage...What a boring project. I want to create arobot that does amazing things, not mess around with silly hello world or LED

    flash programs.

    Patience my friend. Things like that will come in due course, and as the old

    saying goes, You have to crawl before you can walk.

    OK then, so how do we get started?

    You might be tempted to jump straight in and write volumes of code right fromthe start, but I can only say, that in all probability, your software will not work.Now this might sound a bit tedious, but planning is the best way to begin any

    piece of new software. Believe me, in the long run, your code will stand a muchbetter chance of working and it will save you valuable time. Other benefits arethat your code will be structured and documented much better, which means you

    can read through and understand it more easily in the future if the need arises.

    So just how do we get this piece of silicon to do our bidding? In this case - flasha LED.

    Fundamentally, the PIC needs three things to make it work.

    1) 5 volt power source.2) Clock source3) Software

    The 5 volt supply is there to power the chip. The clock source gives the chip theability to process instructions. The software is a list of instructions that wecreate. The PIC will follow these to the letter with no exceptions, so we must

    make sure that they are written correctly or our program will not work asintended.

  • 7/22/2019 Curso Assembler Pic 892

    5/154

    My First PIC Projects- Page 4

    Define the problem

    To begin planning we must first define the LED flash problem that is going to be

    solved by using a PIC. This is the physical needs of the project. You cant write

    reams of software without knowing what the PIC is going to control. You may findthat you need to alter hardware and software as you progress, but dont be

    discouraged. This is normal for a lot of projects and is called Prototyping.

    We can start this discussion by saying that we must have a voltage sourceconnected to the LED to make it light. Usually we put a resistor in series with theLED to limit the current through it to a safe level and in most LEDs the maximum

    current is about 20mA.

    Quite obviously, if the PIC is going to turn the LED on and off for us, then theLED must be connected to one of its pins. These pins can be set as inputs or as

    outputs and when they are set as outputs we can make each individual pin have5 volts connected or 0 volts connected by writing either a Logic 1 or a Logic 0 tothem. We can now define this by saying we set a pin as an output high or as an

    output low.

    When a pin is an output high it will have 5 volts connected to it and is able tosource 20mA of current. When a pin is an output low it will have 0 voltsconnected to it and can sink 25mA of current.

    A red LED will consume about 2 volts across it when it is being used. We know

    that an output pin will have 5 volts connected to it, so that means the seriesresistor needs to consume the remaining 3 volts. 5V - 2V = 3V. By using Ohmslaw we can calculate the value of the resistor which must drop 3 volts with 3mA

    of current flowing through it and the LED.

    V = I R or R = V / I R = 3 / 0.003

    Therefore R = 1000 ohms, or 1K.

  • 7/22/2019 Curso Assembler Pic 892

    6/154

    My First PIC Projects- Page 5

    Our circuit so far is a 1K ohm resistor in series with a red LED.

    Which pin are we going to use to drive this LED? On the 16F84 there are 13

    pins available for us to use and these are divided into 2 Ports.

    PortA has 5 pins which are numbered RA0 - RA4.PortB has 8 pins which are numbered RB0 - RB7.

    At this stage you might think that we can use any one of these, and you would

    be right - except for one thing. Pin RA4 is an open collector, which means that

    it can only connect a pin to 0 volts not 5 volts. Therefore our LED would not turnon if the LED was connected between this pin and ground. It would need to beconnected between this pin and 5 volts.

    There are lots of little hidden gotchas that exist in the world of microcontrollersand the best way of knowing about them is by close examination of the data

    book. You will remember most of these tricks after you become familiar with aparticular chip, but even the most experienced programmers can get caught withthese problems sometimes.

    Even though we can use any pin to drive our LED circuit, for this example we willuse the open collector pin RA4. We are going to use this pin because it

    highlights the fact that it operates differently from all the others.

    To summarise our project so far, we are going toflash a red LED in series with a 1K ohm resistor

    from PortA pin RA4. RA4 can only sink current toground, so the LED cathode is connected to thispin via a 1K ohm resistor. As you may already

    know, a LED can only work if the Cathode is morenegative than the Anode, so the Anode side of theLED will be connected to the 5 volt supply.

    You can easily tell the cathode from the anode. The cathode pin will have a flat

    surface moulded next to it into the red plastic, and the anode pin is longer thanthe cathode pin.

    Now that we have our LED circuit figured out, the next stage is to write thesoftware that will flash it for us.

  • 7/22/2019 Curso Assembler Pic 892

    7/154

    My First PIC Projects- Page 6

    To find out more about how the PIC Port Pins work, click on the blue link to run

    the program called MicroPort.This is quite a lengthy program, so at this stage

    you may like to continue instead.

    Look at the file called program.pdf to see how to build the Experimenters Kit.These files are located in the same directory where the software was installed.You can click on the blue links to activate them.

    Writing The Software

    There are quite a few ways to create software for the PIC. You can write it with asimple text editor, or use MPLAB from Microchip, a Basic compiler such as

    MicroBasic, or you can use the assembler called MicroPlan.

    The PIC doesnt care what method you use to write the software because it onlyunderstands raw HEX code which is placed into the chip by using an appropriateprogrammer.

    This is a sample of HEX code.

    :1000000008308500003086008316173085003E30AA:10001000860083128F0190018221900B0C283330CF:1000200091003030920034309300413094005821D8:100030000F1817284C300E060319362856300E06B6

    Its not very meaningful is it, but that does not matter to us. We are not

    computers, so it is not in our best interests to understand what this data means.What does interest us as programmers, is just how do we create a data listinglike this.

    The answer is - by using an Assembler.

    This is quite an ingenious piece of software because it can read a text file thatwe have created for our program and turn it into a data file similar to the sample

    above. We can actually write our programs by collecting all the HEX values thatour program will use, and then create a data file ourselves. The trouble with thatidea is that it is a very tedious task and it would be terribly painful to try and find

    errors in it.

    http://microport.exe/http://microport.exe/http://microport.exe/http://program.pdf/http://program.pdf/http://program.pdf/http://microport.exe/
  • 7/22/2019 Curso Assembler Pic 892

    8/154

    My First PIC Projects- Page 7

    HEX numbers may be new to you so it will be best to have a quick look at them.At some stage, you may need to use them in your programs as well as Decimaland Binary numbers.

    Number Systems

    Any computer system, whether it be a PIC a PC or a gigantic main frame, canonly understand these 2 things.

    Ones and Zeros - 1s and 0s.

    The reason is quite simple. A computer is made up of millions of switches thatcan either be on or off. If a switch is on, it has a 1 state. If a switch is off, it has a0 state. In computer terms these are called Logic States.

    Logic 1 - switch is on.

    Logic 0 - switch is off.

    It is exactly as we mentioned before when we were talking about the output port

    pins of the PIC. If a pin is output high then it is Logic 1 or 5 volts. If the pin isoutput low then it is Logic 0 or 0 volts. If a pin is configured as an input and 5volts is connected to this pin, then the PIC would see a Logic 1. Similarly, the

    PIC would see a Logic 0 on this pin if 0 volts were connected.

    We were taught to count in a Base 10 or Decimal number system because we

    have 10 fingers on our hands. In this system, we add 1 to each number until wereach 9. We then have to add an extra digit tothe number to equal ten. This pattern continues

    until we reach 99 and then we place anotherdigit in, and so on.

    The computer uses a Base 2 or Binary numbersystem because it only has 2 Logic states to

    work with. If we have only got the numbers 1 and

    0 to use, it would seem obvious that after thenumber 1, we have to start adding morenumbers to the left. If we count from 0 to 15 thisis how it would look in Decimal and Binary.

    Decimal Binary

    0 0

    1 1

    2 10

    3 11

    4 100

    5 101

    6 110

    7 111

    8 1000

    9 1001

    10 1010

    11 1011

    12 1100

    13 1101

    14 1110

    15 1111

  • 7/22/2019 Curso Assembler Pic 892

    9/154

    My First PIC Projects- Page 8

    Now you may be able to see how a computer can represent numbers from 0 to15. To do this it would need at least 4 switches because the number 15represented in Binary is 4 digits long. Another way to say that, is the number is 4binary digitslong. The term Binary Digits is often abbreviated to Bits, henceour binary number is now 4 bits long, or just 4 bits. Perhaps we should redraw

    the table to emphasise the bit count. Remember, 0 bits represent a logic 0 valueso we should not leave them out.

    Decimal Binary

    0 0000

    1 0001

    2 0010

    3 0011

    4 0100

    5 0101

    6 0110

    7 0111

    8 1000

    9 1001

    10 101011 1011

    12 1100

    13 110114 1110

    15 1111

    The HEX number system is Base 16, that is you count 16 numbers beforeadding an extra digit to the left. This is illustrated in the table below.

    Decimal Binary HEX

    0 0000 0

    1 0001 1

    2 0010 2

    3 0011 3

    4 0100 4

    5 0101 5

    6 0110 6

    7 0111 7

    8 1000 8

    9 1001 9

    10 1010 A

    11 1011 B

    12 1100 C

    13 1101 D

    14 1110 E

    15 1111 F

    Notice how letters are used after the number 9, and perhaps you can see why

    we only counted up to the number 15. This is a nice even 4 bit number and incomputer terms this is called a NIBBLE.

  • 7/22/2019 Curso Assembler Pic 892

    10/154

    My First PIC Projects- Page 9

    If you look at the data sheet for the PIC 16F84, you will see that it is an 8 bitdevice. That means it can only deal with Binary numbers that are 8 bits wide.This is how 8 bit numbers are represented.

    Decimal Binary HEX

    0 00000000 001 00000001 01

    2 00000010 02

    3 00000011 03

    4 00000100 04

    5 00000101 05

    6 00000110 06

    7 00000111 07

    8 00001000 08

    9 00001001 09

    10 00001010 0A

    11 00001011 0B

    12 00001100 0C

    13 00001101 0D

    14 00001110 0E

    15 00001111 0F

    16 00010000 1017 00010001 11

    18 00010010 12

    - - - - - -

    252 11111100 FC

    253 11111101 FD

    254 11111110 FE

    255 11111111 FF

    Looking at this table, you can see that an 8 bit Binary number can have amaximum value of 255 in Decimal or FF in HEX.

    8 bit Binary numbers are called BYTES.

    Our binary numbers can get quite large, and as they do so they will get more

    complicated and harder to understand. We are not computers, but we want tounderstand what we write for them, so with the help of an assembler we can useDecimal, Binary and Hex numbers in our software.

    Have a look at a 32 bit binary number.

    10001100101000001000110010100000

    Its not hard to see why Hex and Decimal are easier to use. Imagine a 64 bit

    number and larger still. These are common in todays computers and aresometimes used with PICs.

  • 7/22/2019 Curso Assembler Pic 892

    11/154

    My First PIC Projects- Page 10

    Lets try to see what this value equals.

    First off, split the number into Bytes.

    10001100 10100000 10001100 10100000

    Looking Easier - Now split these into nibbles.

    1000 1100 1010 0000 1000 1100 1010 0000

    Even easier - Now convert these into Hex. This may be difficult at first, butpersevere. After awhile you will be able to convert Binary to Hex and vice versa

    quite easily.

    8 C A 0 8 C A 0

    Combine the Hex numbers for our result.

    8CA08CA0

    To show that we are talking in Hex values we put in a little (h) after the numberlike this.

    8CA08CA0h

    Lets convert that hex number back to Decimal again.

    8 C A 0 8 C A 0

    Remember in Decimal, each value in the columns increases by a power of 10 as

    we move to the left, and in Binary they increase by a power of 2. In Hex, as youmay have guessed, they increase by a power of 16. Be like me if you wish to,cheat and use a calculator to convert between the three number systems, it is

    much easier. In fact having a calculator that does these conversions is a verygood investment for a programmer.

  • 7/22/2019 Curso Assembler Pic 892

    12/154

    My First PIC Projects- Page 11

    Lets start from right to left and convert each individual hex number to decimal.

    Hex Decimal Multiplier Calculate Result

    0 0 1 0 x 1 0A 10 16 10 x 16 160C 12 256 12 x 256 3,0728 8 4,096 8 x 4,096 32,7680 0 65,536 0 x 65,536 0

    A 10 1,048,576 10 x 1,048,576 10,485,760C 12 16,777,216 12 x 16,777,216 201,326,5928 8 268,435,456 8 x 268,435,456 2,147,483,648

    Add up the last column and we get a total of 2,359,332,000.

    Therefore:

    10001100101000001000110010100000 = 8CA08CA0h = 2,359,332,000.

    Isnt it lucky that we dont have to think like computers.

    The PIC can only deal with individual 8 bit numbers, but as your programmingskills increase and depending on your software requirements, you will eventually

    need to know how to make it work with larger numbers.

    When you combine 2 Bytes together, the Binary number becomes a WORD.

    PROGRAMMING TIP:

    Break large problems into many smaller ones because the overallproblem will be much easier to solve. This is when planning becomesimportant.

    Getting back to the topic of assemblers, you should remember that the programdata file that we saw earlier is just a list of HEX numbers. Most of these numbers

    represent the program instructions and any data that these instructions need to

    work with.

    To combine data and instructions together, the PIC uses a special Binarynumber that is 14 bits wide. If you look at the data sheet, you will see that the

    PIC 16F84 has 1024 14 bit words available for program storage. In computerlanguage 1024 means 1K, so this PIC has 1K of program space.

  • 7/22/2019 Curso Assembler Pic 892

    13/154

    My First PIC Projects- Page 12

    We dont want to be overly concerned with Binary numbers when it comes towriting software so we need to become familiar with a language called

    ASSEMBLER. This language enables us to write code in such a way that we can

    understand and write it easily.

    Some people do not want to write code in assembler because they think it is toohard to learn, and writing code in a language such as BASIC is much easier.Sometimes this is true, but sometimes you cannot write tight and fast code withthese higher level languages and this may not be the best course of action for

    your particular project.

    ASSEMBLER IS EASY - EASY - EASY.

    This is especially so with PICs because there are only 35 instructions to workwith. Sometimes you will get into difficulty with assembler such as solvingproblems with multiply and divide, but these routines are freely available from

    many sources including the internet. Once you have them, it is usually only asimple matter of pasting them into your code if needed. Mostly, you will not evencare how they work, but it is a good exercise to learn the techniques involved

    because it builds up your own individual knowledge.

    Saving special code routines in a directory on your PC can be very productive

    and this is termed a Code Library.

  • 7/22/2019 Curso Assembler Pic 892

    14/154

    My First PIC Projects- Page 13

    Mnemonics

    This is a funny looking word. You pronounce it

    Nem On Icks.

    These are quite a powerful concept in programming because they provide an

    interface between us mere mortals and computers. It can become very confusingto write software if we have to refer to RAM addresses and data values by theirbinary numbers. Mnemonics makes it a lot easier for us to understand what we

    are writing by allowing us to assign names and labels to Instructions, RAMlocations and Data values.

    As an example, what do you think this means?

    0000100000000011.

    Any ideas?

    What about this?

    0803h

    Try this.

    movf 03h, 0

    Lets change it to something we can understand using Mnemonics.

    movf STATUS, W

    Thats a little easier to understand dont you think. It is exactly the same thing as

    the original Binary number except the first way the computer understands, thesecond and third ways we may understand, but the fourth way is quite easy tounderstand.

    It means...

    Move the contents of a file register called Status into W.

    Its all too easy. Of course we still need to understand what MOVF, STATUS andW mean, but that will come soon.

  • 7/22/2019 Curso Assembler Pic 892

    15/154

    My First PIC Projects- Page 14

    The assembler is used to generate code that the PIC can understand bytranslating these mnemonics into binary values and store them as a HEX datafile ready for a programmer to use. The standard assembler for a PIC is called

    MPASM for DOS, and MPASMWIN for Windows. These are free programs that

    are available from Microchip and are also supplied with MICROs after you

    install MPLAB from the CD.

    The Assembler

    Writing code for an assembler is quite easy, but as with most things there are afew things to learn.

    First off, the code you are writing for the assembler is called Source Code.After the assembler has assembled the source code successfully, it generates

    another file that is called Object Code. This is the HEX data file we saw earlier.

    When you use the assembler program, there are some options that you canenable or disable such as, generating an error file, case sensitivity in your

    source code and a few others. Mostly you can ignore these and leave the defaultsettings, but when you get more advanced at programming, you may like tochange them to make the assembler work more suitable for your particular

    needs.

    The assembler must be able to understand every line of source code that youwrite or it will generate an error. If this happens, the object code will not becreated and you will not be able to program a chip.

    Each line of code in the source file can contain some or all of these types ofinformation. The order and position of these items on each line is also important.

    LabelsMnemonics

    OperandsComments

    Labels must start in the left most column of the text editor. Mnemonics can startin column two and any other past this point. Operands come after the mnemonicand are usually separated by a space. Comments can be on any line usually

    after an operand, but they can also take up an entire line and are followed by thesemi colon character (;).

  • 7/22/2019 Curso Assembler Pic 892

    16/154

    My First PIC Projects- Page 15

    One or more spaces must separate the labels, mnemonics and operands. Someoperands are seperated by a comma.

    Here is a sample of the text that an assembler expects.

    Title "mICros Simple Program"

    list p=16f84 ; processor type

    ;; -------------; PROGRAM START; -------------; org 0h ; startup address = 0000

    start movlw 0x00 ; simple code movwf 0x05 goto start ; do this loop forever

    end

    You can see the label called startin column 1. Then comes a tab or space(s)

    to separate the label from the mnemonic movlw. Then comes another tab or

    space(s) to separate the mnemonic from the operand 0x00. Finally, you can see

    the comment which comes after the semi colon ; simple code

    Lets have a look at his a bit closer.

    The first line has a Titledirective. This is a predefined part of the assemblers

    internal language and lets you define a name for your source code listing. The

    next line has a listassembler directive and tells the assembler what type of

    processor it is assembling for. In this case, a 16F84 chip. This can be helpful if

    you write too much code for this particular processor to use. The assembler willdecide this while it is building the object code and generate an error if itencounters a problem. Directives like these control how the assembler builds the

    final Object code, and there are quite a few of them available for use so it wouldbe best to consult the help file for MPLAB or MPASM for further details.

    The next lines are just comments put there to help you the programmer knowwhat is going on.

  • 7/22/2019 Curso Assembler Pic 892

    17/154

    My First PIC Projects- Page 16

    It is vital that you get into the habit of writing comments all over your code. Thiswill help you understand what you have written especially when you come backto read it another time. Believe me, it is very easy to get confused trying to follow

    the meaning of your code if there is no explanation on how it works.

    The next lines tell the assembler the meanings of user defined labels.

    Org 0h is another directive that tells the assembler to set the current ROM

    address where the following instructions will be placed from. Remember that the16F84 has 1024 location available for code use. Org 0htells the assembler to

    start loading the instructions starting from ROM address 0. In other words themovlw 0x00 instruction will occupy ROM location 0 and the movwf 0x05

    instruction will occupy ROM location 1. goto startwill occupy ROM location

    2.

    One thing you may have missed here, is the fact that ROM and RAM addresses

    start from location 0, not location 1. The ROM space is actually from 0 to 1023,not from 1 to 1024. Remember 0 is a valid Binary number.

    It is probably best to use the correct terminology now to refer to ROM locations

    as ROM addresses.

    goto start is an instruction mnemonic followed by a label name. The

    assembler knows that the instruction start movlw 0x00is at ROM address

    0, so when it sees the instruction goto start, it will generate code to tell the

    PICs processor to goto ROM address 0 to fetch the next instruction.

    So what happens if there is no label called start anywhere in the source

    listing? The assembler will complain that it cannot find the label, generate anerror and will not complete the assembly process. Usually these errors arewritten to a separate error file and in a listing file as well. These files have the

    same name as your source file but with *.err and *.lst extensions.

    You cannot have two labels like this with the same name either. That confusesthe assembler because it does not know which particular line you are referringto. This also generates an error.

    Dont get too worried about error messages. They are simply there to help youfind problems with the way you wrote your code. Look at the list file that is

    generated to see the offending line, fix the problem back in the source code andthen reassemble.

    You will also receive warning messages at times. These are generated to tellyou that you may be doing something wrong, like forgetting to set a page bit.

  • 7/22/2019 Curso Assembler Pic 892

    18/154

    My First PIC Projects- Page 17

    Warning [205]: Ensure page bits are set.

    If you are certain that your code is correct, you can ignore them. We will talkabout page bits later.

    start movlw 0x00 ; simple code movwf 0x05 goto start ; do this loop forever

    Code that is written like this is called a LOOP. That is because the code

    executes until the goto startinstruction forces the processor to begin again.

    This particular code will LOOP forever or until you remove power from the chip.

    Your code must have some sort of loop to keep it flowing.

    start movlw 0x00 ; simple code movwf 0x05

    If you had code like this, what would happen after the movwf 0x05 instruction

    was executed? The answer is that the PIC would get lost because you have notgiven the processor anything to do past this point.

    So what exactly does this small piece of code do?

    Remember our LED flash problem?

    The LED was connected to pin RA4 which is PortA pin 4.

    PortA has 5 pins available for us to use, but we were only going to use pin RA4and forget about the others. If we had to turn the LED on we have to write a

    Logic 0 to this pin. Remember that once we have done this, 0 volts will appearon the pin if it is set as an output.

    What value should we write to Port A if we want to set pin RA4 to Logic 0? Hereis a small sample of a binary table.

    Decimal Binary

    0 000000

    1 000001

    2 000010

    3 000011

    16 010000

    47 101111

    If we consider that pin RA4 is represented by bit 4 in this table we can see thatwe can write any value as long as this bit equals 0.

  • 7/22/2019 Curso Assembler Pic 892

    19/154

    My First PIC Projects- Page 18

    Another thing you must consider here, is that we can do this because nothingelse is connected to Port A. If some devices were connected to the other Port Apins, we need to be more specific about the value we write to Port A so that we

    do not upset their operation.

    In our case, to turn the LED on, we can write the value 0 to Port A, and to turnthe LED off we can write 16 to PortA. So just how do we do this? If you look atthe original code, this is the first line.

    start movlw 0x00 ; simple code

    First we have a label called start, followed by the instruction movlw 0x00.

    movlwmeans to move a literal value into the W register. A literal value is any

    value that can fit into 8 bits. Remember that the PIC is an 8 bit device. This

    means a literal value can be any value from 0 to 255. In this case it is the value

    0, or 0x00 which is just a way of writing HEX values. This instruction can also bewritten as

    movlw b00000000 ; binary notation movlw d0 ; decimal notation movlw 0h ; another type of HEX notation.

    Binary notation is quite good for writing to the Ports because any bits that are 1means the corresponding output Port pin will be at 5 volts, and any that are 0 willbe at 0 volts. The exception is pin RA4 which is at a high impedance state when

    it is at Logic 1.

    See the program calledMicroPortfor more details.

    Notice the two styles of HEX notations. If any HEX number begins with a letter it

    must use the 0x notation. This is a zero and x. Any other HEX numbers can bewritten in either notation.

    movlw 0xAA movlw 34h movlw 2h movlw 0x33 movlw 0x00 movlw 0xEA

    movlw FFh This is not allowed, it must be: movlw 0xFF

    Looking back at the code, the next line is

    movwf 0x05

    http://microport.exe/http://microport.exe/http://microport.exe/http://microport.exe/
  • 7/22/2019 Curso Assembler Pic 892

    20/154

    My First PIC Projects- Page 19

    movwf means to move the contents of the W register to the file register

    specified. In this case it is RAM address 0x05.

    If you look at the 16F84 data sheet, and most other PICs for that matter, you willsee that RAM address 5 is Port A. So in other words, this instruction moves the

    contents of W to Port A.

    This seems a lot of effort. Why cant we just write 0 to Port A? Unfortunately the

    PIC does not function like that. You cannot directly write any 8 bit values to anyRAM locations. You have to use the W register to do the transfer. That is why itis called the W or Working Register.

    Labels

    We mentioned the use of labels before. With an assembler, we have the luxuryof being able to create our own label names which we can use to define thingslike general RAM addresses, special RAM locations, port pins and more.

    As an example of this concept we can change our original code...

    start movlw 0x00 ; simple code movwf 0x05

    into this...

    start movlw TurnOnLED ; simple code

    movwf PortA

    By writing your code in this way, you can just about comprehend the meaning of

    these two code lines.

    Write a special value to PortA which turns on a LED.

    Now this is all very fine except for one thing. How does the assembler know themeaning of TurnOnLEDand PortA? It has the ability to understand all the PIC

    instructions like movlw, and also what labels are, but you, as the programmer,

    have to tell the assembler the meaning of any new labels that you create.

    To do this you use the equassembler directive. This tells the assembler that the

    label on the left of the equdirective has the value on the right side.

    TurnOnLed equ 0x00 ; value to turn on LED with RA4PortA equ 0x05 ; PortA RAM address

  • 7/22/2019 Curso Assembler Pic 892

    21/154

    My First PIC Projects- Page 20

    You should note something here, and that is the first equate assigns a value to aLabel that will be used as a Literal, and the second equate assigns a value to aLabel that will be used as a RAM address. These are quite interchangeable by

    the way because they are just simple numerical values.

    start movlw PortA ; simple code movwf TurnOnLED goto start ; do this loop forever

    This new piece of code is quite valid and makes sense to the assembler. Whenthe PIC executes this code it will now get the Literal value 0x05 and place it in

    W, and then it will get this value from W and place it into RAM address 0x00.This address is where indirect addressing takes place. Check this out foryourself in the data book. This code does not make much sense now, but the

    assembler does not care what we write. Its only concern is that it cansuccessfully assemble this code.

    Now that we know about Labels, this is how we can rewrite the original codelisting and make it more readable

    Title "mICros Simple Program"

    list p=16f84 ; processor type;; -------------; PROGRAM START; -------------;

    TurnOnLed equ 0x00 ; value to turn on LED with RA4PortA equ 0x05 ; PortA RAM address

    org 0h ; startup address = 0000

    start movlw TurnOnLed ; simple code movwf PortA goto start ; do this loop forever

    end

    Quite simple isnt it.

    One thing to note is that label names must start in the first column on a separate

    line, and you cannot have spaces or TABs before them.

  • 7/22/2019 Curso Assembler Pic 892

    22/154

    My First PIC Projects- Page 21

    Now each time the assembler comes across a Label called TurnOnLED it will

    substitute it for the value 0x00. When it comes across a Label called PortA it

    will substitute it for the value 0x05. The assembler does not care in the least

    what these labels mean. They are there just to make it easier for us to read ourcode. Lets have a quick look at how the assembler turns the source code into a

    HEX file.

    When the assembler begins working, it creates a symbol table which lists all thelabels you created and then links the values you associated with them.

    TurnOnLed equ 0x00 ; value to turn on LED with RA4PortA equ 0x05 ; PortA RAM address

    The symbol table will look like this.

    SYMBOL TABLELABEL VALUEPortA 00000005TurnOnLed 00000000

    The assembler also has a ROM address counter which is incremented by 1 each

    time it assembles a line of code with an instruction in it. This next line is calledan assembler directive and it tells the assembler to set this counter to ROMaddress 0.

    org 0h ; startup address = 0000

    The next code line has an address label attached to it so the assembler also

    adds this to its symbol table. At this stage the ROM address counter equals 0 sothe startlabel gets the value 0.

    start movlw TurnOnLed ; simple code

    The symbol table will look like this.

    SYMBOL TABLELABEL VALUEPortA 00000005TurnOnLed 00000000start 00000000

    Next on this code line is movlw. If you look at the data book the MOVLW

    instruction has a Binary number associated with it. Remember about computersonly understanding 1s and 0s. This is how the PIC understands instructions.

  • 7/22/2019 Curso Assembler Pic 892

    23/154

    My First PIC Projects- Page 22

    MOVLW in Binary =11 00XX kkkk kkkk

    We need to decipher this a bit.

    The 1100XX is the part of the instruction that tells the processor that it is a

    MOVLW instruction. The XX part means that it doesnt matter what value thesetwo bits are. The kkkk kkkk represents the 8 bits of data and will be the

    actual Literal Value. The assembler will look up its symbol table and find the

    label TurnOnLEDand return with the value 0. It will then insert this information

    into the MOVLWinstruction data.

    Therefore the complete instruction becomes 11 0000 0000 0000 which is 3000h.

    Notice there are 14 bits for the instruction, and that the instruction itself isrepresented with the literal data combined. In this way each PIC instruction only

    occupies 1 single ROM address in the chip. Therefore the 16F84 with 1K ofROM space, can store 1024 instructions in total.

    The assembler is now finished with this code line because it does not care aboutthe comment, so it increments its address counter by 1 and continues with thenext line.

    movwf PortA

    MOVWF = 00 0000 1fff ffff

    00 0000 1are the bits that define the MOVWFinstruction and fff ffffare the

    bits that define the RAM address where the W register contents will end up. Theassembler looks up PortA in its symbol table and returns the value 5.

    Therefore the complete instruction becomes 00 0000 1000 0101 which is 0085h.

    So far the assembler has generated the HEX numbers 3000h and 0085h.

    This is the next line.

    goto start ; do this loop forever

    GOTO = 10 1kkk kkkk kkkk

    10 1are the bits that define the GOTOinstruction and the kkk kkkk kkkkare

    the 11 bits that define the new ROM address that the processor will begin fromafter this instruction executes.

  • 7/22/2019 Curso Assembler Pic 892

    24/154

    My First PIC Projects- Page 23

    If you do the math you will see that a number with 11 bits can range from 0 to2047. This means that a GOTO instruction has enough information to let the

    processor jump to any ROM address from 0 to 2047. The 16F84 only has a ROMaddress space that ranges from 0 to 1023 so a goto instruction can let theprocessor jump to anywhere in this device.

    The assembler will look up the symbol table for the label name start and

    return with the value 0.

    Therefore the complete instruction becomes 10 1000 0000 0000 which is 2800h.

    The final HEX code assembled for this little program is 3000h 0085h 2800h.

    This next line is another assembler directive and tells the assembler that this isthe last line of the source file. Please dont leave it out or the assembler will getupset and generate an error.

    end

    Its not hard to imagine now why the assembler will complain if you leave out alabel name or use one that was spelled incorrectly.

    After assembly the next page shows what the listing file will look like. If the nameof your source file was first.asmthen the list file will be first.lstand the

    HEX file will be first.hex. These new files will be created in the same

    directory where first.asm is located and are simple text files which can be

    viewed with any text editor program such as Notepad.

    If you look through this listing you should be able to verify what has just beenmentioned.

    On the far right of the listing, are the current ROM addresses and theseincrement as each instruction is added. You will notice that they start off at 0because of the ORG 0hdirective.

    If a code line has an instruction on it, then next to the ROM address, you will see

    the 4 digit HEX code that represents the instruction.

    After the enddirective you will see the Symbol Table contents, then how much

    of the processors memory was used, and finally, some information on errors thatthe assembler may have encountered.

  • 7/22/2019 Curso Assembler Pic 892

    25/154

    My First PIC Projects- Page 24

    MPASM 02.30 Released FIRST.ASM 12-31-1999 17:26:38PAGE 1

    LOC OBJECT CODE LINE SOURCE TEXT VALUE

    00001 Title "mICros Simple Program" 00002 00003 list p=16f84 ; processor type 00004 00005 ; Copyright Bubble Software 2000 00006 ; 00007 ; ------------- 00008 ; PROGRAM START 00009 ; ------------- 00010 ;00000001 00011 TurnOnLed equ 0x00 ; value to turn on LED with RA400000005 00012 PortA equ 0x05 ; PortA RAM address 000130000 00014 org 0h ; startup address = 0000

    000150000 3000 00016 start movlw TurnOnLed ; simple code0001 0085 00017 movwf PortA0002 2800 00018 goto start ; do this loop forever 00019 00020 00021 end

    MPASM 02.30 Released FIRST.ASM 12-31-1999 17:26:38PAGE 2mICros Simple Program

    SYMBOL TABLELABEL VALUE

    PortA 00000005TurnOnLed 00000000__16F84 00000001start 00000000

    MEMORY USAGE MAP ('X' = Used, '-' = Unused)

    0000 : XXX------------- ---------------- ---------------- ----------------

    All other memory blocks unused.

    Program Memory Words Used: 3

    Program Memory Words Free: 1021

    Errors : 0Warnings : 0 reported, 0 suppressedMessages : 0 reported, 0 suppressed

  • 7/22/2019 Curso Assembler Pic 892

    26/154

    My First PIC Projects- Page 25

    After successful assembly, this is what the generated HEX file looks like.

    :060000000030850000281D:00000001FF

    This has all the information that a programming device needs to write yourprogram code to a chip. The default HEX file generated by MPASM is a style

    called INHX8M, and this is how it is dissected.

    :BBAAAATTLLHH....LLHHCC

    BB This is a 2 digit value of the number of bytes on the line. 16 MAX

    AAAA The starting address of this line of data.

    TT A record type. Normally is 00, but will be 01 on the last line.

    LLHH A data word presented as low byte high byte format.

    CC The checksum value of this line of data.

    :060000000030850000281D

    :06 means 6 bytes of data on the line.Our new code was 3 WORDS long which = 6 BYTES.

    :060000000030850000281D

    0000 means the bytes on this line are programmed starting from ROM address 0

    :060000000030850000281D

    00 means record type, but not on last line

    :060000000030850000281D

    00 30 85 00 00 28 are the 6 data bytes in low byte/ high byte format.If you swap the bytes around and merge them into 3 words, you get

    3000 0085 2800

    This is the same as our code data.

  • 7/22/2019 Curso Assembler Pic 892

    27/154

    My First PIC Projects- Page 26

    :060000000030850000281D

    1D is the checksum for all the data listed on this line. It is sometimes used by aprogrammer to make sure the data on each HEX code line has not been

    corrupted.

    :00000001FF

    The last HEX line has 0 bytes listed and 01 in the record type which means it isthe last line of the file.

    Using a text assembler

    If you havent installed MPLAB on your computer yet, please do so now. It is

    located in a separate directory on the MICROsCD. Wait for the auto start when

    you insert the CD into the drive and click on Install MPLAB when the introscreen is shown. When this program is installed you will have access to theMPLAB Integrated Development Environment by Microchip, and also makes

    MPASMWIN available for separate use.

    For convenience, create a MPASMWIN shortcut for your desktop. Right click on

    a blank part of the desktop screen and a pop up window will appear. Click onNew - Shortcut and a dialog box will appear. Click on the browse button to

    display a directory dialog box. Double click on Program Files - Mplab -Mpasmwin.exe. Now click on Next and change Mpasmwin.exe just toMpasmwin in the edit box displayed. Click finish and a new Mpasmwin icon will

    appear on the desktop.

    Now we are going to do our first assembly using this program.

    Double click on this new icon to start Mpasmwin. You will notice the screenshows various options which are the assembler directives that were mentioned

    earlier. At this stage you do not need to worry about them as we will use thedefault ones shown.

    Click on the Browse button to show a directory dialog box. Double click on the

    c:\ icon to bring the directory listing back to the C drive root directory. Now lookdown this directory to find the softwareinstallation directory. The default name

    will be Bubble. In the directory list on the left side you should be able to find afile called first.asm. This is the source code for the file we have just been

    looking at. Double click on this and it will be listed in Mpasmwin as the file it willassemble. Now click on Assemble and Mpasmwin will assemble this file.

  • 7/22/2019 Curso Assembler Pic 892

    28/154

    My First PIC Projects- Page 27

    If all went well, a new dialog box will bedisplayed with a green progress bar saying that

    assembly was successful. If all didnt go well,this bar will be red and you will have to find out

    where the problem is in the source code beforecontinuing.

    Ill just bet that the assembly process failed and

    that bar is a bright red colour, and the screentells us that there was 1 error encountered.Guess what? Now we have to fix the problem.

    First, click on the OK button to close Mpasmwin.Start the program called Notepad which is

    located in Start - Programs - Accessories -

    NotePad. Perhaps you can create a short cut forthis program as well. Now click on File - Open and open the file called

    first.asmwhich is located in the softwareinstallation directory.

    See if you can see where the problem is.

    If you cant then dont worry, Mpasmwin will tell us. Using Notepad, open up thefile called first.err, and this should be the contents.

    Error[113] C:\...FIRST.ASM 16 : Symbol not previously defined (TurnOnLed)

    It is telling us that a Symbol has not been previously defined, notably,TurnOnLed. You can see that the error occurred on line number 16 of the

    source file. To make things easier, use Notepad to open the file calledfirst.lst. This will look very similar to the listing shown previously. The only

    difference between them is an error listed.

    Error[113] : Symbol not previously defined (TurnOnLed)0000 3000 00016 start movlw TurnOnLed ; simple code

    The offending line is the one following the error message. Now that you can

    actually see the problem, use Notepad to reopen the source file first.asm.

    Now this seems crazy. We have defined TurnOnLedand we have used it in a

    way that should make sense to the assembler.

    Look a little closer and you may see the problem.

  • 7/22/2019 Curso Assembler Pic 892

    29/154

    My First PIC Projects- Page 28

    Here are the two lines that define and use the label.

    TurnonLed equ 0x00 ; value to turn on LED with RA4

    start movlw TurnOnLed ; simple code

    Can you spot the problem yet?

    Is TurnonLedthe same as TurnOnLed?

    No it isnt, because TurnonLed has a lowercase o and TurnOnLed has an

    uppercase O. This makes a big difference to the assembler. Start Mpasmwinagain and you will notice a Case Sensitive checkbox which is checked. Thismeans the assembler treats o as a different character to O. You can decide if

    you want case sensitivity or not when you write your programs.

    You might think that the offending line should be the one that definedTurnonLed, but this is incorrect. If you look at the listing file again, you will see

    that the assembler has TurnonLedlisted in the symbol table.

    TurnonLed 00000000

    Then when it got to the line with TurnOnLed, it could not find this label in the

    symbol table, so it told us of the error at this point.

    Use NotePad to open first.asmagain and change the TurnonLedsymbol to

    TurnOnLedand save the file.

    TurnOnLed equ 0x00 ; value to turn on LED with RA4

    Start Mpasmwin again and first.asmshould still be listed for assembly. Click

    on Assemble and this time you should have a green bar highlighted showing

    success.

    There are lots of different errors that may appear from time to time, and part of

    the art of programming is developing some expertise in finding and fixing them.

    Sometimes the assembler will not generate an error, but the code still doesnt do

    what we wanted. This means that we made an error with the way we wrote thecode and at times this can be very hard to find. Good documentation can easethe pain so make sure you have enough listed to make you understand your

    code as much as possible.

  • 7/22/2019 Curso Assembler Pic 892

    30/154

    My First PIC Projects- Page 29

    Use Notepad to open the file called first.hexwhich has been created in the

    software directory by Mpasmwin. It should match up with this which will be the

    same as the HEX listing that was shown previously.

    :060000000030850000281D

    :00000001FF

    OK, OK, I know. I said we will be creating a LED flasher as our first program, but

    if you think you have got a good grasp on what has been said up to this point,then please feel free to move on. If you are not sure then it is best to go backand have another look.

    The Flowchart

    The flowchart is another part of the planning stage for our projects and they arevery good for documenting larger programs as they enable us to see a pictorial

    representation of the flow of our programs. To create a flow chart, you just needto figure out what your program will do. If it is a large program, dont try to crameverything into a huge flow chart. Break things down into simpler tasks.

    You might think that the previous program will turn on the LED if we programmeda PIC and connected a clock source and applied power. Unfortunately you would

    be mistaken. The LED will not come on at all.

    The reason is because we have not told the PIC to make Port A pin RA4 work as

    an output. If you look at the 16F84 data sheet, you will discover that Port pinsare set as inputs as a default setting, so there are no means yet to drive theLED.

    If you think about it, this should be our first course of action - to set the port pinsappropriately so that we can get control of external devices connected to the PIC

    as quickly as possible after we turn the power on.

    After we have done that, our next task is to make the LED turn on and off. Do wemake the LED flash fast, medium or slow? These are things you have to decideon. Lets say we will make it flash every half second, which is 500 milli seconds,

    or 500mS. The LED will be on for 500mS then off for 500mS.

    Somehow we need to make Port A pin RA4 Logic 1 for 500ms and then Logic 0

    for 500mS. The 500mS is called a DELAY and this must be used each time theLogic state of RA4 is changed.

  • 7/22/2019 Curso Assembler Pic 892

    31/154

    My First PIC Projects- Page 30

    To keep the LED flashing continuously we need to create a LOOP in our codeotherwise the LED would flash just once and stop.

    This then will be the basis for our flowchart.

    Flowcharts are easy to create. They are usually just textsurrounded by boxes, diamonds and circles which areconnected by lines with arrows showing the direction ofprogram flow. Here is the flow chart for the LED flash

    program. As you can see it is quite simple and shows ina pictorial fashion what we mentioned on the previouspage.

    They usually begin with a START statement inside acircle. If you follow the connected line downwards in the

    direction of the arrow the next box tells us to set Port pin

    RA4 as an output. Next comes the box to turn the LEDon followed by a 500mS delay. Then we turn the LED off

    followed by another 500mS delay. After that, the lineLOOPS back to turn the LED on again and this will

    continue while power is applied to the chip.

    When you look at the program with a flowchart it is quite easy to follow what is

    going on. You can write whatever you like in these boxes, but just make sure youunderstand what is going on when it is finished. Create them in pencil to startwith because you will probably change things a quite bit while you develop your

    projects. Now we are ready to begin writing the flash program. We can use most

    of what we have seen already to start with.

    Title "mICros Flash Program"

    list p=16f84 ; processor typeTurnOnLed equ 0x00 ; value to turn on LED with RA4PortA equ 0x05 ; PortA RAM address;; -------------; PROGRAM START; -------------; org 0h ; startup address = 0000

    start movlw TurnOnLed ; simple code movwf PortA goto start ; do this loop forever

    end

  • 7/22/2019 Curso Assembler Pic 892

    32/154

    My First PIC Projects- Page 31

    All thats been changed so far is the Title. The first function block in the flow

    chart tells us to setup pin RA4 as an output. This is going to require 3 lines of

    code to do because we need to set a special RAM location that requires aprogramming trick to access. This is common to most PICs and is calledselecting a RAM Page.

    RAM addressing can get a little bit complicated inside the PIC, but once youknow what is going on it is easy to understand. This topic is covered in detail in

    theMicroPosttutorial program.

    We will look at this briefly here. Remember the MOVWF instruction and how ithas 7 bits available in the instruction that specifies a RAM address.

    MOVWF = 00 0000 1fff ffff

    If you do the Binary math with 7 bits you will see that the maximum number that

    can be represented is 127 decimal. (111 1111 or 7Fh) This means that theMOVWF instruction can only move data to RAM addresses between 0 and 127.

    The register that controls how we make the Port pins behave as an Input or an

    Output is called the TRIS register. There are 2 of these in the 16F84, one forPort A and is called TRISA, and another for Port B and is called TRISB.

    The RAM address for these registers are 133dec and 134dec which is 85h and86h respectively. So how can we use the MOVWF instruction to move data into

    the TRISA register to set pin RA4 as an output instead of being the default inputwhen we only have 7 bits available in the instruction?

    The answer lies in what is called a RAM Page Select Bit. There are 2 RAMpages available in the 16F84. Page 0 has RAM addresses ranging from 0 - 127,and Page 1 has RAM addresses ranging from 128 - 255. Addresses 128 - 255

    need 8 bits of information which is one more than can be used with the MOVWFinstruction. So to use this instruction with the TRISA register we need to get the8th bit from somewhere else.

    This bit comes from the STATUS register, which is at RAM address 3, and iscalled the RP0 bit. Each time you use the MOVWF instruction, RP0 is used as

    the 8th address bit. If this bit is set to Logic 0 then the MOVWF instruction canaccess RAM addresses from 0 - 127. If this bit is set to Logic 1 then the MOVWFinstruction can access RAM addresses from 128 - 255.

    http://micropost.exe/http://micropost.exe/http://micropost.exe/http://micropost.exe/
  • 7/22/2019 Curso Assembler Pic 892

    33/154

    My First PIC Projects- Page 32

    RP0 is bit number 5 in the STATUS register.

    Notice how the bit numbers start from 0 on the right side and finish with 7 on theleft side.

    If we want to set the RP0 bit to logic 1 so that we can send data to the TRISAregister, we can use this instruction.

    BSF 03h,05h ; set RP0 for RAM page 1

    What looks easier to read, the instruction or the comment?

    Either you say!

    What is BSF?

    BSF means Bit Set File, or more simply, set a particular bit in a RAM register toLogic 1. The instruction as written means to set a bit number 5 in RAM register

    3.

    That looks a terrible way to write instructions, so what we want to do is to rewrite

    this instruction so that it makes more sense to us.

    As you are now aware, we can do this by defining new labels. Its just a simple

    matter to add these new label definitions to our existing program.

  • 7/22/2019 Curso Assembler Pic 892

    34/154

    My First PIC Projects- Page 33

    Title "mICros Flash Program"

    list p=16f84 ; processor typeTurnOnLed equ 0x00 ; value to turn on LED with RA4PortA equ 0x05 ; PortA RAM addressStatus equ 0x03 ; Status RAM address

    RP0 equ 0x05 ; Status RP0 bit = bit 5;; -------------; PROGRAM START; -------------; org 0h ; startup address = 0000

    start movlw TurnOnLed ; simple code movwf PortA goto start ; do this loop forever

    end

    Now that we have done that, we can use the labels in our program to set theStatus RP0 bit so that we can access TRISA.

    So instead of writing

    bsf 03h,05h ; set RP0 for RAM page 1

    we can now write

    bsf Status,RP0 ; set RP0 for RAM page 1

    The opposite to this instruction is BCF which means to Bit Clear File, or moresimply, clear a particular bit in a RAM register to Logic 0. After we set the TRISregister we need to set RP0 to Logic 0 so that we can access the PortA register

    which, if you remember, is RAM address 5 and therefore is in RAM Page 0.

    We can write that instruction as

    bcf Status,RP0 ; set RP0 for RAM page 0

    In between these two instructions we need to set the TRISA register so that pinRA4 becomes an output. Do you remember how each bit in the PortA and PortB

    registers corresponds to the Port pins? Bit 0 in PortA = RA0, Bit 1 = RA1 etc.

    The same is for the TRIS registers. Bit 0 in TRISA controls whether RA0 is an

    input or an output, Bit 1 for RA1, Bit 2 for RA2 etc.

  • 7/22/2019 Curso Assembler Pic 892

    35/154

    My First PIC Projects- Page 34

    To make any pin an output we clear the corresponding TRIS bit to Logic 0, andto set any pin as an input we set the corresponding TRIS bit to Logic 1.

    This is easy to remember.

    1 = Input0 = Output

    The default state of the port pins on power up are inputs, which means all the

    bits in both TRIS registers are set to Logic 1.

    Now, to make Port pin RA4 be an output we need to set TRISA bit 4 to Logic 0.

    These are the 3 instructions to do this task.

    bsf Status,RP0 ; set RP0 for RAM page 1

    bcf TrisA,RA4 ; set RA4 = output bcf Status,RP0 ; set RP0 for RAM page 0

    Before this will work with the assembler, we need to add the Labels TRISA and

    RA4 to our source code.

    Title "mICros Flash Program"

    list p=16f84 ; processor typeTurnOnLed equ 0x00 ; value to turn on LED with RA4PortA equ 0x05 ; PortA RAM addressTrisA equ 0x85 ; TRISA RAM addressRA4 equ 0x04 ; PortA RA4 = bit 4Status equ 0x03 ; Status RAM addressRP0 equ 0x05 ; Status RP0 bit = bit 5;; -------------; PROGRAM START; -------------; org 0h ; startup address = 0000

    bsf Status,RP0 ; RP0 set for RAM page 1 bcf TrisA,RA4 ; set RA4 = output

    bcf Status,RP0 ; RP0 set for RAM page 0

    start movlw TurnOnLed ; turn on LED on RA4 movwf PortA goto start ; do this loop forever

    end

  • 7/22/2019 Curso Assembler Pic 892

    36/154

    My First PIC Projects- Page 35

    We have also added the code to make RA4 an output before the program doesanything else. That way we gain control of the Port pins very quickly.

    The next thing to do according to our flowchart is to turn the LED on. This isalready in the code, so the next thing to do is create some code to make a

    500mS delay.

    start movlw TurnOnLed ; turn on LED on RA4 movwf PortA

    To make a simple delay, we need to make the processor waste the time requiredfor the delay and an easy way to do this is to subtract 1 from a RAM register until

    it equals 0. It takes the PIC a certain amount of time to do this, but the trick is tofind out how much time.

    The processor used in the MiniPro PCB runs with a clock speed of 4MHz. That

    is 4 million cycles per second.

    The PIC needs 4 of these cycles to process most instructions, which means theyexecute at a rate of 1 million per second. These are called Instruction Cycles.

    This means that 1 instruction cycle = 4 clock cycles.

    Some instructions, like GOTO and CALL, use 2 instruction cycles to complete.

    For our purposes, all we have to do is figure out how many instruction cycles weneed to waste to create a 500mS delay. Each basic instruction cycle with a clock

    speed of 4MHz takes 1 micro second to execute, so we need 500,000 of them tomake our delay.

    Lets create a small loop that simply decrements a RAM register until it equals 0.

    clrf DelayL ; clear DelayL to 0WaitHere decfsz DelayL,f ; subtract 1 from DelayL goto WaitHere ; if not 0, goto WaitHere

  • 7/22/2019 Curso Assembler Pic 892

    37/154

    My First PIC Projects- Page 36

    Inside the 16F84, there are 68 general purpose RAM registers that we can usefor whatever we need. The first of these registers starts at RAM address 12dec,or 0x0C. We need to use one of these to create the delay code loop above and

    we have called it DelayL. We can define the label like this so that we can use it

    in our program. This means the newly define RAM location is at RAM address 20

    hex, or 32 decimal.

    DelayL equ 0x20 ; delay register LOW byte

    The CLRF DelayL instruction makes the contents of RAM register DelayL

    equal zero, and gives us a known value to start from.

    The DECFSZ DelayL,f instruction means to decrement the contents of

    DelayLand if it now equals 0 then skip over the next instruction.

    Notice the ,fthat follows DelayL. This is called a Destination Designator. If the

    letter fis used, then the result of the operation is placed back into the registerspecified in the instruction. If the letter wis used then the result of the operation

    is placed into the W register.

    Sequence of events for DECFSZ DelayL,f

    W Register = 0xA0DelayL = 0

    Value is read into the Arithmetic Logic Unit (ALU) = 0Value in ALU is then decremented = 0xFF

    DelayL = 0xFFW Register = 0xA0

    Sequence of events for DECFSZ DelayL,w

    W Register = 0xA0DelayL = 0Value is read into the Arithmetic Logic Unit (ALU) = 0

    Value in ALU is then decremented = 0xFFDelayL = 0W Register = 0xFF

    There are quite a few instructions that use ,f or ,w after the instruction to

    specify where the result goes.

    The assembler doesnt care if you omit to use ,fafter an instruction, as it will

    assume that you want the result placed back into the specified register.

  • 7/22/2019 Curso Assembler Pic 892

    38/154

    My First PIC Projects- Page 37

    The assembler will generate a warning message like this, but you can ignore itas long as you know your code is correct.

    Message[305]: Using default destination of 1 (file).

    Notice the 1value. As with all labels used with an assembler, they must have avalue assigned to them. f and w are labels as well, but we dont need to worry

    about them as they are automatically assigned a value by the assembler.

    f assembler assigns value 1

    w assembler assigns value 0

    So how does this value fit inside an instruction.

    Remember the MOVWF instruction?

    MOVWF = 00 0000 1fff ffff

    Can you see the bit that equals 1? This is the destination bit which forms part of

    this instruction. The processor checks this bit during execution and if it is 1,

    sends the result back the specified RAM address. MOVWF always sends theresult back to the specified RAM address, that is why this bit is always 1.

    Instructions like this next one are a little different. DelayL = RAM address 0Ch.

    DECFSZ DelayL,w

    The binary code for this instruction is 00 1011 0000 1100, and the d bit = 0.

    DECFSZ DelayL,f orDECFSZ DelayL

    The binary code for this instruction is 00 1011 1000 1100, and the d bit = 1.

    In future we will not use ,f when we want the result of an instruction placed back

    into the specified RAM address. Now we can move back to the code listing.

    When this code block executes, DelayL will be set to 0. Then it will be

    decremented by one and it will have a value of 0xFF. This new value is notequal to 0, so the next code line is NOT skipped. Therefore the instruction Goto

    WaitHere is executed and the processor loops back to the code line with the

    label WaitHere. DelayL is decremented again, and eventually it will equal 0

    after this code has completed 256 loops. When this happens, the instruction

    goto WaitHerewill be skipped thus breaking the loop and ending the delay.

  • 7/22/2019 Curso Assembler Pic 892

    39/154

    My First PIC Projects- Page 38

    The DECFSZinstruction takes one instruction cycle to complete unless the result

    of the decrement equals 0. It will then take two instruction cycles. A GOTO

    instruction always takes two instruction cycles to complete. If you do the math, itwill take around 768 instruction cycles to complete this routine. This is quite a bit

    shorter than the 500,000 we need and if we were to use the delay routine as it is,

    the LED would flash on and off so fast, we would not see it.

    If you would like to know more about instruction timing there are animated

    tutorials available in the program called MicroPrac.

    What we need to do is use this same delay routine enough times so that 500mS

    is wasted and we can accomplish this by using what is called a Nested Loop.These are just code loops within code loops and to create one we use anotherRAM register to control how many times the existing delay code executes. If this

    is still not enough for the delay we need, then we will have to use more nestedloops and more RAM registers.

    In fact, for a delay of 500mS we need to use 3 RAM registers when the chip isexecuting instructions at a rate of 1 million per second.

    Lets define these registers so we can use them.

    DelayL equ 0x20 ; delay register LOW byteDelayM equ 0x21 ; delay register MID byteDelayH equ 0x22 ; delay register HIGH byte

    Now we can construct a delay routine using these RAM locations with 3 nestedloops.

    clrf DelayL ; clear DelayL to 0 clrf DelayM ; clear DelayM to 0 movlw 3h ; set DelayH to 3 movwf DelayHWaitHere decfsz DelayL ; subtract 1 from DelayL goto WaitHere ; if not 0, goto WaitHere

    decfsz DelayM ; subtract 1 from DelayM goto WaitHere ; if not 0, goto WaitHere decfsz DelayH ; subtract 1 from DelayH goto WaitHere ; if not 0, goto WaitHere

    http://microprac.exe/http://microprac.exe/http://microprac.exe/http://microprac.exe/
  • 7/22/2019 Curso Assembler Pic 892

    40/154

    My First PIC Projects- Page 39

    In this routine DelayL will get decremented until it

    equals 0 as mentioned before. Then DelayM gets

    decremented, because the first goto WaitHere

    instruction gets skipped. DelayMwill now equal 0xFF,

    so the processor executes the second goto

    WaitHere and starts decrementing DelayL again.This double loop will continue until DelayM equals 0

    and then the second goto WaitHere instruction is

    skipped. DelayHis then decremented and it will equal

    2. The third goto WaitHere will execute because

    DelayH does not equal 0 yet. This triple loop will

    continue until DelayHequals 0 which causes the third

    goto WaitHere instruction to be skipped and then

    the 500mS delay is complete. This routine does notcause an exact delay of 500mS but it is close enough

    for our purposes.

    The next task is to place this delay routine into thecode we have so far.

    Title "mICros Flash Program"

    list p=16f84 ; processor type

    TurnOnLed equ 0x00 ; value to turn on LED with RA4TurnOffLed equ 0x10 ; value to turn off LED with RA4PortA equ 0x05 ; PortA RAM address

    TrisA equ 0x85 ; TRISA RAM addressRA4 equ 0x10 ; PortA RA4 = bit 4Status equ 0x03 ; Status RAM addressRP0 equ 0x05 ; Status RP0 bit = bit 5DelayL equ 0x20 ; delay register LOW byteDelayM equ 0x21 ; delay register MID byteDelayH equ 0x22 ; delay register HIGH byte;; -------------; PROGRAM START; -------------; org 0h ; startup address = 0000

    bsf Status,RP0 ; set RP0 for RAM page 1 bcf TrisA,RA04 ; set RA4 = output bcf Status,RP0 ; set RP0 for RAM page 0

    start movlw TurnOnLed ; turn on LED on RA4 movwf PortA

    clrf DelayL ; clear DelayL to 0 clrf DelayM ; clear DelayM to 0 movlw 3h ; set DelayH to 3

  • 7/22/2019 Curso Assembler Pic 892

    41/154

    My First PIC Projects- Page 40

    movwf DelayHWait1 decfsz DelayL ; subtract 1 from DelayL goto Wait1 ; if not 0, goto Wait1 decfsz DelayM ; subtract 1 from DelayM goto Wait1 ; if not 0, goto Wait1 decfsz DelayH ; subtract 1 from DelayH goto Wait1 ; if not 0, goto Wait1

    movlw TurnOffLed ; turn off LED on RA4 movwf PortA ; turn LED off

    clrf DelayL ; clear DelayL to 0 clrf DelayM ; clear DelayM to 0 movlw 3h ; set DelayH to 3 movwf DelayHWait2 decfsz DelayL ; subtract 1 from DelayL goto Wait2 ; if not 0, goto Wait2 decfsz DelayM ; subtract 1 from DelayM goto Wait2 ; if not 0, goto Wait2 decfsz DelayH ; subtract 1 from DelayH goto Wait2 ; if not 0, goto Wait2

    goto start ; do this loop forever

    end

    Our code now matches the flow chart we created for this program. A label thatdefines TurnOffLedhas been added, and notice how the second 500mS delay

    has a Wait2 label. Remember we cant have 2 labels defined with the same

    name.

    Do you notice the value that was used to define TurnOffLed?

    TurnOffLed equ 0x10 ; value to turn off LED with RA4

    Pin RA4 is pin 4, but TurnOffLedis defined as 10h or 16 decimal. How can this

    be?

    Remember RA4 is pin number 4 from Port A, which can also be said as being bit

    4. When bit 4 is represented in Binary, it becomes 10000, which is 0x10.

    Do you also notice something about the 2 delay routines?

    Apart from a different Label name, they are exactly the same code, and theyboth do exactly the same thing. Wouldnt it be nice to use only one of thesedelay routines instead of two. It would save us some typing and it would alsoconserve memory space inside the PIC. These devices have a limited amount of

    memory, so it is in our best interests to write our code as compact as possible.That way we can do more with the resources we have available.

  • 7/22/2019 Curso Assembler Pic 892

    42/154

    My First PIC Projects- Page 41

    We can change these two routines into a one by turning them into a singleSUBROUTINE. A subroutine is a piece of code that is used many times bydifferent parts of your program. We create these because it becomes wasteful to

    have the same code copied many times as you need it.

    To use a subroutine, we use the instruction CALLfollowed by a label name. Thistells the processor to remember where it is now and jump to the part of memorywhere the subroutine is located. After the subroutine is completed we use the

    RETURN instruction to tell the processor to jump back and continue on from

    where it was before it jumped to the subroutine. You can learn how the PIC does

    this by looking at theMicroPosttutorial software.

    This is how we can turn the delay code into a subroutine. Dont forget to add the/R in the comment on the first code line, which will be explained later.

    Delay500 clrf DelayL ; /R clear DelayL to 0

    clrf DelayM ; clear DelayM to 0 movlw 3h ; set DelayH to 3 movwf DelayHWait1 decfsz DelayL ; subtract 1 from DelayL goto Wait1 ; if not 0, goto Wait1 decfsz DelayM ; subtract 1 from DelayM goto Wait1 ; if not 0, goto Wait1 decfsz DelayH ; subtract 1 from DelayH goto Wait1 ; if not 0, goto Wait1 return ; finished the delay

    As you can see the subroutine is exactly the same as the original code. The onlydifference is a Label called

    Delay500which defines the name of the subroutine,

    and the RETURNinstruction placed at the end. Now each time we need to use a

    500mS delay anywhere in our code all we have to do is use this code line.

    call Delay500 ; execute a 500mS delay

    The delay subroutine will save us 9 code lines each new time we need a delay.We can also use the BSF and BCF instructions to turn the LED on and off which

    will shrink the code by a further 2 lines. We can also change the RA4 label toLED so it is even easier to read.

    start movlw TurnOnLed ; turn on LED on RA4 movwf PortA

    start bcf PortA,LED ; turn on LED on RA4

    If you can find ways to make your code more compact you may find that itoperates much more efficiently and you can fit more code into the chip.

    http://micropost.exe/http://micropost.exe/http://micropost.exe/http://micropost.exe/
  • 7/22/2019 Curso Assembler Pic 892

    43/154

    My First PIC Projects- Page 42

    This is the final version of our LED flash code. We have dispensed with theTurnOnLedand TurnOffLedLabels and added BSF and BCF instructions.

    Title "mICros Flash Program"

    list p=16f84 ; processor type

    ;; The purpose of this program is to make a LED turn on and off; The LED is connected to PortA pin RA0; The flash rate is 500mS;PortA equ 0x05 ; PortA RAM addressLED equ 0x04 ; PortA RA4 = bit 4 for LEDTrisA equ 0x85 ; TRISA RAM addressTrisB equ 0x86 ; TRISB RAM addressStatus equ 0x03 ; Status RAM addressRP0 equ 0x05 ; Status RP0 bit = bit 5DelayL equ 0x20 ; delay register LOW byteDelayM equ 0x21 ; delay register MID byteDelayH equ 0x22 ; delay register HIGH byte

    ;; -------------; PROGRAM START; -------------; org 0h ; startup address = 0000

    bsf Status,RP0 ; set RP0 for RAM page 1 clrf TrisA ; all PortA = outputs clrf TrisB ; all PortB = outputs bcf Status,RP0 ; set RP0 for RAM page 0

    start bcf PortA,LED ; turn on LED on RA4 call Delay500 ; execute a 500mS delay

    bsf PortA,LED ; turn off LED on RA4 call Delay500 ; execute a 500mS delay

    goto start ; do this loop forever;; --------------------------------; SUBROUTINE: waste time for 500mS; --------------------------------;Delay500 clrf DelayL ; /R clear DelayL to 0 clrf DelayM ; clear DelayM to 0 movlw 3h ; set DelayH to 3 movwf DelayHWait1 decfsz DelayL ; subtract 1 from DelayL

    goto Wait1 ; if not 0, goto Wait1 decfsz DelayM ; subtract 1 from DelayM goto Wait1 ; if not 0, goto Wait1 decfsz DelayH ; subtract 1 from DelayH goto Wait1 ; if not 0, goto Wait1 return ; finished the delay

    end

  • 7/22/2019 Curso Assembler Pic 892

    44/154

    My First PIC Projects- Page 43

    You may have noticed that the program made both Port A and Port B pins set asoutputs. If the pins are left as inputs with nothing connected to them they willfloat and may cause damage to the chip.

    Floating inputs can also cause the chip to run erratically. By making them

    outputs, and because we have not written a value to PortA or PortB, the pins willbe set at a random logic 0 or logic 1 level when power is applied to the chip.There is nothing connected to any pin other than RA4 with this project so we willdo no harm.

    This program is available in the software directory and is called flash.asm.

    You can run Mpasmwin again and assemble this code if you like. Then you canuse Notepad to see the listing and HEX files produced.

    There are many more things to learn when using assemblers, such as Macros,defining code blocks, constants and others, which are explained in the help file

    for Mpasmwin or in MPLAB.

    It is very hard to remember everything all at once, so just take your time andlearn these things as you need them. Have a brief look through the help files so

    that you have an idea of what to expect and to get a basic knowledge of itsworkings, directives and error messages.

    MicroPlan Assembler

    The MicroPlan beginners assembler can let you write programs just by using themouse. There is no need to type text other than comments and labels, and youdo not need to assemble the code after you finish creating it. The code is

    assembled as you go.

    To startMicroPlanyou can click on START - Programs - mICros - MicroPlan.

    You may even like to create a short cut.

    To make code entry as easy as possible a small prompt window is includedunder the operand panel on the right side of the screen. This tells you what part

    of the code line the assembler is expecting you to enter. You will also notice that

    some text headings are Red in colour. These either indicate where you canselect the next piece of code data from or which data panel that piece of codewill go to when you enter it.

    All of the basic Labels that are used with a PIC 16F84 are already defined whenyou start MicroPlan so you do not need to create these yourself.

    http://microplan.exe/http://microplan.exe/http://microplan.exe/http://microplan.exe/
  • 7/22/2019 Curso Assembler Pic 892

    45/154

    My First PIC Projects- Page 44

    However, you do need to create new Labels for new RAM locations and ROMaddresses. This is easy to do and will be explained shortly.

    The easiest way to see how this program works is to start writing code, so letsbegin with the program that we wrote with a text editor.

    Press the Newbutton to clear the code window.

    If you remember, this is the first code line from the LED flash program.

    bsf Status,RP0 ; set RP0 for RAM page 1

    The prompt window will have the message Expecting Label or Command. This

    means that the assembler will only accept a Label or an Instruction at this point.

    There is no address label on this line of code, so we need to enter the BSF

    instruction first. You can see that each instruction has its own button on screen,so to add a BSFinstruction all you need to do is click on the button called BSF.

    The BSFinstruction now appears in the Command Panel.

    Notice the Code Construction Panel now has the binary value of the BSF

    instruction inserted into the code word, 01 01bb bfff ffff. The b values

    represent the bit that will be set to Logic 1 (0 - 7), and the f values represent

    the RAM register that will be used. The assembler does not know these valuesyet, because you have not chosen them.

    The prompt window will have the message Expecting RAM Address and the

    RAM Listheading is now a red colour indicating that we can use a label from

    here. Our instruction is bsf Status,RP0, so just click on the STATUS label

    from this list.

    The RAM address label Status now appears in the Operand Panel, and the

    fff ffff in the Code Construction Panel has been replaced with the binary

    value of the Status address which is 3. It now reads 01 01bb b000 0011.

    but the assembler still does not know which bit value to set as yet.

    The prompt window will have the message Expecting Constant and theCONSTANTS heading is now a red colour indicating that we can use a label

    from this list. Notice that the keypad digits 0 - 7 are also highlighted in red,

    indicating that we can also choose the bit value from here. We know that RP0is

    bit 5 in the Statusregister, so we can just click on digit 5 if we wish.

  • 7/22/2019 Curso Assembler Pic 892

    46/154

    My First PIC Projects- Page 45

    It would be nicer to use RP0, so look down the Constants List and click on the

    RP0label. Notice that it has the value 5 defined next to it.

    The prompt window will have the message Expecting OK or Comment and the

    binary number in the Code Construction panel is now complete. The bbb hasbeen replaced with 101and the final instruction value is 01 0110 1000 0011.

    You can now enter the comment set RP0 for RAM page 1into the comment

    box in the centre left of screen. You do not need to include the semi colon (;).

    Now press OK and the code line will be inserted into the main listing.

    The assembler is now waiting for you to add some more lines.

    This is the next line of code.

    clrf TrisA ; set all PortA = outputs

    To enter it click on CLRFand then the TrisALabel in the RAM LIST. After that

    enter the comment set all PortA = outputsand press OK.

    Now you can enter this code line.

    clrf TrisB ; set all PortB = outputs

    To enter it click on CLRFand then the TrisBLabel in the RAM LIST. After that

    enter the comment set all PortB = outputsand press OK. Now you

    should be able to enter this line.

    bcf Status,RP0 ; set RP0 for RAM page 0

    The next code line has an address label followed by an instruction.

    start bcf PortA,LED ; turn on LED on RA4

    At this stage we have not defined the LEDLabel, so we will do that now.

    Scroll down the CONSTANTSlist until you find the next empty location. Click on

    the small E button just above this list and then click on the empty constant

    location. A dialog box will open allowing you to define the name and value of this

    Label. Type 4into the Value box and type LEDinto the Label box, then click OK.

    The new label will be entered into the list.

    Now you are ready to enter the code line.

  • 7/22/2019 Curso Assembler Pic 892

    47/154

    My First PIC Projects- Page 46

    To enter the code line we need to use an address target label called startfirst

    off, so click on the STARTlabel in the Jump Tableand it will then appear in the

    Label Panel. Notice that the Jump Tableheading has now turned a black colour

    meaning we can no longer use these labels anywhere else in this line of code.

    The prompt window will have the message Expecting Command.

    To enter it click on BCFand then the PortALabel in the RAM LIST. Then click

    on the newly created LEDlabel in the CONSTANTSlist and this will be entered

    into the code word. Add the comment turn on LED on RA4, and then press

    OK.

    This is the next code line, but it has a label called Delay500 which has not

    been defined yet.

    call Delay500 ; execute a 500mS delay

    This type of label is for a GOTO or CALL address destination so it must be

    entered into the JUMP TABLE list. To do that, click on the small E button

    above this list, click on the next free location in this list, and enter the label nameDelay500 into the Label box that appears, then click OK. The new label will

    appear in the list.

    Now click on the CALLbutton followed by the new Delay500label, then enter

    the comment execute a 500mS delay, and then click OK.

    What do you notice about the HEX code that was generated? It should read20XX. Notice that the Delay500 label in the Jump Table does not have an

    address associated with it yet. The assembler cannot put a ROM address value

    into the code word yet because there are none listed anywhere in the code, so

    the assembler just leaves it as XXfor now.

    You should now be able to enter these next lines.

    bcf PortA,LED ; turn on LED on RA4 call Delay500 ; execute a 500mS delay goto start ; do this loop forever

    After you have finished, the code window should look like this.

  • 7/22/2019 Curso Assembler Pic 892

    48/154

    My First PIC Projects- Page 47

    If the window does not look like this your code has been entered incorrectly and

    you will have to start again.

    The Labels called DelayL, DelayM, and DelayHhave not been defined yet, so

    click on the small E button above the RAM LISTwindow, then click on the freelocation at address 20h. Enter DelayL into the edit box that appears and click

    OK. Do the same for the other two registers for RAM addresses 21h and 22h,

    which will be called DelayMand DelayH.

    The next code line is the start of the Delay Subroutine.

    Delay500 clrf DelayL ; /R clear DelayL to 0

    Click on the Delay500Label in the JUMP LIST, then click on CLRF, and then

    the newly created DelayL label. After this, enter the comment /R clearDelayL to 0then click on OK. Notice the /Rwritten in the comment. Please

    add this as well.

    After you do this, you should notice that the 20XXvalues have been replaced

    with 2009values. That is because the assembler now knows the ROM address

    of the label called Delay500. This is the value 0009and it has also appeared

    next to the Delay500label in the JUMP LIST. Have a look at the ROM address

    number in the code list where the Delay500subroutine label appears. It too has

    a value of 0009. Each line of code listed in MicroPlan will occupy a ROM

    address inside the PIC chip when it is finally programmed.

    You should now be able to enter these code lines.

    clrf DelayM ; clear DelayM to 0 movlw 3h ; set DelayH to 3 movwf DelayH

  • 7/22/2019 Curso Assembler Pic 892

    49/154

    My First PIC Projects- Page 48

    With this next code line you should know how to create the ROM address labelWait1, and then add the instruction.

    Wait1 decfsz DelayL ; subtract 1 from DelayL

    After you click on the label DelayLin the RAM List, you will notice that the To Fand To Wbuttons will be highlighted in red.

    The prompt window will have the message Expecting Destination.

    Remember that a destination must be specified for this type of instruction. Click

    on To Fbecause you want the result to go back to the RAM register specified. If

    you choose To W, DelayL will not decrement because the result of the

    instruction will always go to the W register.

    Enter the comment, subtract 1 from DelayL, and click OK to finish this

    code line.

    You should now be able to enter these next code lines which will complete theprogram.

    goto Wait1 ; if not 0, goto Wait1 decfsz DelayM ; subtract 1 from DelayM goto Wait1 ; if not 0, goto Wait1 decfsz DelayH ; subtract 1 from DelayH goto Wait1 ; if not 0, goto Wait1 return ; finished the delay

    You do not need an endassembler directive with MicroPlan, so you can now

    save this file with any name you like. It is already available in the softwaredirectory called pflash.bls.

    After you save it, and if there are no ROM address labels missing, MicroPlan will

    generate all the necessary files to program a PIC chip from this code, including asource file. It doesnt produce an error file, because you cannot make a syntaxmistake when you enter your code.

    Please bear in mind that MicroPlan is a beginners assembler only. It does have

    all the functionality to generate real PIC code, but if you are going to developlarge programs, you will find that a text based assembler is a much better option.Please use MicroPlan to get yourself familiar with PIC code by creating smallprograms, and then when you become more confident with writing assembler,

    move into Mpasmwin or MPLAB. These programs will allow you to get muchmore productivity out of your time.

  • 7/22/2019 Curso Assembler Pic 892

    50/154

    My First PIC Projects- Page 49

    Why do you set Port A pin RA4 to Logic 0 to turn the LED on, and Logic 1 to turnit off?

    Pin RA4 is an open collector driver when it is set to an output. These twodrawings explain how it works.

    You can see how the pin can only connect external devices to ground,

    More information about port pins can be found in theMicroPorttutorial.

    If you would like to know more about using MicroPlan, see the help file for thisprogram which is activated from a help button on the main screen.

    The Simulator

    Now that you have your first program completed, should you program a chip andhope that it will work, or should you try to find this out before hand?

    It doesnt matter much if you make a mistake with a 16F84, because you canreprogram the chip, but with some PICs, you cant do this. In this case it pays to

    be sure the code works properly, otherwise you may sacrifice a new chip. Theseparticular chips are One Time Programmable (OTP), and if you program themincorrectly, you will more than likely have to throw them away.

    Another programming problem that arises, is that the software appears to workOK for some of the time and then it fails. This type of problem can be very

    difficult to find, especially when large amounts of code are used for a program.

    A way around this problem is to try your program out in a SIMU