using darrel taylor's instant interrupts


Closed Thread
Results 1 to 14 of 14

Hybrid View

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

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