Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC16F628A press key for specific seconds

Status
Not open for further replies.

darkatsuki12

New Member
I am currently using PIC Simulator IDE in creating program for my PIC16F628A and I am having problem in this specific task.
Pressing a key from the keypad for specific seconds for a condition to set in like pressing it for 3 seconds for RA6 to toggle its bit.
 
What language?

I do something similar, but instead of pressing and holding forever (e.g., 3 seconds), I use two presses. Because the button is used for something else, I flash a message after the first press to press again for the alternative function. The alternative function is rarely invoked in practice. Of course, there is always a problem that the chip can't read the user's mind, so you have to wait for the delay to time out. A new user or slow reader probably misses the window the first time, but with a little practice, you can easily get the second press within the window. Of course, the wait window for the second press can be made to fit any user's response time.

John
 
Last edited:
Do you want to hold-up the program for 3 second while it processes the request?
As long as it can satisfy the 3 second press. My situation is like the old 3x4 keypad for cell phone. When you press key 2 for a moment, it will input letter "a" but if you press it longer, it will input the number "2".
 
What language?

I do something similar, but instead of pressing and holding forever (e.g., 3 seconds), I use two presses. Because the button is used for something else, I flash a message after the first press to press again for the alternative function. The alternative function is rarely invoked in practice. Of course, there is always a problem that the chip can't read the user's mind, so you have to wait for the delay to time out. A new user or slow reader probably misses the window the first time, but with a little practice, you can easily get the second press within the window. Of course, the wait window for the second press can be made to fit any user's response time.

John
I am talking about a program for the PIC. I think you thought it was a PC program.
 
I use a "timeout" variable.... If the key is held for "x" number of program cycles the code can determine a short / long keypress... This way you don't hold up execution... If at any time you let go of the key then read the timeout, process the keypress, reset timeout to zero..
 
I use a "timeout" variable.... If the key is held for "x" number of program cycles the code can determine a short / long keypress... This way you don't hold up execution... If at any time you let go of the key then read the timeout, process the keypress, reset timeout to zero..
so like this:
x=0
while key = specifickey
Gosub Keypadpress ' the part of the program where the variable will receive a value depending on which key was press
x = x+1
wend
if x >= timeout then
'do the specified task if press in timeout
else
'do the specified task if press once only
endif
 
My implementation.

C:
void main(void)
   {
   unsigned int key = 0;
   unsigned int shadowkey = 0;
   unsigned long timeout;
   
  ANSEL=0x00;  // All pin are Digital
  TRISA=0b00000001;  // RA0 is input
  TRISC=0b00000000;  // LED outputs
  PORTC=0;
  while (1)
     {  //loop forever
     __delay_ms(10);
     key = keyhit();
     if(!key) timeout = 0;
     if(key && timeout++ >30)
       {
       shadowkey = key;
       if(timeout > 100)
         {
         shadowkey = 0;
         PORTC = 0x55;
         }
       }
     if(!key && shadowkey)
         {
         shadowkey = 0;
         PORTC = 0xAA;
         }
     }  
   }

If I use a two second press PORTB shows one output... If I hold the button for 3+ seconds then another output is displayed..
 
My implementation.

C:
void main(void)
   {
   unsigned int key = 0;
   unsigned int shadowkey = 0;
   unsigned long timeout;
  
  ANSEL=0x00;  // All pin are Digital
  TRISA=0b00000001;  // RA0 is input
  TRISC=0b00000000;  // LED outputs
  PORTC=0;
  while (1)
     {  //loop forever
     __delay_ms(10);
     key = keyhit();
     if(!key) timeout = 0;
     if(key && timeout++ >30)
       {
       shadowkey = key;
       if(timeout > 100)
         {
         shadowkey = 0;
         PORTC = 0x55;
         }
       }
     if(!key && shadowkey)
         {
         shadowkey = 0;
         PORTC = 0xAA;
         }
     } 
   }

If I use a two second press PORTB shows one output... If I hold the button for 3+ seconds then another output is displayed..
i get the guise of it. thanks.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top