Download presentation
Presentation is loading. Please wait.
Published byJuniper Cooper Modified over 8 years ago
1
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 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
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.
3
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.
4
Example of a State Diagram: the Washing Machine
5
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
6
;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
7
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
8
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.
9
;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
10
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.
11
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.
12
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.
13
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'10000000‘;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
14
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
15
Fetching Data from a Look-up Table Try Programming Exercise 5.10
16
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
17
Flow Diagram of Right-to-Left/ Left-to-Right States Try following the ping-pong program simulation tutorial, page 139 End of Lecture Note
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.