Microcontroller time delay

absf

Joined Dec 29, 2010
1,968
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.
Take a look at my 8051 thread here...

I learn by doing more than just reading....

Allen
 

absf

Joined Dec 29, 2010
1,968
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.
What if you need more than 100uS delay, let's say 700uS. Can you still do it with that instruction?

I think MrChips is asking about the range of values that was allowed by R0.

Allen
 

Thread Starter

SmallRedMachine

Joined Feb 25, 2017
48
Take a look at my 8051 thread here...

I learn by doing more than just reading....

Allen
Thanks I will see what I can learn from it, but just at the first glance it looks way too advanced for me, I can start working practically for the first time with a microcontroller(8051) next week, for now all I really know about it is 5 pages of a small book.
 

Thread Starter

SmallRedMachine

Joined Feb 25, 2017
48
What if you need more than 100uS delay, let's say 700uS. Can you still do it with that instruction?

I think MrChips is asking about the range of values that was allowed by R0.

Allen
Right... so if each register has 1 byte then 11111111 would be the max it can take which is 255 decimal.
 

joeyd999

Joined Jun 6, 2011
6,279
256: 100000000 (9 bits)
If I assumed 256 can be moved to R0 in the first place then indeed 256-1= 255 is 11111111.
But the code wants to move the 256 to R0:
Delay: MOV R0,#256
That can't even happen right ?
Obviously only 8 bits are possible.

But what happens if you use only the lower 8 bits?
 

joeyd999

Joined Jun 6, 2011
6,279
Hmmm what's lower 8 bits ? Google didn't help, what's it that I have to do research on ?
You already said 256 requires 9 bits. That was correct.

What are the lower 8 bits of 256?

When you figure the answer, don't reply back. Run it through the code and then tell me what happens and why.
 

Thread Starter

SmallRedMachine

Joined Feb 25, 2017
48
You already said 256 requires 9 bits. That was correct.

What are the lower 8 bits of 256?

When you figure the answer, don't reply back. Run it through the code and then tell me what happens and why.
Alright thought the lower 8 bit was something a lot more complicated than that ;) so:

00000000
Is = 0 so the program moves to RET because it does meet the condition and http://www.bbemuseum.com/museum/item.php?item=1703 then goes to the main program, I have been thinking about what happens next, I'd say it keeping going in and out of delay loop throught the whole code without the required delay
 

joeyd999

Joined Jun 6, 2011
6,279
Alright thought the lower 8 bit was something a lot more complicated than that ;) so:

00000000
Is = 0 so the program moves to RET because it does meet the condition and http://www.bbemuseum.com/museum/item.php?item=1703 then goes to the main program, I have been thinking about what happens next, I'd say it keeping going in and out of delay loop throught the whole code without the required delay
Tell me what DJNZ does -- specifically, in order.
 

Thread Starter

SmallRedMachine

Joined Feb 25, 2017
48
Tell me what DJNZ does -- specifically, in order.
Sorry it's like 4:30am and I'm not functioning anymore, Decrement if not Zero. So when = 0 it will NOT take -1 from it. I think I need to take rest because I'm starting to feel like there is a blender right now in my brain mixing everything I learnt so far. Thank you for your patience, I will post my answer later and I'd really appreciate if you can check it whenever you possibly can.
 
My contribution:

8 bits: is 0 to 255 is unsigned and -128 to +127 if 8 bit 2's complement.

Know the difference between for/next and while/until loops. Sometimes the loop will execute once if your not careful and don't want it too. Also watch compares with "equal too" ESPECIALLY if your working with a floating point number.

Another binary arithmetic concept to learn is "sign extension" How to make a -128 in 8 bit 2's complement into -128 16 bit 2's complement.
 

Thread Starter

SmallRedMachine

Joined Feb 25, 2017
48
My contribution:

8 bits: is 0 to 255 is unsigned and -128 to +127 if 8 bit 2's complement.

Know the difference between for/next and while/until loops. Sometimes the loop will execute once if your not careful and don't want it too. Also watch compares with "equal too" ESPECIALLY if your working with a floating point number.

Another binary arithmetic concept to learn is "sign extension" How to make a -128 in 8 bit 2's complement into -128 16 bit 2's complement.
Thanks a lot, I didn't know anything about 2's complement so did some rading on it.


Incorrect. You are missing a "J".
Decrement Jump if Not Zero
1 (00000000) moving this without 1 to R0, the code takes -1 from it therefore 00000000 - 00000001, using two's complement:

0 0 0 0 0 0 0 1 ---> 1 1 1 1 1 1 1 0 + 1 ---> 1 1 1 1 1 1 1 1
So:
0 0 0 0 0 0 0 0
+
1 1 1 1 1 1 1 1
= 1 1 1 1 1 1 1 1
With the leading bit being 1 it means the sign is negative so to find the magnitude we do the two's complement again and we get 00000001 with a negative sign which is -1 decimal, now what the code does at this part ? 11111111 I assume is read 255 by the code and I don't think the code can do the"leading bit is 1 therefore it's negative so let's do two's complement again to get the magnitude", that should mean once -1 is taken away from 00000000 then it basically resets back to 255, and once we get from 255 to 0, the code checks the condition and since it's zero now, the loop will end.
I would conclude the range is still 0-255 but the loop is repeated 256 times, I mean we don't move 256(100000000) to R0, we technically just move 0 decimal to R0.
 
Last edited:

joeyd999

Joined Jun 6, 2011
6,279
Thanks a lot, I didn't know anything about 2's complement so did some rading on it.



Decrement Jump if Not Zero
1 (00000000) moving this without 1 to R0, the code takes -1 from it therefore 00000000 - 00000001, using two's complement:

0 0 0 0 0 0 0 1 ---> 1 1 1 1 1 1 1 0 + 1 ---> 1 1 1 1 1 1 1 1
So:
0 0 0 0 0 0 0 0
+
1 1 1 1 1 1 1 1
= 1 1 1 1 1 1 1 1
With the leading bit being 1 it means the sign is negative so to find the magnitude we do the two's complement again and we get 00000001 with a negative sign which is -1 decimal, now what the code does at this part ? 11111111 I assume is read 255 by the code and I don't think the code can do the"leading bit is 1 therefore it's negative so let's do two's complement again to get the magnitude", that should mean once -1 is taken away from 00000000 then it basically resets back to 255, and once we get from 255 to 0, the code checks the condition and since it's zero now, the loop will end.
I am sorry @KeepItSimpleStupid invoked signed numbers at this point and confused an otherwise simple concept, but your analysis is otherwise correct -- if not overly complicated.

Understand the concept of "rollover" in digital systems, otherwise known as overflow or underflow with respect to digital mathematical operations.

An 8 bit counter overflows to 00000000 when 1 is added to 11111111, and underflows to 11111111 when when 1 is subtracted from 00000000.

This can be used to great effect when designing timers and counters when programming such things.

In the immediate instance, you can achieve a maximum number of loops by preloading R0 with 0. This results in 256 loops, not the 255 you originally assumed.

As a side benefit, R0 returns to 0 upon the exit of the loop, eliminating the need to reload it prior to execution of the next loop, saving code space.

As a next exercise, you could consider how small you can make the code by complimenting the output bit (as opposed to forcing it to the literal 1 and 0), using 256 loops as your delay time.

The code becomes surprisingly small, especially when you realize you don't really need to preset R0 at all.
 

Thread Starter

SmallRedMachine

Joined Feb 25, 2017
48
complimenting the output bit (as opposed to forcing it to the literal 1 and 0)
Sorry this part is not really clear to me, what's complementing the output bit ?

The range of values is 0-255.

What happens when number = 0?
How many times does it execute the loop?
The loop is executed 256 times. if number=0 then when -1 is taken away from it, the content of R0 becomes the binary value of 11111111 and 255 decimal, so far loop is executed once, then there are the other 255 times that the loop would be executed making it 256 times in total.
 
Top