This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430G2553 printf sprintf以及sscanf中%f不能正确执行

Other Parts Discussed in Thread: MSP430G2553

重载了printf函数,%d,%s格式都能正确输出,%f格式不能正确输出,系统不断重启,sprintf和sscanf中%s均不能正确执行,已经在Library Function Assumptions中设置Level of printf为full。

  • 代码或者工程贴上来。

  • #define UART_PRINTF

    #ifdef UART_PRINTF
    int fputc(int _c, register FILE *_fp);
    int fputs(const char *_ptr, register FILE *_fp);
    #endif

    #ifdef UART_PRINTF
    int fputc(int _c, register FILE *_fp)
    {
     UCA0TXBUF = (unsigned char) _c;
     while (!(IFG2&UCA0TXIFG));

      return((unsigned char)_c);
    }

    int fputs(const char *_ptr, register FILE *_fp)
    {
      unsigned int i, len;

      len = strlen(_ptr);

      for(i=0 ; i<len ; i++)
      {
        UCA0TXBUF = (unsigned char) _ptr[i];
        while (!(IFG2&UCA0TXIFG));
      }

      return len;
    }
    #endif


    /*
     * MSP430G2553IPW20 2Kb Flash;256 Bytes RAM
     */
    int main(void) {
     volatile unsigned int i;
     char str[16];
        WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

        UART0_init();
        LED_init();

        printf("MSP430 UART printf demo\r\n");
        printf("int value:%d\r\n", 125);
        printf("string value:%s\r\n", "hello");

        sprintf(str, "val:%f", 3.13);
        printf("string value:%s\r\n", str);
    //    printf("float value:%.2f\r\n", 3.15);

        _BIS_SR(GIE);

    //    while(1);

     return 0;
    }

    以上是重载了printf中main程序的主要内容,在main函数中尝试试了几种printf的格式包括%d %s 都没问题,%f格式输出就出问题了,系统不断重启。

  •  可能是编译器配置问题,如下图

  • 已经在CCS中设置了如下:

    设置Level  of printf是full,但是还是不能使用%f,难道是因为MSP430G2553 Flash或者RAM资源太少不能使用%f吗?

    你的第二幅截图中的Library Options设置Printf formatter是IAR软件中的设置把?

  • #include "msp430.h"
    #include "stdio.h"
    
    int putchar(int ch)
    {
    	if(ch == '\n')
    	{
            UCA0TXBUF = '\r';
            while (!(IFG2&UCA0TXIFG));
    	}
        UCA0TXBUF = ch;
    	while (!(IFG2&UCA0TXIFG));
    	return(ch);
    }
    
    void main()
    {
        float value = 123.321;
    
        char *string = "http://www.ti.com";
    
        WDTCTL = WDTPW + WDTHOLD;
    
    	if (CALBC1_1MHZ==0xFF)			 // If calibration constant erased
    	{
    		while(1);                        // do not load, trap CPU!!
    	}
    	DCOCTL = 0;                               // Select lowest DCOx and MODx settings
    	BCSCTL1 = CALBC1_1MHZ;                    // Set DCO
    	DCOCTL = CALDCO_1MHZ;
    	P1SEL = BIT1 + BIT2 ;                     // P1.1 = RXD, P1.2=TXD
    	P1SEL2 = BIT1 + BIT2 ;                    // P1.1 = RXD, P1.2=TXD
    	UCA0CTL1 |= UCSSEL_2;                     // SMCLK
    	UCA0BR0 = 104;                            // 1MHz 9600
    	UCA0BR1 = 0;                              // 1MHz 9600
    	UCA0MCTL = UCBRS0;                        // Modulation UCBRSx = 1
    	UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
    
        printf("value = %f\n", value);
        printf("%s\n", string);
    
        while(1);
    }
     
  • Hi  

    我在ccs上验证你提供的代码,用g2553,也没通过。能提供下你的工程么?


  • 我是IAR的工程