TMR0 overflow problem


Closed Thread
Results 1 to 11 of 11
  1. #1

    Default TMR0 overflow problem

    I am trying to use 18F1220 TMR0 as a timer. I have a momentary switch to ground on RB0, and I am looking with a scope for RB4 to go high when the timer overflows. I never see RB4 go high, even if I hold the switch down for a long period of time. Below is my code. Any help would be appreciated. (Compiles fine using MPASM). RB6 supplies power to the LCD.

    TRISA = %00100000
    TRISB = %00000111
    WDTCON = 0
    ADCON0 = 0
    ADCON1 = %01111111
    LVDCON = 0
    CCP1CON = 0
    OSCCON = %01101110

    HIGH PORTB.6

    DEFINE LCD_DREG PORTA
    DEFINE LCD_DBIT 0
    DEFINE LCD_RSREG PORTA
    DEFINE LCD_RSBIT 4
    DEFINE LCD_EREG PORTB
    DEFINE LCD_EBIT 3
    DEFINE LCD_BITS 4
    DEFINE LCD_LINES 2
    DEFINE LCD_COMMANDUS 2000
    DEFINE LCD_DATAUS 50

    TMR0ON VAR T0CON.7
    T08BIT VAR T0CON.6
    T0CS VAR T0CON.5
    T0SE VAR T0CON.4
    T0PSA VAR T0CON.3
    T0PS2 VAR T0CON.2
    T0PS1 VAR T0CON.1
    T0PS0 VAR T0CON.0

    GIE VAR INTCON.7
    PEIE VAR INTCON.6
    TMR0IE VAR INTCON.5
    INT0IE VAR INTCON.4
    RBIE VAR INTCON.3
    TMR0IF VAR INTCON.2
    INT0IF VAR INTCON.1
    RBIF VAR INTCON.0

    Main:

    LCDOUT $fe,1
    LCDOUT $fe,$C0

    TMR0ON = 0
    TMR0IF = 0

    T08BIT = 0
    T0CS = 0
    T0PSA = 0
    T0PS2 = 1
    T0PS1 = 1
    T0PS0 = 1
    GIE = 1


    IF PORTB.0 = 0 THEN GOTO Runtimer

    GOTO Main

    Runtimer:

    TMR0ON = 1
    TMR0IF = 0

    IF TMR0IF = 1 THEN HIGH PORTB.4

    GOTO Main

    Thank you-
    Dave

  2. #2
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Code:
    Runtimer:
    
    TMR0ON = 1
    TMR0IF = 0
    
    IF TMR0IF = 1 THEN HIGH PORTB.4
    
    GOTO Main
    Your polling technique has holes. When you enter Runtimer, you clear the flag and turn the timer on. If it has not overflowed, you go to main and spend a lot of time talking to your LCD ... then you turn the timer off. If the TMR0 overflows while in the LCD routine (odds are that this is where it will overflow), you clear the flag right before polling. Change to something like this:

    Code:
    Runtimer:
    
    TMR0ON = 1
    
    IF TMR0IF = 1 THEN 
    	HIGH PORTB.4
    	TMR0IF=0		' clear flag after you have trapped it
    ENDIF
    
    GOTO Main
    Last edited by paul borgmeier; - 17th June 2008 at 07:49. Reason: EDIT: also add comments - makes your code easier to read
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  3. #3


    Did you find this post helpful? Yes | No

    Default

    Thank you for your help Paul. This looked like a logical solution, but now RB4 goes high on powerup, and stays there. I tried adding "LOW PORTB.4" after the ENDIF, and it goes low and never goes high! Strange behavior, I think.

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Now, I'm really confused. After reading the datasheet again, I tried to add the following lines:

    CONFIG1H = %00001000 'INT OSC Switchover & FSCM disabled, INT OSC enabled
    CONFIG2L = %00000001 'BOR, PWRTEN disabled
    CONFIG2H = 0 'WDT disabled
    CONFIG3H = 0 'MCLR disabled
    CONFIG4L = %10000000 'DEBUG, LVP, STVR disabled
    CONFIG5L = %00000011 'CODE PROTECT disabled
    CONFIG5H = %11000000 'EEPROM & BOOT BLOCK not code protected
    CONFIG6L = %00000011 'WRITE PROTECT off
    CONFIG6H = %11100000 'EEPROM & BOOT BLOCK not write protected
    CONFIG7L = %00000011 'TABLE READ not protected
    CINFIG7H = %01000000 'BOOT BLOCK TABLE READ not protected

    MPASM says "Syntax error". Why? Programs won't compile using PBP 2.50, it says use MPASM. Is this correct?

  5. #5
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Skimask-

    Yes, I read Melanie's fine post. That's why I put those lines in the program. She also said to use what's in the .INC file. There are only three items in my .INC file, none of which correspond exactly to the datasheet.

  7. #7
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by jderson View Post
    Skimask-

    Yes, I read Melanie's fine post. That's why I put those lines in the program. She also said to use what's in the .INC file. There are only three items in my .INC file, none of which correspond exactly to the datasheet.
    Might want to re-read that post again then...because you're not really supposed to add lines to your main program...you can, but there's a few things you have to do first.
    The items in the INC file are also listed in another file, the config options that is.

  8. #8


    Did you find this post helpful? Yes | No

    Default

    And where might I find the "config options" file?

  9. #9
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by jderson View Post
    And where might I find the "config options" file?
    Sorry...I thought I typed more in there Don't know what happened. I'll blame it on Windows
    Umm....config options...stand by for further edit...

    Ok, when I need to know what my actual config options are, as defined by the assembler, MPASM (because the compiler, PBP, could generally care less what your config options are), mine are located at:
    C:\Program Files\Microchip\MPASM Suite\
    And in your case it would be the:
    P18F1220.INC file, way down at the bottom of the file. Paste those options into the PBP INC file as described by the above link and you should be good to go.
    OR, as described by the link above, you can comment out all of the config options in the PBP INC file, and paste them as one line assembly lines into your main program.
    Personally, I almost always use the same config options for each style of chip, therefore, I comment out the 'stock' config options in the PBP INC file for the chip I'm using, and paste in the config options that I always want, as described in the link above. That way, every time I use a particular chip, I'm always configuring it the same way, every time, no surprises. If I go to a different chip, I modify PBP's INC file for that chip, and do it the same way, as described in the link above.
    Last edited by skimask; - 18th June 2008 at 04:37.

  10. #10


    Did you find this post helpful? Yes | No

    Default

    Thank you for your help. I found the file, and I will add them to my program (in the proper manner) tomorrow. I still wonder why PBP does not support this device, when the sales literature says it does. (Not that it matters for the problem I'm having!)

  11. #11
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by jderson View Post
    Thank you for your help. I found the file, and I will add them to my program (in the proper manner) tomorrow. I still wonder why PBP does not support this device, when the sales literature says it does. (Not that it matters for the problem I'm having!)
    It does support it. It has since PBP 2.43, a LONG time ago.
    Changing the config options isn't something new (look at the date on the post of the aforementioned link). PBP doesn't handle config options (it can, but that's not it's job), that's the assembler's job.

Similar Threads

  1. COUNT is not counting again
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 19th June 2009, 04:52
  2. Can't ID interrupt source with this IntHandler??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 3rd June 2009, 02:35
  3. help: TMR0 interrupts disabling PORTAchange interrupts???
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th August 2008, 15:10
  4. Measuring change of frequency with PIC
    By Aussie in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 19th July 2007, 01:47
  5. 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

Members who have read this thread : 1

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