Print floating point values using STM8S103FP Mini Dev Board

Thread Starter

Prasad_123

Joined Jun 19, 2024
2
I am doing a Current, Voltage Measuring project using INA219 Current Sensor and using STM8S103F3P Mini Development Board. And monitoring the data in Serial Monitor. But I am not able to read/write Floating Values. For Eg: If the multimeter measures 21.28 mA then I am only getting 21 when I use int or long int. And if I use float then random symbols like "????" occur in the serial monitor.
What can I do to print floating values or how can I print values more precisely?
 

WBahn

Joined Mar 31, 2012
30,330
You don't give very much information, such as what language you are programming in. But you might try printing your value to a string and sending that string across your serial link.
 

Thread Starter

Prasad_123

Joined Jun 19, 2024
2
You don't give very much information, such as what language you are programming in. But you might try printing your value to a string and sending that string across your serial link.
I am using Embedded C to Program STM via ST-Link V2.

here is small cutout from my program


C-like:
int readCurrent(void) {

    uint8_t reg[2] = {0};

    uint16_t raw = 0;


    I2C_GenerateSTART(ENABLE);

    while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));


    I2C_Send7bitAddress(INA219_ADDRESS << 1, I2C_DIRECTION_TX);

    while (!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));


    I2C_SendData(0x04); // Current register

    while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));


    I2C_GenerateSTART(ENABLE);

    while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));


    I2C_Send7bitAddress(INA219_ADDRESS << 1, I2C_DIRECTION_RX);

    while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));


    while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED));

    reg[0] = I2C_ReceiveData();


    while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED));

    reg[1] = I2C_ReceiveData();


    I2C_GenerateSTOP(ENABLE);


    raw = (reg[0] << 8) | reg[1];


    // Return current in microamperes (µA)

    return raw; // Assuming calibration constant

}


void sendCurrentReadingToUART(void) {

    int current_uA = readCurrent();


    char buffer[50];

    sprintf(buffer, "Current: %d µA\n", current_uA);

    UART1_SendString(buffer);


    UART1_SendString("\n");

}
 

MrChips

Joined Oct 2, 2009
31,173
Your raw data is an 16-bit integer value in steps of 10μV.

For example 32000 decimal is 320.00 mV or 0.32000 V.
You can do all your math in floating-point format if the MCU has enough memory space to do this. I am not a big fan of FP arithmetic on a small MCU.

My suggestion would be to convert the integer into a string array and then insert the decimal point where it is required, i.e. depending on if you want to show V, mV, μV and A, mA, or μA.
 
Top