Professional Documents
Culture Documents
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
You are now ready to start writing your ASM program code in the file Project_001.asm.
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.
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)
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).
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.
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
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
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)
b'11011111'
; 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
Contact Support: Email: Web: Ph.: Fax: Philip Tallents ptal@picokit.com www.picokit.com 0402 239 363 03 6337 0439