CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Kbhit() question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PicNewboy
Guest







Kbhit() question
PostPosted: Tue Apr 19, 2005 2:25 am     Reply with quote

Hello everybody.I have make a program with something like this:
Code:
if (kbhit()) do {...}

thinking that the program wait for a character on rs232 bus and start to do something after this.But isn't so.To work I must write something like this:
Code:
while(!kbhit()) do {...}

Can someone help me to understand it?Thank you.
Paolino



Joined: 19 Jan 2004
Posts: 42

View user's profile Send private message

PostPosted: Tue Apr 19, 2005 3:27 am     Reply with quote

The code with the "if(condition)" statment exits immediatly if the condition is not true. So, for example, if the UART buffer is empty, the condition
Code:

if (kbhit())

is not true, and the program counter skip ahead.

The second code is different. The "while(condition)" statment waits for the condition becomes true. In this case your code stays hanged here
Code:

while (!kbhit())

waiting for a character in the UART buffer.

If you are writing code with the WDT activated, consider to use

Code:

#use delay (clock=4000000, RESTART_WDT)
#fuses WDT


to allow the automatic restart of the watchdog in particular cases, i.e. the while() statments (and also the delay_us() and delay_ms() CCS routine, etc).

Best regards.

Paolo.
PicNewboy
Guest







PostPosted: Tue Apr 19, 2005 3:36 am     Reply with quote

Thank you for your answer. But I think my question wasn't very clear.
With the istruction
Code:
 if(kbhit()) do {...}
the program execute the code in {} brackets without waiting for a key press.This is because on the rs232 there is ever a communication?
Guest








PostPosted: Tue Apr 19, 2005 5:42 am     Reply with quote

Probably yes. Test with a
Code:

...
char c;
....
...

if (kbhit()) {
c=getc();
putc(c);
}


Else, post your code.

Paolo.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Tue Apr 19, 2005 5:43 am     Reply with quote

Paolino wrote:
Quote:

The code with the "if(condition)" statment exits immediatly if the condition
is not true. So, for example, if the UART buffer is empty, the condition Code:

if (kbhit())

is not true, and the program counter skip ahead.


Wrong. kbhit() doesn´t test any buffer status.
kbhit() test only if the Start BIT had been received. In other words,
test if the assigned RCV_PIN is LOW.



PicNewboy wrote:

Quote:

With the istruction Code:
if(kbhit()) do {...}
the program execute the code in {} brackets without waiting for a key
press.This is because on the rs232 there is ever a communication?


Check the level of the RCV_PIN, it must be stable High without receiving
characters, in iddle state.


Humberto
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Apr 19, 2005 6:24 am     Reply with quote

Quote:
Wrong. kbhit() doesn´t test any buffer status.
kbhit() test only if the Start BIT had been received. In other words,
test if the assigned RCV_PIN is LOW.



Wrong if he is using the hardware uart

"If the RS232 is hardware this function returns TRUE is a character has been received and is waiting in the hardware buffer for getc() to read."
DragonPIC



Joined: 11 Nov 2003
Posts: 118

View user's profile Send private message

Re: Kbhit() question
PostPosted: Thu Apr 21, 2005 1:54 pm     Reply with quote

PicNewboy wrote:
Hello everybody.I have make a program with something like this:
Code:
if (kbhit()) do {...}

thinking that the program wait for a character on rs232 bus and start to do something after this.But isn't so.To work I must write something like this:
Code:
while(!kbhit()) do {...}

Can someone help me to understand it?Thank you.


Code:
while(!kbhit());  //wait for a KB hit (notice the semicolon at the end meaning nothing is in the loop, just the argument)
//if KB hit continue
rest of code.....


I think this is what you wanted to do. Wait for something on the rs232 and then execute code. There is no such thing as do {...} in C. There is a do {...}while();.

kbhit() is used for polling. Understanding your loops correctly will help.
_________________
-Matt
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group