tmr2 interrupt problem


Closed Thread
Results 1 to 28 of 28

Hybrid View

  1. #1
    Join Date
    Dec 2007
    Location
    The Netherlands, Groningen
    Posts
    16

    Default tmr2 interrupt problem

    Hello all,

    I have a problem with a tmr2interrupt. I have programmed a several things but that was lways in loops and now 1 want to change my program.

    I use a pic16f628 controller at 4MHz internal ascillator.
    I want to use timer2 to generate an interrupt every 2milliseconds.
    Only problem is that it never gets to the interruptroutine..
    It is for a RTC controlled clock with muxed 7segment displays at two 74hc595 shiftregisters.

    Here is the problem code

    @ DEVICE pic16F628a, INTRC_OSC_NOCLKOUT
    ' System Clock Options
    @ DEVICE pic16F628a, WDT_OFF
    ' Watchdog Timer
    @ DEVICE pic16F628a, PWRT_ON
    ' Power-On Timer
    @ DEVICE pic16F628a, BOD_ON
    ' Brown-Out Detect
    @ DEVICE pic16F628a, MCLR_OFF
    ' Master Clear Options (Internal)
    @ DEVICE pic16F628a, LVP_OFF
    ' Low-Voltage Programming
    @ DEVICE pic16F628a, CPD_OFF
    ' Data Memory Code Protect
    @ DEVICE pic16F628a, PROTECT_OFF
    ' Program Code Protection
    @ DEVICE pic16F628a, MCLR_OFF
    define OSC 4 'runs at 4MHz

    INCLUDE "modedefs.bas" 'for shiftout

    segments var byte
    counter var byte

    Symbol tmr2if=PIR1.1
    Symbol tmr2ie=PIE1.1
    Symbol gie =INTCON.7

    'Turn on INTCON.7(GIE) and INTCON.5 (TMR0IE)
    INTCON = %10100000
    tmr2ie=1
    gie=1

    on interrupt GoTo isr
    GoTo loop

    ;-----------------------------------------------------------
    isr:
    'Clear tmr2if
    disable
    segments = %01111011
    counter=%00000000 'only for test. all segments are on.
    if hr.6=1 then 'mode indication
    segments.7=%1
    endif
    SHIFTOUT PORTB.0, PORTB.1, LSBFIRST,[counter\8,segments\8] 'data, clock
    pulsout PORTA.4,1
    'return
    INTCON.6 =0
    enable
    INTCON.6 =0
    resume

    loop: 'main programloop
    repeat
    'sec=%00000111
    until 0=1
    END

    The full code is about 10 a4 sides of paper..

    I hope that anyone knows what I do wrong and can help me.

    Thanks.
    Martijn

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    almost sure i'm right, you should enable peripheral interrupts as well. INTCON.6=1

    mmm, Tmier2---shouldn't be something to do with PR2 as well?

    i'll dig the datasheet.. been awhile i didn't use that pic now.
    Last edited by mister_e; - 27th December 2007 at 19:18.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Dec 2007
    Location
    The Netherlands, Groningen
    Posts
    16


    Did you find this post helpful? Yes | No

    Default

    Thnx for the fast answere.

    Wel, i only make intcon.6 0 in the isr-loop.

    Hope you can help me..

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    TRY
    Code:
            @ __CONFIG  _INTRC_OSC_NOCLKOUT & _MCLRE_ON  &  _LVP_OFF & _WDT_OFF & _PWRTE_ON  & _BODEN_ON  
    
            CMCON=7
            TRISA=0
            TRISB=0
            PORTA=0
            PORTB=0
          
            PIE1 =  %00000010
                    '0------- Disable EE write complete interrupt
                    '-0------ Disable comparator interrupt
                    '--0----- Disable USART receive interrupt
                    '---0---- Disable USART transmit interrupt
                    '----x--- unimplemented
                    '-----0-- Disable CCP1 interrupt
                    '------1- Enable TMR2 to PR2 match interrupt
                    '-------0 Disable TMR1 overflow interrupt
                    
            T2CON = %00000010
                    'X------- Unimplemented
                    '-0000--- postscale 1:1
                    '-----0-- Timer2 off
                    '------10 prescaler 1:16
                    
                            
            GIE     VAR INTCON.7
            PEIE    VAR INTCON.6
            TMR2IF  VAR PIR1.1
            TMR2ON  VAR T2CON.2
            RELOAD CON 131
            
            ON INTERRUPT GOTO ISR
            TMR2 = RELOAD
            PR2 = 0
            PEIE = 1    ' Enable peripheral interupt
            GIE = 1     ' enable Global interrupt
            TMR2ON = 1  ' TMR2 ON
            GOTO LOOP                
    
            disable
    ISR:
            TOGGLE PORTB.0
            TMR2ON = 0
            TMR2 = RELOAD
            TMR2ON = 1
            TMR2IF = 0  ' Clear interrupt flag
            resume
            enable
            
    LOOP:
            'TOGGLE PORTB.1
            GOTO LOOP
    or another variant of
    Code:
            @ __CONFIG  _INTRC_OSC_NOCLKOUT & _MCLRE_ON  &  _LVP_OFF & _WDT_OFF & _PWRTE_ON  & _BODEN_ON  
    
            CMCON=7
            TRISA=0
            TRISB=0
            PORTA=0
            PORTB=0
          
            PIE1 =  %00000010
                    '0------- Disable EE write complete interrupt
                    '-0------ Disable comparator interrupt
                    '--0----- Disable USART receive interrupt
                    '---0---- Disable USART transmit interrupt
                    '----x--- unimplemented
                    '-----0-- Disable CCP1 interrupt
                    '------1- Enable TMR2 to PR2 match interrupt
                    '-------0 Disable TMR1 overflow interrupt
                    
            T2CON = %00000010
                    'X------- Unimplemented
                    '-0000--- postscale 1:1
                    '-----0-- Timer2 off
                    '------10 prescaler 1:16
                    
                            
            GIE     VAR INTCON.7
            PEIE    VAR INTCON.6
            TMR2IF  VAR PIR1.1
            TMR2ON  VAR T2CON.2
            
            ON INTERRUPT GOTO ISR
            PR2 = 124   ' load period register for 2mSec 
            PEIE = 1    ' Enable peripheral interupt
            GIE = 1     ' enable Global interrupt
            TMR2ON = 1  ' TMR2 ON
            GOTO LOOP                
    
            disable
    ISR:
            TOGGLE PORTB.0
            TMR2IF = 0  ' Clear interrupt flag
            resume
            enable
            
    LOOP:
            'TOGGLE PORTB.1
            GOTO LOOP
    The second one is probably the best method to deal with Timer2. have a look to section 6.0 of your datasheet.
    Last edited by mister_e; - 27th December 2007 at 20:07.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Dec 2007
    Location
    The Netherlands, Groningen
    Posts
    16


    Did you find this post helpful? Yes | No

    Default

    Thanks, it works now!
    First i tried to inplement your code in mine but that didnd work.
    So I fit my program in yours and the first test works.
    Here's my code fore enyone with the same problem.
    Now I'm have to set all my other code in it. Hope it don't make problems with the changed config now.

    Code:
     @ device pic16F628a, INTRC_OSC_NOCLKOUT
     @ device pic16F628a, MCLR_ON
     @ device pic16F628a, LVP_OFF
     @ device pic16F628a, WDT_OFF
     @ device pic16F628a, PWRT_ON
     @ device pic16F628a, BOD_ON   
    INCLUDE "modedefs.bas"  'for shiftout
     
            CMCON=7
            TRISA=0
            TRISB=0
            PORTA=0
            PORTB=0
          
            PIE1 =  %00000010
                    '0------- Disable EE write complete interrupt
                    '-0------ Disable comparator interrupt
                    '--0----- Disable USART receive interrupt
                    '---0---- Disable USART transmit interrupt
                    '----x--- unimplemented
                    '-----0-- Disable CCP1 interrupt
                    '------1- Enable TMR2 to PR2 match interrupt
                    '-------0 Disable TMR1 overflow interrupt
                    
            T2CON = %00000010
                    'X------- Unimplemented
                    '-0000--- postscale 1:1
                    '-----0-- Timer2 off
                    '------10 prescaler 1:16
                    
            segments var byte
            counter var byte                
            GIE     VAR INTCON.7
            PEIE    VAR INTCON.6
            TMR2IF  VAR PIR1.1
            TMR2ON  VAR T2CON.2
            
            ON INTERRUPT GOTO ISR
            PR2 = 124   ' load period register for 2mSec 
            PEIE = 1    ' Enable peripheral interupt
            GIE = 1     ' enable Global interrupt
            TMR2ON = 1  ' TMR2 ON
            GOTO LOOP                
    
            disable
    ISR:
    'first half of the displays
    segments=%01011011 '5
    counter=%01010101  'on displays
    SHIFTOUT PORTB.0, PORTB.1, LSBFIRST,[counter\8,segments\8]   'data, clock
    pulsout PORTA.4,1 'for latch 74hc595
     
    'remaining displays  
    segments = %00110000 '1
    counter=%10101010
    SHIFTOUT PORTB.0, PORTB.1, LSBFIRST,[counter\8,segments\8]   'data, clock
    pulsout PORTA.4,1 'for latch 74hc595
    '------- ^^my try
     
            TOGGLE PORTB.0
            TMR2IF = 0  ' Clear interrupt flag
            resume
            enable
            
    LOOP:
            'TOGGLE PORTB.1
            GOTO LOOP
    end

  6. #6
    Join Date
    Dec 2007
    Location
    The Netherlands, Groningen
    Posts
    16


    Did you find this post helpful? Yes | No

    Default

    Dear,

    I still have a problem.

    I have merged all the code and the beginning works fine now.
    It go's to the loop: makes a I2C read.
    Go's to another loop for I2C write(only if needed).

    Then it have to wait for the the interupt.
    At the interrupt is starts to shiftout the data for the display.
    Until here it works fine.

    The problem is: it keeps doing that and NEVER goes back to the loop..

    This didn't I know becose in the testprogram there was noting to do in the loop..

    I can post the code when someone needs it but it's the same as above, only with some onther code inside it.

    Dos anyone know what I do wrong?

Similar Threads

  1. Problem with Interrupt on PIC18F4620 PORTB
    By rookie in forum Off Topic
    Replies: 1
    Last Post: - 22nd March 2007, 01:34
  2. Problem with PBP interrupt and Serin, please help
    By rgregor in forum mel PIC BASIC
    Replies: 0
    Last Post: - 22nd August 2006, 19:02
  3. Interrupt Problem
    By Kamikaze47 in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 16th November 2005, 20:58
  4. Interrupt stack overflow problem with Resume {label}
    By Yuantu Huang in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 3rd May 2005, 01:17
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

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