I'm trying to write a sample piece of code to explore the use of TMR1, I have TMR1 overflowing and triggering an interrupt which works. Though I appear to have an issue with setting up the Preload of the register. The TMR1 preload should be set up from the analogue input and then transferred later in the program to the TMR1 register. I have put an IF statement in the code to make the time that the LED, that is triggered by TMR1, stay on and off 10 times longer so any change in length caused by the preload is more noticeable.
The issue is, no matter how much I vary the voltage divider on the analogue input it doesn't appear to adjust how long it takes to turn the LED output on and off.
If anybody can give me some help with my code it would be highly appreciated.
Code:
clear
DEFINE OSC 8
'GP0 LED OUT
'GP1 AD1 FOR POT INPUT
'GP2 LED STATUS
'REGISTERS
OPTION_REG = %11000000
INTCON = %11000000
PIE1 = %01000001
PIR1 = %00000000
OSCCON = %01110001
TRISIO = %00000001
ANSEL = %01010001
'T1CON = %00110001
CCP1CON = %00000000
CMCON0 = %00000100
INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts
;---------------------------------------------------------------------------
wsave VAR BYTE $20 SYSTEM ' location for W if in bank0
;wsave VAR BYTE $70 SYSTEM ' alternate save location for W
' if using $70, comment wsave1-3
' --- IF any of these three lines cause an error ?? ------------------------
' Comment them out to fix the problem ----
' -- Which variables are needed, depends on the Chip you are using --
wsave1 VAR BYTE $A0 SYSTEM ' location for W if in bank1
'wsave2 VAR BYTE $120 SYSTEM ' location for W if in bank2
'wsave3 VAR BYTE $1A0 SYSTEM ' location for W if in bank3
' --------------------------------------------------------------------------
'OUTPUT PINS
LEDOut VAR GPIO.0
LEDStatus VAR GPIO.2
T1CON = %00110001
'VARIABLES
LENGTH VAR WORD
PauseA var byte
PASS var byte
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, _StateChange, PBP, yes
INT_Handler AD_INT, _PulseLength, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
ADCON0 = %00000111
'MAIN PROGRAM
MAIN:
pausea = 0
WHILE PauseA <= 100
PAUSEA = PAUSEA + 1
pause 1
wend
toggle ledstatus
GOTO MAIN
'SUB PROGRAMS
PulseLength:
LENGTH.highbyte = ADRESH 'SET UP THE TIMER SET UP VARIABLES
LENGTH.lowbyte = ADRESL
ADCON0 = %00000111
@ INT_RETURN ;RETURN
StateChange:
if pass=10 then
TOGGLE lEDOUT
pass = 0
else
pass = pass + 1
endif
TMR1H = LENGTH.highbyte 'Transfer the timer variables to the timer register
TMR1L = LENGTH.lowbyte
T1CON = %00010101
@ INT_RETURN ;RETURN
END
Bookmarks