'The following code provides an example of using timer one 
'as an interrupt counter for a way of "time slicing" of your
'program.  The code is written for the Proton Compiler 
'from Crownhill and Associates.
'It uses software interrupts, therefore reasonably accurate
'time keeping can only be obtained by adding a "fudge factor" 
'to the timer offset after you have written your code and 
'tested it.
'Cut, paste and modify the following into your Proton Compiler.

Device 16F628A

'the oscillator freq. you selected in the Pic Timer calc tool is below

XTAL = 4.0                                                                

ALL_DIGITAL = true
TRISA = %0000000

Symbol T1CKPS0 = T1CON.4    ' Timer1 Input Clock Prescale Select bits
Symbol T1CKPS1 = T1CON.5    ' Timer1 Input Clock Prescale Select bits

Dim InterruptCount         As Byte
Dim Count_10_Interrupts    As Byte
Dim Count_100_Interrupts   As Byte
Dim Count_1000_Interrupts  As Word
Dim TimerOffset            As Word


'the offset & prescaler setting you selected in the Pic Timer calc tool is below

TimerOffset = 0                                                               
T1CKPS0 = 0                                                                    
T1CKPS1 = 0                                                                                          


Dim TimerSetLow As TimerOffset.LowByte
Dim TimerSetHigh As TimerOffset.HighByte

PIE1.0 =   1                    'enable TMR1 interupts
INTCON.6 = 1                    'enable all unmasked interrupts
INTCON.7 = 1                    'enable Global interrupts
PIR1.0 =   0                    'reset interupt flag
T1CON.0 = 0                     'stop the timer
TMR1H = TimerSetHigh            'load a start up value into the timer
TMR1L = TimerSetLow
T1CON.0 =  1                    'start the timer



On Interrupt ISR                'Interrupt Service Routine

GoTo Main                       'bypass interrupt routine

'the interrupt routine occurs every  15.2590                                   
'times a second and the "ISR" routine below is carried out

Disable                         'disable interrupts while in interrupt routine

ISR:                            'Interrupt Service Routine
T1CON.0 = 0                     'stop the timer
TMR1H = TimerSetHigh            'load a start up value into the timer
TMR1L = TimerSetLow
PIR1.0 = 0                      'reset interupt flag

Inc InterruptCount

If InterruptCount > 10 Then
Inc Count_10_Interrupts
Clear InterruptCount

If Count_10_Interrupts > 10 Then
Inc Count_100_Interrupts
Clear Count_10_Interrupts

If Count_100_Interrupts > 10 Then
Inc Count_1000_Interrupts
Clear Count_100_Interrupts
EndIf
EndIf
EndIf

T1CON.0 = 1         'start the timer

Resume              'return back to where set timer was called

Enable              're enable interrupts

Main:

While 1=1
If Count_10_Interrupts   = 10   Then GoSub On_10_Interrupts
If Count_100_Interrupts  = 10   Then GoSub On_100_Interrupts
If Count_1000_Interrupts = 10   Then GoSub On_1000_Interrupts


On_10_Interrupts:

'**** DO SOMETHING HERE, LIKE CHECK A PORT FOR A CHANGE*******
'**** CHECK AN INPUT, SET AN OUTPUT ETC. *********************
Return


On_100_Interrupts:

'**** DO SOMETHING HERE, LIKE CHECK A PORT FOR A CHANGE*******
'**** CHECK AN INPUT, SET AN OUTPUT ETC. *********************
Return


On_1000_Interrupts:

Clear Count_1000_Interrupts
'**** DO SOMETHING HERE, LIKE CHECK A PORT FOR A CHANGE*******
'**** CHECK AN INPUT, SET AN OUTPUT ETC. *********************
Return

Wend