This program works, sometimes for a couple minutes, some times only for a few seconds, but always crashes eventually. I'm using a PIC12F675 and I've read that it is not the most reliable chip. So I'm wondering if there's something wrong with my code or maybe it's the chip. If it were my code, I would think it would be more consistent in the point at which it crashed or simply wouldn't work at all.
This is what happens:
Push button1 and its LEDs work perfectly all the time. It's Push button2 that causes the problems. Eventually Push Button2 and its LEDs lock up and will not respond. When this happens, the LEDs associated with Push button1 toggle once. If I press Push button1, this seems to reset Push Button 2, and Push Button 2 and its LEDs will work fine until it locks up again.
The code for push button2 is identical to push button1, it just uses different variable names and I/Os
' Name : BUTTONTEST2.pbp
' Compiler : PICBASIC PRO Compiler 3.0
' Assembler : PM
' Target PIC : 8-pin PIC12F675 or similar type
' Hardware : Kee
' Oscillator : 4MHz internal
' Keywords : HIGH, LOW
' Description : use a push button to toggle two LEDs. THere are two push buttons.
'each push button toggles a separate set of LEDs. Program also debounces
#config
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _CPD_OFF
#endconfig
Define OSCCAL_1K 1 ' PIC12F675, Calibrate internal oscillatordefine MCLRE_OFF
define BUTTON_PAUSE 5
LED Con 0 'Alias GPIO.0 to LED
LED2 con 1 'Alias GPIO.1 to LED2
LED3 con 4 'Alias GPIO.4 to LED3
LED4 con 5 'Alias GPIO.5 to LED4
PB Var GPIO.2 'Alias GPIO.2(pin 5) to PB(push button1)
PB2 var GPIO.3 'Alias GPIO.3(pin 4) to PB2(push button2)
LEDstate var bit 'LED on = 1, LED off = 0
LEDstate2 var bit 'LED4 on = 1, LED4 off = 0
ANSEL = 0 'Set all digital
CMCON = 7 'Analog comparators off
high LED 'begin with LED on
low LED2 'begin with LED2 off
high LED4 'begin with LED4 on
low LED3 'begin with LED2 off
LEDstate = 1 'begin with LED recognized as on
LEDstate2 = 1 'begin with LED4 recognized as off
main:
If (PB = 0) and (LEDstate = 1) Then 'push button1 is pressed when LED is on
low LED 'toggle LED off
high LED2 'toggle LED2 on
pause 10 'wait 10ms for debounce
goto WaitForRelease1 'go to subroutine for releasing push button1
endif
if (PB = 0) and (LEDstate = 0) then 'push button1 is pressed when LED is off
high LED 'toggle LED on
low LED2 'toggle LED2 off
pause 10 'wait 10ms for debounce
goto WaitForRelease2 'go to subroutine for releasing push button1
Endif
If (PB2 = 0) and (LEDstate2 = 1) Then 'push button2 is pressed when LED4 is on
low LED4 'toggle LED4 off
high LED3 'toggle LED3 on
pause 10 'wait 10ms for debounce
goto WaitForRelease3 'go to subroutine for releasing push button2
endif
if (PB2 = 0) and (LEDstate2 = 0) then 'push button2 is pressed when LED4 is off
high LED4 'toggle LED4 on
low LED3 'toggle LED3 off
pause 10 'wait 10ms for debounce
goto WaitForRelease4 'go to subroutine for releasing push button2
Endif
goto main
'subroutines for action upon release of push buttons. Debounces, changes LEDstate() variable,
'and prevents looping when button is pressed and held
WaitForRelease1:
if PB = 1 then 'push button1 is released after toggling LED off
pause 10 'wait 10ms for debounce
LEDstate = 0 'recognize LED as off
endif
goto main
WaitForRelease2:
if PB = 1 then 'push button1 is released after toggling LED on
pause 10 'wait 10ms for debounce
LEDstate = 1 'recognize LED as on
endif
goto main
WaitForRelease3:
if PB2 = 1 then 'push button2 is released after toggling LED4 off
pause 10 'wait 10ms for debounce
LEDstate2 = 0 'recognize LED4 as off
endif
goto main
WaitForRelease4:
if PB2 = 1 then 'push button2 is released after toggling LED4 on
pause 10 'wait 10ms for debounce
LEDstate2 = 1 'recognize LED4 as on
endif
goto main
'end of subroutines
End
Bookmarks