Hello! I'm currently working on IAR and Launchpad with MSP430G2553 microcontroller. The task is to read analog value from potentiometer, calculate type float variable, holding respective voltage value and transmit it to PC (using RealTerm) via UART module. Difficulties start, when i need to send that 4 bytes. I think that problem is defining union for float variable, but because of my lack of knowledge in deeper programming, if can't figure out how to make it work. Code is pretty straight forward - Timer interrupt occurs every 1sec, where ADC10 reads value from pot (with Vcc as V+ref), calculate voltage and send to PC.
I would appreciate help from more experienced members.
Interrupt code below:
// Timer A0 interrupt service routine #pragma vector=TIMER0_A0_VECTOR __interrupt void Timer_A (void) { P1OUT ^= RED; //Toggle RED led ADC10CTL0 |= ENC + ADC10SC; //Enable conversion and start conversion. ADC10IFG=1 so conversion is loaded to ADC10MEM ADC_val = ADC10MEM; //Assigns the value held in ADC10MEM to the float variable called ADC_val ADC_voltage = (3.55 * ADC_val)/1023.0; //Calculation to represent ADC voltage. V+REF = Vcc = 3.55V. volatile union //Create union { float ADC_voltage; struct { char dataArray[4]; } TXdata; }; UCA0TXBUF = TXdata.dataArray[0]; //Load float variable 1st byte into transmit buffer while (!(IFG2&UCA0TXIFG)); //Wait little bit for value to be sent UCA0TXBUF = TXdata.dataArray[1]; //Load float variable 2nd byte into transmit buffer while (!(IFG2&UCA0TXIFG)); //Wait little bit for value to be sent UCA0TXBUF = TXdata.dataArray[2]; //Load float variable 3rd byte into transmit buffer while (!(IFG2&UCA0TXIFG)); //Wait little bit for value to be sent UCA0TXBUF = TXdata.dataArray[3]; //Load float variable 4th byte into transmit buffer while (!(IFG2&UCA0TXIFG)); //Wait little bit for value to be sent }
Values in terminal also isn't what would be expected: