how to make inputs toggle


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2005
    Posts
    23

    Smile how to make inputs toggle

    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
    Last edited by MARAD75; - 19th February 2007 at 14:01.

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Sure it can be done with interrupts but here's something to try...
    Code:
            @ __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
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Dec 2005
    Posts
    23


    Did you find this post helpful? Yes | No

    Thumbs up Thanks Steve!

    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

  4. #4
    Join Date
    Dec 2005
    Posts
    23


    Did you find this post helpful? Yes | No

    Red face One more thing

    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

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    In MicroCodeStudio, make sure you have selected MPASM as assembler.

    Now, those
    Code:
    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/show...75&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
    Code:
    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.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Toggle command
    By mr.sneezy in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 16th December 2011, 02:07
  2. How come they make electronics hard as poss
    By George in forum Off Topic
    Replies: 9
    Last Post: - 13th July 2007, 23:54
  3. TOGGLE code space and ICD
    By duncan303 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 25th October 2006, 16:08
  4. dip switchs & inputs
    By grounded in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 19th September 2006, 13:37
  5. Using portb as inputs PIC goes crazy
    By peterdeco1 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 14th November 2005, 14:15

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts