PDA

View Full Version : simple counting program - please help!!



john meiers
- 16th February 2006, 20:46
I'm new at this (obviously) - I need a program segment that will simply count button presses on a portA pin and flash an LED on portB when the count gets to 10, then reset the count and repeat. I've tried a bunch of things, but my LED always lights up after about 5-6 presses. Whats goin on here??

john meiers
- 16th February 2006, 21:21
One more thing - I don't think this is a switch bounce issue. If I simply hold down the pushbutton for a good second my LED will come on. I think the PIC is counting the amount of time the switch is closed, rather than the transisitons from low to high.

Melanie
- 18th February 2006, 09:30
The problem I have is I don't have PBC only PBP (which is also a reason I don't answer PBC problems too often!), so you might want to step through the code and ensure I've not slipped any 'PBP only' features or ways of doing things into my example...

Lets assume the Button is on PortB.7 connected between PIC pin and Vss, with a pull-up Resistor (anywhere between 4K7 and 47K) connected between the PIC pin and Vdd).

The LED is on PortB.0 connected between the PIC pin and Vss with a Resistor in series (any value between 220R and 330R).



Symbol CounterA=B0 ' Using B0 as our Button Press Counter

Input 7 ' Button on this Pin
Output 0 ' LED on this Pin

Start:
Low 0 ' LED is OFF
CounterA=0 ' Zero Counter
MainLoop:
If Pin7=1 then MainLoop ' Do nothing when Button not pressed
CounterA=CounterA+1 ' Button pressed so increment Counter
Pause 75 ' Allow 75mS for Debounce
WaitLoop:
If Pin7=0 then WaitLoop ' Kill time untill User releases Button
If CounterA<10 then MainLoop ' Count next Button Press
High 0 ' Ten-Count reached, so turn on LED
Pause 2000 ' Wait a couple of Seconds
Goto Start ' Reset for another run...

I don't know what PIC you're using, whether it needs Comparators or ADC's disabled, but since you say you already have a partially working example I'm sure you can translate the Input to PortA. Just a reminder... please keep things on the forum.

john meiers
- 20th February 2006, 14:17
Thank you so much! It works beautifully!