Calculating max delay using delay stacks @ 1 MHz clock frequency (answer apparently is 49.94 s)

Go To Last Post
4 posts / 0 new
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

;Program to sequence LEDs on port C using a delay subroutine

;Status Register Address
.equ     SREG   =$3F              ;Status Register Address

;Stack and Stack Pointer Addresses
.equ     SPH    =$3E              ;High Byte Stack Pointer Address
.equ     SPL    =$3D              ;Low Byte Stack Pointer Address
.equ     RAMEND =$25F             ;Stack Address

;Port Addresses
.equ     PORTC  =$15              ;Port C Output Address
.equ     DDRC   =$14              ;Port C Data Direction Register Address

;Register Definitions
.def     leds   =r0               ;Register to store data for LEDs
.def     temp   =r16              ;Temporary storage register
.def     save   =r19              ;Temporary storage register for status register
.def     YL     =r28              ;Define low byte of Y
.def     YH     =r29              ;Define high byte of Y

;Program Initialisation
;Set stack pointer to end of memory
         ldi    temp,high(RAMEND)
out    SPH,temp          ;Load high byte of end of memory address
         ldi    temp,low(RAMEND)
out    SPL,temp          ;Load low byte of end of memory address

;Initialise output ports
         ldi    temp,$ff
out    DDRC,temp         ;Set Port C for output by sending $FF to direction register

;Initialise Main Program
         sec                      ;Set carry flag to 1
         clr    leds              ;Clear LEDs

;Main Program
forever: out    PORTC,leds        ;Display leds to port C
         rcall  delay             ;Call delay subroutine
         rol    leds              ;Rotate leds left by 1 bit through carry flag
         rjmp   forever           ;Continue forever

;Delay Subroutine (25.351 ms)
delay:   in     save,SREG         ;Preserve status register
         ldi    YH,high($18C0)    ;Load high byte of Y
         ldi    YL,low($18C0)     ;Load low byte of Y
loop:    sbiw   Y,1               ;Decrement Y
         brne   loop              ;and continue to decrement until Y=0
         out    SREG,save         ;Restore Status register
         ret                      ;Return


Last Edited: Wed. Dec 11, 2019 - 02:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Welcome to AVRFreaks!


Well there are two ways to come up with your answer,

the hard way is to lookup how many clock cycles for each instruction, and then calculate the total time,

or the easy way, is load up the simulator and run your program with a break point were the call to the delay routine made, clear the cycle counter,

and run the simulator until the delay function returns, and read the cycle count/elapse timer value!





(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"


  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DohnJoe567 wrote:

answer apparently is 49.94 s

How do you figure?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]


  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

I think we have a troll at hand here.

no question, just a bit of code and a one liner in the subject.


My guess ( as it is the time of year again were projects have to end and semesters have to be done, so homework gets important pretty quickly, this is a school assignment.

He needs to get an answer for the question "with the given code what is the maximum delay?"

just give a random shot for the bow, and have freaks here give the answer, and with a bit of luck a complete explanation......


it starts already that this has nothing to do with Atmel Studio at all. The code will behave the same on every compiler used.

Topic locked