This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Is it posibil to make somthing like kbhit()?

Hi.
I'm using an eval board with C1610 (Phytec KC161).
I know that KEIL doesn't have the function kbhit(). But I need somthing like that function, is it posible to make one? If does anyone know, please give me ideeas, or, the body of he function, even it is for over device.
Thank you.

  • What do you actually need?

    Presumably, you need to know when some button on your system has been pressed?

    First, you need to know how the button is connected to your processor - a port pin, perhaps?

    One approach would be to just continuously read (or "poll") that input until it shows that the button has been pressed.
    Or perhaps you could use an interrupt?

    You may or may not need to consider debouncing the switch input...

  • Presumably, you need to know when some button on your system has been pressed?
    No, I want to know if it has typed a key from PC keyboard(during connnection with the C161O device), or if the device has been recieved a character from serial interface...

    Thnaks.
    Damian.

  • "No, I want to know if it has typed..."

    If what has typed? What is "it"?

    "(during connnection with the C161O device)"

    How is it connected?
    How do you propose that connection will give access to the PC's keyboard?

    "or if the device has been recieved a character from serial interface..."

    That just standard serial comms - read the Datasheet to see how the chip's serial comms works, and look at the examples provides with your Keil installation.
    http://www.keil.com/download/list/c166.htm

    No doubt Infineon & ST also have examples, application notes, etc on their websites...

    Or maybe you need to be looking here: http://www.keil.com/condb/search.asp

  • Well, what I want in my aplication:
    For exemple the device (C161) is executing something(doesn't metter), also it is connected frough serial interface to one serial port to the PC, and use Hyper terminal. If had typed a string, I want C161 to execute what I entered from PC keyboard, else the device will execute the rest code whithout entering the body of if(kbhit()){...}

    void main(){
      //....  declarations of variables
      //...
      P3  |= 0x0400;       /* SET PORT 3.10 OUTPUT LATCH (TXD)              */
      DP3 |= 0x0400;        /* SET PORT 3.10 DIRECTION CONTROL (TXD OUTPUT)  */
      DP3 &= 0xF7FF;        /* RESET PORT 3.11 DIRECTION CONTROL (RXD INPUT) */
      S0TIC = 0x80;         /* SET TRANSMIT INTERRUPT FLAG                   */
      S0RIC = 0x00;         /* DELETE RECEIVE INTERRUPT FLAG                 */
      S0BG  = 0x19;         /* SET BAUDRATE TO 19200 BAUD AT 16MHZ           */
      S0CON = 0x8011;       /* SET SERIAL MODE                               */
    /*
    ........
    */
     while(1){
    //....the main instructions
     if(kbhit()){
        scanf("%s",command);
        if(strncmp(command,trn,3)==0){ //command 1
             scanf("%d", &nr);
        //....
     }
     }
    }
    

  • I tryed the folwing code, is it good?

    bit kbhit(void){
    //Return 1 if character available, otherwise 0
            if ( S0RIR )
                    return ( 1 );
            return ( 0 );
    }
    


    Thank you.
    Damian.

  • Bad concept. If you do implement a kbhit(), it will only tell you that there is at least one character available.

    You will not know if you have enough data for the call to scanf().

    First of all, avoid scanf().

    A better alternative is often to retrieve all data in a buffer, and then use sscanf(). Or tu just use atoi(), strtol() or similar to extract the data.

  • I tryed to avoid scanf(), and I used like this:

    char aString[10];
    int nr;
    gets(aString, sizeof(aString)-1);
    nr = atoi(aString);
    printf("Number introduced: %d", nr)
    


    But in the Hyper terminal the device jump quickly to printf(), an don't let me introduce something. So , I can't use gets()!
    But when I used scanf it worked fine...
    Thank you.

  • So why do you not receive the characters one by one, until you have enough characters to form a full command? If you are sending the commands directly from the hyperterminal, you have enough when you receive a newline character.

    If you get too much characters without a newline character, you can throw everything away and wait until a newline character is received before starting to collect a new command.

    gets() and sprintf() are extremely bad commands when used by a PC program. They are not getting any better to use, just because you are programming an embedded application.

    Exactly how do you think that gets() can know how much buffer space you have for receiving the expected command? If there is 1k characters available, gets() will try to receive a 1k string - even if your buffer is 20 characters large...

    You can not leave it to the external equipment (or the user) to make sure that you can't send dangerous information to your application!

  • So why do you not receive the characters one by one, until you have enough characters to form a full command?
    I think the instruction scanf("%s", command) can read the command I want.
    What do you think about this code, the question is after code:

    //........
    bit kbhit(void){
    //Return 1 if character available, otherwise 0
            if ( S0RIR )
                    return ( 1 );
            return ( 0 );
    }
    
    void main(void){
      char command[10];
      unsigned int nr;
      serial_setup(); //like previous message
    //...ports init...
    /*
    ...
    some intructions dependent of which command I typed from hyper teminal
    ...
    */
      if(kbhit()){
       printf("Type a command: ");
    //I have defined some commands wich I use above
       scanf("%s", command);
       if(strncmp(command, train, 3)==0)
          // if the string I typed match a defined command
          // here I change some variables wich I use above
      }
    }
    The problem is that kbhit() is always true, why? Is it dependent of how many characteres I entered? How can I modify ?
    


    Thank you.

  • I soleved the problem, I had to delete the S0RIR at the end of the body of if(kbhit()){...}
    It work fine , with scanf()...

  • I think the instruction scanf("%s", command) can read the command I want.

    How large is the command buffer?

    Try to send a couple of extra characters to the unit, and check what happens with your other RAM variables stored directly after the command buffer...

    gets() and scanf() are two of the most dangerous functions available in the C library, and you specifically want to use them despite being warned?

    Immediately plan for using the watchdog in the processor. You might need it sooner than you think. However, the watchdog can only help to restart a hung application. It will not protect the application from producing erroneous results, caused by buffer overruns.

    If you really have decided to use scanf(), at least always specify a maximum field width when retrieving strings. Without a field width, scanf() will behave similar to gets() - trying to receive any number of characters, whatever the size of your buffer is!