PDA

View Full Version : cycle length to do various tasks



Lestat
- 30th July 2012, 23:23
Below is an extract from a piece of code, it is an interrupt routine triggered by TMR2 to produce a single pulse of a set length controlled by the PR2length byte sized variable. This uses Darrel Taylor's interrupt code.

Currently the pulse length is longer than I intend it to be due to the lines of code prior to the T2CON line (turning the pulse on) and also the interrupt triggering routine. I know I need to change the PR2length variable so that it triggers earlier to produce a pulse of the correct length.

I could do this by trial and error, though I was wondering if anybody had any experience or detail how many cycles it takes to do various tasks in PIC BASIC PRO. e.g. how many cycles it would take to copy the PR2Length variable to the PR2 register, and how long it would take to run Darrel's code to allow it to jump to the interrupt routine.

Any information or guidance on how to find this out would be appreciated.


PulseRestart:
TMR2 = 0
PR2 = PR2length
T2CON = %01001111 'Restart pulse length timer
TMR1H = Tmr1LENGTH.highbyte 'Transfer the timer variables to the timer1 register
TMR1L = tmr1LENGTH.lowbyte
T1CON = %00010101 'Start Pulse High Timer
pulseout = 1
@ INT_RETURN ;RETURN

This is using a PIC12F683

Darrel Taylor
- 1st August 2012, 05:01
So you are using 2 timers to make 1 pulse?

Well, I can say that interrupt latency can be accounted for by ADDING a reload value to the accumulated timer value.
And nothing in your ISR is using PBP's system variables, so the handler "Type" can be ASM, which will reduce the latency to be accounted for.

I won't go into too much detail because I think you're probably doing it the wrong way.

What are the minimum and maximum pulse widths?
And what is the interval between them?

Lestat
- 1st August 2012, 13:41
Darrel

I am effectivly trying to produce a PWM signal to control a servo, I know there is lots of examples out there already, but I want to use the timers to control the time the pulse is high using tmr1 and tmr2 tells the PIC when to restart the high pulse, allows me to learn how the timers work and how to accurately control timer lengths.

As this is a PWM signal for a servo, TMR1 produces a high pulse between 1 and 2ms and tmr2 restarts tmr1 every 20ms.

I am still new to the whole world of programming and PIC Basic, but what variables are PBP system variables?

Thank you for the interrupt code, it really has made setting up interrupts without using oninterrupt a dream.

What I am interested in how many cycles it takes to set up the timer registers and also how long it takes for an interrupt to trigger? or alternatively a method to find out how long it takes to run the various bits of code?

Many thanks for your help.

Darrel Taylor
- 1st August 2012, 15:38
I thought it might be for a servo.

First, take a look at this program I did many years ago ...
http://www.pbpgroup.com/modules/wfsection/article.php?articleid=2

It uses a single timer for both the on and off times.

As for the execution times for various statements, there is no one answer.
It depends on what size the variables are, what bank the registers are in, and where in the code the statement is located.
A statement like A = B can take anywhere from 2 to 8 assembly language instructions. Which at 4mhz would be 2 to 8uS.

For getting in and out of the interrupt, the differences are even greater depending on the type of handler used.

The PWPS program accounts for those times by adding the reload values to the accumulated timer values.
It's ISR is strickly assembly language, but the same thing can be done with DT_INTS.

Lestat
- 2nd August 2012, 13:20
That program was an interesting read thank you. It certainly makes me think I should consider learning some assembly code, to allow me to control the number of cycles of time critical programs and also ensure that variables are saved in certain banks so the PIC does not need to spend time jumping from one bank to the other when running a critical task.

For the time being I will stick with DT-INTS as I believe writing my own assembly level interrupt is seriously beyond me. Maybe something to try in a few years.

I think I'll spend sometime adjusting the numbers being loaded into the various timer registers and see if I can learn some assembly code.

Thanks for your help