Chapter 5 Building Assembler Programs The aims of this chapter are to introduce: how to visualise a program, and represent it diagrammatically; how to.

Slides:



Advertisements
Similar presentations
Getting started with MPLAB Launch MPLAB Set Toolbar as in the next slide by clicking the leftmost icon Open a new Source file by choosing [FILE][NEW] Type.
Advertisements

Dr. Rabie A. Ramadan Al-Azhar University Lecture 3
The Assembly Language Level
Slides created by: Professor Ian G. Harris PIC Development Environment MPLAB IDE integrates all of the tools that we will use 1.Project Manager -Groups.
The 8085 Microprocessor Architecture
Microprocessor and Microcontroller
Microprocessor and Microcontroller Based Systems Instructor: Eng.Moayed N. EL Mobaied The Islamic University of Gaza Faculty of Engineering Electrical.
Prof. Jorge A. Ramón Introducción a Microcontroladores.
TK 2633 Microprocessor & Interfacing Lecture 3: Introduction to 8085 Assembly Language Programming (2) 1 Prepared By: Associate Prof. Dr Masri Ayob.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
Recap – Our First Computer WR System Bus 8 ALU Carry output A B S C OUT F 8 8 To registers’ input/output and clock inputs Sequence of control signal combinations.
Microprocessor and Microcontroller Based Systems Instructor: Eng.Moayed N. EL Mobaied The Islamic University of Gaza Faculty of Engineering Electrical.
Lecture – 5 Assembly Language Programming
Chapter 4 Starting to Program – an Introduction to Assembler The aims of this chapter are to introduce: the essentials of Assembler programming; the Microchip.
1/1/ / faculty of Electrical Engineering eindhoven university of technology Speeding it up Part 2: Pipeline problems & tricks dr.ir. A.C. Verschueren Eindhoven.
9/20/6Lecture 21 -PIC Architecture1 PIC Architecture Instruction Set.
Microcontroller Programming How to make something almost do something else Raffi Krikorian MAS November 2003.
Department of Electronic & Electrical Engineering Embedded system Aims: Introduction to: Hardware. Software Ideas for projects ? Robotics/Control/Sensors.
Instruction Sets and Pipelining Cover basics of instruction set types and fundamental ideas of pipelining Later in the course we will go into more depth.
Building Assembler Programs Chapter Five Dr. Gheith Abandah1.
Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow.
1 4.2 MARIE This is the MARIE architecture shown graphically.
Training program: Mechanic - electrotechnician Program name: Digital processing - microprocessors III. class Microcontrollers Elaborated by: Vlastimil.
Chapter 4 - Implementing Standard Program Structures in 8086 Assembly Language from Microprocessors and Interfacing by Douglas Hall.
Lecture – 4 PIC18 Family Instruction Set 1. Outline Literal instructions. Bit-oriented instructions. Byte-oriented instructions. Program control instructions.
Execution of an instruction
Chapter 2 Introducing the PIC Mid-Range Family and the 16F84A The aims of this chapter are to introduce: The PIC mid-range family, in overview The overall.
 Mini-Computer  Microprocessor  The Brains  Arithmetic Logic Unit (ALU)  Control Unit  Program/ Data Storage  Peripherals (Input/Output) Low-Cost.
Chapter 7 Larger Systems and the PIC 16F873A The aims of this chapter are to introduce: The architecture of the 16F873A microcontroller; The 16F873A memory.
EEE237 Introduction to Microprocessors Week x. SFRs.
Processor Architecture
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
Introduction to Microprocessors - chapter3 1 Chapter 3 The 8085 Microprocessor Architecture.
(Superficial!) Review of Uniprocessor Architecture Parallel Architectures and Related concepts CS 433 Laxmikant Kale University of Illinois at Urbana-Champaign.
Department of Electronic & Electrical Engineering Template program. Input / Output (IO) ● Template program. ● Introduction to IO ● Setting up for IO ●
Chapter 11 Data Acquisition and Manipulation The aims of this chapter are to introduce: the main features of a data acquisition system; the characteristics.
Department of Electronic & Electrical Engineering Lecture 2 ● Introduction to IO ● Using a subroutine ● Driving a 7seg display.
Department of Electronic & Electrical Engineering Introduction to microcontrollers A microcontroller is a small computer on a single integrated circuit.
Chapter 12 Some PIC Microcontroller Advances The aims of this chapter are to introduce: To introduce in overview two microcontrollers which show some enhanced.
Simple ALU How to perform this C language integer operation in the computer C=A+B; ? The arithmetic/logic unit (ALU) of a processor performs integer arithmetic.
Working with Time: Interrupts, Counters and Timers
ICS124 Session 9 Flowcharting 1. By the end of this section the student will be able to:  Name the three structures of the Structure Theorem  Identify.
Department of Electronic & Electrical Engineering Lecture 3. ● Template program. ● Introduction to IO ● PORTA PORTB TRISA TRISB ● Using a subroutine ●
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CPIT Program Execution. Today, general-purpose computers use a set of instructions called a program to process data. A computer executes the.
Programming PIC 16F84A in Assembly. PIC16F84 pin-out and required external components.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
An Introduction to Embedded Systems, and the PIC Microcontroller Lecture 8: Interrupts The aims of this session are to explore the structure and application.
Microprocessor Systems Design I
Microprocessor Systems Design I
Microprocessor Systems Design I
Microprocessor Systems Design I
Microprocessor Systems Design I
PIC 16F877.
16.317: Microprocessor System Design I
Tiny Computers, Hidden Control
EE Embedded Systems Fall 2015
EECE.3170 Microprocessor Systems Design I
Introducing the PIC Mid-Range Family and the 16F84A
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
MARIE: An Introduction to a Simple Computer
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
EGR 2131 Unit 12 Synchronous Sequential Circuits
EECE.3170 Microprocessor Systems Design I
Presentation transcript:

Chapter 5 Building Assembler Programs The aims of this chapter are to introduce: how to visualise a program, and represent it diagrammatically; how to use program branching and subroutines; how to implement delays; how to use look-up tables; logical instructions; how to simplify and optimise Assembler programming; more advanced features of software simulators. 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 is The copyright to all diagrams is held by Microchip Technology, or T. Wilmshurst, unless otherwise stated

Initial Program Design – Flow Diagrams It is easy in Assembler to produce “spaghetti code”, ie code which has no structure, with branches going anywhere, and which is incomprehensible to any but the programmer, and incomprehensible even to him/her after a week. Therefore it is essential to plan a programme structure. While it is now viewed as quite an old technique, flow diagramming is still an appropriate tool to use. Flow diagrams emphasise the linear nature of a programme, and they can be drawn so that they can be readily translated into a programme, BUT they do not in themselves exert good programming discipline.

An Alternative Programme Visualisation - State Diagrams This is an alternative to the flow diagram. In a State Diagram we recognise that the embedded system proceeds through a number of distinct states, and may stay in each state for a considerable period of time. Transition from one state to the next is determined by certain conditions being satisfied. The state diagram is useful for visualising certain types of programme, but not all. It does not translate very directly into code. A simple example of a state machine programme is a washing machine controller, which will move from pre-wash, to wash to rinse states.

Example of a State Diagram: the Washing Machine

Conditional Branching One of the most important features of any microprocessor or microcontroller program is its ability to make “decisions”, i.e. to act differently according to the state of logical variables. Many microprocessors have within their instruction sets a number of instructions which allow them to test a particular bit, and either continue program execution if the condition is not met, or branch to another part of the program if it is. Often these variables are bit values in the Status Registers. The PIC 16 Series microcontrollers have four conditional “skip” instructions. These test for a certain condition, and skip just one instruction if the condition is met, and continue normal program execution if it is not. The most versatile and general-purpose of these are: btfscf,b btfssf,b

;The “main” program starts here movlw 00;clear all bits in port A and B movwf porta movwf portb loopbcfportb, 3;preclear port B, bit 3 btfss porta, 3 bsfportb, 3;but set it if button pressed ; bcfportb, 4;preclear port B, bit 4 btfss porta, 4 bsfportb, 4;but set it if button pressed gotoloop end Testing and Manipulating Single Bits Try Programming Exercises 5.1 and 5.2

Subroutines and the Stack The subroutine is a program section structured in such a way that it can be called from anywhere in the program. Once it has been executed the program continues to execute from wherever it was before. The PIC 16 Series subroutine call and return instructions are call and return. A special return instruction, retlw, is also available. Subroutine Call and Return instructions must always work in pairs. Try Programming Exercise 5.3

Generating Time Delays and Intervals As long as the microcontroller is driven by a stable clock oscillator, we know precisely how long any instruction takes to execute. We can therefore write time delays of very high accuracy.

;Delay of 5ms approx. Instruction cycle time is 5us. delay5 movlw D'200' ;200 cycles called, each taking 5x5=25us movwf delcntr1 del1 nop;1 inst. cycle nop;1 inst. cycle decfsz delcntr1,1;1 inst. cycle, when no skip goto del1;2 inst. cycles return A Delay Subroutine ;500ms delay (approx);100 calls to delay5 delay500 movlw D'100' movwf delcntr2 del2 call delay5 decfsz delcntr2,1 goto del2 return Nested Subroutines for Greater Delay Try Programming Exercise 5.4

Try Programming Exercise 5.5 More Use of the MPLAB Simulator: Breakpoints Once programs become long, it becomes increasingly tedious to step through them when simulating. We need a means of getting them to run through the code that we may not be interested in, but stopping where we need to take a closer look at what is happening. Breakpoints let this happen. In their simplest form, breakpoints allow you to run a program up to a specified instruction. Program execution then stops, and memory and register values can be inspected.

Try Programming Exercises 5.6 and 5.7 More Use of the MPLAB Simulator: Stopwatch A weakness of the software simulator is that it does not run in real time, yet in embedded systems we have a strong desire to understand the timing behaviour of our programs. The Stopwatch facility of the simulator allows accurate time measurements to be simulated. It simply requires that the simulator “knows” what the oscillator frequency is. As it can record the number of instruction cycles executed, it can then calculate time taken.

Try Programming Exercise 5.8 More Use of the MPLAB Simulator: Trace The various windows available in MPSIM give a good picture of the state of the processor status and memory locations at any time, but they do not tell us the history of program execution. Even if program execution has halted at a breakpoint, there may have been a number of program paths for it to go down to reach that point. The Trace function is there to give a record of the recent past of the program execution.

Introducing Logical Instructions So far we have seen a good selection of the 16 series instructions, but have yet to see any logical ones. These instructions, like andwf, andlw, iorwf, or xorwf, perform logical operations between the contents of the W register, and either a literal value, or a value held in a memory location. They do it on a bitwise basis. ;Changes led pattern, using OR and rrf instructions pattern_IOR btfsc flags, 0 goto patt_IOR1;here if first visit to SR bsfflags,0 clrf portb;set up initial output pattern return patt_IOR1 rrf portb,0;here if 2nd or later visit to SR iorlw B' ‘;add another 1 bit to pattern btfsc status,c ;has pattern reached carry flag? clrw ;if yes, clear W movwf portb return Try Programming Exercise 5.9

The instruction movlw allows us to introduce within the program a byte of constant data. We have already seen this in previous programs, for example with the instruction combination: movlw D'100' movwf delcntr2 This is fine for introducing single bytes of data into a program, or just a few. But suppose we want to place in the program a whole list of numbers. Suppose also that we want to be able to record where we are in the list, with some sort of marker. The movlw instruction is then not really up to the job, and we need to apply a way of setting up and accessing a block of data. This is called a look-up table. The PIC 16 Series approach to look-up tables is shown in the next slide. The table is formed as a subroutine. Every byte of data in the table is accompanied by a special instruction, retlw. This instruction is another “return from subroutine”, but with a difference - it requires an 8-bit literal operand. As it implements the subroutine return, it picks up its operand and puts it into the W register. The table is essentially a list of retlw instructions, each with its byte of data. Look-up Tables

Fetching Data from a Look-up Table Try Programming Exercise 5.10

Rule Violation:Scoring 1. Attempting to hit the ball whenYou score when your opponent it is not at your end violates a rule. 2. Not hitting ball when it is atSuccessful Return Hit your end Hitting the ball when it is at your end. Note: Ball is at “your end” when led nearest you is lit. The Ping-pong Program Visualised as a State Diagram

Flow Diagram of Right-to-Left/ Left-to-Right States Try following the ping-pong program simulation tutorial, page 139 End of Lecture Note