PDA

View Full Version : Interrupts and WDT are driving me nuts!



andywpg
- 27th February 2013, 01:49
I am adding a feature to an existing device (I know, I know, leave well enough alone).

Essentially, when one of the switches is on (logic low), I want an LED on steady. When the switch is off, I want the LED to be on for 1/4 second out of two - in other word, off for 1.75 seconds.

To accomplish this I planned on a sleep (already in the program - the existing wakes up only on change of state for PORTB.0) and have it wake up every 250 (or so) milliseconds, go to the interrupt handler and, based on a counter, turn the LED on or off.

I have used interrupts when I programmed in assembler but never in PBP.

I have tried several versions of this, and have even pared the program down to JUST doing the above - but no matter what I do, it doesn't function properly - the blinking doesn't happen, but the switch DOES turn on the LED as long as its held. Almost like the WDT is not running, or its not jumping to the interrupt handler.

BTW, its a 16F886.

Anyway, below is the listing. If anyone has any idea what I'm doing wrong, let me know - I'm tired of tearing my hair out. And this is PERSONAL now! :wink:


#CONFIG
__config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _LVP_OFF & _CP_OFF

#ENDCONFIG

DEFINE OSC 8 'LETS PBP KNOW THE OSCILLATOR IS RUNNING AT 8MHz

OSCCON = %01110001 '8 MHz INTERNAL OSCILLATOR, INTERNAL OSCILLATOR IS USED FOR SYSTEM CLOCK

DISABLE DEBUG 'NO DEBUG

ANSEL = 0 'PORTA ALL DIGITAL
ANSELH = 0 'PORTB ALL DIGITAL

OPTION_REG = %10000000 'PORT B PULLUPS ENABLED, TMR0 PRESCALER IS *NOT* ASSIGNED TO WDT
WPUB = %01111111 'PULL UPS ENABLED ON PORTB 0-6

INTCON = %10001000 'GIE ENABLED, PORTB CHANGE OF STATE INTERRUPT ENABLED
IOCB = %00000001 'PORTB.0 INTERRUPT ON CHANGE ENABLED
WDTCON = %00010000 '1:8192 PRESCALER (272MS)

TRISA = 0 'PORTA ALL OUTPUTS
TRISB = %01111111 'B7 OUTPUT, ALL THE OTHERS INPUTS
TRISC = 0 'PORTC ALL OUTPUTS

SWITCH VAR PORTB.0
LED VAR PORTC.6

DEBOUNCE VAR BYTE
DUMMY VAR BYTE
BLINK_COUNT VAR BYTE

DEBOUNCE = 30 'MILLISECONDS

ON INTERRUPT GOTO MY_INT
BLINK_COUNT = 0
MAIN_LOOP:
DO 'ENDLESS LOOP
IF SWITCH = 0 THEN 'SWITCH ON
PAUSE DEBOUNCE 'DELAY TO DEBOUNCE
IF SWITCH = 0 THEN 'SWITCH STILL ON
DISABLE INTERRUPT
LED = 1 'TURN LED ON STEADY WHILE SWITCH IS OPERATED
DO WHILE SWITCH = 0
LOOP
PAUSE DEBOUNCE
LED = 0 'TURN OFF THE LED TO ALLOW THE INTERRUPT HANDLER TO BLINK IT
BLINK_COUNT = 0
GOTO DONE_LOOP
ENDIF
ENDIF
DONE_LOOP:
INTCON = %10001000 'TURN ON GIE AND INTERRUPTS FOR PORTB.0 CHANGE OF STATE AND CLEAR FLAGS
WDTCON = %00010000 '1:8192 PRESCALER - 272MS (CLRWDT RESETS THIS TO 17MS)
ENABLE INTERRUPT
ASM
SLEEP 'SLEEP UNTIL WDT TIMES OUT OR PORTB.0 CHANGES STATE
NOP 'NOP BECAUSE THE PROCESSOR PREFETCHES PC+1 ON SLEEP
ENDASM

LOOP

DISABLE 'DISABLE INTERRUPTS BELOW THIS POINT
MY_INT:
DUMMY = PORTB 'READ PORTB TO CLEAR THE SWITCH MISMATCH IF THAT GENERATED THE INTERRUPT
IF BLINK_COUNT = 8 THEN BLINK_COUNT = 0
IF STATUS.4 = 0 THEN 'WDT TIME OUT OCCURRED
BLINK_COUNT = BLINK_COUNT + 1
IF BLINK_COUNT > 1 THEN
LED = 0
ELSE
LED = 1
ENDIF
ENDIF
INTCON = %10001000 'TURN ON GIE AND INTERRUPT PORTB.0 CHANGE OF STATE, AND CLEAR FLAGS
WDTCON = %00010000 '1:8192 PRESCALER - 272MS (CLRWDT RESETS THIS TO 17MS)
RESUME 'IF NO WDT TIME-OUT OCCURRED, THEN THE SWITCH WAS TURNED ON NO NEED TO DO ANYTHING
'AS THE SWITCH WILL BE CAUGHT BY THE MAIN LOOP

END