Delay Loops

A Dr. Taylor Tutorial

Delay Loops

        ldi  r16, val
loop:
        dec  r16
        brne loop
                             # of times    # of clocks   Total clocks
        ldi  r16, val            1               1             1
loop:
        dec  r16                 val             1             val
        brne loop                val             2             2*val-1
                             # of times    # of clocks   Total clocks
        ldi  r17, val1           1               1             1
loop2:
        ldi  r16, val2           val1            1             val1
loop1:
        dec  r16                 val2*val1       1             val1*val2
        brne loop1               val2*val1       2             2*val1*val2
        dec  r17                 val1            1             val1
        brne loop2               val1            2             2*val1

The Stack

      ldi  r16, HIGH(RAMEND-0x20)
      out  SPH, r16
      ldi  r16, LOW(RAMEND-0x20)
      out  SPL, r16
      push r0
      push r1
      push r2
      ...
      push r31
      ; call some subroutine
      pop  r31
      ...
      pop  r2
      pop  r1
      pop  r0

Stack Example

.nolist
.include "m32def.inc"
.list

.cseg
.org 0
        rjmp start

.org 0x2a
start:
        ldi  r16, HIGH(RAMEND-0x20)  ; Initialize stack pointer
        out  SPH, r16
        ldi  r16, LOW(RAMEND-0x20)
        out  SPL, r16

        ldi  r20, 0x25               ; Initialize registers r20-r22
        ldi  r21, 0x50
        ldi  r22, 0x75

        push r20                     ; Save r20-r22 register values
        push r21                     ; by placing r20-r22 onto the stack
        push r22

        ; blah blah blah

        pop  r22                     ; Restore r20-r22 registers by
        pop  r21                     ; by popping their values off the stack
        pop  r20
forever:
        rjmp forever                 ; This loop is tight

Tutorials for Other Courses

Additional Links

Site Mirror

This is a mirror to be used when http://myweb.msoe.edu/taylor/, (EDU instead of US) is not available.