I was looking at a program that is about a sound note sequencer that plays different sound notes with different duration, in the middle of some sound notes there will be a delay for like 0.5s or 0.25s, then I cannot figure out a way to calculate the value to load into the register to achieve the duration that I want.
I have searched wiki and some other online resoures such as from this website : https://www.avr-tutorials.com/assembly/calculating-execution-time-sequential-code to find out how to calculate the execution time for a function.
delay:
push {lr}
ldr r10, =12000 @ in fact this function will result in a delay of 0.25s
b loop
loop:
..... @ some instructions to play sound
subs r10, #1 @ in each loop the time counter is subtracted by 1
cmp r10, #0 @ when time counter reaches zero, branch to a end function
beq end
b loop @ loop the function until time counter hits zero
for example, the above function with register r10
as a time counter, and in another loop
function,a value of 0
(meaning 0 Hz sound is input into the register
to create a delay between sound notes) is added to register r0
by mov r0, #0
, and the I want to find out what value do I need to load into the time counter r10
in order to let me play the 0 Hz sound for like 0.5s
or 0.25s
and how do I calculate this value to be loaded in. I don't know what is the exact way of saying this but perhaps I will phrase it as " calculating delay time in a program?"
The delay
function that I found somewhere else will in fact really result in a 0.25s
delay but I don't understand how does it work. I am guessing it takes 0.25s for the time counter r10
to reach 0 by subtracting 1 from it in each loop. But I really cannot figure out how does this calculation comes about.
Thank you.
Just answering on ..in fact really result in a 0.25s delay but I don't understand how does it work
part (in fact, this is the only question I can recognise as a question):
A CPU instruction takes some fixed time to execute(*) on a given CPU clocked at a given frequency. Knowing this data you can calculate the number of loop iterations necessary to get the delay you need. It's just a simple arithmetic task.
(*) This does not hold for most modern CPUs and this method of implementing a delay is strongly discouraged for a number of reasons.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.