C code for push buttons??

Thread Starter

harami

Joined Jun 20, 2011
66
how do you write a code in C language for push buttons. if im to use two push buttons as an input to pic18f452. push buttons will be pressed one after another. and when push button A is pressed followed by push button B, number will increment and if button B is pressed followed by button A number will decrement. any suggestions/help??
 

John P

Joined Oct 14, 2008
2,025
Let me make it more complicated for you. What if the user only presses one button, does the processor wait forever for the other button? How about if the sequence is A-B-A; is that read as AB followed by BA, or is it AB and then the beginning of a second AB sequence that's never completed? In fact it looks as if alternating A's and B's will cause either increments or decrements, depending on how it began. That doesn't seem like a very robust control method.
 

Thread Starter

harami

Joined Jun 20, 2011
66
i am trying to built a programme which can monitor vehicle IN and OUT. im going to place two sensors at the entrance, A and B. Therefore A folliwed by B will be IN and B followed by A will be OUT. the reason im using push buttons is just for a testing purpose. so how do we write a programme for increment and decrement with respect to the inputs.
 

THE_RB

Joined Feb 11, 2008
5,438
Is vehicle counting the only homework they give out these days? ;)

if A pressed;
1. wait up to 1 second for B to be pressed
2. if B pressed in <1 second, then vehicle+1

if B pressed;
1. wait up to 1 second for A to be pressed
2. if A pressed in <1 second, then vehicle-1
 

Thread Starter

harami

Joined Jun 20, 2011
66
Is vehicle counting the only homework they give out these days? ;)

if A pressed;
1. wait up to 1 second for B to be pressed
2. if B pressed in <1 second, then vehicle+1

if B pressed;
1. wait up to 1 second for A to be pressed
2. if A pressed in <1 second, then vehicle-1
this is exactly what im trying to programme. i have only written simple programme and debugged it. but now im trying to built something using microcontroller, i just dont know how to start. i mean how do i write a code to input A and B??
 

panic mode

Joined Oct 10, 2011
2,715
most micro-controllers have I/O pins that are configurable to one of several functions. in order to use one of pins as input, you need to configure it to act as input (digital input in this case). this may involve enabling internal pull-up resistors, tristating output etc. (read the datasheet).

then you read the corresponding port and test the bit (this can be done in different ways , depending on instruction set of a given MCU).

now that you have the bit value of interest, you use it in piece of code that was described earlier (simple debounce logic).
 
Top