TMR2 not working..


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1
    Join Date
    Jan 2007
    Location
    Brazil
    Posts
    108

    Default TMR2 not working..

    Hello..

    I'm trying to make 16f88 'interrupt' every 500ms. TMR0/1 are busy. So, I've set TMR2 to interrupt every 1ms and then count var TMR2_clock till 500. But, it is interrupting every 3ms or something... Would you help me?

    Code:
    '------- initial -------------------- 
    TMR2_clock   VAR   WORD
    OPTION_REG = %00000000 
    INTCON 	= %11000000 ' enable interrupts
    TRISB = %00000000 ' portb all output
    TRISA = %00111111 ' some analog inputs
    ANSEL = %00001111 ' some analog inputs
    CMCON = %00000111 ' turn of comparators
    ADCON1 = %10000000' right justified
    T2CON = %00000011 ' TMR2 prescale 1:16
    '--------------------------------------------------- 
    TMR2_clock = 0
        
        
        Lcdout $FE,1 ' clear screen
        
        PR2 = 194 ' load tmr2 PR2 to reset every 1ms
        PIE1.1 = 1 ' enable TMR2 interrupt
        PIR1.1 = 0 ' disabled interrupt flag - TMR2
        T2CON.2 = 1 ' turn TMR2 on
    
        ON INTERRUPT GOTO tick
    
    MAIN:
    ' do nothing
    GOTO MAIN
    
        DISABLE      
    tick:
        PR2 = 194 'reload PR2
        TMR2_clock = TMR2_clock + 1
        Lcdout $FE,$A0, DEC TMR2_clock
        IF TMR2_clock = 500 THEN 
             TMR2_clock = 0
             ' Set some flag..
        ENDIF
        PIR1.1 = 0
    resume
        ENABLE
    Thanks..!

    Sylvio
    Last edited by sirvo; - 1st August 2007 at 20:41.

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


    Did you find this post helpful? Yes | No

    Default

    The default LCD_COMMANDUS is 2000 us. (2 ms)
    So moving the cursor to a specific location within the ISR makes it wait that much longer before it Resume's.

    Add in the 50us for each character displayed, and the 1ms for the interrupt itself, and you get "3ms or something...".

    HTH,
    DT

  3. #3
    Join Date
    Jan 2007
    Location
    Brazil
    Posts
    108


    Did you find this post helpful? Yes | No

    Default

    Right.. i just put the LCDOUT command to compare the count with my hand watch. So, the routine is correct.. thanks Darrel.

    Sylvio

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


    Did you find this post helpful? Yes | No

    Default

    ... So, the routine is correct..
    Good to go ...
    One thing to note ...
    You don't need to reload PR2 each time. It doesn't change.
    <br>
    DT

  5. #5
    Join Date
    Jan 2007
    Location
    Brazil
    Posts
    108


    Did you find this post helpful? Yes | No

    Default

    0.o I've read that a thousand times and still reloaded..Thanks again..

    Sylvio

  6. #6
    Join Date
    Jan 2007
    Location
    Brazil
    Posts
    108


    Did you find this post helpful? Yes | No

    Default tmr2 is running crazy yet

    HI Again... I have tried again taking the LCDOUT command out of interrupt routine and putting inside the MAIN sub. NOthing! It is running crazy and I've also tried setting postscaler 1:16 or/and prescaler 1:16 and still nothing. Does the code look really correct? What I want is to set any flag every 500ms using TMR2... A little help please?

    Thanks again.. Sylvio,

    Code:
    '------- initial -------------------- 
    TMR2_clock   VAR   WORD
    OPTION_REG = %00000000 
    INTCON 	= %11000000 ' enable interrupts
    TRISB = %00000000 ' portb all output
    TRISA = %00111111 ' some analog inputs
    ANSEL = %00001111 ' some analog inputs
    CMCON = %00000111 ' turn of comparators
    ADCON1 = %10000000' right justified
    T2CON = %00000011 ' TMR2 prescale 1:16 --> TESTED WITH POST and PRESCALER (1:16)
    '--------------------------------------------------- 
    TMR2_clock = 0
        
        
        Lcdout $FE,1 ' clear screen
        
        PR2 = 194 ' load tmr2 PR2 to reset every 1ms
        PIE1.1 = 1 ' enable TMR2 interrupt
        PIR1.1 = 0 ' disabled interrupt flag - TMR2
        T2CON.2 = 1 ' turn TMR2 on
    
        ON INTERRUPT GOTO tick
    
    MAIN:
        Lcdout $FE,$A0, DEC TMR2_clock ---> CHANGED
    GOTO MAIN
    
        DISABLE      
    tick:
        TMR2_clock = TMR2_clock + 1
        IF TMR2_clock = 500 THEN 
             TMR2_clock = 0
             ' Set some flag..
        ENDIF
        PIR1.1 = 0
    resume
        ENABLE

Similar Threads

  1. 2x16 lcd not working with pic16f72
    By vu2iia in forum Schematics
    Replies: 4
    Last Post: - 16th February 2011, 14:59
  2. Blink.Bas on 18f45k20 Newbie seeks working example.
    By DiscoEd in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 29th December 2009, 03:36
  3. Pic 16 F628A not working
    By turkuaz in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 17th March 2009, 12:26
  4. Troubled with Instant Ints and TMR2
    By Josuetas in forum mel PIC BASIC Pro
    Replies: 27
    Last Post: - 7th October 2007, 06:23
  5. Hserin not working...
    By robert0 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 22nd August 2005, 12:25

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