Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] How to use 8 buttons in a pic for different function

Status
Not open for further replies.

Johnny Churne

Member level 5
Joined
Jun 5, 2015
Messages
83
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
554
Hi all dear friends I am very struggle and need the solution please kindly give suggestion.
I am making project that using pic16f876a(using mikroc) I need 8 to connect 8 buttons to portb. when the button press it gives portc output and clear port, when the button release it gives portc another output and clear port again. this process will use for each 8 buttons.
 

Code:
void (*const PositionTable[])(char BNum) = {B0, B1, B2, B3, B4, B5, B6, B7}

Code:
static char prevPortState = 0;
char cnt;
for (cnt = 0; cnt != 8; cnt++)
{
  if (PORTB & (1<<cnt) & prevPortState)
  {
    BNum[cnt](cnt);
    prevPortState = PORTB;
  }
}

Code:
void B0 (char BNum)
{
  //button 1 code here
}
......
void B7 (char BNum)
{
  //button 8 code here
}
 

how if when the button 1 still pressing and button 2 needed to press too is it work ok?
 

how if when the button 1 still pressing and button 2 needed to press too is it work ok?

These styles came from the typewriter era. You needed to press shift and then hit the character key to get the upper case equivalent character printed.

With computers keyboards, more keys came and we have n-key roll over and lockout. It can also distinguish key pressed and key released (essential for roll over)

In your setup, the first key pressed is taken into account and the second key is ignored.

You have to check for the following sequence:

Key1 pressed >> key2 pressed >> key2 released >> key1 released

To identify a sequence like CTRL-ALT-DEL (CAD) you need to check:

C pressed >> A pressed >> D pressed >> D released >> A released >> C released
or
C pressed >> A pressed >> D pressed >> D released >> C released >> A released
or
A pressed >> C pressed >> D pressed >> D released >> A released >> C released
or
A pressed >> C pressed >> D pressed >> D released >> C released >> A released

That is because the order of pressing for C and A does not matter but D must be pressed last. Sometimes only the presses are considered:

C pressed >> A pressed >> D pressed >> D released OR A pressed >> C pressed >> D pressed >> D released

It can get messy. For the desktop PC, the keyboard has a dedicated microcontroller.
 

but how to write in the pic 16f876a I need just 8 buttons
These styles came from the typewriter era. You needed to press shift and then hit the character key to get the upper case equivalent character printed.

With computers keyboards, more keys came and we have n-key roll over and lockout. It can also distinguish key pressed and key released (essential for roll over)

In your setup, the first key pressed is taken into account and the second key is ignored.

You have to check for the following sequence:

Key1 pressed >> key2 pressed >> key2 released >> key1 released

To identify a sequence like CTRL-ALT-DEL (CAD) you need to check:

C pressed >> A pressed >> D pressed >> D released >> A released >> C released
or
C pressed >> A pressed >> D pressed >> D released >> C released >> A released
or
A pressed >> C pressed >> D pressed >> D released >> A released >> C released
or
A pressed >> C pressed >> D pressed >> D released >> C released >> A released

That is because the order of pressing for C and A does not matter but D must be pressed last. Sometimes only the presses are considered:

C pressed >> A pressed >> D pressed >> D released OR A pressed >> C pressed >> D pressed >> D released

It can get messy. For the desktop PC, the keyboard has a dedicated microcontroller.

- - - Updated - - -

Capture.PNG I can not compile the code!
 

All functions have to be declared first.
Anyway, if you button action is time consumping and can't be interrupted, possible, RTOS will be suitable for you purpose.
 

I strongly recommend that you study a bit about 2-key lock out and n-key roll over and how to handle these events.

Remember that the world of electronics, there is no 'simultaneous' event (you cannot press two keys at the same time; even if you do, it will not be recorded as such). Hence you must watch for every key press and key release continuously and keep them in a ring buffer. How do you examine the buffer is the job of the software.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top