Microcontroller time delay

Thread Starter

SmallRedMachine

Joined Feb 25, 2017
48
I hope there is enough info in the picture, I don't understand why the delay required has to be half of 200 microseconds, I initially assumed you can choose any value for the delay.
Does the 100us delay make the period of signal cycle = 200us + 100us(delay) = 300us ? If yes why can't we choose the delay for example to be 600us instead ? I'm completely clueless how this thing works...

d3dkSnr_SmallRedMachine.jpg
https://m.imgur.com/a/mlrln
 

qrb14143

Joined Mar 6, 2017
112
You need to study carefully the definitions of signal period and signal frequency.

The signal period is the time taken for one complete cycle, ie the time taken between a given point on the first wave and the same point on the next wave. Using this, you should be able to figure out why the delay in your example affects the period which in turn dictates the frequency.
 

Thread Starter

SmallRedMachine

Joined Feb 25, 2017
48
Is this a Homework?
No, just reading a book.

You need to study carefully the definitions of signal period and signal frequency.

The signal period is the time taken for one complete cycle, ie the time taken between a given point on the first wave and the same point on the next wave. Using this, you should be able to figure out why the delay in your example affects the period which in turn dictates the frequency.
Please correct me if my understanding of this example is wrong: We are trying to change the f= 11.0592MHz to 5kHz and clock cycle of 90.423ns to 200us, so for that to happen we need a particular delay time, and in this case delay for HIGH is 100us and for LOW 100us. And we will use the 88 decimal in the programming to write the register decrement instruction.
 

shteii01

Joined Feb 19, 2010
4,644
The example asks you to generate 5 kHz square wave.
5 kHz=1/T
T=1/5 kHz
T=200 microseconds
The square wave can be divided into two segments, high and low.
Since the period of square wave of frequency 5 kHz is 200 microseconds, the high segment will be half the period, the low segment will be the other half of the period. Therefore the high is 100 microseconds, the low is 100 microseconds. So. You need to generate 5 volts for 100 microseconds (high) and 0 volts for 100 microseconds (low) to produce 5 kHz square wave.
End story.

It is my unasked opinion, the way this book uses word delay is inappropriate.
 
Last edited:
Agree, DELAY is used weirdly. Using terms like ON and OFF time would make more sense and more appropriate for future problems. Since the duty-cycle is 50%, the ON and OFF times are the same.
You need to generate timings for the ON time and the OFF time.

Contrast this with, when port something, bit something, changes state form high to low, generate a 1 second, logic low pulse on port something, bit something.
 
Last edited:

Thread Starter

SmallRedMachine

Joined Feb 25, 2017
48
The example asks you to generate 5 kHz square wave.
5 kHz=1/T
T=1/5 kHz
T=200 microseconds
The square wave can be divided into two segments, high and low.
Since the period of square wave of frequency 5 kHz is 200 microseconds, the high segment will be half the period, the low segment will be the other half of the period. Therefore the high is 100 microseconds, the low is 100 microseconds. So. You need to generate 5 volts for 100 microseconds (high) and 0 volts for 100 microseconds (low) to produce 5 kHz square wave.
End story.

It is my unasked opinion, the way this book uses word delay is inappropriate.
It's teaching how to write an assembly program to add a delay to something, like:
Delay: MOV R0,#number
Take: DJNZ R0,TAKE
RET

So in the example the number is 88 decimal, I assumed writing this program will change the cycle and frequency so each cycle will last longer ?
 

MrChips

Joined Oct 2, 2009
30,821
Here are your homework questions:

1) Explain what each line of the code does.
2) What is the range of values for number?
 

Thread Starter

SmallRedMachine

Joined Feb 25, 2017
48
Here are your homework questions:

1) Explain what each line of the code does.
2) What is the range of values for number?
Delay: MOV R0,#number ;moves whatever number to R0
Take: DJNZ R0,TAKE ;decrement -1 from number then
;check if =0 move on, els repeat
RET ;return to main program

Don't know about the range, if number is 88 then it's from 88 to 0 decimal.
So what's wrong about the assumption I have made in my last post ?
 

MrChips

Joined Oct 2, 2009
30,821
Delay: MOV R0,#number ;moves whatever number to R0
Take: DJNZ R0,TAKE ;decrement -1 from number then
;check if =0 move on, els repeat
RET ;return to main program

Don't know about the range, if number is 88 then it's from 88 to 0 decimal.
So what's wrong about the assumption I have made in my last post ?
There is nothing wrong with your assumption.
If you understood what the statements did then you would have been able to answer your own questions.
 

Thread Starter

SmallRedMachine

Joined Feb 25, 2017
48
There is nothing wrong with your assumption.
If you understood what the statements did then you would have been able to answer your own questions.
I really couldn't get to a conclusion just by knowing what the code meant. The frequencies, clock cycles and delay were confusing me and in fact I still don't get why using the word Delay is wrong here. Thanks everyone for the help.
 

MrChips

Joined Oct 2, 2009
30,821
This is a software delay function. It does not change the frequency of anything except that it consumes processor cycles and prevents the CPU from doing anything else.

What is missing is the total context in which this function will be used. This function has to be called twice interleaved with instructions to turn ON and turn OFF an output pin.

Your answer for range of valid numbers is not sufficient. You need to think about this more carefully.
 

philba

Joined Aug 17, 2017
959
To build a purely software delay function one needs to simply understand how long it takes to execute each instruction. Then, it is just counting instructions.

Since it usually is a loop at the heart of this code, you can break your code into two parts: fixed overhead and loop. The overhead part is all the instructions that get executed only once - either at the beginning of the code or after the loop ends. The loop portion is, clearly, the part that repeats. You count the overhead instruction time (call it F) and determine the amount of time each pass of the loop takes (call it L).

Then you get a formula for the time to execute: T = F+(N*L) where N is the number of passes in the loop. Solving for N gives you N = (T-F)/L. You can build this into the delay function so you just pass it the actual delay time you want. If you want to get really precise, you can look at the remainder of the division and get to within the length of the shortest instruction cycle.
 

philba

Joined Aug 17, 2017
959
By the way, I hope that the next section of the book talks about using Timers to do timing. That's a much better way to do it.

And, of course, that 50% duty cycle square wave could easily be created with a timer as well - a little set up code and then just the HW doing it.
 

joeyd999

Joined Jun 6, 2011
5,287
By the way, I hope that the next section of the book talks about using Timers to do timing. That's a much better way to do it.

And, of course, that 50% duty cycle square wave could easily be created with a timer as well - a little set up code and then just the HW doing it.
Let's put a finer point on it:

TS: pass your exam and then forget you ever learned this concept of delay.

If I had my druthers, I'd exorcise every library implementation of delay() ever written.
 

absf

Joined Dec 29, 2010
1,968
Since it usually is a loop at the heart of this code, you can break your code into two parts: fixed overhead and loop. The overhead part is all the instructions that get executed only once - either at the beginning of the code or after the loop ends. The loop portion is, clearly, the part that repeats. You count the overhead instruction time (call it F) and determine the amount of time each pass of the loop takes (call it L).

Then you get a formula for the time to execute: T = F+(N*L) where N is the number of passes in the loop. Solving for N gives you N = (T-F)/L. You can build this into the delay function so you just pass it the actual delay time you want. If you want to get really precise, you can look at the remainder of the division and get to within the length of the shortest instruction cycle.
I sim the circuit with 8051 running at 12MHz, so I changed the value of L to '46' but I didn't change 'F' and the frequency I get is about correct - 5 KHz.

SRM 8051.PNG

And the software
Code:
    ORG    0000
START:
    MOV     P1,#0    ;SET PORT1 AS OUTPUT
LOOP:
    MOV    P1,#0x80     ;P1.7=H
    CALL     DELAY100
    MOV     P1,#0x00    ;P1.7=L
    CALL     DELAY100
    JMP     LOOP
DELAY100:              ;DELAY 100US
    MOV     R0,#46    ;Value of 'L'
TAKE:    DJNZ     R0,TAKE
    RET

    END
 

joeyd999

Joined Jun 6, 2011
5,287
I sim the circuit with 8051 running at 12MHz, so I changed the value of L to '46' but I didn't change 'F' and the frequency I get is about correct - 5 KHz.

View attachment 140097

And the software
Code:
    ORG    0000
START:
    MOV     P1,#0    ;SET PORT1 AS OUTPUT
LOOP:
    MOV    P1,#0x80     ;P1.7=H
    CALL     DELAY100
    MOV     P1,#0x00    ;P1.7=L
    CALL     DELAY100
    JMP     LOOP
DELAY100:              ;DELAY 100US
    MOV     R0,#46    ;Value of 'L'
TAKE:    DJNZ     R0,TAKE
    RET

    END
As long as we are writing code that makes the processor completely useless, can we make it code efficient at least?

How about just complementing the bit so as to eliminate the redundant delay call?

This will also provide a true 50% duty cycle.
 

Thread Starter

SmallRedMachine

Joined Feb 25, 2017
48
Thanks guys, I obviously need to study a lot about these stuff, I'm only a week into it. By any chance does anyone have any good book recommendation about microcontrollers, particularly 8051 ? because that's the one we are going to use soon. I don't know how to feel about the book I'm reading now, the next section just jumps to something more complicated and that's just a few pages away.
 

absf

Joined Dec 29, 2010
1,968
As long as we are writing code that makes the processor completely useless, can we make it code efficient at least?

How about just complementing the bit so as to eliminate the redundant delay call?

This will also provide a true 50% duty cycle.
I'll leave that to the TS as an exercise.

And he has to learn how to use timer to implement the same task more efficiently.

Besides, he still owe MrChips an answer to the question in post #12....
Your answer for range of valid numbers is not sufficient. You need to think about this more carefully.
Allen
 

Thread Starter

SmallRedMachine

Joined Feb 25, 2017
48
Besides, he still owe MrChips an answer to the question in post #12....


Allen
I would say the range depends on what the delay time needed is, the number of instructions and the time they take and the clock cycle, so in the example from the book the loops would be repeated 88 times but that range could be different for other delay requirements. That's the only way I can think about the range at the moment.
 
Top