Switch in PIC BASIC HELP!!!!


Closed Thread
Results 1 to 35 of 35

Hybrid View

  1. #1
    Join Date
    May 2010
    Posts
    43

    Default Switch in PIC BASIC HELP!!!!

    Hay,all!!!!
    Can somebody help me in Picbasic programing!
    I need some code to switch with one button,to change to next program!
    Example:
    program1:
    some doing
    goto program 1

    program2:
    some doing
    goto program2

    program3:
    some doing
    goto program3 .......


    and can some help me how I write some code for the button,when I press button ONE TIME,to I change to
    program1
    program2
    program3......

    to change program,when I PRESS MY BUTTON ONE TIME,TO AUTOMATIC GO TO CHANGE PROGRAM!

    If some body can help me,how i do that un PIC BASIC!

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default

    Hi,
    There are several possible ways depending on what it is your doing on the various "programs", here's one aproach.
    Set up one of the PIC's timers, say TMR0 as a counter, have your button, with some debounce circuitry connected to the external input of the timer.

    Then you have a main loop which checks the value of the TMR0 register and jumps to appropriate sub program, when the value of TMR0 is lager than the "last" sub-program you reset the TMR0 register to 0.

    Something like, for a 16F628:
    Code:
    TRISA.4 = 1
    OPTION.5 = 1   		'Set TMR0 as Counter, input is on RA4.
    OPTION.4 = 0   		'Count on rising edge.
    TMR0 = 0       		'Reset TMR0 register
    
    Main:
    Select Case TMR0
      Case 0
        Goto ProgramZero
      Case 1
        Goto ProgramOne
      Case 2
        Goto ProgramThree
      Case Else
        TMR0 = 0
    Goto Main
    
    
    ProgramZero:
      'Do This
     Goto Main
    
    ProgramOne:
       'Do That
     Goto Main
    
    ProgramThree:
      'Do Something else
     Goto Main
    Another aproach is to wire the switch to an external interrupt pin and use the interrupt request flag to increment a software pointer quite similar to the TMR0 counter above. On the 16F628 PortB.0 is the interrupt pin
    Code:
    TRISB.0 = 1    'PortB.0 as input.
    OPTION.6 = 1   'Set interrupt flag on rising edge of PortB.0
    
    Main:
    If INTCON.1 = 1 then
       INTCON.1 = 0   		'Reset flag
       myPointer = myPointer + 1
       If myPointer = 3 then myPointer = 0  'Wrap around
    Select Case myPointer
    and then like in the first example...

    If you want "instant" switching of programs then you'll need to actually enbale the interrupt as well, not only check the flag, and then do the "program switching" in the interrupt handler.

    Well, there's a couple of ideas for you.

    /Henrik.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default

    Hello again,
    I'm sorry but I missed the fact that you wasn't using PBP. In that case it get's a little more complicated, the Select Case (which I don't think PBC supports) can be substitutes with some If Then lines. But the reading and writing of the various register will have to be done with PEEK and POKE and you need to use the datasheet for your particular PIC to figure out which physical adresses the various registers are at.

    /Henrik.

  4. #4
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default Switch in PIC BASIC HELP!!!!

    Thanks, a lot Henrik,
    but when I try compile your program,I have syntax error!
    I am new in programing,can you write me some new code,
    about PEEK OR POKE,how you write me last time,
    because I do no,t now how I do that!
    Thanks a lot again!!!

  5. #5
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default

    One question more!
    What you mean line
    OPTION.5=1
    AND
    OPTION.4=0

    WHAT I MUST WRITE IN THIS 2 LINES?
    Because I have syntax error.
    Last edited by dragan77; - 13th May 2010 at 13:27.

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default

    Hello,
    OPTION.5 = 1 simply sets bit 5 in the option register but like I said in my previous post PBC doesn't support direct register access like that so you'll have to use POKE to write the registers and PEEK to read them. Look at the memory map of the device you're working with. You'll find at which adresses the various registers are. On the 16F628 the OPTION register is at location 80h for example.

    Look in the PBC manual on how to use POKE and PEEK.

    Why did I want to set bit5 in the option register? That's because it is the bit that select if TMR0 is a timer or counter and I found that information in the section of datasheet dedicated to TMR0.

    /Henrik.

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


    Did you find this post helpful? Yes | No

    Default

    Another way to do it

    Code:
    ButtonPressCounter = 0
    
    SWITCH-LOOP:
    IF PORTx.x = 0 THEN     'this is where your button is connected to, obviously
       IF ButtonPressCounter = 3 THEN ButtonPressCounter = 0
       ButtonPressCounter    = ButtonPressCounter + 1
       IF ButtonPressCounter = 1 THEN GOSUB Program1:
       IF ButtonPressCounter = 2 THEN GOSUB Program2:
       IF ButtonPressCounter = 3 THEN GOSUB Program3:
    ENDIF
    GOTO SWITCH-LOOP:
    
    Program1:
    ...do something
    RETURN
    
    Program2:
    ...do something
    RETURN
    
    Program3:
    ...do something
    RETURN
    Roger

  8. #8
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default

    Thanks,Roger
    but can i ask you some!
    Is it finish code for compile,or some of combination,to I use!

    Can you write finish code for compile, because I am new in programing!

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by dragan77 View Post
    Is it finish code for compile...
    Almost!

    Set the PORTx.x you are going to use for your button and program your routines and you're done.
    Roger

  10. #10
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default

    Thanks,but when I use this code:

    ButtonPressCounter = 0

    SWITCH-LOOP:
    IF PORTA.2 = 0 THEN
    IF ButtonPressCounter = 3 THEN ButtonPressCounter = 0
    ButtonPressCounter = ButtonPressCounter + 1
    IF ButtonPressCounter = 1 THEN GOSUB Program1
    IF ButtonPressCounter = 2 THEN GOSUB Program2
    IF ButtonPressCounter = 3 THEN GOSUB Program3
    ENDIF
    GOTO SWITCH-LOOP:

    Program1:
    high portb.1
    RETURN

    Program2:
    high portb.2
    RETURN

    Program3:
    high portb.3
    RETURN

    WHEN I TRY COMPILE IN PIC BASIC,THEN GIVE ME THIS ERRORS!

    ERROR Line 1: Syntax error.
    ERROR Line 3: Syntax error.
    ERROR Line 4: IF without a matching ENDIF.
    ERROR Line 5: Bad expression.
    ERROR Line 5: Bad expression or missing THEN.
    ERROR Line 5: IF without a matching ENDIF.
    ERROR Line 6: Syntax error.
    ERROR Line 7: Bad expression.
    ERROR Line 7: Bad expression or missing THEN.
    ERROR Line 7: IF without a matching ENDIF.
    ERROR Line 8: Bad expression.
    ERROR Line 8: Bad expression or missing THEN.
    ERROR Line 8: IF without a matching ENDIF.
    ERROR Line 9: Bad expression.
    ERROR Line 9: Bad expression or missing THEN.
    ERROR Line 11: Syntax error.


    I DO NOT NOW WHY,WHAT IS HAPPENED?

  11. #11
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Is that the whole code?

    Is the line
    Code:
    ButtonPressCounter VAR BYTE
    in there someplace?
    Dave
    Always wear safety glasses while programming.

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


    Did you find this post helpful? Yes | No

    Default

    This is what I understand you may be looking for (not tested yet).
    Code:
    MAIN:
       IF PORTA.2 = 0 THEN
          PAUSE 300 'debounce
          GOTO Program1:
       ENDIF
       GOTO MAIN:
    
    Program1:
       IF PORTA.2 = 0 THEN
          PAUSE 300
          GOTO Program2:
       ENDIF
       HIGH PORTB.1
       PAUSE 500
       LOW PORTB.1
       PAUSE 500
       GOTO Program1:
    
    Program2:
       IF PORTA.2 = 0 THEN
          PAUSE 300
          GOTO Program3:
       ENDIF
       HIGH PORTB.2
       PAUSE 500
       LOW PORTB.2
       PAUSE 500
       GOTO Program2:
    
    Program3:
       IF PORTA.2 = 0 THEN
          PAUSE 300
          GOTO Program1:
       ENDIF
       HIGH PORTB.3
       PAUSE 500
       LOW PORTB.3
       PAUSE 500
       GOTO Program3:
    
    END
    Quote Originally Posted by dragan77
    ...Can you write finish code for compile, because I am new in programing!
    Quote Originally Posted by dragan77
    Thanks,for your help,but not working!
    Quote Originally Posted by dragan77
    But,can,you ex plane me,what I must do, which peace,of your code,I must change...
    .....
    My question to you dragan77: is this thread really 30 replies worth? What are your personal inputs and efforts?

    It looks a bit frustrating to me....
    Roger

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