Newbie - 16F628A and switch latching


Closed Thread
Results 1 to 40 of 46

Hybrid View

  1. #1
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Wink the last ...

    Hi, Malc

    try this:

    CMCON=7
    TRISB=%00000011 'set RB0 & 1 as input and the rest output

    SW1 var PORTB.0 'switch input pin 6 (RB0)
    SW2 var PORTB.1 'switch input pin 7 (RB1)


    LED1 var PORTB.4 'LED1 on pin 10 (RB4)
    LED2 var PORTB.5 'LED1 on pin 11 (RB5)

    Swset1 var bit
    Swset2 var bit

    Swset5 var bit


    low led1
    low led2

    Swset1=0
    Swset2=0

    swset5=0 'Var swset is set low

    Main:

    if SW1=0 then
    'If switch is LOW (Grounded) then

    IF swset5 =1 THEN

    swset1 = 0
    swset2 = 0
    swset5 = 0

    loop: IF sw1=0 then loop 'wait for button release ... Yessss !!!

    GOTO jump

    ENDIF

    Toggle Swset1 'change swset1 from 0 to 1
    pause 500 'debounce delay
    endif


    if SW2=0 then 'If switch is LOW (Grounded) then
    Toggle Swset2 'change swset2 from 0 to 1
    pause 500 'debounce delay
    endif

    Jump:

    If Swset1=1 then 'if swset1 is 1
    high led1 'then turn on RB4 (and hence the LED)
    else
    low led1 'else, LED is off
    endif

    If Swset2=1 then 'if swset2 is 1
    high led2 'then turn on RB5 (and hence the LED)
    else
    low led2 'else, LED is off
    endif

    If swset1=1 and swset2=1 then
    toggle swset5
    endif

    Goto Main
    End

    And now try to understand WHY !!!

    Alain
    Last edited by Acetronics2; - 15th May 2006 at 15:36.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  2. #2
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics
    Hi, Malc

    try this:
    Thanks, and yes it compiles OK and in tests works as required approx 70% (ie 7 out of 10) of the time. On three attepts LED2 remained lit, but its a lot better than my 0% success rate


    And now try to understand WHY !!!
    OK I'll have a go...


    CMCON=7
    TRISB=%00000011 'set RB0 & 1 as input and the rest output

    SW1 var PORTB.0 'switch input pin 6 (RB0)
    SW2 var PORTB.1 'switch input pin 7 (RB1)


    LED1 var PORTB.4 'LED1 on pin 10 (RB4)
    LED2 var PORTB.5 'LED1 on pin 11 (RB5)

    Swset1 var bit
    Swset2 var bit

    Swset5 var bit


    low led1
    low led2

    Swset1=0
    Swset2=0

    swset5=0 'Var swset is set low
    Sets up the PIC and the varibles

    Main:

    if SW1=0 then
    'If switch is LOW (Grounded) then

    IF swset5 =1 THEN

    swset1 = 0
    swset2 = 0
    swset5 = 0
    If pin is grounded then check to see if varible bit swset5 is set high, and if so then swset 1,2 and 3 are all set low

    loop: IF sw1=0 then loop 'wait for button release ... Yessss !!!
    constantly loop round checking pin sw1 to see if grounded


    GOTO jump

    ENDIF
    and if so, goto the section of the code labled jump....


    Jump:

    If Swset1=1 then 'if swset1 is 1
    high led1 'then turn on RB4 (and hence the LED)
    else
    low led1 'else, LED is off
    endif

    If Swset2=1 then 'if swset2 is 1
    high led2 'then turn on RB5 (and hence the LED)
    else
    low led2 'else, LED is off
    endif
    if the varibles swset 1 is high then turn on its corresponding LED, if low then turn off the LED, same thing for swset2

    If swset1=1 and swset2=1 then
    toggle swset5
    endif

    Goto Main
    If swset1 and swset2 are both high then change the state of swset5 to high, which iwhen checked by the line above

    IF swset5 =1 THEN

    swset1 = 0
    swset2 = 0
    swset5 = 0

    resets swset1, swset2 and swset5 back to 0, and conversly all LEDs are turned off


    Toggle Swset1 'change swset1 from 0 to 1
    pause 500 'debounce delay
    endif


    if SW2=0 then 'If switch is LOW (Grounded) then
    Toggle Swset2 'change swset2 from 0 to 1
    pause 500 'debounce delay
    endif


    End
    Checks sw2 and changes the state of swset2 (and thus LED2)


    Well how did I do ??

  3. #3
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Guys you crack me up... here am I trying to get technical and you guys are trying to put me mind off the subject by posting pictures of pussy on a public forum

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


    Did you find this post helpful? Yes | No

    Default

    i hope you remember that you miss one word !

    The Melanie bad hair day is back!
    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=445&d=1125012225">

    While we're at that... http://www.picbasic.co.uk/forum/showthread.php?t=2050
    Last edited by mister_e; - 16th May 2006 at 03:20.
    Steve

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

  5. #5
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    That's a shame, either its my browser or the plug in, but I only get audio when viewing that clip

    With regards to the topic of this thread (Mel, I am trying to keep this on topic !) I hooked up a test track and placed a magnet on a loco to see how it worked, as I had been using magnets passing over the switches by hand to test the code.

    I don't know if it's due to the speed of the train (slower than my hand tests) but I got a 1 in 10 success rate of both LEDs being turned off when the train exited the siding

    The other question I have, is as I need 8 inputs and 8 outputs, can I use port A in the same way as port B. The thing that concerns me is the MCLR pin... do I have to do something to configure this as a normal input as I've read that this can cause problems if not treated right ??

    Cheers

    Malcolm
    (rapidly aging and going bald by pulling my hair out over this "issue")

  6. #6
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Wink Time to grow ...

    Hi, Malc

    Don't you think it would be the time to test the "BUTTON" statement, instead of " if SW1=0 then ..." ???

    May be we could also try more sophisticated debouncing ... but one thing after one !!!

    you want to play the " Speedy Gonzalez " ... no problem this side !!!

    Alain

    PS: Try this : http://www.darreltaylor.com/files/ouchywhip.wav ...
    Last edited by Acetronics2; - 16th May 2006 at 15:58.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  7. #7
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Yeah, I think I might have a go at the BUTTON statement...

    Let me go away and RTFM first, have a play, try and get something together before bailing be out

    Before I start though, any comments on configuring PORTA (inc MCLR) for in/outputs ??

    Malc

Similar Threads

  1. 16F628A - Stops if release power switch.
    By dene12 in forum General
    Replies: 16
    Last Post: - 14th February 2009, 07:57
  2. Addressing with Dip Switch or ???
    By tazntex in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 16th September 2008, 12:19
  3. 16f628a wont wake up
    By Peter1960 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 25th August 2008, 14:22
  4. SLEEP mode 16F628A
    By Michael in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 25th April 2006, 17:26
  5. 16F628A using PORTA.4, and it works, but...
    By zx81 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th August 2005, 08:45

Members who have read this thread : 0

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