Counting is counting, whether it's Binary, Decimal, Hexadecimal or in Latin. It's how you finally OUTPUT it that counts. Here is a simple program that will count (add one) each time your Button is pressed, and output the status in BINARY to an array of LEDs.
Consider the following Program and circuit.
You have a PushButton connected between PortA.0 and Vss. A pull-up Resistor (say 10K) is connected between PortA.0 and Vdd. That means the pin is usually High (Logic 1) when NOT pressed, and goes Low (Logic Zero) when pressed.
PortB has an LED on each pin connected between the pin and Vss via a Resistor (one for each LED), lets say 330R.
The program variable CounterA is incremented once each time the PushButton is pressed, it's status is then sent to PortB so you can see it happen. The Pause is there to debounce the Pushbutton from giving you multiple counts on one press. The While-Wend pair is there to ensure you lift your finger off the button before a new count is registered. The rest you can figure yourself between the PICBasic manual and the Datasheet.
Code:
'
' Program Hardware Defines
' ------------------------
PushButton var PortA.0
LEDArray var PortB
'
' Program software Defines
' ------------------------
CounterA var Byte
'
' Initialise Program
' ------------------
TRISA=%11111111
TRISB=%00000000
CounterA=0
'
' Main Program Loop
' -----------------
Loop:
If PushButton=0 then
CounterA=CounterA+1
LEDArray=CounterA
Pause 10
While Pushbutton=0:Wend
endif
Goto Loop
end
Bookmarks