How to use the scanf() function in a real 8051 mcu ?

Hi, I use the 8051 mcu and Keil C51 to teach my students for the C programming language. The 8051+KeilC51 will make the students understand the power of the C programming language. Students can gain immense confidence. In my teaching, I can use the printf() function for the example programs, but when I use the scanf() function, I encountered some difficulities. 

My example program is:

#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   = 0xfd; /* TH1:  reload value for 9600 baud @ 11.0592MHz   */
	TR1   = 1;    /* TR1:  timer 1 run                          */
	TI    = 1;    /* TI:   set TI to send first char of UART    */
}

I download the .hex file (which generated by the example code above) to my 8051 mcu, then I send some numbers to the variables a, b, c, such as:

When I click the sendData button, the software got a lot of message as the picture shows above.  These message runs forever, they are not stop.

But, if I click the "Start debug Session" button:

The keil will enter the debug session, and I can run the example code above correctly:

Unfortunately, the  debug session is a simulation tool, it is not real running in the 8051 mcu.

Does anyone can help me, how can I use the scanf() in a real 8051 mcu, not just running it in a simulation session ?

Best regards.

The example project:

test1224.zip

  • Will your students have development boards with flash-based MCU's that support in-circuit debugging?  Hopefully so, as this will provide the best learning experience. I don't know all of the chip manufacturers that support this, but Silicon Labs does via their USB Debug Adapter and so does Nuvoton with their Nu-Link.   If memory serves, the Nuvoton debugger runs right in the Keil IDE, but I'm not sure if the Silicon Labs does.

  • The 8051+KeilC51 will make the students understand the power of the C programming language

    That argument is bass-ackwards, to put it mildly.  The 8051 is just about the worst possible choice to demonstrate "power" these days, and simililarly C51 is among the worst possible choices to exercise the C programming language on.  There are simply too many idiosyncrasies and limitations the compiler needs to accomodate the platform, and that introduces considerable amounts of breakage into the language itself.  If your actual goal really is to teach the language, any random desktop programming environment would be a better choice than C51.  Even if you're targetting embedded usage, there are platforms that are leaps and bounds ahead of C51 in terms of actually supporting the language as it is meant to be.  ARM is, of course, the absolute standard, but even the more exotic platforms in the embedded field these days invariably have better C language coverage than C51.  At the very least they'll be able to support versions of the standard language younger than 30 years --- C51 is, and most likely forever will be, stuck in 1990..

  • Here are some hints to get your application up and running.

    • It seems that in your terminal program, you are sending the values 4,5,6 as hex values and not as text. scanf expects text.
    • I don't know this device and this chip vendor, but if you want to debug this application on a target system please check if this device has a debug interface and that there is a µVision compatible debug adapter. If not, you could use the ISD51 (see https://developer.arm.com/documentation/101655/0961/ISD51-User-s-Guide) to use the RS23 interface as a debug interface. It's not as powerful as a real debug interface, but better than nothing. Unfortunately, 8051 devices (unlike Arm Cortex-M based devices) do not have a standardized debug interface.
    • I agree with the previous two replies. So if you are staring to build up a training for your students, I would select an Arm Cortex M based device with an evaluation board from a chip vendor (there are tons available). Many evaluation board already have a CMSIS-DAP or ST-Link compatible debug interface so that you can connect them to your PC right away. You could then use our MDK 5 or MDK 6.