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.

uint32_t to float casting help!!

Other Parts Discussed in Thread: TM4C1230C3PM

I am using tm4c1230c3pm platform and coding with Keil uVision5.

I want to cast the uint32_t  into float with 3 significant figures.

I tried  casting  so many times referring to other suggestions in the web with channel 4 output in my code, but it gave me ERROR every single time.

help me please~~~~

any suggestions would be appreciated!!

here's my entire codes

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc\hw_uart.h"
#include "inc/hw_gpio.h"
#include "inc\hw_ints.h"

#include "driverlib\pin_map.h"
#include "driverlib\rom.h"
#include "driverlib\rom_map.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"
#include "driverlib/uart.h"
#include "driverlib/ssi.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"

#include "utils/uartstdio.h"
#include "utils/softssi.h"

#define DATA_LEN 16
uint32_t g_ui32SendData[DATA_LEN], g_ui32RcvData[DATA_LEN];

void readdata(void);
int nullVal = 0;

uint32_t adcdata[4]; 

void delay(volatile unsigned long ulDelay){

ROM_SysCtlDelay((ROM_SysCtlClockGet() / (1000*3)) * ulDelay);
}

void ConfigureUART(void){

ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

ROM_UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
UARTStdioConfig(0, 115200, 16000000);
}

void ConfigureSSI(void){

ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_INT | SYSCTL_MAIN_OSC_DIS);

ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI1);

ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);


ROM_GPIOPinConfigure(GPIO_PD0_SSI1CLK);
ROM_GPIOPinConfigure(GPIO_PD1_SSI1FSS);
ROM_GPIOPinConfigure(GPIO_PD2_SSI1RX);
ROM_GPIOPinConfigure(GPIO_PD3_SSI1TX);

ROM_GPIOPinTypeSSI(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1| GPIO_PIN_2| GPIO_PIN_3);

ROM_SSIClockSourceSet(SSI1_BASE, SSI_CLOCK_PIOSC);

ROM_SSIConfigSetExpClk(SSI1_BASE, ROM_SysCtlClockGet(), SSI_FRF_MOTO_MODE_0,
SSI_MODE_MASTER, 115200, 16);

ROM_SSIEnable(SSI1_BASE);
}

float uintToFloat (uint32_t intVal){
uint32_t temp = intVal;
float floatVal = temp;
return floatVal;
}

int main(void){
volatile float rxData;


ROM_FPULazyStackingEnable();

ConfigureSSI();
ConfigureUART();
// CONVST
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_5);

while(1){
unsigned char i;
ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, 0);
ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, GPIO_PIN_5);

//CS
ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1, 0);

while(ROM_SSIBusy(SSI1_BASE));

while(ROM_SSIDataGetNonBlocking(SSI1_BASE, &g_ui32RcvData[0]));

SSIDataPut(SSI1_BASE, nullVal);
SSIDataGet(SSI1_BASE, &adcdata[0]);
UARTprintf(" \nreceived %d", adcdata[0]);
SSIDataPut(SSI1_BASE, nullVal);
SSIDataGet(SSI1_BASE, &adcdata[1]);
UARTprintf(" \nreceived %d", adcdata[1]);
SSIDataPut(SSI1_BASE, nullVal);
SSIDataGet(SSI1_BASE, &adcdata[2]);
UARTprintf(" \nreceived %d", adcdata[2]);


SSIDataPut(SSI1_BASE, nullVal);
SSIDataGet(SSI1_BASE, &adcdata[3]);
rxData = uintToFloat(adcdata[3]);
UARTprintf(" \nreceived %.3f", rxData);

UARTprintf("\nCh1 voltage 1= %d Volts", adcdata[0]/3266); // *((float*)&adcdata[0])/3266)
UARTprintf("\nCh2 voltage 2= %d Volts", adcdata[1]/3266);
UARTprintf("\nCh3 voltage 3= %d Volts", adcdata[2]/3266);


UARTprintf("\nCh4 voltage 4= %.3f Volts", (float) rxData/3266.0);

while(ROM_SSIBusy(SSI1_BASE));

delay(1000);
}
}

OUTPUT

  • This is a TIVA related forum, yes code is TIVA based C but errors are basic programming failure due to low knowledge of it... A basic C programming course string management and basic value input output solve your problem.
  • Sukgyu Koh said:
    UARTprintf(" \nreceived %.3f", rxData);

     This float qualifier is not supported so you need some alternate way to print it example sprintf() is fully implemented on

    Sukgyu Koh said:
    UARTprintf("\nCh1 voltage 1= %d Volts", adcdata[0]/3266); // *((float*)&adcdata[0])/3266)

     The form I see after comment is strange, you convert array element integer variable to his address (pointer) then cast as float address, try dereference and divide by integer 3266

     It doesn't work!

     (float)adcdata[0])/3266.0

    first value cast to float, then divided by a float value return a float

    char mystring[30];

    sprintf(mystring," integer is %d float is %5.3f",adcdata[x],(float)adcdata[x])/3266.0);

    uartprintf("%s",mystring);