Push button on PIC16F887


Closed Thread
Results 1 to 22 of 22

Hybrid View

  1. #1
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    I need it in basic.
    The code in the cd is asm.
    But thats ok.
    I have managed.
    Thanks.

  2. #2


    Did you find this post helpful? Yes | No

    Default

    if PIN =1 then gosub BUTTON_PRESSED

    PINisthe port you are using (PORTD.2 for example) BUTTON_PRESSED is what you want to happen after the button has been pressed. What do you want it to do?what code have you written already???

  3. #3
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    SW1 is connected to PORTB.0
    But you must enable its analog :
    ANSELH = %00000000

  4. #4
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    I have improved the code of this source:
    http://www.picbasic.co.uk/forum/showthread.php?t=2129
    Still no optimization, but it looks great.
    I programmed it so when in setup mode (clock setting) to increment the time, only when button is up, it will increment, but there is a delay in display.

    Menta.
    Code:
        INCLUDE "modedefs.bas"
        
    TRISB=$01               'GP1 = input for button 
    ANSELH = %00000000 ' Set PortA Analogue
    OPTION_REG=%00000111   'weak pullups on, TMRO prescale = 256
    INTCON=0               'interrupts off
    OPTION_REG.7=1    'Turn off weak pullups
    TMR0=0	          'Reset TMRO
    INTCON.2= 0       'Clear TMRO overflow flag
    
    HzTimer VAR Word       '1/2 second counter (2 Hz)
           
    HH VAR Byte  ' Hours 1-12
    MM VAR ByTE  ' Minutes 0-59
    SS VAR Byte  ' Seconds 0-59
    X VAR Byte   ' temp variable
    B0 var byte
    col VAR Bit  ' colon 1=on, 0=0ff
    HOLD var byte
    
    
    HzTimer=$7A12        'for 1/2 Sec
    HH=0:MM=0:SS=0:col=0 'initial conditions
    
    Pause 2000           'settle Time for Serial LCD
    
    ' **************************************************************
    'SET INITIAL TIME WITH PUSH BUTTON (PART 1 of Program)
    
    Serout PORTB.1,6,[254,1]
    Pause 100
    
    SEROUT PORTB.1,6,[72,72,58,77,77,58,83,83]   'Display HH:MM
    ;goto Main           ' Skip setup 
    WHILE PORTB.0=1    'Wait here until user pushes button
        button PORTB.0,0,255,0,B0,1,SetHH
        PAUSE 300
        SEROUT PORTB.1,6,[254,128,72,72]
        PAUSE 300
        SEROUT PORTB.1,6,[254,128,32,32]
    WEND
        PAUSE 150
        SEROUT PORTB.1,6,[254,128,48,48]  
        HOLD=0
    SetHH: 'Set hours, HH
    while PORTB.0=0       ' check if button is pushed for 3 seconds
        pause 1000
    FAST:
        HOLD=HOLD+1
        if HOLD>4 then 
           HH=HH+1
              IF HH=13 THEN
                 HH=1
              ENDIF
              IF HH>9 THEN
                 X=128
               ELSE
                 X=129
              ENDIF
              if HH<9 then
                 SEROUT PORTB.1,6,[254,128,32]
              endif
          SEROUT PORTB.1,6,[254,X,#HH]
          pause 150
          if PORTB.0=1 then StopFast
          goto FAST
        endif
    StopFast:    
    wend
        if (Hold>2) and (HOLD<4) then setmm
        hold=0
        IF HH=13 THEN
            HH=1
        ENDIF
        IF HH>9 THEN
            X=128
          ELSE
            X=129
        ENDIF
        while PORTB.0=1         'wait for key to press again
            SEROUT PORTB.1,6,[254,X,#HH]
            PAUSE 100
        wend
    HH=HH+1
    goto sethh
    
    SetMM: 'Set hours, MM
    hold=0
    WHILE PORTB.0=1    'Wait here until user pushes button
        PAUSE 150
        SEROUT PORTB.1,6,[254,131,77,77]
        PAUSE 150
        SEROUT PORTB.1,6,[254,131,32,32]  
    WEND 
        PAUSE 150
        SEROUT PORTB.1,6,[254,131,48,48]    
    setMM1:
    while PORTB.0=0       ' check if button is pushed for 3 seconds
        pause 1000
    FASTMM:
        HOLD=HOLD+1
        if HOLD>3 then 
           mm=mm+1
              IF mm=60 THEN
                 mm=1
              ENDIF
              IF mm>9 THEN
                 X=131
               ELSE
                 X=132
              ENDIF
              if mm<9 then
              endif
          SEROUT PORTB.1,6,[254,X,#mm]
          pause 150
          if PORTB.0=1 then StopFastMM
          goto FASTMM
        endif
    StopFastMM:    
    wend
        if (Hold>2) and (HOLD<4) then startloop
    hold=0
        IF mm=60 THEN
            mm=1
        ENDIF
        IF mm>9 THEN
            X=131
            SEROUT PORTB.1,6,[254,131,48]
          ELSE
            X=132
        ENDIF
        while PORTB.0=1         'wait for key to press again
            SEROUT PORTB.1,6,[254,131,48]
            SEROUT PORTB.1,6,[254,X,#MM]
            PAUSE 100
        wend
    mm=mm+1
    goto setMM1
    StartLoop:
    
    ' **************************************************************
    'TIME KEEPING LOOP (PART 2 of Code)
    
    'Requires variables, HH for Hours 1-12, and MM for minutes 0-59
    '(HH and MM manually set above in Part 1; SS & col = 0 from above)
    
    Main:
    ClockLoop: IF INTCON.2=0 THEN ClockLoop ' Wait for TMRO overflow
    INTCON.2=0 'Clear TMRO overflow flag
    
    HzTimer = HzTimer - $1000  'decrement timer
    
    IF HzTimer<$1000  THEN
        IF Col=1 THEN 'update time'
            SS=SS+1
            IF SS=60 THEN
                SS=0 
                MM=MM+1
                IF MM=60 THEN
                    MM=0
                    HH=HH+1
                    IF HH=25 THEN
                        HH=1
                    ENDIF
                ENDIF
                IF HH>9 THEN
                    SEROUT PORTB.1,6,[254,128,#HH,58]
                ELSE
                    SEROUT PORTB.1,6,[254,128,32,#HH,58]
                ENDIF
                IF MM>9 THEN
                    SEROUT PORTB.1,6,[254,131,#MM]
                ELSE
                    SEROUT PORTB.1,6,[254,131,48,#MM]
                ENDIF
             ELSE
    ;             Serout PORTB.1,6,[254,130,58]   'colon on
    ;             Serout PORTB.1,6,[254,133,58]   'colon on
             ENDIF
                IF SS>9 THEN
                    SEROUT PORTB.1,6,[254,134,#SS]
                ELSE
                    SEROUT PORTB.1,6,[254,134,48,#SS]
                ENDIF          
        ELSE
    ;        Serout PORTB.1,6,[254,130,32]    ' Colon off
    ;        Serout PORTB.1,6,[254,133,32]    ' Colon off        
        ENDIF
        Col=Col+1
        HzTimer=HzTimer+$7A12
    ELSE
        'Do something here but must be less than 65,500 instructions 
        '(e.g., less than 65.5 mSec total time)
    
    ENDIF
    
    GOTO Main
    ' **************************************************************
    END
    Last edited by menta; - 22nd June 2008 at 00:04.

  5. #5
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    Now I need to get interrupt when button is pressed,
    can anyone lead to a good example?
    I tried some from the examples here, and I understand it need to be set to MPASM, but I get errors and compilation fails.
    I get error :"source file path exeeds 62 characters"
    I tried those examples http://darreltaylor.com/DT_INTS-14/hello.html

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by menta View Post
    can anyone lead to a good example?
    Google?

    I tried some from the examples here, and I understand it need to be set to MPASM, but I get errors and compilation fails.
    I get error :"source file path exeeds 62 characters"
    Put your source file to where the path to that source file doesn't exceed 62 characters.

  7. #7
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    Put your source file to where the path to that source file doesn't exceed 62 characters.
    What?
    Is this english ?

Similar Threads

  1. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43
  2. Code check -- button not working
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 2nd March 2006, 22:43
  3. Button Push within 3 second Window
    By Tissy in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd December 2005, 10:06
  4. push button problem
    By ngeronikolos in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 2nd June 2005, 19:44
  5. Push Button 'Menu' to Choose Variable.
    By Tissy in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 10th March 2005, 07:00

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