Switch in PIC BASIC HELP!!!!


Closed Thread
Results 1 to 35 of 35

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    To be fair, paste up your complete code as it is and then people can see what you want to do and hopefully post a corrected version for you. To ask for help and only post up cryptic bits of code makes it hard to provide the support you are seeking

  2. #2
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default

    Ok,I try modification this code:

    Code:
    switch var byte
    
    main:
    if portb.2 = 1 then 
    switch = switch +1
    
    If switch = 1 then goto program1
    If switch = 2 then goto program2
    If switch = 3 then goto program3
    endif 
    goto main
    
    program1:
    HIGH PORTB.1
    PAUSE 500
    LOW PORTB.1
    PAUSE 500
    GOTO program1
    
    program2:
    HIGH PORTB.2
    PAUSE 500
    LOW PORTB.2
    PAUSE 500
    GOTO program2
    
    program3:
    HIGH PORTB.3
    PAUSE 500
    LOW PORTB.3
    PAUSE 500
    GOTO program3
    and what I need,
    when I press button,JUST ONE TIME,TO, PORTB.2,
    TO GO TO PROGRAM 1,
    AND STILL WORKING IN THE PROGRAM 1.

    when I press button,NEXT TIME,TO, PORTB.2,
    TO GO TO PROGRAM 2,
    AND STILL WORKING IN THE PROGRAM 2.

    when I press button,NEXT TIME,TO, PORTB.2,
    TO GO TO PROGRAM 3,
    AND STILL WORKING IN THE PROGRAM 3,.......

    THAT I NEED!!!

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


    Did you find this post helpful? Yes | No

    Default

    Long way, but possibly a fix is to incorporate the test for switch in each program

    Code:
    main:
    if portb.2 = 1 then 
    switch = switch +1
    
    If switch = 1 then goto program1
    If switch = 2 then goto program2
    If switch = 3 then goto program3
    endif 
    goto main
    
    program1:
    HIGH PORTB.1
    PAUSE 500
    LOW PORTB.1
    PAUSE 500
    if portb.2 = 1 then 
    switch = switch +1
    
    If switch = 1 then goto program1
    If switch = 2 then goto program2
    If switch = 3 then goto program3
    endif 
    GOTO program1
    
    program2:
    HIGH PORTB.2
    PAUSE 500
    LOW PORTB.2
    PAUSE 500
    if portb.2 = 1 then 
    switch = switch +1
    
    If switch = 1 then goto program1
    If switch = 2 then goto program2
    If switch = 3 then goto program3
    endif 
    GOTO program2
    
    program3:
    HIGH PORTB.3
    PAUSE 500
    LOW PORTB.3
    PAUSE 500
    if portb.2 = 1 then 
    switch = switch +1
    
    If switch = 1 then goto program1
    If switch = 2 then goto program2
    If switch = 3 then goto program3
    endif 
    GOTO program3
    crude code, but may work (I've not tested as I'm at work)

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by dragan77 View Post
    THAT I NEED!!!
    Oh and no need to shout !

  5. #5
    Join Date
    Sep 2009
    Posts
    19


    Did you find this post helpful? Yes | No

    Default

    Hello.
    Please forgive me, but I'm still not sure what are you trying to do. What puzzles me most is PAUSE 500 because when a program is executing PAUSE it can't do anything else until the specified period expire (unless abrupted by interrupt). In your case, whenever the program hits PAUSE 500 it is just sitting there for the half of a second.
    In my example the program is looping in MAIN waiting a key to be pressed (with some sort of debounce routine) and counting ~500ms ON/OFF time for the appropriate LED (it counts nothing until the key is pressed for the first time). After keypress it jumps to SWITCH_LED subroutine where it adjusts index (indx variable) to grab a value from the LOOKUP table in order to toggle an appropriate LED (that way I'm addresing a port by its number instead by its name), and then returns in main loop.


    if I like to change, which port I like to be low,of high,I mean because I like to make some ,to combinations of led,to high,or low,to I do it myself!
    To change ports with attached LEDs you need to change values in LOOKUP table. You can use numbers 0-15 instead of port names, where 0 is portb.0, and 15 would be porta.7. However on 16F628 you can go as far as 12 (porta.4) or 13 (porta.5), depending whether you use external or internal oscillator. It's not same for all PICs. For example on 16F877 you can use full range of 0-15 because it spreads over portb and portc.

  6. #6
    Join Date
    Sep 2009
    Posts
    19


    Did you find this post helpful? Yes | No

    Default

    Hello.

    First, I want to apologize for taking liberty to correct other people's code. In post no. 31 mr. Roger (flotulopex) accidentaly made a mistake:
    Code:
    MAIN:
       IF PORTA.2 = 0 THEN
          <font color="red">PAUSE 300 'debounce</font>
          GOTO Program1<font color="red">:</font>
       ENDIF
       GOTO MAIN<font color="red">:</font>
    ...
    Those colons marked red are surplus and that PAUSE 300 doesn't do any debounce here. It is simply executed immediately after the porta.2 goes low. The most simple way to verify that is to change pause to some larger value, e.g. 1000 (1 sec.) and apply very short (less than 1 sec.) push on button. What will happen is that LED will toggle its state after the pause no matter how shortly you kept the button pressed.
    If it was written like this:
    Code:
    IF PORTA.2 = 0 THEN
    	PAUSE 300
    	IF PORTA.2 = 0 THEN Program1
    	ENDIF
    On button-press the program would make a 300ms debounce-pause and then check again if button is still pressed and perceived if it was real button-press or just random noise.

  7. #7
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default mistake or no mistake?

    Hi grinder,

    Thanks for your corrections but there is sometimes an explanation behind "strange looking" code.

    If you didn't tested this yet, do so and you will discover that the "debounce" will prevent the code to jump from the MAIN loop directly to the Program2 label; just try...and see by yourself

    You're right, the colons are not necessary but this is just because I copy/paste the code instead of retyping it. BTW, this will make absolutely no change to the compiled code.

    Dragan77 seems to be looking for a simple program made of simple commands for simple functionality; I'm not sure his application needs "complexity" up to using LOOKUP and so on.
    Roger

  8. #8
    Join Date
    Sep 2009
    Posts
    19


    Did you find this post helpful? Yes | No

    Default

    Hello.

    Mr. Roger (flotulopex), I apologize once more.

    If you didn't tested this yet, do so and you will discover that the "debounce" will prevent the code to jump from the MAIN loop directly to the Program2 label; just try...and see by yourself
    No need to test it, you're absolutely right about that. I made a mistake taking piece of your code out of the content. My intention was to explain that debouncing should represent a protection against random noise, beside inadvertently key presses. I'm really sorry, I didn't eplained that adequately.

    You're right, the colons are not necessary but this is just because I copy/paste the code instead of retyping it...
    That's why I said that you accidentaly made a mistake.

    Dragan77 seems to be looking for a simple program made of simple commands for simple functionality; I'm not sure his application needs "complexity" up to using LOOKUP and so on.
    I beleive you're right about that as well. I put my example more as a probe to see if that's what mr. Dragan77 wants to achieve.

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