Great! Now lets put that on hold for a bit and see if we can get a timer interrupt going - then we'll glue it together.
I am a bit worried about the somewhat limited amount of RAM on '684 but we'll just have to see. I won't try to go into too much detail about DT-Ints, there's been several threads and several examples on the forum and obviosuly on Darrels site. If you don't already have the needed files you get them here. Place them either in the project directory or in the root folder of your PBP installation.
TMR1 is a 16bit timer and it causes an interrupt (if enabled) at rollover from $FFFF to 0. If we would let the timer free-run we'd get an interrupt every 65536th instruction cycle. At 8Mhz, that would be every 32.768ms or a frequency of ~30.5 Hz. If we want any other frequency we need to make sure that the timer doesn't start at 0 but at some other value. This is done by preloading the timer inside the interrupt service routine.
At 8Mhz one instruction cycle is 500ns. If we want an interrupt frequency of 100Hz we need the timer to roll over every 10ms, or once every 20000 instruction (20000*500ns=10ms). Because the timer interrupt when it rolls over we use 65536-20000=45536.
OK, now the code:
Code:
' Included the interrupt system
INCLUDE "DT_INTS-14.bas"
INCLUDE "ReEnterPBP.bas"
' When compiling for the 16F684 DT-Ints tells us to add these variables so we do that
wsave VAR BYTE $20 SYSTEM
wsave1 VAR BYTE $A0 SYSTEM
TMR1_Reload CON 45536 ' Reload value for ~100Hz interrupt frequency at 8MHz, prescaler 1:1
TMR1_Temp VAR WORD ' Temporary variable use for reloading the timer
IntCount VAR BYTE ' Keep track of number of interrupts
oldCount VAR BYTE
newCount VAR BYTE
Frequency VAR WORD
UpdateDisplay VAR BIT
LED1 VAR PortC.4
LED2 VAR PortC.5
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, _ISR, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
T1CON.0 = 1 ' Start TMR1
@ INT_ENABLE TMR1_INT ; Enable Timer 1 Interrupts
Main:
Toggle LED1
Pause 1000
Goto Main
' This is the interrupt routine for the TMR1 interrupt.
ISR:
T1CON.0 = 0 ' Stop timer
TMR1_Temp.HighByte = TMR1H ' Get current "time"
TMR1_Temp.LOWBYTE = TMR1L
TMR1_Temp = TMR1_Temp + TMR1_Reload ' Add reload value to get correct interval
TMR1H = TMR1_Temp.HIGHBYTE ' Move result back to timer
TMR1L = TMR1_Temp.LOWBYTE
T1CON.0 = 1 ' Start timer
If IntCount = 19 THEN
Toggle LED2
IntCount = 0
ENDIF
@ INT_RETURN
The main routine in the above code will blink LED1 at 0.5Hz. The timer interrupt should cause LED2 to blink at ~5Hz. The code compiles here but I haven't tested it. Obviosuly you need to add your CONFIG, TRIS, ANSEL, CMCON, whatever at the top too.
Give it a go, see if you can calculate and set the desired frequency.
/Henrik.
Bookmarks