PDA

View Full Version : Timer issue



Ioannis
- 2nd January 2011, 16:10
I am not getting the time I expect from the following Interrupt Driven timer.

Controller is 16F887,internal OSC at 8MHz, expected interrupt every 25usec and I get every 101usec.

If I change the reload value to 63, the expected interrupt is 100usec and I get 146usec.

A check on the IntOSC with Clock out on RA6 prooves that the Clock is correct @ 2MHz (8MHz/4).

I tested also with the Timer2 w/o pre-post scalers with exactly the same results.

Here is the test code that is making me nervous:



DEFINE OSC 8

OSCCON = %01110001 'SET SYSTEM CLOCK SWITCH BIT

@Line1 = _DEBUG_OFF & _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_ON
@Line2 = _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT

@ __CONFIG _CONFIG1, Line1 & Line2

@ __CONFIG _CONFIG2, _WRT_HALF & _BOR40V

PORTA=0:PORTB=0:PORTC=0:PORTD=0:PORTE=0

TRISA=%00101101
TRISB=%00000000
TRISC=%10000000
TRISD=%00000011
TRISE=%00000000

OPTION_REG=00001111

WPUB = %00111111

ADCON0 = %11000001
ADCON1 = %00000000 'Set PORTA/E analog/digital, right justify result
ANSEL = %00101101 'lower port A as Analog input
ANSELH = %00000000 'all others Digital inputs

DEFINE ADC_BITS 10 'Number of bits in Adcin result
DEFINE ADC_CLOCK 3 'ADC clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50

pwm1 var portc.2

INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts

'************************************************* *************************
'****
'**** Initialization
'****
'************************************************* *************************

't2con = %0000100 ;Fosc/4, no prescaler, no postscaler

't1con = %00110001 'Enable Timer1 (16bit) /8 prescaller, int. Fosc/4 clock
'@ 20MHz times out every 104,86ms
'::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR0_INT, _UpdateTimer, PBP, yes
; INT_Handler AD_INT, _ADC_Sub, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

'::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::

@ INT_ENABLE TMR0_INT ; enable Timer 1 interrupts
;@ INT_ENABLE AD_INT ; enable ADC Interrupt

CLEAR

loop1:
pause 100
goto loop1

UpdateTimer: 'This interrupt should happen every 25usec :(
tmr0=213
toggle portc.2
@ INT_RETURN


I miss something here but ... what?

Ioannis

mackrackit
- 2nd January 2011, 17:02
What happens if you make it an ASM interrupt instead of a PBP int.?

rsocor01
- 2nd January 2011, 17:41
Ioannis,

Try changing the PAUSE 100 to PAUSE 1. I read somewhere that using Pause 1 instead of a bigger number is better for the interrupts. Give it a try and let me know.

Robert

Ioannis
- 2nd January 2011, 17:44
Thanks Dave.

Hmm, almost half.

PBP int., 207 reload, 101 usec
ASM int., 207 reload, 41 usec

But according to the calculations, 8MHz clock/4=2MHz or 0,5usec clock to TMR0. So a 207 reload should give about 24usec.

Instead we get 41 or 101 usec, according to selected Interrupt.

And this leads me to ask, why/when to use ASM instead of PBP as long as I use PBP in the ISR?

@Robert. This is true for ON INTERRUPTS of the Compiler. Here I use DT_INTS14, so this is of no importance. Thanks anyway.

Ioannis

Ioannis
- 2nd January 2011, 18:02
OK, after some tweaking, and using ASM as TYPE (Don't know why yet), is getting correct results up to a reload value of around 180.

The changes to get close are:



@ INT_DISABLE TMR0_INT
TMR0=TMR0+180
toggle pwm1
@ INT_ENABLE TMR0_INT


By adding the value,we have more precise control of the reload value.

But not close to my target of 25usec

Ioannis

mackrackit
- 2nd January 2011, 18:44
Only guessing here, but I think Darrel ment the ASM type when there is ASM code in the ISR. I have noticed better accuracy with timers just by changing the type though.

Now that I read this page again about the underscore, I may be doing something wrong. http://darreltaylor.com/DT_INTS-14/asm_ints.html

Hmmm. Always something interesting..

Ioannis
- 2nd January 2011, 18:56
There are some shadows on the DT-INTs routines as there is no Manual. I am willing to help writing a manual document whenever Darrel can provide more insides.

But for the moment the ASM idea seem to get closer to the target.

Ioannis

mister_e
- 2nd January 2011, 19:06
Check the fine print at the bottom of the following ;)
http://darreltaylor.com/DT_INTS-14/kudos.html

mackrackit
- 2nd January 2011, 19:15
Fine print... There is always fine print....

Hey Steve, Good to read you again!!!

mister_e
- 2nd January 2011, 19:18
Hehe, you'll probably see me a bit more in 2011. Who knows?

Acetronics2
- 2nd January 2011, 19:22
Steve : 2 points

Dave : 1 point

Simply Using MPSIM could have clearly shown what was going on.

Interrupt duration always must be smaller than the period of the timer.

Obvious, when it is told like this ...;)

Alain

mister_e
- 2nd January 2011, 19:28
Sims sucks :D

Ioannis
- 2nd January 2011, 20:04
Hi Steve! What a surprise? Thanks for the tip.

Yeah, I missed that one. Surely I am walking over the limit here.

Ioannis