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
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.
#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 */ }
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