PDA

View Full Version : how to make inputs toggle



MARAD75
- 19th February 2007, 13:36
Hi all!
I havnt posted in a while, so here goes!
Using a 12F675 and need a way to toggle an input.
If a button is pushed, it goes to a label that is a simple flash on and off routine which counts up to a number using a for-next loop. When it reaches the number, it stops by going out of that label.
What I need is a way to push the same button again and cause it to stop flashing immediately and reset, regardless of the for next count. (I am also using the MCLR as a momentary pulldown reset to turn everything off by resetting the program to the start of the code)
This push button is only a momentary close, but need it to work like a mechanical "push on-push off" switch. There are a couple of these push buttons which each work with their own outputs
Anyone have ideas - and if so, maybe some sample code?
Thanks much for any help!
Ron

mister_e
- 19th February 2007, 13:59
Sure it can be done with interrupts but here's something to try...


@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON

GPIO = 0
TRISIO = %11111110
ANSEL = 0
CMCON = 7
LED var GPIO.0
PushButton var GPIO.1
Delay var byte
CounterA var Byte
MaxCount con 100

pause 100

Start:
if pushbutton=0 then
while pushbutton=0 : wend
pause 50
Gosub Routine1
endif

goto start

Routine1:
for countera=0 to maxcount
toggle led
GOSUB DoDelay
next
return

DoDelay:
for delay=0 to 50
pause 10
if pushbutton=0 then
while pushbutton=0 : wend
pause 50
delay=50
countera=maxcount
led=0
endif
next
return

MARAD75
- 19th February 2007, 19:52
Steve,
This "something to try" works like a champ!! - thanks much! It is a perfect building block for what I am trying to do.
However, here is a couple of things I can't figure out.
Here is the code as used: (PS I don't use the one line assembly code to set up below. For some reason, I have trouble with it in MSP)
DEFINE OSC 4 '4 MHZ INTERNAL OSCILLATOR
DEFINE OSCAL_1K_1
INTRC_OSC_NOCLKOUT
WDT_ON
PWRTE_ON
MCLRE_ON
BODEN_OFF
GPIO = 0
TRISIO = %00001111
ANSEL = 0
CMCON = 7
LED VAR GPIO.5
PUSHBUTTON VAR GPIO.1
DELAY VAR BYTE
COUNTERA VAR BYTE
MAXCOUNT CON 100
PAUSE 100

START:
IF PUSHBUTTON = 1 THEN
WHILE PUSHBUTTON = 1: WEND
PAUSE 50
GOSUB ROUTINE1
ENDIF
GOTO START

ROUTINE1:
FOR COUNTERA = 0 TO MAXCOUNT
TOGGLE LED
GOSUB DODELAY
NEXT
RETURN

DODELAY:
FOR DELAY = 0 TO 50
PAUSE 10
IF PUSHBUTTON = 1 THEN
WHILE PUSHBUTTON = 1: WEND
PAUSE 50
DELAY = 50
COUNTERA = MAXCOUNT
LED = 0
ENDIF
NEXT
RETURN

Problem is - notice the trisio and what I need is, to push button (gpio.1) to flash gpio.5. The other button (gpio.0) will eventually be set up to flash gpio.4! However, even with the trisio the way it is, the pushbutton flashes ONLY gpio.0. (Can't get it to change to io5) I have erased the chip and reprogrammed several times but will not change.
Secondly, every other time it goes through the routine, the flasher stays on. Can I use, say 99 instead of maxcount in the second routine to trick it into toggling off?
Again, I really appreciate your help. I would not have thought of doing it this way - toooo newbie in this stuff!!!
Ron

MARAD75
- 20th February 2007, 00:38
Steve,
I have since tried another 12F675 and now I'm really baffled!!
I thought MPLAB IDE (release 7.51) was supposed to erase a flash chip! In fact, I checked it afterward and it said "chip is blank". Now, I reprogrammed the other one and it still was flashing gpio.0. The new chip (same hex file) flashes gpio.5! Go figure. I haven't a clue.
Ron

mister_e
- 20th February 2007, 16:09
In MicroCodeStudio, make sure you have selected MPASM as assembler.

Now, those

INTRC_OSC_NOCLKOUT
WDT_ON
PWRTE_ON
MCLRE_ON
BODEN_OFF
don't do anything else than creating Labels.. to bad. So i guess you got the Overwriting previous... MPASM message right?

have a look at the following...
http://www.picbasic.co.uk/forum/showpost.php?p=6775&postcount=5
this will cure the problem

NOW, if your PIC programmer have erased the OSCCAL value one day or another, your example will NEVER work.. or with some trouble. remove the following

DEFINE OSCAL_1K_1

Bah.. anyways, you're lucky, the way you wrote it is bad.. hence it do nothing. DEFINE OSCCAL_1K 1

Using your code with my config fuse line, it's working as it suppose to.

Last thing, make sure you have a pull-down resistor on GPIO.1.