PDA

View Full Version : Understanding timer issues



boroko
- 28th March 2009, 22:59
Hi all,
trying to understand why this isn't working as it seems it should.

The Transmit: TMR1 Int handler is ~30mS and works fine. At the end of the TMR1 int, TMRO is given settings so that the first cycle through, there should be a ~190uS int cycle. After the first cycle, the TMR is reloaded so that the next passes are ~2.74 mS. For some reason, the two timer loads affect each other and changing one affects both areas in ways that I'm not understanding. I can't get any single point to adjust the two timing areas.


<font color="#000000"><b>DEFINE </b>OSC 8
<b>DEFINE </b>DEBUG_REG PORTA
<b>DEFINE </b>DEBUG_BIT 0
<b>DEFINE </b>DEBUG_BAUD 2400
<b>DEFINE </b>DEBUG_MODE 0
<b>CLEAR
</b>TRISA = %00100000 <font color="#0000FF"><i>' PortA : All outputs except RA.1 for INT_INT
</i></font>TRISB = %01000100 <font color="#0000FF"><i>' PWM1,3,5 outputs,RB2=INT, RB0,2,3: DIO
</i></font>ADCON1 = %00001111 <font color="#0000FF"><i>'
</i></font>T1CON = %10000001 <font color="#0000FF"><i>' 16bit, 1:1 ps, 30 mS INT
</i></font>RCON = %00011111 <font color="#0000FF"><i>' NO Priority for interrupts
</i></font>OSCCON = %11111110 <font color="#0000FF"><i>' INTOSC, 8MHz
</i></font><b>DEBUG </b><font color="#FF0000">&quot;CSS1330-2inc&quot;</font>,13,10
<font color="#0000FF"><i>'::::::::::::
</i></font><b>INCLUDE </b><font color="#FF0000">&quot;DT_INTS-18.bas&quot; </font><font color="#0000FF"><i>'
</i></font><b>INCLUDE </b><font color="#FF0000">&quot;ReEnterPBP-18.bas&quot; </font><font color="#0000FF"><i>'
'::::::::::::

</i></font>EchoTime <b>VAR WORD
</b>OutPulseCnt <b>VAR BYTE
</b>Gain <b>VAR BYTE
</b>GCA <b>VAR </b>PORTA.1
GCB <b>VAR </b>PORTA.2
GCC <b>VAR </b>PORTA.3
GCD <b>VAR </b>PORTA.4
PulseOut <b>VAR </b>PORTB.0
EchoIn <b>VAR </b>PORTB.2
ledR <b>VAR </b>PORTB.3 <font color="#0000FF"><i>'
</i></font>ledG <b>VAR </b>PORTB.4 <font color="#0000FF"><i>'
</i></font>Prog <b>VAR </b>PORTB.6 <font color="#0000FF"><i>'
</i></font>CntSw <b>VAR WORD
</b>flag <b>VAR BYTE
</b>IncCnt <b>VAR BYTE
</b>MdCnt <b>VAR BYTE
</b>TimOut <b>VAR BYTE
</b>Lock <b>VAR </b>flag.0 <font color="#0000FF"><i>' 1= locked, 0 = unlocked

</i></font><b>ASM
</b><font color="#008000">INT_LIST macro </font><font color="#0000FF"><i>; IntSource, Label, Type, ResetFlag?
</i></font><font color="#008000">INT_Handler INT2_INT, _GetEcho, ASM, yes </font><font color="#0000FF"><i>;straight I/P
</i></font><font color="#008000">INT_Handler TMR0_INT, _BlankGain, ASM, yes
INT_Handler TMR1_INT, _Transmit, ASM, yes
endm
INT_CREATE </font><font color="#0000FF"><i>;
</i></font><b>ENDASM
</b><font color="#008000">@ INT_ENABLE TMR1_INT </font><font color="#0000FF"><i>; enable Timer 1 interrupts
'************************************************* **************************
</i></font><b>GOTO </b>Start

<font color="#0000FF"><i>'*** Sub to Select MODE *************************************
</i></font>ModeSelect:
<font color="#0000FF"><i>' first time through, need to unlock. Then can pass to others
</i></font><b>IF </b>MdCnt = 2 <b>THEN </b><font color="#0000FF"><i>'1st check for unlock
</i></font>Lock = 0
<b>ENDIF
IF </b>Lock = 0 <b>THEN
</b>ledG = 1 <font color="#0000FF"><i>'
</i></font><b>ELSE
</b>ledG = 0
<b>GOTO </b>DoneCk
<b>ENDIF
SELECT CASE </b>MdCnt
<b>CASE </b>1
Lock = 1
<b>DEBUG </b><font color="#FF0000">&quot;Case 1&quot;</font>,13,10
<b>GOTO </b>DoneCk
<b>CASE </b>2
Lock = 0
<b>GOTO </b>DoneCk
<b>DEBUG </b><font color="#FF0000">&quot;Case 2&quot;</font>,13,10
<b>CASE </b>3
<b>DEBUG </b><font color="#FF0000">&quot;Case 3&quot;</font>,13,10
<b>CASE IS </b>&gt;3
<b>DEBUG </b><font color="#FF0000">&quot;Case &gt;3&quot;</font>,13,10
Lock = 1

<b>END SELECT
</b>DoneCk:
IncCnt = 0 <font color="#0000FF"><i>' reset MODE counter
</i></font><b>RETURN
</b><font color="#0000FF"><i>'**********************
'* END Subs
'**********************
</i></font>Start:
Lock = 1
Mainloop:
<b>DEBUG </b>#EchoTime,13,10

<font color="#0000FF"><i>'code for menu selection here
</i></font><b>GOTO </b>Mainloop
<font color="#0000FF"><i>'*********** Int handlers ***********
'---[TMR1 - interrupt handler]-------
' TMR1 = 16 bit, 1:1, 65535 - 5543(preload) ~30 mS cycle
</i></font>Transmit:
<font color="#008000">@ INT_DISABLE INT_INT </font><font color="#0000FF"><i>; Disable external ints to stop Echo RX
</i></font>TMR1H = $15 <font color="#0000FF"><i>'
</i></font>TMR1L = $A7 <font color="#0000FF"><i>'
</i></font>OutPulseCnt = 0 <font color="#0000FF"><i>' start new cycle
</i></font>Gain = 0 <font color="#0000FF"><i>'
</i></font>PORTA = Gain <font color="#0000FF"><i>' turn off the gain
</i></font><b>ASM
</b><font color="#008000">BSF</font> <font color="#008000">PORTB,0
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BCF PORTB,0
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BSF</font> <font color="#008000">PORTB,0
NOP
NOP
NOP
NOP
NOP
NOP
NOP
BCF PORTB,0
</font><b>ENDASM
</b>T0CON = %11001000 <font color="#0000FF"><i>' On, 8b, PSon, 1:1, For Blanking
</i></font>TMR0L = 70 <font color="#0000FF"><i>' Blanking inhibit 190uS ???
</i></font><font color="#008000">@ INT_ENABLE TMR0_INT </font><font color="#0000FF"><i>; Enable TMR0 to start Blanking/Gain block
</i></font><font color="#008000">@ INT_RETURN
</font><font color="#0000FF"><i>'---[TMR0 - interrupt handler]-------------------------------
' inhibit GetEcho for 1 cycle through TMR1 with value loaded at end of transmit
' code then change TMR1 to ramp up gain in 2.74mS steps
</i></font>BlankGain:
OutPulseCnt = OutPulseCnt +1 <font color="#0000FF"><i>'1st pass, TMR0 Blanking set @ end of &quot;TX&quot;
</i></font><b>SELECT CASE </b>OutPulseCnt
<b>CASE </b>1
T0CON = %11000101 <font color="#0000FF"><i>' On, 8b, PSon, 1:64, for Gain step
</i></font>TMR0L = 170 <font color="#0000FF"><i>' Preload to 170 for 2.74mS INT
</i></font><font color="#008000">@ INT_ENABLE INT2_INT </font><font color="#0000FF"><i>; enable INT2 to catch Echo
</i></font><font color="#008000">@ INT_RETURN </font><font color="#0000FF"><i>; go back for next INT on TMR0
</i></font><b>CASE </b>2
<b>GOTO </b>MoreGain <font color="#0000FF"><i>' 2nd pass: Gain inc 1
</i></font><b>CASE </b>3
<b>GOTO </b>MoreGain <font color="#0000FF"><i>' gain inc'd to 2
</i></font><b>CASE </b>4
<b>GOTO </b>MoreGain <font color="#0000FF"><i>' gain inc'd to 3
</i></font><b>CASE </b>5
<b>GOTO </b>MoreGain <font color="#0000FF"><i>' gain inc'd to 4
</i></font><b>CASE </b>6
<b>GOTO </b>MoreGain <font color="#0000FF"><i>' gain inc'd to 5
</i></font><b>CASE IS </b>&gt; 6
<b>GOTO </b>EndGain
<b>END SELECT
</b>MoreGain:
Gain = Gain + 2 <font color="#0000FF"><i>' RA.0 is I/P, so count x2
</i></font>PORTA = Gain <font color="#0000FF"><i>'sent out RA.1-RA.4, set gain amp
</i></font>SkipGain:
<font color="#008000">@ INT_RETURN

</font>EndGain:
<font color="#008000">@ INT_DISABLE TMR0_INT </font><font color="#0000FF"><i>; Shut-off Blanking &amp; Gain incr until next cycle
</i></font><font color="#008000">@ INT_RETURN

</font><font color="#0000FF"><i>'---[INT - interrupt handler]-------------------------
</i></font>GetEcho:
EchoTime.LowByte=TMR1L
EchoTime.HighByte=TMR1H <font color="#0000FF"><i>' capture the counts
</i></font><font color="#008000">@ INT_RETURN
</font><b>END


</b>

The DEBUG quits after the splash. I suspect it is getting blown up by the interrupt.

Thanks for looking at it.
Bo

boroko
- 30th March 2009, 13:57
Hi all,
Still working on a timer issue and hoping that someone might see what I'm not.
Here is a little better explanation and some cleaned up code if you don't mind looking at it again.

The problem is with the TMR0. It seems to not recognize the preloaded value. I can't seem to make any changes that are predictable. The function is this:

1) TMR1 starts a ~30mS cycle with a pulse. (This timer works well.)
2) At the end of the pulse, it loads values for, and enables TMR0
3) First time through TMR0, it should wait ~.9mS for the interrupt and then
enable INT2_INT to capture the count on TMR1 when a pulse comes in
4) At the end of the first TMR0_INT, a new value gets loaded for an ~2.74mS delay,
5) each of the next cycles use the 2.74mS delay until they go through a certain number and get shut off.
6) The next TMR1_INT starts the whole thing again.

<font color="#000000"><b>DEFINE </b>OSC 8
TRISA = %00100000 <font color="#0000FF"><i>' PortA : All outputs except RA.1 for INT_INT
</i></font>TRISB = %01000100 <font color="#0000FF"><i>' PWM1,3,5 outputs,RB2=INT, RB0,2,3: DIO
</i></font>ADCON1 = %00001111 <font color="#0000FF"><i>' Set up ADCON1
</i></font>T1CON = %10000001 <font color="#0000FF"><i>' 16bit, 1:1 ps, 30 mS INT
'T0CON = %11000101 ' On, 8b, PSon, 1:64 : DIDN'T HELP
</i></font>RCON = %00011111 <font color="#0000FF"><i>' NO Priority for interrupts
</i></font>OSCCON = %11111110 <font color="#0000FF"><i>' INTOSC, 8MHz
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
</i></font><b>INCLUDE </b><font color="#FF0000">&quot;DT_INTS-18.bas&quot; </font><font color="#0000FF"><i>' Base Interrupt System
</i></font><b>INCLUDE </b><font color="#FF0000">&quot;ReEnterPBP-18.bas&quot; </font><font color="#0000FF"><i>' Include if using PBP interrupts
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
' Variable definition
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
</i></font>EchoTime <b>VAR WORD
</b>OutPulseCnt <b>VAR BYTE
</b>Gain <b>VAR BYTE
</b>PL1lo <b>CON </b>$A7 <font color="#0000FF"><i>' Preload for TMR1 LO byte, Cycle time
</i></font>PL1hi <b>CON </b>$15 <font color="#0000FF"><i>' Preload TMR1 HI byte ,PL =5543/ 1:1
'************************************************* **************************
</i></font><b>ASM
</b><font color="#008000">INT_LIST macro </font><font color="#0000FF"><i>; IntSource, Label, Type, ResetFlag?
</i></font><font color="#008000">INT_Handler INT2_INT, _GetEcho, ASM, yes </font><font color="#0000FF"><i>;straight I/P
</i></font><font color="#008000">INT_Handler TMR0_INT, _BlankGain, ASM, yes
INT_Handler TMR1_INT, _Transmit, ASM, yes
endm
INT_CREATE </font><font color="#0000FF"><i>; Creates the interrupt processor
</i></font><b>ENDASM
</b><font color="#008000">@ INT_ENABLE TMR1_INT </font><font color="#0000FF"><i>; enable Timer 1 to start sonic cycle
'************************************************* **************************
</i></font>Mainloop:
<font color="#0000FF"><i>' code here
</i></font><b>GOTO </b>Mainloop
<font color="#0000FF"><i>'*********** Int handlers **************************************************
'---[TMR1 - interrupt handler]-------------------------------
' TMR1 = 16 bit, 1:1, 65535 - 5543(preload) =30 mS cycle, (WORKS WELL)
</i></font>Transmit:
<font color="#008000">@ INT_DISABLE INT_INT </font><font color="#0000FF"><i>; Disable external ints to stop Echo RX
</i></font>TMR1H = PL1hi <font color="#0000FF"><i>'Preload HI byte
</i></font>TMR1L = PL1lo <font color="#0000FF"><i>'Preload LO byte =
</i></font>OutPulseCnt = 0 <font color="#0000FF"><i>'clear everything &amp; start new cycle
</i></font>Gain = 0 <font color="#0000FF"><i>' zero the gain counter
</i></font>PORTA = Gain <font color="#0000FF"><i>' turn off the gain
</i></font><b>ASM
</b><font color="#008000">BSF</font> <font color="#008000">PORTB,0 </font><font color="#0000FF"><i>; pulse out
</i></font><font color="#008000">NOP
NOP
BCF PORTB,0
NOP
NOP
</font><b>ENDASM

</b><font color="#0000FF"><i>'+++++++++++ HERE IS THE ISSUE and different things tried ++++++++++++++++++++

' T0CON = %11000100 ' On, 8b, PSon, 1:32, For Blanking
' TMR0L = 199 ' Blanking delay: .9mS : NOPE
</i></font><font color="#008000">@ INT_ENABLE TMR0_INT </font><font color="#0000FF"><i>; Enable TMR0 to start Blanking/Gain block
' TMR0L = 199 ' Blanking delay: .9mS : WAY OFF!
' T0CON = %11000100 ' On, 8b, PSon, 1:32
</i></font>TMR0L = 40 <font color="#0000FF"><i>' Blanking delay: .9mS : CLOSEST!? WHY???
</i></font>T0CON = %11000010 <font color="#0000FF"><i>' On, 8b, PSon, 1:8
' TMR0L = 40 ' tried loading after T0CON, no dif
</i></font><font color="#008000">@ INT_RETURN
</font><font color="#0000FF"><i>'---[TMR0 - interrupt handler]-------------------------------
' 1 cycle through TMR0 with value loaded at end of transmit (TMR1), (~.9mS)
' then change TMR0 to ~2.74mS increments
</i></font>BlankGain:
OutPulseCnt = OutPulseCnt +1 <font color="#0000FF"><i>'1st pass, TMR0 Blanking set @ end of &quot;TX&quot;
</i></font><b>SELECT CASE </b>OutPulseCnt
<b>CASE </b>1
<font color="#0000FF"><i>' TMR0L = 84 ' PL 2.74mS INT : THIS MESSES IT ALL UP
' T0CON = %11000100 ' On, 8b, PSon, 1:32, for Gain step
</i></font><font color="#008000">@ INT_ENABLE INT2_INT </font><font color="#0000FF"><i>; enable INT2 to catch Echo
</i></font><font color="#008000">@ INT_RETURN </font><font color="#0000FF"><i>; go back for next INT on TMR0

'+++++++++++++END OF SUSPECTED THINGS THAT CAN AFFECT THE ISSUE+++++++++++

</i></font><b>CASE </b>2
<b>GOTO </b>MoreGain <font color="#0000FF"><i>' 2nd pass: Gain inc 1
</i></font><b>CASE </b>3
<b>GOTO </b>MoreGain <font color="#0000FF"><i>' gain inc'd to 2
</i></font><b>CASE </b>4
<b>GOTO </b>MoreGain <font color="#0000FF"><i>' gain inc'd to 3
</i></font><b>CASE </b>5
<b>GOTO </b>MoreGain <font color="#0000FF"><i>' gain inc'd to 4
</i></font><b>CASE </b>6 <font color="#0000FF"><i>'.......
'........
</i></font><b>CASE IS </b>&gt; 14
<b>GOTO </b>EndGain <font color="#0000FF"><i>'
</i></font><b>END SELECT
</b>MoreGain:
Gain = Gain + 2 <font color="#0000FF"><i>' increment gain: RA.0 is I/P, so count x2
</i></font>PORTA = Gain <font color="#0000FF"><i>'sent out RA.1-RA.4 to control gain on amp
</i></font>EndGain:
<font color="#008000">@ INT_DISABLE TMR0_INT </font><font color="#0000FF"><i>; Shut-off Blanking &amp; Gain incr until next cycle
</i></font><font color="#008000">@ INT_RETURN

</font><font color="#0000FF"><i>'---[INT - interrupt handler]-----------------------------------------------
</i></font>GetEcho:
<font color="#0000FF"><i>'@ INT_DISABLE TMR0_INT ; Haven't seen a need for this yet
</i></font>EchoTime.LowByte=TMR1L <font color="#0000FF"><i>' capture the counts
</i></font>EchoTime.HighByte=TMR1H
<font color="#0000FF"><i>'@ INT_ENABLE TMR0_INT ; Re-enable TMR0
</i></font><font color="#008000">@ INT_RETURN
</font><b>END


</b>

Again, thank you for looking at it.

Bo

Darrel Taylor
- 30th March 2009, 22:04
Transmit:
@ INT_DISABLE INT_INT ; Disable external ints to stop Echo RX

INT_INT is the same as INT0_INT.
But you are using INT2.

In the BlankGain: handler, you have ...

CASE IS > 14
GOTO EndGain '

That will use PBP's system variables. Which means the handler can not be an ASM type anymore.

A CASE ELSE might fix that problem.

There may be other issues, but that's a start.

boroko
- 1st April 2009, 02:32
Thanks for the observations. The INT_INT was a leftover from a different chip and I didn't catch it there.

I'm afraid the problem still exists. I have been trying to get a pattern so that I can at least identify why the timer seems to be out of control. I'm sure it is going to be something stupid, but last nights session didn't reveal it to me. I'm going to devise a few more tests tonight to try again. Decided to take a break from it and work on other areas for a while for a break.

I will say that my understanding of PBP and PIC's in general has come a long way by working through recent projects. That has only been possible because of all of the help that this forum and it's members. I know that it can be frustrating, but your efforts are very appreciated.

Thanks
Bo

boroko
- 7th April 2009, 02:56
A few more questions on this mystery...

I'm using a 18F1330 and TMR0 is a selectable width timer (8/16bit).
My understanding is that because it has TMR0L and TMR0H, in 8 bit mode, you preload TMR0L only. There is no TMR0 register specifically. Do I have that right? Are there any other issues with "changeable" timers

I know that I have read threads concerning changing timer values on the fly in the program, but at the time, I was not facing those problems and wasn't smart enough to leave a trail to find my way back now that I need the insight. I have tried searching, but I haven't hit the right terms yet. Anybody remember a link to their favorite discussion on changing timer values?

If anything jumps to mind, I would appreciate it. In the mean time, I'll see if I can cajole an answer from some testing.

Thanks, I learn a great deal from you all. (sometimes, what questions NOT to ask!...) ;-)

Bo