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 count key press

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
Hello all dear member I want to count how many time I did press the button, I couldn't get any good result and not really understand how the Mikroc work, it doesn't work as C language. please help?
 

I'm tempted to mention that you're unlikely facing a problem of MikroC, which offers a genuine C programming environment with some built-in libraries.

You should start with describing your hardware. The "button" is a momentary switch pulling a PIC GPIO pin to ground?

If so, keywords for your coding problem are "detecting input change" and "switch debouncing".
 

There will be two signals associated with every key press: key press and key release. They will be useful to determine combination switch presses (combinations)- e.g., press shift key and while pressed, press key A and then release both). I suggest you count (number of) key release events.
 

I am not so sure about it. I use pic16f84a RB0 as input connect o ground

- - - Updated - - -

how to do it if I use only one button? do I need to use keyboard_Init()?
 

With one button it should be easy. Are you using interrupts? Connect RB0 to Vcc via a 10K and connect to ground via the switch. If you have not much load on the CPU, just test with a simple loop read digital input for RB0. Can you get to this much correctly?
 

no I am not using Interrupt.
here is my code


Code C - [expand]
1
2
3
4
5
6
7
8
void main(){
    int count=0
    while(1){
        if(PORTB.RB0==0){
            count = count+1;
        }
    }
}

 
Last edited by a moderator:

If you want to know the exact number of times key pressed (not press and hold), go for counter.
Configure your counter pin and connect to the switch, in this way you could read exact number of time. If go for normal GPIO then you may miss to count multiple times when key is pressed and hold.
 

I don't understand please tell me more detail the code. how can I go to the counter ? Please !
 

For pic16f84a use RA4 pin configured as counter.
1.Set the bit T0CS to operate the TImer0 in counter mode
2. TMR0 will have the number of times keys pressed

additionaly you can enable Interrupt using GIE EEIE T0IE and T0IF if your counter value overflows


Refer to section 5.0 Timer0 module in datasheet.
 

Thanks very much for your kindness. I really don't know how to use those things such as TMR0, but now it work by just add delay after if(PORTB.RB)==0); Thank very much!
 

no I am not using Interrupt.
here is my code


Code C - [expand]
1
2
3
4
5
6
7
8
void main(){
    int count=0
    while(1){
        if(PORTB.RB0==0){
            count = count+1;
        }
    }
}


You are ignoring the transition; if portB is held pressed, it will count multiple times. Increment count only when you see the transition. Your code is wrong as it appears.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top