ARM Technical Support Knowledge Articles

SCANF %F FAILS WITH USER-DEFINED GETKEY FUNCTION

Applies to: C51 C Compiler

Answer


Information in this article applies to:


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("
Enter a Number (try 123.45): ");
      scanf("%f",&flt);    // scanf fails here
      printf("%.6f
",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:

STATUS

This problem will not be corrected.

Article last edited on: 2005-06-17 15:43:26

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential