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] PIC18f45k22 TimeOut microC

Status
Not open for further replies.

yanal

Junior Member level 1
Joined
Sep 20, 2015
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
149
Hello friends .. i already have working on project that implement can protocol .. i have made digital locker that open and close by sending commands from the pic ..

i tried to set keypad that when key clicked .. the key value will insert into array to be send to another lock using can protocol ..

the problem is that i used this function as follow :


Code C - [expand]
1
2
3
do{
     kp = Keypad_Key_Click();       // store key code in kp variable
     }while (!kp );



and i want to make a time out for it ,, for example after 5 second the loop well break and go out from it to continue with the code .. i dont have any idea how to make this .. anyone can help me please with thanks ..
View attachment testforkeypad.rar

and i attached the full code also for more info ,,
 
Last edited by a moderator:

Since the program is already in a loop checking kp, you could just count the number of times it is going through the loop and break out after 5 seconds. To determine the count that will equal 5 seconds, you may need to experiment or go to the list file and determine how many instruction cycles it takes to go through the loop. (The example given, 6600000, is just an example and is not meant to be correct for 5 seconds.) When it breaks out of the loop, kp will still equal 0, so the program will need to be OK with that result.

amended code from above.

Code:
unsigned long count;   // add this variable to count the loops
                                                     
   kp = 0;
   count1 = 0;
 
   do
   // kp = Keypad_Key_Press(); // store key code in kp variable
     {
     kp = Keypad_Key_Click();        
     count ++;                                
     if( count == 6600000 ) // number of times to = 5 seconds
        break;       // break out of do-while loop
     }
   while (!kp);
 
Last edited:
  • Like
Reactions: yanal

    yanal

    Points: 2
    Helpful Answer Positive Rating
Hi,

Packman style... The faster the computer the the faster the 5 seconds.
In some years the 5 seconds will be only parts of seconds.
Additionally it needs a lot of processing power.


****
I'd go for a timer.
Define a global integer variable "to"

Timer with 100ms timeout.
On every timer event decrease "to" until it is zero.

Within your key routine simply set to "to" 50. ( 50 times 100ms= 5 s)
Now simply loop until "to" is zero.
Maybe you need to give processing power to other threads/events. (Like "doevents" in VB6)

Klaus
 
  • Like
Reactions: yanal

    yanal

    Points: 2
    Helpful Answer Positive Rating
When a key is clicked start say a 500ms timer interrupt. Use a counter in Timer1 ISR and when counter reaches the value 10 (5 sec) , clear the counter, stop the Timer1 and set a flag like fiveSecondCompleteFlag to 1.

Now do like this

Code:
do{
     kp = Keypad_Key_Click();       // store key code in kp variable
     
     if(fiveSecondCompleteFlag)break;      
     }while (!kp);
 
  • Like
Reactions: yanal

    yanal

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top