Timer0
SwordfishUser.Timer0 History
Show minor edits - Show changes to output
Changed line 27 from:
to:
INTCON.7=0
Changed line 31 from:
to:
INTCON.7=1
Changed lines 4-5 from:
It can be used via interrupts or polling methods.
to:
It can be used via interrupts or polling methods. The make calculating the appropriate preload/prescaler values, I suggest the fabulous PicMultiCalc tool from MisterE.
Added line 26:
Disable(ISR)
Added line 30:
Enable(ISR)
Deleted line 122:
Deleted line 126:
Added lines 1-173:
Timer0 Module Available for SwordFish.
This module was designed and tested using the EasyPIC3 (18F452).
It can be used via interrupts or polling methods.
For interrupts, see the example code for calling inside a master interrupt service handler.
!!!Example Code
=code [=
// device and clock...
Device = 18F452
Clock = 32
Config
OSC=HSPLL
Include "DL_Timer0.bas"
Include "utils.bas"
Dim LED As PORTD.0
Event LEDFlash()
Toggle(LED)
End Event
Interrupt ISR()
If Timer0.InterruptFlag =1 Then
Timer0.SInterrupt()
EndIf
End Interrupt
// main
Utils.SetAllDigital()
Output(LED)
Timer0.Initialize(LEDFlash)
Timer0.SetPrescaler(Timer0.PS256)
Timer0.EightBit = 0
Timer0.Preload=49911
Timer0.EnableInterrupt()
Timer0.Enabled()
Enable(ISR)
While true
Wend
=]
!!!Module Code
=code [=
{
*****************************************************************************
* Name : Timer0.BAS *
* Author : Darryl Quinn *
* Notice : Copyright (c) 2008 Darryl Quinn *
* : All Rights Reserved *
* Date : 4/11/2008 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
}
Module Timer0
Type TEvent = Event()
Public Const
SRCInternal = 0,
SRCPIn = 1,
EDGEFalling = 1,
EDGERising = 0,
PSDisabled = 1,
PSEnabled = 0,
PS256=7,
PS128=6,
PS64=5,
PS32=4,
PS16=3,
PS8=2,
PS4=1,
PS2=0
Dim FPreload As Word,
FPrescaler As Byte,
FEnabled As T0CON.7,
FInterrupt As INTCON.5,
OnISR As TEvent
Public Dim
Preload As FPreload,
EightBit As T0CON.6,
Source As T0CON.5,
Edge As T0CON.4,
PrescalerEnabled As T0CON.3,
InterruptFlag As INTCON.2
Public Sub Enabled()
TMR0L = FPreload
FEnabled=1
End Sub
Public Sub Disabled()
FEnabled=0
End Sub
Public Sub EnableInterrupt()
FInterrupt=1
End Sub
Public Sub DisableInterrupt()
FInterrupt=0
End Sub
Public Sub ClearInterrupt()
InterruptFlag=0
End Sub
Public Sub SInterrupt()
intcon.7=0
OnISR()
TMR0L = FPreload.Byte0
TMR0H = FPreload.Byte1
ClearInterrupt()
intcon.7=1
End Sub
Public Sub SetPrescaler(ByVal pPrescaler As Byte)
Select pPrescaler
Case PS2
FPrescaler = PS2
PrescalerEnabled=PSEnabled
Case PS4
FPrescaler = PS4
PrescalerEnabled=PSEnabled
Case PS8
FPrescaler = PS8
PrescalerEnabled=PSEnabled
Case PS16
FPrescaler = PS16
PrescalerEnabled=PSEnabled
Case PS32
FPrescaler = PS32
PrescalerEnabled=PSEnabled
Case PS64
FPrescaler = PS64
PrescalerEnabled=PSEnabled
Case PS128
FPrescaler = PS128
PrescalerEnabled=PSEnabled
Case PS256
FPrescaler = PS256
PrescalerEnabled=PSEnabled
Else
PrescalerEnabled=PSDisabled
End Select
T0CON.0=FPrescaler.0
T0CON.1=FPrescaler.1
T0CON.2=FPrescaler.2
End Sub
Public Sub Initialize(pEvent As TEvent = 0)
Disabled()
EightBit = 1
Source = SRCInternal
PrescalerEnabled = PSDisabled
DisableInterrupt()
OnISR = pEvent
End Sub
=]
This module was designed and tested using the EasyPIC3 (18F452).
It can be used via interrupts or polling methods.
For interrupts, see the example code for calling inside a master interrupt service handler.
!!!Example Code
=code [=
// device and clock...
Device = 18F452
Clock = 32
Config
OSC=HSPLL
Include "DL_Timer0.bas"
Include "utils.bas"
Dim LED As PORTD.0
Event LEDFlash()
Toggle(LED)
End Event
Interrupt ISR()
If Timer0.InterruptFlag =1 Then
Timer0.SInterrupt()
EndIf
End Interrupt
// main
Utils.SetAllDigital()
Output(LED)
Timer0.Initialize(LEDFlash)
Timer0.SetPrescaler(Timer0.PS256)
Timer0.EightBit = 0
Timer0.Preload=49911
Timer0.EnableInterrupt()
Timer0.Enabled()
Enable(ISR)
While true
Wend
=]
!!!Module Code
=code [=
{
*****************************************************************************
* Name : Timer0.BAS *
* Author : Darryl Quinn *
* Notice : Copyright (c) 2008 Darryl Quinn *
* : All Rights Reserved *
* Date : 4/11/2008 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
}
Module Timer0
Type TEvent = Event()
Public Const
SRCInternal = 0,
SRCPIn = 1,
EDGEFalling = 1,
EDGERising = 0,
PSDisabled = 1,
PSEnabled = 0,
PS256=7,
PS128=6,
PS64=5,
PS32=4,
PS16=3,
PS8=2,
PS4=1,
PS2=0
Dim FPreload As Word,
FPrescaler As Byte,
FEnabled As T0CON.7,
FInterrupt As INTCON.5,
OnISR As TEvent
Public Dim
Preload As FPreload,
EightBit As T0CON.6,
Source As T0CON.5,
Edge As T0CON.4,
PrescalerEnabled As T0CON.3,
InterruptFlag As INTCON.2
Public Sub Enabled()
TMR0L = FPreload
FEnabled=1
End Sub
Public Sub Disabled()
FEnabled=0
End Sub
Public Sub EnableInterrupt()
FInterrupt=1
End Sub
Public Sub DisableInterrupt()
FInterrupt=0
End Sub
Public Sub ClearInterrupt()
InterruptFlag=0
End Sub
Public Sub SInterrupt()
intcon.7=0
OnISR()
TMR0L = FPreload.Byte0
TMR0H = FPreload.Byte1
ClearInterrupt()
intcon.7=1
End Sub
Public Sub SetPrescaler(ByVal pPrescaler As Byte)
Select pPrescaler
Case PS2
FPrescaler = PS2
PrescalerEnabled=PSEnabled
Case PS4
FPrescaler = PS4
PrescalerEnabled=PSEnabled
Case PS8
FPrescaler = PS8
PrescalerEnabled=PSEnabled
Case PS16
FPrescaler = PS16
PrescalerEnabled=PSEnabled
Case PS32
FPrescaler = PS32
PrescalerEnabled=PSEnabled
Case PS64
FPrescaler = PS64
PrescalerEnabled=PSEnabled
Case PS128
FPrescaler = PS128
PrescalerEnabled=PSEnabled
Case PS256
FPrescaler = PS256
PrescalerEnabled=PSEnabled
Else
PrescalerEnabled=PSDisabled
End Select
T0CON.0=FPrescaler.0
T0CON.1=FPrescaler.1
T0CON.2=FPrescaler.2
End Sub
Public Sub Initialize(pEvent As TEvent = 0)
Disabled()
EightBit = 1
Source = SRCInternal
PrescalerEnabled = PSDisabled
DisableInterrupt()
OnISR = pEvent
End Sub
=]