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
  • 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

Reply
  • 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

Children