Instant Interrupts - Revisited


Results 1 to 40 of 773

Threaded View

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


    Did you find this post helpful? Yes | No

    Default Advanced Blinky Light (fader)

    Chris,

    The prescaler can be changed with T1CON.
    $31 = 1:8
    $21 = 1:4
    $11 = 1:2
    $01 = 1:1

    And the Timer is reloaded by putting values in TMR1H:TMR1L.

    So first, decide what frequency you need, I'll go with 100hz.

    Then, go to this thread...
    Testing Forms [TimerCalc] Working I think?
    http://www.picbasic.co.uk/forum/showthread.php?t=2031

    Enter the OSC value you are using, say 20mhz.
    I'll also go with 1:1 prescaler, enter that. (ok, it's already there)
    Then enter 100 in the Freq field at the bottom.

    Now you can see that it will take a total of 50,000 counts (ticks) to achieve 100hz.

    Decide what resolution you want for the Duty-Cycle. ummm, 8-bit (256).

    Now you just calculate the ON count from that with...
    50000*DutyCycle/256 which is the same thing as...
    50000*/DutyCycle in PBP
    and the OFF count is 50000 - ONcount

    Ok, so let's put that into the Blinky Light example


    Code:
    
    ; Initialize your hardware first.
    
    
    INCLUDE "DT_INTS-18.bas"     ' Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas"     ' Include if using PBP interrupts
    
    
    LED1       VAR PORTB.1
    DutyCycle  VAR BYTE
    
    
    TotalCount CON 50000
    ONcount    VAR WORD
    OFFcount   VAR WORD
    
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1_INT,  _ToggleLED1,   PBP,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    
    
    T1CON = $00                ; Prescaler=1:1, TMR OFF
    @ INT_ENABLE  TMR1_INT     ; enable Timer 1 interrupts
    
    
    Main:
        FOR DutyCycle = 0 TO 255           ; Ramp up
            GOSUB  SetDutyCycle
            PAUSE  5
        NEXT DutyCycle
    
    
        FOR DutyCycle = 254 TO 1 STEP -1   ; Ramp down
            GOSUB  SetDutyCycle
            PAUSE  5
        NEXT DutyCycle
    GOTO Main
    
    
    
    
    SetDutyCycle:
        ONcount = TotalCount*/DutyCycle
        OFFcount = TotalCount - ONcount
        IF DutyCycle = 0 THEN
            T1CON.0 = 0    ; turn off timer
            LOW  LED1      ; idle LOW
        ELSE
            T1CON.0 = 1    ; timer on if DutyCycle > 0
        ENDIF 
    RETURN
    
    
    DISABLE DEBUG
    '---[TMR1 - interrupt handler]------------------------------------------------
    @Timer1=TMR1L             ; map timer registers to a word variable
    Timer1 VAR WORD EXT
    
    
    ToggleLED1:
        IF LED1 THEN
            LOW LED1
            T1CON.0 = 0         ; stop the timer
            Timer1  = OFFcount  ; Load OFF count
            T1CON.0 = 1         ; start timer
        ELSE
            HIGH LED1
            T1CON.0 = 0         ; stop the timer
            Timer1  = ONcount   ; Load ON count
            T1CON.0 = 1         ; start timer
        ENDIF
    @ INT_RETURN
    ENABLE DEBUG
    
    Last edited by mackrackit; - 1st February 2012 at 09:35. Reason: HTML
    DT

Similar Threads

  1. Clock using Instant Interrupts
    By PICpocket in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 16th February 2009, 21:43
  2. DT instant interrupts with mister_e keypad
    By Tomexx in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 26th November 2008, 20:02
  3. DT's Instant Interrupts trouble
    By Tomexx in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 24th November 2008, 20:48
  4. Keypad and DT's Instant Interrupts
    By Homerclese in forum General
    Replies: 11
    Last Post: - 27th April 2007, 06:32
  5. Replies: 1
    Last Post: - 1st November 2006, 03:11

Members who have read this thread : 6

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

Tags for this Thread

Posting Permissions

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