Switch in PIC BASIC HELP!!!!


Closed Thread
Results 1 to 35 of 35

Hybrid View

  1. #1
    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.

  2. #2
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default

    Hello,
    I don.t understood how I do that.
    I use device PIC16f628,

    I just need when I PUSH BUTTON ONE TIME,
    then working program 1,
    when I push button next time,
    go to next program 2,
    when I push button next time,
    go to next program 3,......

    Can you write me code in PIC BASIC code for that,
    if you cant,
    thanks anyway!!!

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


    Did you find this post helpful? Yes | No

    Default

    I have not used Pic Basic so I can not help much but here is a "not very good idea" you might play with while you are working out the register reading Henrik is pointing you to.


    Setup a BUT_CHECK routine with a RETURN at the end of it.
    In each of the program loops have a GOSUB BUT_CHECK.
    The BUT_CHECK routine will check if the button IS pressed and if it is increment a variable by one.
    When the code returns to the program running the next line will have something like.
    IF BUT_VAR = 2 GOTO PROG 2

    Lots of problems with doing it this way but if things are not critical....
    When you figure out the registers with Pic Basic you should see how bad of an idea this is.
    Dave
    Always wear safety glasses while programming.

  4. #4


    Did you find this post helpful? Yes | No

    Default Program selection on button press

    Try this idea, it works with PBP in a PIC18F4620 and should work with any PIC that has EEPROM.

    I set a byte variable "ProgramState" to 1 in a data statement and write this to EEPROM when the program is loaded into the PIC.

    At power up, the code reads the value stored in ProgramState, here set to 1, and jumps to the appropriate code block.

    MAIN:
    READ 0, ProgramState
    If ProgramState = 1 then RunCode1
    If ProgramState = 2 then RunCode2
    If ProgramState = 3 then RunCode3

    If ProgramState > 3 then
    write 0, 1 'reset ProgramState to first code block
    goto MAIN
    endif

    RunCode1:
    execute codeblock 1
    if ButtonDown = 1 then
    write 0, 2
    endif
    goto MAIN

    RunCode2:
    execute codeblock 2
    if ButtonDown = 1 then
    write 0, 3
    endif
    goto MAIN

    etc

    HTH
    BrianT
    Last edited by BrianT; - 14th May 2010 at 01:45. Reason: pushed TAB by mistake

  5. #5
    Join Date
    May 2010
    Posts
    43


    Did you find this post helpful? Yes | No

    Default

    Thanks,Braian,for help,
    I try your idea,but doesn't,work.
    I try explain what I need,example:

    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 now I don't now how,how I make code for switch!

    I need,when I press switch,JUST ONE TIME,
    and every time when I press switch again,
    to change program1,program2,program3,......

    that is problem!

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


    Did you find this post helpful? Yes | No

    Default with two buttons

    Hope this helps.

    This code will need changing as it's in PBP, but the logic should still work with PB. I've used this to select options in a menu by pressing one button and then another to jump to the option selected, so in theory it could be used to select which program you run

    Code:
    LCDOUT $FE,2,"Main Menu"
    lcdout $FE, $C0, "Select Option"
    
    if Option = 1 THEN lcdout $FE, $D4, "Set Time and Date  "
    if option = 2 then lcdout $FE, $D4, "Set Night Period   "
    if option = 3 then lcdout $FE, $D4, "Set Normal Temps   "
    if option = 4 then lcdout $FE, $D4, "Run                "
    
    IF !H_butt THEN Option = option + 1 ; if button is low
    pause 150
    if option >4 then option = 0
       
    If option = 1 Then ; if option =1 and S_but is taken low then reset LCD and gosub debounce followed by jump to option selected
    If S_butt = 0 Then
    LCDOUT $FE,1 
    Gosub SetButtonRelease
    goto  setup
    endif
    endif
    
    
    If option = 2 Then
    If S_butt = 0 Then 
    LCDOUT $FE,1
    Gosub SetButtonRelease
    goto nitetime
    endif
    endif
    
    
    If option = 3 Then
    If S_butt = 0 Then 
    LCDOUT $FE,1
    Gosub SetButtonRelease
    goto GetSetpoint
    endif
    endif
    
    If option = 4 Then
    If S_butt = 0 Then
    LCDOUT $FE,1 
    Gosub SetButtonRelease
    goto main
    endif
    endif
    
    Goto mainmenu
    The reason I use two buttons (one to select the option and one to confirm it) is that if you don't I found it hard to get passed the first option. For example, if you had

    Code:
    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
    
    
    switch =0
    if portB.0 = 0 then switch = switch +1
    
    If switch = 1 then goto program 1
    If switch = 2 then goto program 2
    If switch = 3 then goto program 3
    if switch >3 then switch=0
    then as soon as you press your switch and the variable switch=1 it would jump to program 1 and never reach program 2 or 3.

    Hope this helps, and sorry it's not converted into PB that you can use directly, but hopefully that won't be too hard to cchange
    Last edited by malc-c; - 14th May 2010 at 12:42.

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