View previous topic :: View next topic |
Author |
Message |
PicNewboy Guest
|
Kbhit() question |
Posted: Tue Apr 19, 2005 2:25 am |
|
|
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
|
|
Posted: Tue Apr 19, 2005 3:27 am |
|
|
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
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
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
|
|
Posted: Tue Apr 19, 2005 3:36 am |
|
|
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
|
|
Posted: Tue Apr 19, 2005 5:42 am |
|
|
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
|
|
Posted: Tue Apr 19, 2005 5:43 am |
|
|
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
|
|
Posted: Tue Apr 19, 2005 6:24 am |
|
|
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
|
Re: Kbhit() question |
Posted: Thu Apr 21, 2005 1:54 pm |
|
|
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 |
|
|
|