Presentation is loading. Please wait.

Presentation is loading. Please wait.

H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and.

Similar presentations


Presentation on theme: "H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and."— Presentation transcript:

1 H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and infinite loops Looping Mechanisms 1.do statement S forever 2.For i = n1 to n2 do statement S or For i = n2 downto n1 do statement S 3.While C do statement S 4.Repeat statement S until C Program loops are implemented by using the conditional branch instructions and the execution of these instructions depends on the contents of the CCR register.

2 H. Huang Transparency No.2-2 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning

3 H. Huang Transparency No.2-3 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Condition Code Register Four types of branch instructions: -Unary (unconditional) branch: always execute -Simple branches: branch is taken when a specific bit of CCR is in a specific status -Unsigned branches: branches are taken when a comparison or test of unsigned numbers results in a specific combination of CCR bits -Signed branches: branches are taken when a comparison or test of signed quantities are in a specific combination of CCR bits Two categories of branches -Short Branches: in the range of -128 ~ +127 bytes -Long Branches: in the range of 64KB

4 H. Huang Transparency No.2-4 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning

5 H. Huang Transparency No.2-5 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Compare and Test Instructions -Condition flags need to be set up before conditional branch instruction should be executed. -The HCS12 provides a group of instructions for testing the condition flags.

6 H. Huang Transparency No.2-6 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Loop Primitive Instructions -HCS12 provides a group of instructions that either decrement or increment a loop count to determine if the looping should be continued. -The range of the branch is from $80 (-128) to $7F (+127).

7 H. Huang Transparency No.2-7 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Implementation of Looping Constructs for I = n1 to n2 do S n1equxx; starting index n2equyy; ending index … ids.b1; loop index variable … movb#n1,i; initialize i to n1 loopfldaai; check index i cmpa#n2 bgtnext; if i is greater than n2, exit the loop …; performs loop operations …;“ inci; increment loop index braloopf next…

8 H. Huang Transparency No.2-8 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning for i = n2 downto n1 do S n1equxx; starting index n2equyy; ending index … ids.b1; loop index variable … movb#n2,i; initialize i to n1 loopfldaai; check index i cmpa#n2 bltnext; if i is greater than n2, exit the loop …; perform loop operations …;“ deci; increment loop index braloopf next…

9 H. Huang Transparency No.2-9 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning While loop  Loop terminating condition is checked at the start of the loop  In the following example, icount == 0 is the condition to be check.  The update of icount is done by an interrupt service routine (not shown below). Nequxx … icountds.b1 … movb#N,icount wloopldaa#0 cmpaicount beqnext … brawloop next…

10 H. Huang Transparency No.2-10 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Repeat S until C  Often used when a certain operation need to be performed a fixed number of times  The template of this looping construct is as follows: Nequxx … ldy#N; Y is used as the loop counter loopr…; perform the desired operations …;“ dbneY,loopr; decrement the loop counter and decide whether to ; to continue

11 H. Huang Transparency No.2-11 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Example 2.14 Write a program to add an array of N 8-bit numbers and store the sum at memory locations $1000~$1001. Use the For i = n1 to n2 do looping construct. Solution: Nequ20 org$1000 sumds.b2 ids.b1 org$1500 movb#0,i movw#0,sum; sum  0 loopldabi cmpb#N; is i = N? beqdone ldx#array abx ldab0,X; sum  sum + array[i] ldysum;“ aby;“ stysum;“ inci braloop doneswi

12 H. Huang Transparency No.2-12 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning arraydc.b1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 end Example 2.15 Write a program to find the maximum element from an array of N 8-bit elements using the repeat S until C looping construct. Solution:

13 H. Huang Transparency No.2-13 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Nequ20 org$1000 max_valds.b1 org$1500 ldaaarray; set array[0] as the temporary max max staamax_val; “ ldx#array+N-1 ; start from the end of the array ldab#N-1; set loop count to N - 1 loopldaamax_val cmpa0,x bgechk_end ldaa0,x staamax_val; update the array max chk_enddex; move to the next array element dbneb,loop ; finish all the comparison yet? foreverbraforever arraydb1,3,5,6,19,41,53,28,13,42,76,14 db20,54,64,74,29,33,41,45 end


Download ppt "H. Huang Transparency No.2-1 The HCS12/MC9S12 Microcontroller Copyright © 2010 Delmar Cengage Learning Program Loops Types of program loops: finite and."

Similar presentations


Ads by Google