PDA

View Full Version : TMR0 overflow problem



jderson
- 17th June 2008, 03:46
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

paul borgmeier
- 17th June 2008, 07:43
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:



Runtimer:

TMR0ON = 1

IF TMR0IF = 1 THEN
HIGH PORTB.4
TMR0IF=0 ' clear flag after you have trapped it
ENDIF

GOTO Main

jderson
- 17th June 2008, 14:46
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.

jderson
- 17th June 2008, 19:35
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?

skimask
- 17th June 2008, 21:30
http://www.picbasic.co.uk/forum/showthread.php?t=543

jderson
- 17th June 2008, 22:49
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.

skimask
- 18th June 2008, 03:40
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.

jderson
- 18th June 2008, 04:13
And where might I find the "config options" file?

skimask
- 18th June 2008, 04:30
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 :D
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.

jderson
- 18th June 2008, 04:47
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!)

skimask
- 18th June 2008, 04:50
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.