SF Timer Module stops my Timer

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

SF Timer Module stops my Timer

Post by TonyR » Thu Apr 28, 2016 12:18 am

I am trying to use one PIC timer to gate on/off another PIC timer which is used as a counter in an 18F87K22. I need a frequency counter with a one second gate time.

My timer (Timer1) is set up as a simple counter, the signal I'm measuring comes in continuously as Timer1's clock on T1CKI. All I do is set timer1L and timer1H registers to 0 when I want to and wait one second then read their contents to get the number of pulses in one second. Works great.

To try and make a gate work next I set up an SF timer (using the SF timer module) and Timer3 in the PIC to call an interrupt once every second (exactly as in the SF interrupt timer help sample program). Works great and toggles my LED in the interrupt function. I want to use this to gate timer1. But on compilation it stops my timer1 from working. And vice versa. It looks like my Timer1 and the SF Timer3 function are trying to use only one resource? I don't know what happens in the SF timer module. If I delete the T1CON.7=1 and T1CON.6=0 lines below my timer fails and the SF Timer 3 works. If I put them back in my Timer1 function works and the SF Timer3 stops. Both timers dont seem to be independent.

If anyone has suggestions where I should look, much appreciated......

T1CON.6=0

Here is my simple Timer1 set up

Code: Select all

Public Sub SetupTimer1()
'
'  Set up Timer 1 as counter
'
ANCON0.4 = 0  ' set pin 33 as digital
'
' Timer 1 source
'
T1CON.7=1   ' source depends on SOSCEN bit
T1CON.6=0

T1CON.5=0   ' no prescaler
T1CON.4=0

T1CON.3=0   ' source is T1CKI pin 33 signal comes in here   
T1CON.2=0
T1CON.1=0   ' read as 16 bit not 8 bit

TMR1H= 0
TMR1L= 0

T1CON.0=1   ' turn timer 1 on
End Sub

Here is my timer setup using the SF interrupt library

Code: Select all

Timer.Initialize(4)
Timer.Items(3).Interval = 1000       // 1s
Timer.Items(3).OnTimer = @OnTimer   // timer event handler
Timer.Items(3).Enabled = true
Timer.Start

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: SF Timer Module stops my Timer

Post by Jerry Messina » Thu Apr 28, 2016 9:40 am

I think you might misunderstand how the SF timer module works. The ISRTimer.bas module uses hardware timer 1 to do all the counting.

The "timers" that the module creates are software timers (just an array of data structures really), so when you say Timer.Items(3) you're still actually using TMR1.

The easiest thing would be to change your functions to use one of the other timers. The 87K22 has TMR3/5/7 which are similar to TMR1.

TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Re: SF Timer Module stops my Timer

Post by TonyR » Thu Apr 28, 2016 11:50 pm

Thanks Jerry, I changed to Timer 0 and its working fine.

It would be just lovely if there was somewhere I could read this stuff before finding out the long and painful way and/or pestering you guys! If Davids reading this maybe a note at the top of the timer module help page. If I knew that in advance I wouldn't have spent two full days trying to debug what I had. (Unless there is one and I missed it:-) )

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: SF Timer Module stops my Timer

Post by Jerry Messina » Fri Apr 29, 2016 11:38 am

I suppose it would be great if modules would specify what hardware they use/require, but I think in the end the best source of information is in the module itself.

It's probably a good idea to take the time to peruse over the source for any of the libraries you use. You can gain a lot of insight that way. It might seem like a bother, but that's actually one of the things I like best about SF in that you CAN look at all the library source code.

In most other languages the source is either closed, hidden, obj libraries, written in some weird macro language, or god forbid - ASM. Here it's all (usually) in high-level SF basic so it's much easier to see exactly what's going on, and you can change it to suite your needs or fix any shortcomings. If that ISRtimer module had to be changed to use one of the other timers all you would have to do is change a few lines and you'd be done. Try that with some of the others and see how you fare.

Don't worry about pestering us... that's the whole point of the forum in the first place. I think you could go back and look at some of the older posts and you'd find the same "stupid questions" from all of us. Plus, you never know if you might have stumbled across some bug or limitation that would make future changes to the libs better/easier to use.

Sure beats pulling your hair out for days!

Post Reply