use of Timer1 and portA interrupts together


Closed Thread
Results 1 to 14 of 14

Hybrid View

  1. #1
    Join Date
    Dec 2009
    Location
    Canada
    Posts
    68

    Default use of Timer1 and portA interrupts together

    Hi Gentlemen,

    I have met an unexpected obstakle today when tried to use TIMER1 interrupt while have PORTA interrupts enabled. Will very apreciate it if someone could look:


    @ DEVICE pic16F688, WDT_OFF, INTOSCIO, PWRT_ON, MCLR_ON, BOD_OFF
    @ DEVICE CPD_OFF, PROTECT_OFF 'DATA AND CODE MEMORY PROTECTION
    define OSC 4
    REDLED VAR PORTC.4
    GREENLED VAR PORTC.5
    clear

    CMCON0 = 7 'TURN COMPARATORS OFF
    VRCON = 0 'VOLTAGE REFERENCE OFF TO SAVE POWER OPTION_REG.7 = 0
    OPTION_REG = 0
    ANSEL = %1000 'PORTA4/AN3 IS ANALOG

    PORTC=0
    TRISC = 0

    LINE21: INTCON.3 = 1 'this is the problem!!!
    INTCON.4 = 1
    LINE23: OCA = %100100 'ENABLES INTERRUPTS ON CHANGE ON PORTA.2 'AND PORTA.5 - ANOTHER part of the PROBLEM
    T1CON.0 = 1 'TIMER ON
    T1CON.1 = 0 'INT CLOCK Fosc/4
    T1CON.4 = 1 'PRESCALER
    T1CON.5 = 1 'PRESCALER
    T1CON.6 = 0
    PIE1.0=1 'TIMER1 OVERFLOW INTERRUPT ENABLE BIT (TMR1IE)
    INTCON.6 = 1 'PERIPHERAL INT ENABLE
    INTCON.7 = 1 'GLOBAL INT ENABLE
    ON INTERRUPT GOTO INT_CHECK
    START:
    GOTO START 'WAIT HERE TILL INTERRUPTED

    DISABLE
    INT_CHECK:
    IF GREENLED = 0 THEN
    GREENLED =1
    REDLED=0
    ELSE
    GREENLED =0
    REDLED =1
    ENDIF
    PIR1.0 = 0
    RESUME
    ENABLE

    When I comment line 21 or line 23 (not necessary both), then my timer interrupt works. If those lines are uncommented, then I have both LEDs ON and nothing work. Interrupt on change (lines marked as line21: and line23: needed for a part of code removed from here to have this text smaller

    Please look if I can use Timer1 in this situation at all or have to use Timer0 instead. The MCU is PIC16F688

    Thank you very much

    Alexey

  2. #2
    Join Date
    Dec 2009
    Location
    Canada
    Posts
    68


    Did you find this post helpful? Yes | No

    Default Re: use of Timer1 and portA interrupts together

    I just realized that having both LEDs on means that they trigger too fast. Is it possible that activating Interrupt on Change on PORTA I affect my TIMER1 prescaler somehow?

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


    Did you find this post helpful? Yes | No

    Default Re: use of Timer1 and portA interrupts together

    You have enabled Interrupt On Change (INTCON.3), External interrupt (INTCON.4) and Timer1 Ints (PIE1.0).
    But the ISR only clears the flag for Timer1. So it will be continuously interrupting when the other interrupts get triggered.

    It needs to differentiate between the different interrupts, run separate code depending on which one fired, and clear the appropriate flags before resuming.

    Remember that with Int On Change you have to read the port to clear the mismatch condition before you can clear the flag.
    DT

  4. #4
    Join Date
    Dec 2009
    Location
    Canada
    Posts
    68


    Did you find this post helpful? Yes | No

    Default Re: use of Timer1 and portA interrupts together

    Hello Darrel,

    Strange why my interrupts on change affect the timer if I do not press buttons, but yes, clearing those flags makes the timer interrupts work better with regular intervals. Also I added IF PIR!.0 condition in the interrupt handler, but it seems not working well. if interrupt on change flags are not cleared I still have irregular time interwals - probably because of skipping timer handler when other interrupts trigger I do not know what is the reason.

    if this something to do with oscillator mode? I use internal osc at 4 MHz, PORTA uses inputs on A.2 and A.5 and A.4 is analog
    PORTC is used bor DEBUGIN on C.! and others are outputs

    I noticed it works better with HS setting for oscillatio for timer interrupt and Int on change seems need INTOSCIO which mode is correct?

    Thanks again,
    Alexey

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


    Did you find this post helpful? Yes | No

    Default Re: use of Timer1 and portA interrupts together

    Did you add IF tests for all the interrupts you are using? (IOC and INT)
    Are you sure you want to use INT? The INT pin is also an IOC pin.

    For a 4Mhz crystal, you would use the XT oscillator mode.
    The oscillator mode has no effect on IOC interrupts.
    And whether it's internal or external, as long as the OSC is 4mhz, it has no effect on the Timer interrupts either.
    DT

  6. #6
    Join Date
    Dec 2009
    Location
    Canada
    Posts
    68


    Did you find this post helpful? Yes | No

    Default Re: use of Timer1 and portA interrupts together

    Hello Darrel,

    Yes, I already learnt that interrupts require a great attention and careful programming Sometimes I could not understand why I am getting different results with (on my opinion) same approach but probably just one little thing outside of my attention makes all difference. I just noticed that simple use of SLEEP in a branch that is not executed when my TIMER1 is used does not allow timer to work Simple commenting SLEEP in that branch restores work of the TIMER1. Not sure why. Maybe if I use sleep, PICBASIC reserves the timer1 for it, although I thought it uses TIMER0 (will look how WDT affects this - maybe I had it disabled - will check)
    I use interrupt on change on PORTA.5 and RA2/INT on PORTA.2 which helps to filter when A.2 button pressed and when I have signal on A.5 Yes, I do use IFs to handle interrupts

    Oh... how many things I need to learn...

    Thank you for help!
    Last edited by Alexey; - 10th June 2011 at 05:53.

Members who have read this thread : 0

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