Connect with us

need help in counter programming+ pic16f877a

Discussion in 'Microcontrollers, Programming and IoT' started by ikram1991, Mar 8, 2014.

Scroll to continue with content
  1. ikram1991


    May 5, 2013
    i have push button as a increment for counter.
    each time push button is press counter will + 1.

    before this i learn increment for counter increment but its no suitable for my project where when the button is pressed and press to long the counter will keep +++.

    can anyone help me :confused:
  2. jpanhalt


    Nov 12, 2013
    You can configure the 16F877a to count only on the rising or falling edge. One way is to use the CCP module.

    You also may need to compensate for switch bounce to avoid multiple counts. A small delay is usually adequate.

  3. ikram1991


    May 5, 2013
    would you mind to tech me how to configure ccp module:confused:
  4. Divedeep


    Feb 2, 2014
    Using a loop after pressing the button will solve the increment issue.

    After your increment, i.e value = value + 1

    put a loop until button = 0 (or 1 depending on your setup)

    doing this means the button has to be released and pressed again to increment the value..

    Hope this helps
    Last edited: Mar 8, 2014
  5. gorgon


    Jun 6, 2011
    The easiest is to use a flag or semaphore to store the last value of the input. you compare the current state with that, and only do the + when the input change from an old '0' to a new '1'. You store the new value as the flag, whenever there is a change. Using this procedure, you can implement a counter to debounce any glitches in your pushbutton. For example, you need to read the same value 3 times before changing state. To do this you will need an extra semaphore to store the intermediate state, and a counter.

    Using a semaphore system, you don't have to wait for state changes, and your program will run even if the button is pushed.
    Last edited: Mar 8, 2014
  6. kpatz


    Feb 24, 2014
    The way I do debounce is like this (this is pseudo-code, not an actual language):

      begin loop
      if (buttonstate != lastbuttonstate)
        count = 100   (adjust this based on the speed of your loop)
        lastbuttonstate = buttonstate
      if (count > 0)
        decrement count
        if (count == 0)
          process new button state (pressed or released)
      end loop
    So, when the button state changes (from unpressed to pressed, or pressed to unpressed), I check to make sure the button stays in the new state for X passes through the loop before handling the button state change. If the button bounces from on to off and back to on before the count runs out, the count resets.

    For polling a button inside the main loop in your code, a higher count like 100 is good. If you're doing it inside a timer interrupt (say, 10 times a second), use a smaller count like 3.
  7. ikram1991


    May 5, 2013
    If(SENSOR == 1)

    Where I can put the loop..did you have any source code example?
  8. Anish


    Feb 5, 2011
    Put the loop where you want to stall the processor.
    if you want to press the button, wait (we are actually slow)then the above code will be like this:

    if(sensor){ //check if button pressed
    i++; //increment counter
    while(sensor); //stall processor till the button pressed
    __delay_ms(50); //small delay to avoid key debouncing error
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day