Menu

Strange effect understandable ?

Help
Eddylvi
2017-01-18
2017-01-22
  • Eddylvi

    Eddylvi - 2017-01-18

    Hi,
    I made this program, but the LED lights up, or goes out AFTER releasing the button,
    not direct after pressing the button... Strange.
    The button is between Ub and the input, and the input is with 1200 Ohm to ground.
    Is there any reason for ?

    'This code will wait until a button is pressed,
    'than it will invert an output.
    
    #chip 16F54, 1
    Dir PortA In
    Dir PortB Out
    PortB = 0
    Do
      If PortA.0 = 1 then
        If PortB.0 = 0 then
          PortB.0 = 1
          Else
          PortB.0 = 0
        End if
        Wait 100 ms
        Wait until PortA.0 = 0
        Wait 100 ms
      End if
    Loop
    
     
  • Anobium

    Anobium - 2017-01-18

    Hi Eddy,

    Would you be able to hand sketch a really simple diagram of your circuit? Show the 5v and 0v plus all devices and resistors. A device includes the switch.

    This will really help us help you.

     
  • kent_twt4

    kent_twt4 - 2017-01-18

    Sounds like the button needs to be debounced. You can try waiting say 30 ms after Porta.0 = 1 and test condition again before proceeding. That is a pretty strong pulldown on the button, so shouldn't have to wait for port to go zero.

     
  • Eddylvi

    Eddylvi - 2017-01-18

    Thanks !
    I make a diagram tomorrow, it is 0:42 now.
    For the debouncing I used the two wait-s of 100 ms.

     
  • kent_twt4

    kent_twt4 - 2017-01-19

    Zero wait time between test of button and toggle of PortB pin.

     
  • Eddylvi

    Eddylvi - 2017-01-19

    But it runs only one time before a wait time ? Ain't that logic enough than ?
    Here the schematic

     
  • Eddylvi

    Eddylvi - 2017-01-19
    'This code will wait until a button is pressed,
    'than it will invert an output.
    
    #chip 16F54, 1
    Dir PortA In
    Dir PortB Out
    PortB = 0
    Do
      If PortA.0 = 1 then
      Wait 100 ms  ' <---------- does not change
        If PortB.0 = 0 then
          PortB.0 = 1
          Else
          PortB.0 = 0
        End if
        Wait 100 ms
        Wait until PortA.0 = 0
        Wait 100 ms
      End if
    Loop
    
     

    Last edit: Eddylvi 2017-01-19
  • Anobium

    Anobium - 2017-01-19

    @Eddy,

    What outcome are trying to achieve?

    is this code that part of a larger solution?

    The easiest way is to use the following: It will set the state of the LED to the state of the switch. #option volatile portb.0 ensures the output is set correctly. (I am sure everyone noticed this new capability in the release note last year.. :-) )

    #chip 16F54, 1
    Dir PortA In
    Dir PortB Out
    PortB = 0
    #option volatile  portb.0
    
    Do
    
      portb.0 = porta.0
    
    Loop
    
     
  • Eddylvi

    Eddylvi - 2017-01-19

    What I try to do is One push on the button is LED ON
    Another push: LED GOES OUT
    But the LED should change at the PUSH, not the release of the button.
    And possible max 8 times on one chip, A.0 >> B.0.....A.7 >> B.7

    Many thanks !

    Eddy

     
  • Anobium

    Anobium - 2017-01-19

    Now we can help. We understand the requirements. :-)

     
  • Anobium

    Anobium - 2017-01-19

    Before we solution. Have you used interrupts before?

     
  • Anobium

    Anobium - 2017-01-19

    I am going to assume we should do this without interrupts. Interrupts are for later.

    There are about 20 very good demos in your demo folder that do exactly what you want. Please search your demo folder for LED demos.

    And,

    Some psuedo code with portb assumed to have 8 LEDs attached:

    set portb to zero
    set a_switch_flag to zero

    Do a loop
        if switch is on then...
            if a_switch_flag is zero then  
                increment portb led array
                set a_switch_flag to 1
             else
                 set a_switch_flag to 0
             endif
             wait until the switch is off
          endif
    end the loop
    
     
  • kent_twt4

    kent_twt4 - 2017-01-19

    The 16f54 is a baseline device, so no interrupt. Small stack, so use of subroutines is very limited.

    Along the lines with what Anobium has suggested; Here is debounce routine that exits quickly. Code is not tested, but debounce method has.

    #chip 16F54, 1
    Dir PortA In
    Dir PortB Out
    PortB = 0
    
    Main:
    'Mask off PortB pins that are not leds?
    'Scan PortA, assumes unused PortA pins pulled down
    buttons = PortA
      If buttons > 0 Then
        'read current state of leds
        leds = PortB
        'debounce button
        Call ModeChange
        'toggle selected led as required
        If debounce = True Then
          Select Case buttons
            Case 1
                PortB = leds XOR b'00000001'
            Case 2
                PortB = leds XOR b'00000010'
            Case 4
                PortB = leds XOR b'00000100'
            Case 8
                PortB = leds XOR b'00001000'
            Case Else
              nop
          End Select
        End If
      End If
    'wait for long press as required
    Do Until PortA = 0
      wait 100 ms
    Loop
    goto Main
    
    SUB ModeChange
    debounce = False
    glitch = 0
    Do
      If PortA > 0 Then
         debounce += 1
      else
         glitch += 1
      end if
      wait 10 ms
      If debounce > 2 Then
        debounce = true
        exit Do
      End If
      If glitch > 1 Then exit Do
    Loop
    END SUB
    
     
  • Eddylvi

    Eddylvi - 2017-01-20

    Sorry, I was suddenly in hospital yesterday, so I could not answer.
    I try to understand the new answers today.

     
  • Eddylvi

    Eddylvi - 2017-01-21

    Hello again,

    I have not completely understood everything yet,
    but I will try again with the examples.

    Many thanks !

    Eddy

     
  • Anobium

    Anobium - 2017-01-21

    Try this.... this is simplier.

    #chip 16F54, 1
    Dir PortA In
    Dir PortB Out
    
    #option volatile  portb.0
    
    flag = 0
    
    repeat 4
    portb=!portb
    wait 500 ms
    end repeat
    PortB = 0
    
    Do
      if porta.0 = 1 then
          if portb=0 then c = 1
          rotate portb left
        wait while porta.0 = 1
      end if
    Loop
    
     
  • Eddylvi

    Eddylvi - 2017-01-22

    I am really sorry.... honest !
    But I still write the solution here, so that everyone knows what went wrong.
    After that I grounded the input with 100 Ohm (!!!!) and the LED stil was blinking
    as if that was programmed, (without touching the button) I could not believe that.
    Than I found out that the button was connected to A.1 instead of A.0.
    A.0 was open.... BUT I got (strange) reactions from pushing the button, so I was not
    thinking of an error like this.
    After changing the connection to pin 17 instead of 18 everything worked
    as it should.

    Again, very sorry, that this stupid error also have cost you all your time.
    I still wrote it here, so that in future same errors can be recognised.

    Kind regards,

    Eddy

     
  • CDRIVE

    CDRIVE - 2017-01-22

    This perspective comes from a GCB nube but a seasoned electronics forums member....

    This issue would have been caught a long time ago if a clear photo of your project had been posted. Believe it or not "Duh" momements like you had happen all the time. ;-)

    Sometimes it just takes another set of eyes.

    Chris

     

Log in to post a comment.