Timer0 user module slow to start

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
be80be
Registered User
Registered User
Posts: 90
Joined: Mon Feb 23, 2009 2:15 am
Location: tn

Timer0 user module slow to start

Post by be80be » Fri Mar 30, 2012 4:17 am

I was trying out the Timer0 module and it works fine after it makes the fist cycle the led comes on and stays on for like a second then off for about the same time then it blinks at the rate it should

Code: Select all

Device = 18F2520

Clock = 8 // 8MHz clock
Config osc = intio67
Include "intio8"   //SETS OSCCON TO $72 FOR 8 mhz
Include "SetDigitalIO"
Include "Timer0.bas"
Include "utils.bas"

Dim LED As PORTC.0

Event LEDFlash()
      Toggle(LED)
End Event

Interrupt ISR()
     INTCON.7=0
     If Timer0.InterruptFlag =1 Then
        Timer0.SInterrupt()      
     EndIf
     INTCON.7=1
End Interrupt

// main
SetAllDigital()
Output(LED)

Timer0.Initialize(LEDFlash)
Timer0.SetPrescaler(Timer0.PS256)
Timer0.EightBit = 0
Timer0.Preload=60500
Timer0.EnableInterrupt()
Timer0.Enabled()
Enable(ISR)

While true
Wend
I'm using the sample only changed the preload value

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

Post by Jerry Messina » Fri Mar 30, 2012 10:26 am

There's two problems that I see.

When using the timers in 16-bit mode, you must write the high-byte then the low-byte, so make some changes to the Timer0.bas file:

Code: Select all

//
// a 16-bit timer must be set highbyte then lowbyte to set the TMRH latch first.
// use a macro here instead of a sub since it's used in an ISR, so low overhead
// and no frame variables used. The syntax of the macro allows it to be used 
// with variables and/or constant values.
//
public macro LoadCount(wval)
    TMR0H = ((wval) >> 8)
    TMR0L = byte(wval)
end macro

Public Sub SInterrupt()
       OnISR()
       LoadCount(FPreload)
       ClearInterrupt()
End Sub
Now, the code in main never initially sets a value for the timer count, so the first time it'll be wrong until it gets loaded after the first interrupt. Change the main routine around a bit:

Code: Select all

Dim LED As PORTC.0

Event LEDFlash()
      Toggle(LED)
End Event

Interrupt ISR()
     If Timer0.InterruptFlag =1 Then
        Timer0.SInterrupt()
     EndIf
End Interrupt

// main
SetAllDigital()
Output(LED)

Timer0.Initialize(LEDFlash)
Timer0.SetPrescaler(Timer0.PS256)
Timer0.EightBit = 0
Timer0.Preload = 60500
Timer0.LoadCount(Timer0.Preload)		// **ADDED** to load count
Timer0.EnableInterrupt()
Timer0.Enabled()
Enable(ISR)

While true
Wend
I added initially loading the timer using the new LoadCount() macro, and got rid of statements in the ISR messing around with INTCON.7 (which is the GIE bit). No need to play around with this.

See if that works out any better. I didn't test it, so let us know.

be80be
Registered User
Registered User
Posts: 90
Joined: Mon Feb 23, 2009 2:15 am
Location: tn

Post by be80be » Sat Mar 31, 2012 12:11 am

I figured there was something going on with the Preload

Thanks Jerry it works like it should now

Post Reply