Keil Logo

C51: SCANF %F FAILS WITH USER-DEFINED GETKEY FUNCTION


Information in this article applies to:

  • C51 All Versions

SYMPTOM

I have a problem with the following application code that uses the scanf function and a customized version of the getkey function.

#include <reg52.h>
#include <stdio.h>

void Setup (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 @ 16Mz
    TR1   = 1;     // TR1:  timer 1 run
    TI    = 1;     // TI:   set TI to send first char of UART
}

char _getkey(void) {
   char c;
   while (!RI);
   c = SBUF;
   RI = 0;
   return(c);
}

void main(void) {
   float flt;

   Setup(); //Initialize serial port
   while(1)  {
      printf("\r\nEnter a Number (try 123.45): ");
      scanf("%f",&flt);    // scanf fails here
      printf("%.6f\r\n",flt);
   }
}

When using the '%f' format parameter, the scanf function fails at OPTIMIZE level 2 and level 3. What is the reason for this problem?

CAUSE

The scanf function does not use a temporary buffer when processing input. Instead, it directly processes the input returned by the getkey function.

Unfortunately, there is a problem with the floating-point processing buffer that only appears when the getkey function requires overlayable data. This problem also occurs when getkey is compiled with OPTIMIZE level 2 or level 3 (no CPU register optimization is performed at these optimizer levels).

RESOLUTION

This problem may be solved by any of the following:

  • Use the CPU register optimization of the C51 Compiler (OPTIMIZE level 4 or higher) so that getkey does not require overlayable data.
  • Disable data overlaying for the getkey function using the Linker/Locater OVERLAY directive with the parameter:
    OVERLAY (* ! _getkey)
    
  • Use an input buffer and process the input with sscanf instead.

STATUS

This problem will not be corrected.

Last Reviewed: Friday, June 17, 2005


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.