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

scanf() function for reading integer from serial port

5 feb 2003
I want to use scanf() function to read input (integer or character) from serial port.I am using KEIL compiler , 89c51 microcontroller. How to do it? early reply awaited please..
thanks

Parents Reply Children
  • Just use the scanf function as normal. It will work.

    Jon

  • 7 feb 2003,
    dear john,
    I have used printf and scanf in my program.
    printf works fine but scanf does not .why?
    gopal
    #include<stdio.h>
    #include<reg51.h>

    void init_8051(void);

    main()
    {
    // this is a program example shown on page 262 of keil software
    // c51 users guide chapter 8 , library reference
    // USERS GUIDE 01.97


    char a;
    int b;
    long c;
    int argsread;
    init_8051();
    while(1)
    {
    printf("\n enter a signed byte ,int, and long \n");
    argsread=scanf("%b %d %ld " ,&a,&b,&c );
    printf("\n %d arguments read \n",argsread);
    printf("\n %b %d %l \n", a,b,c );
    }
    }


    void init_8051(void)
    {
    SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
    TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
    PCON |= 0x80;
    /* SMOD = 1 baud 19.2 */
    TH1 = 0xFD; //reload value for 19.2 k bauds cpg
    TR1 = 1; /* TR1: timer 1 run */
    TI = 1;
    RI = 1; // is it required or not ?
    /* to enable timer0 isr */
    TL0=0Xff; /* db ff is for 10 ms timing at 11.0592 mhz cpg 24.7.98*/
    TH0=0Xdb; /* earlier value was EBFE for 6.144 mhz .. cpg 24.7.98 */
    TMOD |=0X01;
    TR0=1;
    ET0=1; // enable timer int for 10 ms timer
    EX0=1; // enable hardware int for int 0 for isac
    EA=1; // is it required or not ?
    }


  • Gopal,

    You need to use:

    argsread=scanf("%bd %d %ld " ,&a,&b,&c );

    The 'b' flag modifies the 'd' specifier to make scanf expect an 8 bit value, it can't be used on its own. Likewise:

    printf("\n %bd %d %l \n", a,b,c );

    I don't think you need to set RI. RI goes true when a character arrives. scanf() calls _getkey() - you'll find the source for this in your c51\lib directory. _getkey() detects the arrival of a character by waiting for RI to go true.

    Remember that scanf() will need to see a whitespace character after the last number so that it can recognise the end of conversion and terminate.

    You do need the EA=1 to enable the other interrupts you are using, but you need to keep the serial interrupt (ES) disabled otherwise you will need to rewrite the _getkey() function.

    Stefan

  • There were several problem with your program (as Stefan) pointed out.

    1. There were enabled interrupts that there were no interrupt service routines for.

    • The serial port initialization caused a spurious receive interrupt.

    • The format specifiers for scanf were invalid.

    Therefore, you program worked as I expected (it didn't). I fixed all of the problems and the program ran correctly.

    #include <reg51.h>
    #include <stdio.h>
    
    void init_8051(void);
    
    
    void main(void)
    {
    char a;
    int b;
    long c;
    int argsread;
    init_8051();
    while(1)
    {
    printf("\n enter a signed byte ,int, and long \n");
    argsread = scanf("%bd %d %ld", &a, &b, &c);
    printf("\n %d arguments read \n",argsread);
    printf("\n %bd %d %ld \n", a, b, c );
    }
    }
    
    
    void init_8051(void)
    {
    SCON  = 0x50;	/* SCON: mode 1, 8-bit UART, enable rcvr      */
    TMOD |= 0x20;   /* TMOD: timer 1, mode 2, 8-bit reload        */
    TH1   = 221;    /* TH1:  reload value for 1200 baud @ 16MHz   */
    TR1   = 1;      /* TR1:  timer 1 run                          */
    TI    = 1;      /* TI:   set TI to send first char of UART    */
    }

    Jon

  • 8 feb 2003,
    Hello jon ward,
    Thanks you for the prompt replies and kind
    interest shown to help me. But unfortunately the program is not working on host pc dscope simulator (after compilation). I shall send you the *.lst ,*.m51 ,omf ,*.hex ,*.h,*.c files as attachment files.
    Please send me your email address. My address is c_p_gopal@hotmail.com
    thanks
    gopal

  • Contact technical support directly.

    Jon

  • 10 feb 2003
    dear jon ward
    thank you very much for your kind replies .I shall do as told by you and contact technical support directly.

    I once again thank you for the wonderful advice and help.

    thank you
    gopal