Chris,
The prescaler can be changed with T1CON.
$31 = 1:8
$21 = 1:4
$11 = 1:2
$01 = 1:1
And the Timer is reloaded by putting values in TMR1H:TMR1L.
So first, decide what frequency you need, I'll go with 100hz.
Then, go to this thread...
Testing Forms [TimerCalc] Working I think?
http://www.picbasic.co.uk/forum/showthread.php?t=2031
Enter the OSC value you are using, say 20mhz.
I'll also go with 1:1 prescaler, enter that. (ok, it's already there)
Then enter 100 in the Freq field at the bottom.
Now you can see that it will take a total of 50,000 counts (ticks) to achieve 100hz.
Decide what resolution you want for the Duty-Cycle. ummm, 8-bit (256).
Now you just calculate the ON count from that with...
50000*DutyCycle/256 which is the same thing as...
50000*/DutyCycle in PBP
and the OFF count is 50000 - ONcount
Ok, so let's put that into the Blinky Light example
Code:; Initialize your hardware first. INCLUDE "DT_INTS-18.bas" ' Base Interrupt System INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP interrupts LED1 VAR PORTB.1 DutyCycle VAR BYTE TotalCount CON 50000 ONcount VAR WORD OFFcount VAR WORD ASM INT_LIST macro ; IntSource, Label, Type, ResetFlag? INT_Handler TMR1_INT, _ToggleLED1, PBP, yes endm INT_CREATE ; Creates the interrupt processor ENDASM T1CON = $00 ; Prescaler=1:1, TMR OFF @ INT_ENABLE TMR1_INT ; enable Timer 1 interrupts Main: FOR DutyCycle = 0 TO 255 ; Ramp up GOSUB SetDutyCycle PAUSE 5 NEXT DutyCycle FOR DutyCycle = 254 TO 1 STEP -1 ; Ramp down GOSUB SetDutyCycle PAUSE 5 NEXT DutyCycle GOTO Main SetDutyCycle: ONcount = TotalCount*/DutyCycle OFFcount = TotalCount - ONcount IF DutyCycle = 0 THEN T1CON.0 = 0 ; turn off timer LOW LED1 ; idle LOW ELSE T1CON.0 = 1 ; timer on if DutyCycle > 0 ENDIF RETURN DISABLE DEBUG '---[TMR1 - interrupt handler]------------------------------------------------ @Timer1=TMR1L ; map timer registers to a word variable Timer1 VAR WORD EXT ToggleLED1: IF LED1 THEN LOW LED1 T1CON.0 = 0 ; stop the timer Timer1 = OFFcount ; Load OFF count T1CON.0 = 1 ; start timer ELSE HIGH LED1 T1CON.0 = 0 ; stop the timer Timer1 = ONcount ; Load ON count T1CON.0 = 1 ; start timer ENDIF @ INT_RETURN ENABLE DEBUG




Bookmarks