You are on page 1of 8

Learning Microchip Lesson 1-1

MPLAB is the best software to learn for programming Microchip PICs because it is designed and used by the same people who make them. These tutorials will help you step by step so that you get a good foundation for a future in using Microchip. Please follow each step closely, and when you feel like you understand something, then by all means test this out in MPLAB and change the code to your own liking. Good programmers always experiment And persistence really pays off!

Contents
Installing the MPLAB Software ............................................................................................................... 2 Creating a Project in MPLAB ................................................................................................................... 2 Programming your first ASM file (for the PIC16F505 device) ................................................................. 3 What to Write (OPCODE Instructions): ............................................................................................... 3 Where to Write (File Registers): ......................................................................................................... 3 How to Write (OPCODE Structure and Format): ................................................................................. 5 Bit-oriented Structure and Examples:............................................................................................. 5 Byte-oriented Structure and Examples: .......................................................................................... 5 Numerical Values: ........................................................................................................................... 5 OPCODE Example (to be placed in the .asm file) ............................................................................ 6 Some Last Things before Compiling .................................................................................................... 6 Generate the HEX file (Compiling) ...................................................................................................... 7 Write the HEX file with PicoFlow LT........................................................................................................ 7 Tips for Successful Programming: ........................................................................................................... 8

Philip Tallents 2011

Installing the MPLAB Software


1. Extract the zip file to a suitable location 2. Run setup (for MPLAB Tools installation) 3. Answer the Installation questions 4. A few minutes pass during installation 5. Select NO for Hi-TECH C installer 6. Finish 7. Windows Security Would you like to install this device software Install 8. Close the MPLAB IDE Document Select

Creating a Project in MPLAB


1. Open the software MPLAB IDE in your start menu 2. Create a new project Project->Project Wizard->Next I. Select Device: Device=PIC16F505 II. Language ToolSuite: MPASM Assembler -> Next III. Create new project file: C:\My First Project\Project_001.mcp -> OK IV. Add existing files to your project: C:\Program Files (x86)\Microchip\MPASM Suite\P16F505.INC ->Add->Next V. Summary -> Finish Your Project Tree will now have a folder structure and one file under Header Files. 3. Create an ASM Source File I. File -> Add New File to Project -> Project_001.asm -> Save

You are now ready to start writing your ASM program code in the file Project_001.asm.

Philip Tallents 2011

So far you have installed the MPLAB software for writing your code, and now we will consider a few things about Microchip devices and the ASM (Assembly) language.

Programming your first ASM file (for the PIC16F505 device)


A simple program is to turn on one LED. We might call this Hello World (or No.5 is Alive for 80s movie fans). But to do this we need to know what to write, where to right it to, how to write, and some other stuff.

What to Write (OPCODE Instructions):


There are 33 instructions we can use for the 16F505 but to turn on (or off) a LED we should only need 2. o BSF (Bit Set in File) or BCF (Bit Clear in File)

All microchips work in binary code (1 or 0) where 1 is Set and a 0 is Clear. Changing an individual bit or a collection of bits together is how we control the device. So technically, BSF & BCF are the only two instruction ever required, but often you will want to change (and read) more than 1 bit at a time. So this tutorial requires four more instructions to learn; these are byte-oriented meaning they modify a collection of 8 bits of memory. o MOVLW (Move Literal to Working) o MOVWF (Move Working to File register) o OPTION (Move Working to OPTION register) o TRIS (Move Working to TRISB & TRISC register)

Where to Write (File Registers):


The PIC16F505 uses a collection of 8 bits in every data File. In Microchip lingo, this is also termed a Register or Register File. So we also need to know the File that houses each of the Bits that we should be setting and clearing. Microchip uses a Register File Map to indicate all memory locations. We will use the PORTB & PORTC registers which have addresses (but the W, OPTION & TRISB & TISC registers dont have physical addresses).

Philip Tallents 2011

The Register Files we are interested in right now are called SFRs (Special Function Registers) but there are also GPRs (General Purpose Registers). Microchip uses another table to indicate the 8 bits within each of the SFR registers as shown below.

Here we can see the PORTB, PORTC, TRISB, TRISC and OPTION registers and their associative bits. Note, the W register is not listed in this table, but it is shown in the PIC16F505 Block Diagram which can be found on page 12 of the Data Sheet from Microchip. There are two steps needed to make a pin turn on a LED. The 1st is to configure the pin as an Output because it is already an Input by default. This is done by modifying the TRISB or TRISC (Tri-State) Register Files where a 1=Input and 0=Output. The 2nd is to modify the Port Output of the pin to a 1=High or 0=Low by modifying the PORTB or PORTC register files. These bits in the TRIS and PORT registers directly control the microelectronic circuit that drives the associated I/O pins. This

configuration allows each I/O pin to be either of 3 states: 1. Output=High to VDD 2. Output=Low to VSS 3. Input=Read to RD a floating voltage).

Philip Tallents 2011

To find out which pins we look at Microchips Pin Diagram for the PIC1 6F505. Notice pins RB5, RB4, RB3, RB2, RB1, RB0 are physical pins 2, 3, 4, 11, 12, 13 respectively. Also, note the arrows on the diagram for the PORT pins - RB3s arrow indicates it is an Input Only pin, so it cant be changed to an output with TRIS.

We will also modify the OPTION register to de-associate physical pin 2 from CLKIN.

How to Write (OPCODE Structure and Format):


Every Instruction OPCODE (e.g. BSF, BCF, MOVLW, MOVWF, TRIS & OPTION) expects a specific structure. Generally, all Bit-oriented (such as BSF & BCF) have the same structure, as do most Byte-oriented (such as MOVLW, MOVWF & TRIS).
Bit-oriented Structure and Examples:

Tab OPCODE Tab Tab FILE comma BIT Tab Tab Semi-colon Tab Comment BSF BCF PORTB, 5 PORTB, 2 ; Sets (to +ve) pin 5 of the PORT/file ; Clears (to gnd) pin 2 of the PORT/file

Byte-oriented Structure and Examples:

Tab OPCODE Tab Tab FILE/LITERAL Tab Tab Semi-colon Tab Comment MOVWF MOVLW TRIS OPTION PORTB ; Moves 8 bits of (W)orking register to PORTB

b'00000000' ; Moves 8 bits of 0 into (W)orking register PORTB ; Copies W register into TRISB register ; Copies W register into OPTION register

The main difference is that Bit-oriented instructions need a comma and bit number directly after the register name, but Byte-oriented instructions dont.
Numerical Values:

You may be wondering what b'00000000' means above. This is one way of writing a numerical (or literal) value. Remember, all register files on the PIC16F505 have 8

Philip Tallents 2011

bits hence it is called an 8-bit device. These 8-bit registers can be represented in numerous ways; in Decimal (0 to 255), in Hexadecimal (0 to FF), or in Binary (00000000 to 11111111). Our example uses binary b'00000000' because we are interested in the individual bits of the file. For the above example we could have also written 0h or 0x00 to represent Hexadecimal or plainly written 0 or .0 to represent Decimal. But no matter which way you represent it, they are all the same value to the microcontroller. For bit-oriented registers such as TRIS, OPTION, and PORT it is beneficial to use Binary because every bit in the register file corresponds to the individual option (eg. the bits in PORTB, PORTC, TRISB and TRISC represent the IO pins, and the bits in OPTION represent 8 individual settings; For mathematical calculations or counting it is beneficial to use Decimals because it is easier to understand; And for addressing memory it is beneficial to use Hexadecimal values to correspond with the Microchip graphs and tables in the specification document.
OPCODE Example (to be placed in the .asm file)

MOVLW OPTION MOVLW TRIS MOVLW MOVWF BSF

b'11011111'

b'11111000' PORTB b'00000000' PORTB PORTB, 2

; Binary value moved into the W register to ; clear T0CS to turn off CLKIN in OPTION reg. ; Binary value moved into the W register to ; select RB2, 1 , 0 as Outputs in TRISB reg. ; ; Initialise the PORTB with all LEDs off ; Turns On a LED at RB2 for PicoLights

Some Last Things before Compiling


Three necessary statements to include in your *.asm code before compiling. 1. Add the statement #include <P16F505.INC> to the start of your .asm file. This will tell MPLAB to include the Header File that you specified when you Created your Project file. (Nb. The INC file helps with Register and Bit names) 2. Add the statement __CONFIG _IntRC_OSC & _WDT_OFF & _MCLRE_OFF just after the #include. This will configure the PIC16F505 to use an Internal Resistor/Capacitor Oscillator, NO Watchdog Timer & NO Master Clear Reset. 3. Add the statement end to the end of your .asm file. This will tell MPLAB where the end of your code is.

Philip Tallents 2011

Generate the HEX file (Compiling)


Now that you have written you first program in ASM code using MPLAB, we need to build the HEX file. To build the file all we need to do is press F10. You should see the Output window appear with the message BUILD SUCCEEDED. This means the hex file has been generated correctly and is now ready for programming. If you see the message BUILD FAILED then look through the build output to find Warning and Error messages. Double-clicking the message will take you to the corresponding error. Fix it where possible or seek help from another source.

Write the HEX file with PicoFlow LT


With a successfully built HEX file you can now program it to your kit for testing. 1. Open the PicoFlow LT software (dont close MPLAB because you may want to modify your HEX file later). 2. Change to the Load Hex File tab 3. Select device PIC16F505 4. Press Load HEX 5. Browse to your Project Directory & select the HEX file you created in MPLAB I. The window will close and you should see the HEX file data loaded 6. Change to the Programming tab 7. Now program your project as normal press Write Device Once you have completed these steps to Load your HEX file, the software will remember where your file is located and update itself with your code every time you press Write Device. So then Writing to the Device becomes as easy as 1-2-3. 1. Make your OPCODE adjustments in MPLAB 2. Press F10 in MPLAB to rebuild the HEX file 3. Press Write Device in PicoFlow LT But dont forget to save your work regularly.

Philip Tallents 2011

Tips for Successful Programming:


1. Learn the Acronyms: What is BSF? It is the Bit Set in File instruction. When you know the acronym then you have a clue to the function. Microchip uses acronyms everywhere and if you dont know them then you wont understand them. 2. Once you get it partly working, copy the entire project folder as a draft. This is extremely important: I cant suggest this enough! Follow this advice and then you can always go back to a working copy, even if you really screw things up I know I have many times. 3. Always modify little and test frequently. Even with this tutorial, test one thing at a time. Dont write lots of code because all youll get is lots of errors. 4. Experiment! Try it! Change it! Make it Yours! Youll only ever work it out for yourself if you use what you learn: and use it in various ways! 5. Start to read the Data Sheet. I know Its difficult But skip to the bits you can understand and use the contents and index for searching. Bit by bit you will learn more and if you dont then please ask someone for help try Google, microchip forums, Microchip, or even me at PicoKit. 6. If you havent realised, all yellow highlighted text in this tutorial can be copied directly into an .asm file to be compiled into a HEX file. Hope this helps. 7. Lastly, if you find Microchips MPLAB too difficult for programming, then please consider our soon to be released flowchart software PicoFlow Alpha from PicoKit.

Contact Support: Email: Web: Ph.: Fax: Philip Tallents ptal@picokit.com www.picokit.com 0402 239 363 03 6337 0439

Philip Tallents 2011

You might also like