Maker Pro
Maker Pro

need help in counter programming+ pic16f877a

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:
 
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.

John
 
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.

John

would you mind to tech me how to configure ccp module:confused:
 
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:
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:
The way I do debounce is like this (this is pseudo-code, not an actual language):

Code:
  begin loop
  if (buttonstate != lastbuttonstate)
    count = 100   (adjust this based on the speed of your loop)
    lastbuttonstate = buttonstate
  endif
  if (count > 0)
    decrement count
    if (count == 0)
      process new button state (pressed or released)
    endif
  endif
  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.
 
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

If(SENSOR == 1)
{
I=i+1
}

Where I can put the loop..did you have any source code example?
 
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
}
 
Top