Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Time delay

Status
Not open for further replies.

4electros

New Member
This section of code is taken from tutorial 1.2 of nigel tutorails:

Delay movlw d'250' ;delay 250 ms (4 MHz clock)
movwf count1
d1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0

decfsz count1 ,f
goto d1
retlw 0x00


The delay subroutine is supposed to make a delay of 1ms...I've calculated the time by myself and found that the dealay time is 994us not 1000us=1ms.
I don't know whether my calculation is wrong or the result is approximated to 1ms... what's the matter?!

I depend with my calculation upon considering the goto instruction takes 2 instruciton cycles and decfsz instruction only takes 1 instruction cycle.is that true?!
 
4electros said:
I depend with my calculation upon considering the goto instruction takes 2 instruciton cycles and decfsz instruction only takes 1 instruction cycle.is that true?!

Not according to the datasheet:
 

Attachments

  • decfsz.gif
    decfsz.gif
    12.6 KB · Views: 754
Run the routine through the MPLAB simulator to get definitive answer on how much is the delay precisely.

994us or 1000us? Have you taken into acount the act of calling the delay routine? That's 4usec for calling and returning from the routine. Is it that important in your application?

I prefer to not use delay loops for anything longer than a few microsecs. There are several timers available on the PIC that will give you precise delays without haing to count the instruction steps.
 
motion said:
I prefer to not use delay loops for anything longer than a few microsecs. There are several timers available on the PIC that will give you precise delays without haing to count the instruction steps.

I NEVER count the instruction steps, there's a delay code generator on the PICList that generates the code for you - and even gives you an exact timing for it - the delays in my tutorials were generated by it.

The section of code listed is just such a delay routine, except it's surrounded by an extra counter, so you can set the number of 1mS delays you get - in this case 250mS.
 
Even if there is a program that counts instructions, I would still not use delay loops. Try using them in program with RS-232 communications, LCD display and other tasks at the same time. They are for beginning programmers, IMHO.
 
motion said:
Even if there is a program that counts instructions, I would still not use delay loops. Try using them in program with RS-232 communications, LCD display and other tasks at the same time.

However, the way most timer based delays are written is not really much different to a software loop, including most of those in the MicroChip application notes.

They mostly put the PIC in a loop waiting for the timer flag to be set, this is no different to using a software loop - and no better for most tasks. It has a slight advantage in that interrupts during the delay won't have an effect on the delay time (unless they occur after the timer flag is set), but in most cases this won't make any significant difference anyway.

And my tutorials actually have examples using RS232, LCD, and other things, using software loops.

They are for beginning programmers, IMHO.

That's the whole point of the tutorials!. If a particular program works perfectly, why is any particular technique 'beginner' or 'advanced'?. You could just as accurately judge them as 'reliable programmer' and 'show off'.

Although I do understand your point of view, and if you've had the odd occasion where you needed a timer based solution, it's simple to use the code again in the future even when it offers no advantages.
 
motion said:
Run the routine through the MPLAB simulator to get definitive answer on how much is the delay precisely.
How do you do this in MPLAB?
Also, what is the "PIClist" that Nigel referred to?
 
Hiya Len,
Here's the link to the piclist mate,
And here's the link to the delay code generator Nigel was talking about
The piclist sure makes for good reading and it's been around for years.

Hope this helps out

Cheers Bryan :D
 
And my tutorials actually have examples using RS232, LCD, and other things, using software loops.

I don't have time to read your tuturials but can you point me to an example with RS-232, LCD within a 1.000 second software delay loop?

If a particular program works perfectly, why is any particular technique 'beginner' or 'advanced'?.

If it works, I'd say the technique is advanced. However, I don't see software delays where the CPU is put in tight loop to waste cycles for long periods being employed commercial (not hobbyist) applications specially in realtime embedded applications. If a technique's use is abandoned in advanced applications, I'd say it's beginner.
 
motion said:
If it works, I'd say the technique is advanced.
[...]
If a technique's use is abandoned in advanced applications, I'd say it's beginner.

What if it works, but is abandoned in advanced applications? :roll:

There are efficient and inefficient techniques, that, I agree. But "beginner" and "advanced" just don't make any sense.

Software loops are inefficient, but their goal is exactly that. They try to be as "precisely inefficient" as possible to slow down software execution. In some contexts, they might even be the ideal and/or only acceptable solution.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top