Download presentation
Presentation is loading. Please wait.
Published byPatience Nash Modified over 8 years ago
1
© Mike Stacey 2008 Single Chip Microcontrollers 7765J Mount Druitt College of TAFE Lesson 6 Instruction Timing and Control Loops
2
© Mike Stacey 2008 Instruction timing [1] One of the more important subroutines you will use is that which gives a time delay in a program. You often need to have these delays to wait for an event to occur. For example, you may need to wait for a user to respond to a prompt, or you may need to wait, in real time, for a slower device to provide input. The microcontroller operates at very high speed, performing millions of instructions per second. While this is advantageous in most applications it does not serve the control of slower equipment very well. A microprocessor relies on its clock to synchronise its operations. All control signals are derived from the clock, and the processor generates most of them. The speed of operation of the system is therefore directly dependent on the clock. The time taken to perform a particular operation, such as a memory access, depends on the number of actions required to execute the instruction. The following terms are used to describe the timing of an instruction.
3
© Mike Stacey 2008 Instruction timing [2] Instruction Cycle This is the total time taken to fetch, decode and execute an instruction. An instruction cycle is divided into a number of specific steps, generally referred to as machine cycles. Machine Cycle This is the time required to complete one specific operation; such as reading from or writing to memory, accessing input or output ports, or acknowledging an external request. Each machine cycle consists of a number of clock cycles. Clock Cycle A clock cycle is the smallest subdivision of a machine cycle and is usually the same frequency, or period, as the system clock.
4
© Mike Stacey 2008 Calculating the time taken to execute an instruction To determine how long a group of instructions takes to execute we must refer to the manufacturer’s data sheets for the uC or look in an appropriate textbook to ascertain the number of clock cycles each instruction requires. We then multiply this number by the period of the clock. When we know how long each instruction execution takes we can add the duration of each instruction to find the total time
5
© Mike Stacey 2008 Simple delay wait: ldi temp,$ff mov countreg, temp delay_1: dec countreg cpi countreg, $00 brne delay_1 ret How many clock cycles? 1 1 2 if true 1 Totals: rcall:3 wait: ldi:1 mov1 delay_1: dec1 cpi1 brne2 - 4 per loop * 254 true loops and 1 false loop ret4 3 + 1 + 1 + (4 * 254) + 1 + 4 = 1026 For a clock frequency of 8MHz, that’s (1/8 * 10 6 ) * 1026 = 0.13mS Also need an rcall wait 3 1
6
© Mike Stacey 2008 How to get a longer delay? Insert the previous code into another loop A Nested Loop
7
© Mike Stacey 2008 Nested loops for longer delays delay: ldi temp, $ff mov temp_outer, temp delay_outer: mov countreg_inner, temp delay_inner: dec countreg_inner cpi countreg_inner, $00 brne delay_inner dec countreg_outer cpi countreg_outer, $00 brne delay_outer ret inner loop outer loop Total Clock Cycles: delay: ldi: 1 mov: 1 delay_outer: mov: 1 delay_inner dec: 1 cpi: 1 brne: 2 if true, 1 if false dec: 1 cpi: 1 brne: 2 if true, 1 if false ret: 4 -What is the total? -What is the delay time for an 8MHz clock? ((4*254)+1) + (((1+1+1+2)*254)+1) + 1 + 1 + 3 + 4
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.