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.

Expected an Expression IAR

Other Parts Discussed in Thread: MSP430G2553

Hi,

Im getting this error message on IAR regarding converting integer values to a float result

The compiler keeps on giving the same error; Error[Pe029]: expected an expression

Result is defined as a float

Rterm is #define Rterm = 1000;

Result=(float))(((float)(Top-Middle)))/((float)(Middle-Bottom)*Rterm);

Any ideas guys

  • There is one bracket too much:

    Result=(float))

    Dennis
  • Hi Dennis,
    Is that at the first float in the line of code? I get expected a ) if I remove it
    thanks
  • You cannot open one bracket and close two.
  • The lines you posted contains obvious errors: There is an "=" in the #define statement and the parentheses don't add up in the "Result=" line. 

    Please post a complete, yet small, example. By "complete" I mean something that can be copy-pasted into a source file and compiled immediately. This requires, for example, that the "Result" line is placed in a functions, that relevant header files are included etc.

    Don't see this as a put down -- I would like to help you, but I simply don't have enough information to do so.

        -- Anders Lindgren, Author of the IAR compiler for MSP430

  • Anders Lindgren said:
    There is an "=" in the #define statement

    Oh yeah, right.

    James, from your other thread I know that Top, Midlle and Bottom are unsigned int values. Your calculation should be the same when just writing

    #define Rterm 1000.0f
    
    Result = ((Top - Middle) / ((Middle - Bottom) * Rterm));

    Dennis

  • Hi guys,
    The code itself is not too big, Ill just copy it directly in.
    Ive decided to use your changes Dennis as they are much neater and more efficient than what I had before.
    The code below should compile other than the issue

    #include "msp430g2553.h"

    #define Rterm = 1000;

    unsigned int Top, Middle, Bottom; // ADC RESULT VARIABLES
    float Result; //Calculated result output
    void Read_Value( int A ); // Read ADC values
    void UART_OutUDec( unsigned long n ); // Convert result to ASCII decimals
    void UART_output( char data ); // Output UART data



    int main( void )
    {
    WDTCTL = (WDTPW | WDTHOLD); // Stop WDT

    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 = UCSWRST; // USCI reset
    UCA0CTL1 |= UCSSEL_2; // SMCLK
    UCA0BR0 = 104; // 1MHz 9600
    UCA0BR1 = 0; // 1MHz 9600
    UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
    UCA0CTL1 &= ~UCSWRST; // Release USCI from reset
    IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt

    while( 1 )
    {
    Read_Value( 1 ); // Setup for ADC P1.0
    Read_Value( 2 ); // Setup for ADC P1.1
    Read_Value( 3 ); // Setup for ADC P1.3
    //add in temp conversion
    Result=(float))(((float)(Top-Middle)))/((float)(Middle-Bottom)*Rterm);
    UART_OutUDec( Top ); // TX TOP ADC RESULT
    UART_output( 0x09 ); // TAB
    UART_OutUDec( Middle ); // TX MIDDLE / THERMISTOR ADC RESULT
    UART_output( 0x09 ); // TAB
    UART_OutUDec( Bottom ); // TX BOTTOM ADC RESULT
    UART_output( 0x09 ); // TAB

    UART_OutUDec(Result); // TX Result ADC RESULT
    UART_output( 0x09 ); // TAB
    UART_output( 0x0A ); // NEW LINE
    }
    }



    void Read_Value( int A )
    {
    ADC10CTL0 = 0x00; // Stop ADC

    if( A == 1 )
    {
    ADC10CTL1 = INCH_0 | ADC10SSEL_3; // Input Channel A0 and SMCLK
    ADC10AE0 = 0x01; // Enable analog input for p1.0
    }

    if( A == 2 )
    {
    ADC10CTL1 = INCH_1 | ADC10SSEL_3; // Input Channel A1 and SMCLK
    ADC10AE0 = 0x02; // Enable analog input for p1.1
    }

    if( A == 3 )
    {
    ADC10CTL1 = INCH_3 | ADC10SSEL_3; // Input Channel A3 and SMCLK
    ADC10AE0 = 0x08; // Enable analog input for p1.3
    }

    ADC10CTL0 = ADC10ON | ENC; // Turn on ADC and Enable Conversion
    ADC10CTL0 = ADC10CTL0 | ADC10SC; // Start conversion

    while( ADC10CTL1 & ADC10BUSY ) // Wait until conversion is complete
    {
    }

    if( A == 1 )
    {
    Top = ADC10MEM; // ADC VALUE top_R100
    }

    if( A == 2 )
    {
    Middle = ADC10MEM; // ADC VALUE Middle/ 1k Thermistor
    }

    if( A == 3 )
    {
    Bottom = ADC10MEM; // ADC VALUE Bottom_R100K
    }

    }



    void UART_OutUDec( unsigned long n )
    {
    UART_output( (n / 1000) + '0' );
    n %= 1000;
    UART_output( (n / 100) + '0' );
    n %= 100;
    UART_output( (n / 10) + '0' );
    n %= 10;
    UART_output( (n % 10) + '0' );
    }



    void UART_output( char data )
    {
    while( !(IFG2 & UCA0TXIFG) );
    UCA0TXBUF = data;
    }
  • You should think about if you need the floating point numbers at all. You could also multiply everything by a known factor and calculate with integers. Of course you can use floating point, even on the small MSP430G2553, but in general this causes much larger code size and is quite slow.

    Just an example: Instead of using 1V, you could also work with 1,000mV or 1,000,000µV.
  • So i could really do the result = and allow the msp430 to just  round of f  the values itsekf?

  • Sorry, I do not understand that. Can you explain it more in detail?
  • Sorry Dennis, I was trying to say I could just use your code example:

    #define Rterm 1000.0f

    Result = ((Top - Middle) / ((Middle - Bottom) * Rterm));
  • One other thing, the result it gives when I convert the ADC value to resistance is correct.
    Its just that the variable in the debug window shows what should 1050 for example as 1.0504444 E-3 for example
    Any ideas?
  • Because it is a floating point number. But I guess it shows 1.0504444E+3 instead of E-3.
    This means 1.0504444 * 10 * 10 * 10 = 1050
  • Hi!

    Thanks for posting a compilable version of the program. After changing the following lines, the program compiled just fine with the IAR tools.

    Line 3:

    #define Rterm 1000
    

    Line 44:

    Result=(float)(((float)(Top-Middle)))/((float)(Middle-Bottom)*Rterm);
    

        -- Anders Lindgren, Author of the IAR compiler for MSP430

**Attention** This is a public forum