PDA

View Full Version : using darrel taylor's instant interrupts



Michael Wakileh
- 20th July 2007, 21:33
Hi everyone...
I just had a glace at Darrel Taylors great instant interrupts page (http://darreltaylor.com/DT_INTS-14/intro.html),
and I would like to apply these in the near future.

I would like to learn to have interrupts after a certain amount of time/manually preload the timer.. For example, to
make a light toggle every 70ms, I could use timer 1, prescaler 1:2 and preload of 30536 (thanks Mister E!).

I figured that I could base my code on:
the blinky light code http://darreltaylor.com/DT_INTS-14/blinky.html
the timer template http://darreltaylor.com/DT_INTS-14/TimerTemplate.html

I HAVE 2 QUESTIONS:

QUESTION 1: looking at the blinky light code: Do I understand correctly that the timer is still running while the interrupt handler is executing.


QUESTION 2 deals with altering the timer template code:
I figure it would be easiest for me to changing the code which calculates the timer reload constant. here it is:

ASM ; Calculate Timer Reload Constant
ReloadInst = 8 ; # of Intructions used to reload timer
if ((Prescaler == 1)||(Prescaler == 2)||(Prescaler == 4)||(Prescaler == 8))
MaxCount = 65536 + (ReloadInst / Prescaler)
TimerReload = MaxCount - ((25000 / Freq) * (OSC*10/Prescaler))
if ((TimerReload < 0) || (TimerReload > (65535-ReloadInst)))
error Invalid Timer Values - check "OSC", "Freq" and "Prescaler"
endif
else
error Invalid Prescaler
endif
ENDASM


QUESTION 2: CAN THIS SIMPLY BE REPLACED BY

ASM
TimerReload = 30536
ENDASM


OR HAVE I MISSED SOMETHING?

Charles Linquis
- 21st July 2007, 00:06
#1 The timers run as long as they are enabled, so they will run in while you are in your ISR.

#2 You can reload (or pre-load) the timers at any time with any number - but you MUST load the high byte first.

Darrel Taylor
- 21st July 2007, 04:00
Since it takes 8 instructions to reload the timer, the actual number would be 30540 for 70ms.

But,... the TimerReload is an External Constant so you can't just give it a value like it's a variable.

You could change the ...

TimerReload CON EXT

-- to --

TimerReload CON 30540

But, if you want it to be easily changable, you might want to change the original code to this.
;--- Change these to match the desired interrupt frequency -------------------
;--- See http://DarrelTaylor.com/DT_INTS-14/TimerTemplate.html for more Info.
@Interval = 70 ; Interval of Interrupts in ms
@Prescaler = 2 ; Timers Prescaler setting
T1CON = $10 ; $10 = Prescaler 1:2, TMR1 OFF
; $00=1:1, $10=1:2, $20=1:4, $30=1:8 -- Must match @Prescaler value



;---[TMR1 reload - interrupt handler]-----------------------------------------
ASM ; Calculate Timer Reload Constant
ReloadInst = 8 ; # of Intructions used to reload timer
if ((Prescaler == 1)||(Prescaler == 2)||(Prescaler == 4)||(Prescaler == 8))
MaxCount = 65536 + (ReloadInst / Prescaler)
TimerReload = MaxCount - ((((OSC*1000000/4)/Prescaler)*Interval)/1000)
if ((TimerReload < 0) || (TimerReload > (65535-ReloadInst)))
error Invalid Timer Values - check "OSC", "Interval" and "Prescaler"
endif
else
error Invalid Prescaler
endif
ENDASM

Then you can just change the Interval and Prescaler numbers to anything you wanted, without having to recalculate the Reload value.

hth,

Michael Wakileh
- 24th July 2007, 15:53
Thanks...you rock!!!

Darrel Taylor
- 24th July 2007, 18:32
Sometimes I wonder about that Rock part.

When I looked back at your first post, I saw.


QUESTION 2: CAN THIS SIMPLY BE REPLACED BY

ASM
TimerReload = 30536
ENDASM

Ummm, the answer to that should have been....
YES! Exactly that! (except 30540).

Would have worked great.
Can't imagine what I was thinking. Doh!

LOL,

Michael Wakileh
- 28th July 2007, 00:07
Hi Darell...Your above code will nevertheless be of value for many...

I'v finally just about finished writing my first timer template/instant interrupts based program. It just has 1 little bug when compiling...
If anyone has time, please take a look at this code snippet:

I am trying to use HPWM on a 16f690 in the main program loop.
The controlling variable "enabler" is determined by code in the timer1interrupt handler. When it is =1 the hpwm is turned on briefly.

Turning the HPWM on and off is where I have my problem..
I get WARNING Bad token " " for the 2 lines that set t2con in the main program loop, but not for the line setting t2con just before the main program loop. Any ideas?

best regards,
mike

------------------------------------------------------------------------
DEFINE OSC 4
OSCCON = %01100110 'should be OK too
ANSEL = $00
ANSELH = 0
ccp1con = 0
CM1CON0 = 0
CM2CON0 = 0

DEFINE I2C_HOLD 1
DPIN1 var PORTb.4 ' I2C data pin
CPIN1 var PORTb.6 ' I2C clock pin

INCLUDE "DT_INTS-14.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP.bas" ; Include if using PBP interrupts

enabler var bit

' LOTS MORE VARIABLES DECLARED HERE

ASM
INT_LIST macro ' use the format IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, ReloadTMR1, ASM, no
INT_Handler TMR1_INT, _MATHhandler, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

T1CON = $15 'prescaler; timer on
@ INT_ENABLE TMR1_INT ; enable Timer 1 interrupts

T2CON = $00 'HERE I DON'T GET A WARNING

main:
IF enabler = 1 THEN 'this variable controlls possible hpwm activation
TRISC = 1 'Datasheet said to turn off pwm outputs first
PR2 = 63 'Timer 2 period register
CCPR1L = 60 'MSBS of DUTYCYCLE
CCP1CON = %00001100 'CCP1M<3:0> = %1100 P1M<1:0> = %00 'turn on pwm…
PSTRCON = $0F '%00001111 PULSE STEERING CONTROL REGISTER- send same pwm signal to 4 outputs

T2CON = %00000100 ' HERE I GET A WARNING -precaler 1:1, postcaler 1:1, bit 2 = Timer2 ON

TRISC = 0
enabler = 0
PAUSE 100 'I could even change the vibration time by software (pause x)
CCP1CON = 0 'TURN OFF HPWM
T2CON = %00000000 'HERE I GET A WARNING -TURN OFF TMR2 - minimize power constumption

TMR2 = 0 'SHOULD RESET THE TIMER
endif
goto main


MATHhandler:

'ROUTINES THAT DETERMINE WHEN THE VARIABLE ENABLER=1 thus activating a short pwm signal in the main program

@ INT_RETURN



ASM
TimerReload = 30540
ENDASM
@Timer1 = TMR1L ; map timer registers to a word variable
Timer1 VAR WORD EXT
TimerReload CON EXT ; Get the External Constant
TMR1ON VAR T1CON.0 ; Alias the Timers ON/OFF bit
ASM
ReloadTMR1
MOVE?CT 0, T1CON, TMR1ON ; 1 stop timer
MOVLW LOW(TimerReload) ; 1 Add TimerReload to the
ADDWF TMR1L,F ; 1 value in Timer1
BTFSC STATUS,C ; 1/2
INCF TMR1H,F ; 1
MOVLW HIGH(TimerReload) ; 1
ADDWF TMR1H,F ; 1
MOVE?CT 1, T1CON, TMR1ON ; 1 start timer
INT_RETURN
ENDASM
StartTimer:
Timer1 = TimerReload ; Load Timer
TMR1ON = 1 ; start timer
RETURN
StopTimer:
TMR1ON = 0 ; stop timer
RETURN
--------------------------------------------------------------------------

Archangel
- 28th July 2007, 05:15
Hello Michael,
I "ASS-U-ME" you copied and pasted this code from your microcode studio or other editor. If so, then you have a punctuation error:
INT_LIST macro <font color=red>'</font color> use the format IntSource, Label, Type, ResetFlag? <p> In assembly comments always use ; not ' <p> That fixes the error, still get lots of warnings though, still working on it.
JS

Michael Wakileh
- 28th July 2007, 11:22
Thank you for the heads up Joe!
When I compile I only those 2 warnings though... Please post what other warnings you get when compiling...

Michael Wakileh
- 30th July 2007, 12:37
Now that I am declaring T2CON IN HEX that part is working, and I am not getting any more bad token errors. I see what you meant about the many warnings... Looks like I will have to spend more time debugging...

Executing: "C:\Program Files\Microchip\MPASM Suite\MPAsmWin.exe" /q /p16F690 "PWMPROG.ASM" /l"PWMPROG.lst" /e"PWMPROG.err"
Warning[205] C:\DOCUMENTS AND SETTINGS\ADMIN\DESKTOP\PWMPROG.ASM 769 : Found directive in column 1. (endm)
Warning[206] C:\DOCUMENTS AND SETTINGS\ADMIN\DESKTOP\PWMPROG.ASM 770 : Found call to macro in column 1. (INT_CREATE)
Warning[206] C:\DOCUMENTS AND SETTINGS\ADMIN\DESKTOP\PWMPROG.ASM 767 : Found call to macro in column 1. (INT_Handler)
Warning[206] C:\DOCUMENTS AND SETTINGS\ADMIN\DESKTOP\PWMPROG.ASM 768 : Found call to macro in column 1. (INT_Handler)
Warning[206] C:\DOCUMENTS AND SETTINGS\ADMIN\DESKTOP\PWMPROG.ASM 966 : Found call to macro in column 1. (MOVE?CT)
Warning[203] C:\DOCUMENTS AND SETTINGS\ADMIN\DESKTOP\PWMPROG.ASM 967 : Found opcode in column 1. (MOVLW)
Warning[203] C:\DOCUMENTS AND SETTINGS\ADMIN\DESKTOP\PWMPROG.ASM 968 : Found opcode in column 1. (ADDWF)
Warning[203] C:\DOCUMENTS AND SETTINGS\ADMIN\DESKTOP\PWMPROG.ASM 969 : Found opcode in column 1. (BTFSC)
Warning[203] C:\DOCUMENTS AND SETTINGS\ADMIN\DESKTOP\PWMPROG.ASM 970 : Found opcode in column 1. (INCF)
Warning[203] C:\DOCUMENTS AND SETTINGS\ADMIN\DESKTOP\PWMPROG.ASM 971 : Found opcode in column 1. (MOVLW)
Warning[203] C:\DOCUMENTS AND SETTINGS\ADMIN\DESKTOP\PWMPROG.ASM 972 : Found opcode in column 1. (ADDWF)
Warning[206] C:\DOCUMENTS AND SETTINGS\ADMIN\DESKTOP\PWMPROG.ASM 973 : Found call to macro in column 1. (MOVE?CT)
Warning[206] C:\DOCUMENTS AND SETTINGS\ADMIN\DESKTOP\PWMPROG.ASM 974 : Found call to macro in column 1. (INT_RETURN)
Loaded C:\Documents and Settings\Admin\Desktop\PWMPROG.cod.
BUILD SUCCEEDED: Mon Jul 30 13:24:24 2007

Darrel Taylor
- 30th July 2007, 12:47
Be carefull with the spacing of ASM statements.

In particular ... (quoted from Post #6)
ASM
INT_LIST macro ' use the format IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, ReloadTMR1, ASM, no
INT_Handler TMR1_INT, _MATHhandler, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

Should be ...

ASM
INT_LIST macro ; use the format IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, ReloadTMR1, ASM, no
INT_Handler TMR1_INT, _MATHhandler, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

Michael Wakileh
- 30th July 2007, 23:36
THANK YOU DT! I just came home from a strenuous day at the hospital...and that piece of information was very uplifting...I thought I would be banging my head for at least a month trying to solve those...

IN CASE ANY OTHER NEWBIES ARE READING, HERE IS THE LAST PIECE OF CODE THAT HAD WRONG INDENTATION/SPACING AND HAS BEEN CORRECTED:

ASM
ReloadTMR1
MOVE?CT 0, T1CON, TMR1ON ; 1 stop timer
MOVLW LOW(TimerReload) ; 1 Add TimerReload to the
ADDWF TMR1L,F ; 1 value in Timer1
BTFSC STATUS,C ; 1/2
INCF TMR1H,F ; 1
MOVLW HIGH(TimerReload) ; 1
ADDWF TMR1H,F ; 1
MOVE?CT 1, T1CON, TMR1ON ; 1 start timer
INT_RETURN
ENDASM


My code now compiles fine and i will have the chance to test it on the weekend. Apparrently i will have to buy a decent book on assembly after my next exam in october. Also I will clearly have to find a way to send beer to Darrell :)

Michael Wakileh
- 30th July 2007, 23:38
UNFORTUNATELY IT IS NOT BEING DISPLAYED CORRECTLY HERE... JUST HAVE A QUICK LOOK at http://darreltaylor.com/DT_INTS-14/TimerTemplate.html

Archangel
- 31st July 2007, 01:16
That's all it was, where do you learn those little tidbits short of having a great mentor, Ugh, OH GREAT ONE?

Pic_User
- 31st July 2007, 04:07
Great work Darrel!

Hi Michael,


UNFORTUNATELY IT IS NOT BEING DISPLAYED CORRECTLY HERE... JUST HAVE A QUICK LOOK at http://darreltaylor.com/DT_INTS-14/TimerTemplate.html

The forum will keep re-formatting your code. It removes spaces and changes things. To avoide this use the “vB code” code box.

To use the “Code Brackets” see the “vB code” Hot button in the “Posting Rules” box at the bottom left of the screen while you are posting.

It puts the code in the special box that protects the format better that the normal forum posting does...
Hope that helps,
-Adam-