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

Parents
  • 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.
Reply
  • 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.
Children
No data