EE 319 K Introduction to Microcontrollers Lecture 5

  • Slides: 21
Download presentation
EE 319 K Introduction to Microcontrollers Lecture 5: Conditionals, Loops, Modular Programming, Subroutines, Parameter

EE 319 K Introduction to Microcontrollers Lecture 5: Conditionals, Loops, Modular Programming, Subroutines, Parameter passing 5 -1

Conditionals > < ≥ ≤ conditional branch instructions must follow a subtract compare or

Conditionals > < ≥ ≤ conditional branch instructions must follow a subtract compare or test instruction, such as suba subb sbca sbcb subd cba cmpb cpd cpx cpy tsta tstb tst Equality/Inequality check Ramesh Yerraballi 5 -2

Signed Conditional bge target ; Branch if signed greater than or equal to (

Signed Conditional bge target ; Branch if signed greater than or equal to ( ), ; if (N^V)=0, or (~N. V+N. ~V)=0 bgt target ; Branch if signed greater than (>), ; if (Z+N^V)=0, or (Z+~N. V+N. ~V)=0 ble target ; Branch if signed less than or equal to ( ), ; if (Z+N^V)=1, or (Z+~N. V+N. ~V)=1 blt target ; Branch if signed less than (<), ; if (N^V)=1, or (~N. V+N. ~V)=1 Ramesh Yerraballi 5 -3

… Signed Conditional , <, , > checks Ramesh Yerraballi 5 -4

… Signed Conditional , <, , > checks Ramesh Yerraballi 5 -4

Unsigned Conditional bhs target ; Branch if unsigned greater than or equal to (

Unsigned Conditional bhs target ; Branch if unsigned greater than or equal to ( ), ; if C=0, same as bcc bhi target ; Branch if unsigned greater than (>), ; if C+Z=0 bls target ; Branch if unsigned less than or equal to ( ), ; if C+Z=1 blo target ; Branch if unsigned less than (<), ; if C=1, same as bcs Ramesh Yerraballi 5 -5

… Unsigned Conditional , <, , > checks Ramesh Yerraballi 5 -6

… Unsigned Conditional , <, , > checks Ramesh Yerraballi 5 -6

Problem Solving When we solve problems on the computer, we need to answer these

Problem Solving When we solve problems on the computer, we need to answer these questions: What does being in a state mean? List state parameters What is the starting state of the system? Define the initial state What information do we need to collect? List the input data What information do we need to generate? List the output data How do we move from one state to another? Actions we could do What is the desired ending state? Define the ultimate goal Ramesh Yerraballi 5 -7

Successive Refinement Start with a task and decompose the task into a set of

Successive Refinement Start with a task and decompose the task into a set of simpler subtasks Subtasks are decomposed into even simpler sub-subtasks. Each subtask is simpler than the task itself. Make design decisions Subtask is so simple, it can be converted to software code. Ramesh Yerraballi 5 -8

Decomposition “do A then do B” “do A and B in either order” “if

Decomposition “do A then do B” “do A and B in either order” “if A, then do B” “for each A, do B” “do A until B” “repeat A over & over forever” “on external event do B” “every t msec do B” Ramesh Yerraballi → sequential → conditional → iterative (condition always true) → interrupt 5 -9

Successive Refinement: Example Ramesh Yerraballi 5 -10

Successive Refinement: Example Ramesh Yerraballi 5 -10

if-then-else Two alternative implementations Ramesh Yerraballi 5 -11

if-then-else Two alternative implementations Ramesh Yerraballi 5 -11

while loop Ramesh Yerraballi 5 -12

while loop Ramesh Yerraballi 5 -12

for loop Ramesh Yerraballi 5 -13

for loop Ramesh Yerraballi 5 -13

Modular Design Goal Clarity Create a complex system from simple parts Definition of modularity

Modular Design Goal Clarity Create a complex system from simple parts Definition of modularity Maximize number of modules Minimize bandwidth between them Entry point (where to start) The label of the first instruction of the subroutine Exit point (where to end) The rts instruction Good practice, one rts as the last line Ramesh Yerraballi 5 -14

Modular Design Public (shared, called by other modules) Add underline in the name, module

Modular Design Public (shared, called by other modules) Add underline in the name, module name before Private (not shared, called only within this module) No underline in the name Helper functions Coupling (amount of interaction between modules) Data passed from one to another (bandwidth) Synchronization between modules Ramesh Yerraballi 5 -15

Subroutines and the Stack classical definition of the stack push saves data on the

Subroutines and the Stack classical definition of the stack push saves data on the top of the stack, pull removes data from the top of the stack implements last in first out (LIFO) behavior stack pointer (SP) points to top element many uses of the stack temporary calculations subroutine (function) return addresses subroutine (function) parameters local variables Ramesh Yerraballi psha push Register A on the stack pshb push Register B on the stack pshx push Register X on the stack pshy push Register Y on the stack des S=S-1(reserve space) pula pull from stack into A pulb pull from stack into B pulx pull from stack into X puly pull from stack into Y ins S=S+1 (discard top of stack) 5 -16

Registers to pass parameters High level program 1) Sets Registers to contain inputs 2)

Registers to pass parameters High level program 1) Sets Registers to contain inputs 2) Calls subroutine 6) Registers contain outputs Ramesh Yerraballi Subroutine 3) Sees the inputs in registers 4) Performs the action of the subroutine 5) Places the outputs in registers 5 -17

Introduction to Pointers are addresses pointing to objects. The objects may be data, functions,

Introduction to Pointers are addresses pointing to objects. The objects may be data, functions, or other pointers: • If register X or Y contains an address we say it points into memory Ramesh Yerraballi 5 -18

Use of Index registers Ramesh Yerraballi 5 -19

Use of Index registers Ramesh Yerraballi 5 -19

Functional Debugging Instrumentation: dump into array without filtering Assume happy is strategic 8 -bit

Functional Debugging Instrumentation: dump into array without filtering Assume happy is strategic 8 -bit variable. SIZE Buf Pt equ 20 rmb SIZE rmb 2 #define SIZE 20 unsigned char Buf[SIZE]; unsigned char *Pt; Pt will point into the buffer. Pt must be initialized to point to the beginning, before the debugging begins. ldx #Buf stx Pt Ramesh Yerraballi Pt = Buf; 5 -20

… Functional Debugging The debugging instrument saves the strategic variable into the Buffer. Save

… Functional Debugging The debugging instrument saves the strategic variable into the Buffer. Save pshb pshx ldx cpx bhs ldab stab inx stx done pulx pulb rts ; save Pt ; X=>Buf #Buf+SIZE done ; skip if full happy 0, X ; save happy ; next address Pt void Save(void){ if(Pt < &Buf[SIZE]){ (*Pt) = happy; Pt++; } } Next, you add jsr Save statements at strategic places within the system. Use the debugger to display the results after program is done Ramesh Yerraballi 5 -21