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.

Delay per instruction

Status
Not open for further replies.

oPiaNts

New Member
I am using an 877A and I need a precise timing. How do I know the time taken to execute each instruction?
 
It depends on the clock frequency and it takes 4 clock cycles per instruction cycle on 16F range chips.
Eg. a 4Mhz clock takes 1uS per instruction cycle.
Most instructions use one cycle - but a few sometimes use 2 such as conditional instructions eg. DECFSZ.
For precise timing loops there is a useful generator provided by the "piclist" at:

Alternatively, you could use one of the counters - they are very accurate and can be set to interrupt when the desired delay has been reached.
 
picasm said:
It depends on the clock frequency and it takes 4 clock cycles per instruction cycle on 16F range chips.
Eg. a 4Mhz clock takes 1uS per instruction cycle.
Most instructions use one cycle - but a few sometimes use 2 such as conditional instructions eg. DECFSZ.
For precise timing loops there is a useful generator provided by the "piclist" at:

Alternatively, you could use one of the counters - they are very accurate and can be set to interrupt when the desired delay has been reached.

Thanks.

How do I use the counters you are saying? Do you mean the interrupts using the INTCON register?
 
To configure your chosen counter and interrupts it is a good idea to first download the datasheet for your chip from the microchip site and also download the "Mid-Range MCU Family manual" from there also.
This is a very detailed manual with whole chapters for each timer and interrupts including example code showing how to configure them.

Basically, you need to write a section to initialise a timer then make the appropriate interupt config - timer0 is the simplest to set-up as it just needs the intcon bit setting. Timer2 is a bit more complicated as needs the peripheral interrupts enabling (PIE1).
When the timer interval is reached, it jumps to your interrupt routine.

This may all seem a bit complex to begin with - but you can use timers without needing interrupts - you just make a loop to poll the intcon timer overflow flag.
 
You can count the overflows of TIMER0. Since it is a 8 bit register, it counts up to 255, then it overflows and an interrupt occurs. Then it starts from 0 again.
Interrupts must be enabled with the INTCON register of course.
TIMER0 will overflow x / 256 times per second, where x depends on the frequency of your clock.
 
Last edited:
If all you're wanting is a simple software delay, look on the PICLIST, they have a 'delay code generator' on line, tell it what you want, and it generates the code for you.
 
eng1 said:
You can count the overflows of TIMER0. Since it is a 8 bit register, it counts up to 255, then it overflows and an interrupt occurs. Then it starts from 0 again.
Interrupts must be enabled with the INTCON register of course.
TIMER0 will overflow x / 256 times per second, where x depends on the frequency of your clock.

It's even simpler than that, he doesn't need to use interrupts to see timer overflows. he can just spin and watch for the timer overflow (either seeing a 255 to 0 transition or the timer0 int bit getting set).

Interrupts, of course, are a better way of doing that, though.
 
picasm said:
Eg. a 4Mhz clock takes 1uS per instruction cycle.
Most instructions use one cycle
This isn't my area but itsn't that 250nS, if each instruction takes one cycle.
[latex]Time = \frac~1{Frequency}[/latex]
Right?
 
no because the PIC instruction clock takes 4 basic clock cycles. divide by 4 to get the instruction execution time.

by the way datasheets will tell you which instructions take 2 clocks.
 
eng1 said:
You can count the overflows of TIMER0. Since it is a 8 bit register, it counts up to 255, then it overflows and an interrupt occurs. Then it starts from 0 again.
Interrupts must be enabled with the INTCON register of course.
TIMER0 will overflow x / 256 times per second, where x depends on the frequency of your clock.

actually, I'm already using that. I am using 4.17... Mhz crystal that gives me exacly 64 overflows for a second. The thing is, I need two different timer interrupts since I need a 1 second and a 100 milisecond interrupt. I cant find any exact crystal here with settings that can overflow the Timer0 every 100 milisecond or a setting that gives 100 as an exact multiple.

If I use the crystal for my 1 sec interrupt, it would give me 93.75ms every 6 overflows. Inside the 100 ms interrupt there will be some conditional statements. If I know the delay per instructions of the instructions inside the interrupt then maybe I can have an exact 100 ms and 1 sec interrupts.
 
Use a 4 mhz crystal and timer 2. You can get an interrupt every 4 mS (pre 4, post 10, 100 in PR2). Then count to 25 in the ISR for 100 mS, count to 250 (or 10 times 100mS) for a second. Or using pre 4, post 10, PR2 250 you can get an int every 10 mS. the overhead is pretty minimal in either case.
 
Sorry, but how do I catch overflows/interrupts of timer2? Nvm I got it to work. Thanks.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top