PDA

View Full Version : Timer1 and Interupt wierdness



mind
- 23rd August 2005, 01:24
Hi all

I have a 4MHz PIC16F88 with a button, a serial LCD and a LED. Eventually, the LED will be the LCD backlight on a timer after the user has pressed a button. This is just for testing things at the moment, but I am being driven NUTS trying to figure out what is wrong.

TIMER1 is enabled, and counts up. When an overflow is reached, it toggles the LED. The button is used for a menu system. Everything is interrupt driven and works nicely, except for one issue:

It appears that pressing the button starts/stops TIMER1 !

Since I can't see anything in the code that is responsible for this, I think it may be something nasty with how Picbasic handles interupts or uses the timer for SLEEP or PAUSE.

If anyone has ANY ideas or pointers for debugging this, it would be greatly appreciated.

Best regards
Richard

Code snipet below:


disable interrupt
timer var word
timer = 0

gosub LCD_Startup
gosub Startup_Display ' Splash screen
gosub StartupTimer ' Timer1 should startup from here
on interrupt Goto Keyhandler
enable interrupt

waitforkey:
pauseus 10 ' will use sleep/low power mode later
goto waitforkey
disable interrupt
end
' ******************************
KeyHandler:
if PIR1.0 then TimerTimeout
if Button_menu = 1 then MenuButton_Down
' something else happened ?
reSUME
MenuButton_Down:
while button_menu = 1
NAP 3
Wend
pause 50 ' debouce - do not remove
INTCON.0 = 0 ' clear mismatch
if menu = 0 then Setmenu1
if menu = 1 then Setmenu2
RESUME
' ******************************
StartupTimer:
T1CON = %00110000 ' Timer1 1:8 prescale.
TMR1H = 0 ' Clear time counts before Timer1 re-start
TMR1L = 0 ' Clear time counts before Timer1 re-start
PIR1.0 = 0 ' Clear over-flow flag before enable
T1CON.0 = 1 ' Turn Timer1 on
PIE1.0 = 1 ' Enable Timer1 interupt
return

TimerTimeout:
gosub DOubleLineMode
gosub MoveToLineTwo
shiftout LCD_SerialIn, LCD_Clock, mode, ["Timer Incr ", timer+48]
pause 250
timer = timer + 1
if timer > 9 then ' granularity is 65msec * scaler
if LCD_Backlight then
LCD_Backlight = 0
else
lcd_backlight = 1
endif
timer = 0
endif
gosub StartupTimer
RESUME