using darrel taylor's instant interrupts


Closed Thread
Results 1 to 14 of 14
  1. #1

    Question using darrel taylor's instant interrupts

    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?

  2. #2
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    #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.
    Charles Linquist

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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.
    Code:
    ;--- 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,
    DT

  4. #4


    Did you find this post helpful? Yes | No

    Thumbs up

    Thanks...you rock!!!

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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,
    DT

  6. #6


    Did you find this post helpful? Yes | No

    Question

    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
    --------------------------------------------------------------------------

  7. #7
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Punctuation

    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
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  8. #8


    Did you find this post helpful? Yes | No

    Default

    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...

  9. #9


    Did you find this post helpful? Yes | No

    Angry

    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

  10. #10
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Be carefull with the spacing of ASM statements.

    In particular ... (quoted from Post #6)
    Code:
    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 ...
    Code:
    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
    DT

  11. #11


    Did you find this post helpful? Yes | No

    Talking

    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

  12. #12


    Did you find this post helpful? Yes | No

    Default

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

  13. #13
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Yep!

    That's all it was, where do you learn those little tidbits short of having a great mentor, Ugh, OH GREAT ONE?
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  14. #14
    Join Date
    Apr 2006
    Location
    New Hampshire USA
    Posts
    298


    Did you find this post helpful? Yes | No

    Post the “vB code” code box

    Great work Darrel!

    Hi Michael,

    Quote Originally Posted by Michael Wakileh View Post
    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-
    Ohm it's not just a good idea... it's the LAW !

Similar Threads

  1. Instant Interrupts - Revisited
    By Darrel Taylor in forum Code Examples
    Replies: 772
    Last Post: - 17th February 2016, 22:14
  2. darrel taylor's instant interrupt compiler error
    By delta_boogie in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 20th October 2009, 19:07
  3. Clock using Instant Interrupts
    By PICpocket in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 16th February 2009, 21:43
  4. DT instant interrupts with mister_e keypad
    By Tomexx in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 26th November 2008, 20:02
  5. DT's Instant Interrupts trouble
    By Tomexx in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 24th November 2008, 20:48

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts