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.

Calculation for different types of delay

Status
Not open for further replies.

patricktran

New Member
Hi there,
I reckon we have totally 3 types of delay that can be implemented in a PIC 16F877.
1. Busy loop
2. TMR0
3. TMR1
Asume that we use 4Mhz cyrstal and the delay of 1 sec is desired. I get so confused about the ways of calculating the delay. Not the code, just the way people think of the numbers. Can someone please give an example of calculation the delay of 1 sec?
Thanks
 
Ok, let me have a go first with TMR1 of pic 16F877:

TMR1H:TMR1L = TMR1:0x0000 -> 0xFFFF
Each round of TMR1: 0xFFFF = 65535 instructions = 65.5 msec
If I take a prescaler of 1:16, the result would be: 65.5 * 16 = 1048 msec
which means 1.048 sec.
This is close enough to 1 sec.
However I could not find a prescaler register for this TMR1. :( I dont know, maybe we dont have prescaler for TMR1 like TMR0. :roll:
So I think just loop the TMR1 for 16 times:

movlw d'16'
movwf counter ; counter of 16 times

ISR ; when TMR1 overflows
decf counter
movfw counter
xorlw d'0'
btfss status, z ; if z = 1, counter = 0 , skip
call reset_timer
call TurnOnLED
retfie

Wow, this code seems too complex. Please correct me if I am incorrect. Thanks again :)
 
Timer2 is easier to use for accurate delays as it has the PR2 register - this is basically a comparison value, when the timer reaches this value it times out and resets the timer.

But for simple delays it's easier to use delay loops, you can generate the code with a routine on the PICList, just tell it your oscillator speed, the delay required, and press the button - instant delay code!.

If you're starting out learning PIC, it's probably better to avoid using the extra hardware when you can - generally it's more complicted than doing it yourself. You will meet situations where the hardware is essential, then you can start worrying about it - but by then you should have a good grasp of assembler.
 
patricktran said:
<...snip...>
However I could not find a prescaler register for this TMR1. :( I dont know, maybe we dont have prescaler for TMR1 like TMR0.
<...snip...>

Grab the datasheet and search for T1CON. Timer 1 control register.
Allows prescalers 1:1, 1:2, 1:4 and 1:8
 
Thanks EXO, I found it!!
Uhm, can someone please give me some sample code for a busy loop with crystal of 4MHz for 1 sec with explanation how can you get the ...secrete numbers :shock:
Thanks
 
patricktran said:
Thanks EXO, I found it!!
Uhm, can someone please give me some sample code for a busy loop with crystal of 4MHz for 1 sec with explanation how can you get the ...secrete numbers :shock:
Thanks

As I mentioned above, just go on the PICList and use the delay code generator - it's really great!.

It's here .
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top