Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Starting to Program – an Introduction to Assembler The aims of this chapter are to introduce: the essentials of Assembler programming; the Microchip.

Similar presentations


Presentation on theme: "Chapter 4 Starting to Program – an Introduction to Assembler The aims of this chapter are to introduce: the essentials of Assembler programming; the Microchip."— Presentation transcript:

1 Chapter 4 Starting to Program – an Introduction to Assembler The aims of this chapter are to introduce: the essentials of Assembler programming; the Microchip MPLAB TM Integrated Development Environment; the PIC 16 Series instruction set, and the use of certain instructions; program simulation, and the MPLAB software simulator MPSIM TM ; program download to a target system. Designing Embedded Systems with PIC Microcontrollers: Principles and Applications 2 nd Edition. Tim Wilmshurst Instructors using Designing Embedded Systems with PIC Microcontrollers are welcome to use or change these slides as they see fit. Feedback, to t.j.wilmshurst@derby.ac.uk, is welcomed.t.j.wilmshurst@derby.ac.uk The copyright to all diagrams is held by Microchip Technology, or T. Wilmshurst, unless otherwise stated

2 The Problem of Programming, and Programming Options What options do we have for programming a microcontroller? Use Machine Code Use High Level Language Use Assembler 11010010 01010010 11010101 01001011 01100011.... Shall I compare thee to a summer day........?

3 The Assembler Option

4 Developing a Simple Project Write/modify Source Code Assemble/Compile (Simulate) Download Test in Hardware

5 The PIC 16 Series ALU f for file (i.e. memory location in RAM), a 7-bit number; b for bit, to be found within a file also specified, a single bit; d for destination, as described above, a single bit; k for literal, an 8-bit number if data, or 11-bit if address

6 The PIC 16 Series Instruction Set

7 clrw clears the value in the W register to zero. There are no operands to specify. Column 5 tells us that the Status register Z bit is affected by the instruction. As the result of this instruction is always zero, the bit is always set to 1. No other Status register bits are affected. clrf f clears the value of a memory location, symbolised as f. It is up to the programmer to specify a value for f, which needs to be a valid memory address. Again, because the result is zero, the Status register Z bit is affected. addwf f,d adds the contents of the W register to the contents of a memory location symbolised by f. There is a choice of where the result is placed, determined by the value of the operand bit d. Because of the different values that the result can take, all three condition code bits, i.e. Z, C, and DC are affected by the instruction. addlw k adds a literal, i.e. an 8-bit number written into the program and represented by k, to the value held in the W register. Like the addwf instruction, The Z, C, and DC Status register bits can all be affected by the instruction. bcf f,b clears a single bit in a memory location, symbolised by f. The bit number b will take a value from 0 to 7, to identify any one of the 8 bits in a memory location. No Status register flags are affected, even though it is possible to imagine that the result of the instruction could be to set a memory location to zero. goto k This instruction causes the program execution to jump to another point in the program, whose address is given by the constant k. It is up to the programmer to give a value for k. No Status bits are affected. Some Example Instructions

8 Writing in Assembler label instruction operand(s) ;comment Assembler format: ;now switch on red led Start bsf status,5 ;select memory bank 1 addwf counter For example: labelcomment operands commentLeft-most space optional instruction

9 Assembler Directives These look like Assembler mnemonics, but are instructions to the (Cross-) Assembler program itself. They differ from one Assembler to the other, though there does tend to be some similarity. Example MPASM Directives Assembler DirectiveSummary of Action listimplement a listing option* #includeinclude additional source file orgset program origin equdefine an assembly constant; this allows us to assign a value to a label endend program block

10 Representing Numbers RadixExample Representation Decimal D‘255’ Hexadecimal H‘8d’ or Ox8d Octal O‘574’ Binary B‘01011100’ ASCII ‘G’ or A‘G’

11 Assembler File Structure (Simple Form) For us, this will be MPLAB Written by you, as a text file, in Assembler format Files that the Assembler (e.g. MPLAB) generates Source File Assembler Executable File List File Error File.asm.hex.lst.err

12 ;****************************************************************** ;Very first program ;This program repeatedly adds a number to the Working Register. ;TJW 1.11.08 Tested 1.11.08 ;****************************************************************** ; ; use the org directive to force program start at reset vector org 00 ;program starts here clrw ;clear W register loop addlw 08 ;add the number 8 to W register goto loop end ;show end of program with "end" directive A First Program

13 Introducing MPLAB Continue here with the MPLAB tutorial from page 86 of the book.

14 A Larger Program – Using Data Memory and Moving Data ;***************************************************************************** ;Fibo_simple ;In a Fibonacci series each number is the sum of the two previous ones, ;e.g. 0,1,1,2,3,5,8,13,21.... ;This program calculates Fibonacci numbers within an 8-bit range. ;Program intended for simulation only, hence no input/output. ;TJW 6.11.08 Tested by simulation 6.11.08 ;**************************************************************************** ;these memory locations hold the Fibonacci series Fib0equ 20 ;lowest number fib1 equ 21 ;middle number fib2 equ 22 ;highest number fibtemp equ 23 ;temporary location for newest number org 00 ;preload initial values clrf fib0;clear location fib0 movlw 1;move value 1 to W register movwf fib1;move W register to fib1 movwf fib2;move W register to fib2 ; forward movf fib1,0 ;move the contents of fib1 to W register addwf fib2,0;add W reg to fib2 movwf fibtemp;move new number formed to fibtemp ;now shuffle numbers held, discarding the oldest (ie fib0) movf fib1,0 ;move fib1 to W register movwf fib0;move W register to fib0 movf fib2,0;move fib2 to W register movwf fib1;move W register to fib1 movf fibtemp,0 ;move fibtemp to W register movwf fib2;move W register to fib2 goto forward end

15 ;************************************************************** ;ELECTRONIC PING-PONG DATA MOVE ;This program moves push button switch values from Port A to the ;leds on Port B TJW 21.2.05 ;************************************************************** ; ;Configuration Word: WDT off, power-up timer on, code protect off, RC oscillator ; list p=16F84A ;specify SFRs status equ 03 porta equ 05 trisa equ 05 portb equ 06 trisb equ 06 ; org 00 ;Initialise startbsfstatus,5 ;select memory bank 1 movlwB'00011000' movwftrisa;port A according to above pattern movlw00 movwftrisb;all port B bits output bcfstatus,5 ;select bank 0 ; ;The “main” program starts here clrf porta;clear all bits in ports A loopmovfporta,0;move port A to W register movwfportb;move W register to port B gotoloop end Opening explanatory comments SFR locations specified, by equating labels to numbers, taken directly from RAM memory map Initialisation Actual Program Program start address specified Directive specifying processor A Simple Data Transfer Program

16 Downloading to a Microcontroller It is possible to program the memory of the microcontroller either by removing it, and placing it in a dedicated programmer such as the PICstart; or (if the right connections are made, and the microcontroller has the capability) by connecting an in-circuit programmer, such as the PICkit 2. Both PICstart, and PICkit 2, connect back to a PC running MPLAB.

17 Instruction Formats of PIC Mid-Range Microcontrollers End of Lecture Note


Download ppt "Chapter 4 Starting to Program – an Introduction to Assembler The aims of this chapter are to introduce: the essentials of Assembler programming; the Microchip."

Similar presentations


Ads by Google