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
It already does this right out of the box. There's nothing special you need to do except setup the serial port. I guess you haven't looked at the Hello World example. Jon
6 feb 2003, Dear john ward , Thanks .Your are the very first to reply to my query. I have already looked into all the sample programs .But unfortunately nowhere scanf() is used. gopal
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.
#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