
ECE 4510/5530 Microcontroller Applications Week 2 Lab 2 Preparations Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering College of Engineering and Applied Sciences Lab 2 Elements • Hardware Development – Parallel I/O ports (Chap. 7.5) – LEDs (Chap. 4.11) – Dip switches (Chap. 4.11) – 7-segment display (Chap. 4.11, Chap. 7.6) – Keypad (Chap. 7.9) – Clock-recovery-generator (Chap. 6.6, 6.7) • Software Development Environment – Software control flow (Chap. 2.6, Chap. 5.4) – Function calls (Chap. 5.6) – Software delay loops (Chap. 2.10) ECE 2510 2 SOFTWARE TIME DELAY ECE 3 4510/5530 Creating Time Delays • There are many applications that require the generation of time delays. – Program loops are often used to create a certain amount of delay unless the time delay needs to be very accurate. • The creation of a time delay involves two steps: – Select a sequence of instructions that takes a certain amount of time to execute. (a basic unit) – Repeat the selected instruction sequence for an appropriate number of times. (Delay = Number x basic unit) ECE 2510 4 Program Execution Time • The HCS12 uses the bus clock (E clock) as a timing reference. – The frequency of the E clock is half of that of the onboard clock oscillator (clock, 48 MHz, E-clock, 24 MHz). – Execution times of the instructions are also measured in E clock cycles ECE 2510 5 Program Execution Time • Execution times of each instruction can be obtained from Appendix A of the textbook or from the reference manual – Number of letters in the column “Access Detail” of Appendix A indicates the number of E cycles that a specific instruction takes to execute that particular instruction – For example access detail column of PULA instruction contains three letters UFO which indicates that the PULA instruction takes 3 E cycles to complete ECE 2510 6 Program Execution Time • Speed of the E clock varies from controller to controller – Higher E clock indicates higher processing times – The E clock for the HCS12 is 24 MHz = 41.67ns – The least amount of time HCS12 takes to execute an instruction is 41.67ns – So HCS12 can only be used in applications that require a per instruction computation time of >= 41.67ns • How to Computing Program Execution Time – Count all the E Cycles in the code – Multiply by 41.67 nsec ECE 2510 7 Program Execution Time Example: • The following instruction takes 14E cycles for 1 loop Loop: psha ; 2E cycles pula ; 3E cycles psha ; 2E cycles pula ; 3E cycles nop ; 1E cycle Dbne X,Loop ; 3E cycles condition not satisfied ; 3E cycle condition satisfied – Assume that the HCS12 runs under a crystal oscillator with a frequency of 16 MHz, with an E freq. of 8 MHz. The E-clock period is then 125 ns. – Therefore, each repeated loop would take 14*125=1750ns=1.75us ECE 2510 8 to execute. Total delay 1.75usec x X_value Program Execution Time (2) Example: • Time the following instructions ldx #1234 ; 2E cycles Loop: psha ; 2E cycles pula ; 3E cycles psha ; 2E cycles 14/12 E-clock pula ; 3E cycles dex ; 1E cycle bne Loop ; 3E cycles branch taken ; 1E cycle branch not taken • This requires a slightly different count! X = 1234 ECE 2510 2 + ( (X-1)*14 + (1)*12) E Cycles = 17,276 E Cycles 9 Program Execution Time • Example: find delay created if E = 41.67ns = 1/24 MHz. ldx #$1234 ; 2E Loop1: ldy #$ABCD ; 2E Loop2: dbne Y, Loop2 ; 3E taken/3E not dbne X, Loop1 ; 3E taken/3E not • Total time – Inner loop: (2 + ($ABCD-1)*3 + 3) E Cycles – Assume IL = (($ABCD)*3 )*41.67ns = 43981*3/24 MHz – IL = 0.005497625 sec – Outer loop: (2+$1234*IL+($1234-1)*3 +1) * 41.67ns – Total = ($1234*$ABCD*3 +$1234*3) * 41.67ns ECE 2510 10 – Total = (4660*(43981+1)*3) /24 MHz = 25.629515 sec Program Execution • How many E cycles in 0.001 sec? – 24 MHz * 0.001 sec 24000 E-cycles – Or 8000*3 inner loops. Use $1F40 • How many inner loops needed for 1 sec? – 1 sec/0.001 sec 1000 or $03E8 • If we used these numbers in the last program … – Total = ($03E8*$1F40*3 +$ 03E8 *3) * 41.67ns – Total = (1000*(8000+1)*3) /24 MHz = 1.000125 sec (use Y=>$1F3F and X=>$03E8 instead for 1.00 sec) • Comment: if the average instruction requires 3 E cycles – The machine would average 24/3 8 Mips ECE 2510 11 Program Execution Time Loop: psha ; 2 E cycles pula ; 3 E cycles psha ; 2 E cycles pula ; 3 E cycles psha ; 2 E cycles pula ; 3 E cycles psha ; 2 E cycles 37 3 D 1 37 3 pula ; 3 E cycles Time reg psha ; 2 E cycles 24,000,000 pula ; 3 E cycles psha ; 2 E cycles pula ; 3 E cycles psha ; 2 E cycles pula ; 3 E cycles nop ; 1 E cycle nop ; 1 E cycle dbne d,loop ; 3 E cycles when condition is not satisfied ; 3 E cycles when condition is satisfied ECE 2510 12 Software Delay: Example 2.25 Write a program loop to create a delay of 100 ms, assuming E=125ns. Solution: A delay of 100 ms can be created by repeating the previous loop 20,000 times. The following instruction sequence creates a delay of 100 ms. ldx #20000 ; 2 E cycles loop psha ; 2 E cycles pula ; 3 E cycles psha pula 2 40 Dreg 1 40 psha Timesec pula 8,000,000 MHz psha pula psha 800,000 2 40 Dreg 1 40 pula psha pula Dreg 20,000 $4E20 psha pula nop ; 1 E cycle nop ; 1 E cycle dbne x,loop ; 3 E cycles/3 E cycles ECE 2510 13 (2(ldx)+40*(20000))*E ≈ 20000*E ≈ 20000*125ns ≈ 100ms. Software Time Delays • Time delays created by program loops are not accurate – Some overhead is required to set up the loop count – To reduce the overheads one can adjust the number to be placed in the index registers – By doing so one can achieve closer to the required time delays • Program loops are not advisable to use in applications requiring precise timing calculation – Interrupts may exist that could significantly change time delays • Typically, Microcontroller Timer modules are used in applications requiring precise timing calculations – Enhanced Capture Timer – 8 PWM channels ECE 2510 14 SUBROUTINE & FUNCTION CALLS ECE 15 4510/5530 Subroutines/Functions • Good program design is based on the concept of modularity – Modularity partitioning of a large program into subroutines • Program can be divided into two sections – Main program – Subroutines • Main program contains the logical structure of the algorithm • Subroutines Execute many of the details involved in the program ECE 2510 16 Subroutine Hierarchy • Structure of a modular program can be visualized as shown • Principles of program design involved in high-level languages can be applied to assembly language programs – Subroutine “objects” with calls and returns ECE 2510 17 Subroutines • Subroutine is a sequence of instructions that can be called from many different places in a program • A key issue in a subroutine call is to make sure that the program execution returns to the point immediately after the subroutine call after the subroutine completes its computations. • The address for returning to the point immediately after the subroutine completes its computation is known as the “return address” • This is normally achieved by saving and retrieving the return address in and from the stack ECE 2510 18 Subroutine Program Flow • The subroutine call (2) saves the return address, which is the address of the instruction immediately following the subroutine call instruction, in the stack system • After completing the subroutine computation task (3), the subroutine will return to the instruction immediately following the instruction that made the call (4) • This is achieved by executing a return instruction (4), which will retrieve the return address from the stack and transfer CPU control to it ECE 2510 19 Issues in Subroutine Calls • Parameter passing • Local variables allocation – Use registers – Allocated by the callee – Use the stack or stack frame – Use the following instruction is – Use global memory the most efficient way for local variable allocation – leas -n,sp ; • allocate n bytes in the stack for local variables • Returning results • Local variables deallocation – Use registers – Performed by the subroutine – Use the stack frame – Use the following instruction is (caller created a hole in which the most efficient way the result will be placed) – Leas n,sp ; – Use global memory • deallocate n bytes from the stack ECE 2510 HCS12 Assembly Language Subroutines Instructions • HCS12 provides three different instructions for making subroutine calls and two different instructions for returning from a subroutine • Subroutine calls –BSR Branch Subroutine –JSR Jump Subroutine – Call Call a Subroutine • Return Instructions –RTS Return from Subroutine –RTC Return from Call ECE 2510 21 Jump and Subroutine Table Mnemonic Function Operation BSR Branch to subroutine SP – 2 SP RTNH : RTNL M(SP) : M(SP+1) Push PC PC + Rel offset PC CALL Call subroutine in expanded SP – 2 SP memory RTNH:RTNL M(SP) : M(SP+1) Push PC SP – 1 SP (PPAGE) M(SP) Push PPAGE Page PPAGE Subroutine address PC JMP Jump Address PC JSR Jump to subroutine SP – 2 SP RTNH : RTNL M(SP) : M(SP+1) Push PC Subroutine address PC RTC Return from call M(SP) PPAGE SP + 1 SP Pull PPAGE M(SP) : M(SP+1) PCH : PCL SP + 2 SP Pull PC RTS Return from subroutine M(SP) : M(SP+1) PCH : PCL SP + 2 SP Pull PC The return address is ECE 2510 stored on the stack! 22 C Language Function Calls • Function prototype in included header file or “main” code typc func_name(type variable_to_pass); • Compose the function (included with main or in another *.c) typc func_name(type argiments_to_pass) { … return(typed_return_value); } • C does the rest automatically ….
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages88 Page
-
File Size-