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.

[参考译文] TM4C1294NCPDT:函数返回两个值,浮点转换为整数以用于打印

Guru**** 2928990 points
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1074840/tm4c1294ncpdt-function-returning-two-values-float-to-integer-conversion-for-printing-purposes

部件号:TM4C1294NCPDT

尊敬的支持:

当我处理一些小数输入时,我试图创建一个函数来改进代码的组织。

由于某些原因,它无法工作,也不会生成警告或错误。 也许我处理指针的方法不正确。

请提供一些帮助?  该代码基于提供的样本"湿度_sht21.c"。

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

#include "lib/hmi/floatto2partinteger.h"

[...]

int32_t IntPart, FraPart;

// ----------------
// Inputs from HMI
// ----------------
struct HMI_Input_Type {
	[...];
    float totalTime;              
};

struct HMI_Type {
    struct HMI_Input_Type   Input;
};

struct HMI_Type HMI;

void
UpdateInputs(){

    static char pcFourthInput[5];

    FloatTo2PartInteger(HMI.Input.totalTime, &IntPart, &FraPart);

    usprintf(pcFourthInput, "%3d.%1d", IntPart, FraPart);
    
}


/*
 * floatto2partinteger.h
 *
 *  Created on: Feb 4, 2022
 *      Author: Gustavo Wegher
 */

#ifndef LIB_HMI_FLOATTO2PARTINTEGER_H_
#define LIB_HMI_FLOATTO2PARTINTEGER_H_

extern FloatTo2PartInteger();

#endif /* LIB_HMI_FLOATTO2PARTINTEGER_H_ */


/*
 * floatto2partinteger.c
 *
 *  Created on: Feb 4, 2022
 *      Author: Gustavo Wegher
 */

#include <stdint.h>        // Integer definition
#include <stdbool.h>       // Bool definition


void FloatTo2PartInteger(float IntegerToConvert, int32_t* IntegerPart, int32_t* FractionPart){

    *IntegerPart = (int32_t) IntegerToConvert;                   // Convert IntegerPart from float number
    *FractionPart = (int32_t) (IntegerToConvert * 10.0f);        // Convert FractionPart from float number
    *FractionPart = *FractionPart - (*IntegerPart)*10;             // Remove IntegerPart from FractionPart

    if (*FractionPart < 0) {                                             // If FractionPart is negative
        *FractionPart *= -1;                                             // Multiply itself per -1 to obtain positive part
    }

}


提前感谢!

此致,
古斯塔沃·韦格尔

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    我期待着第一个论点被提升为两倍,这将使随后的论点被放弃。 这是发明函数原型来防止的事情之一。 尝试更换:

    >extern FloatTo2PartInteger ();

    使用

    >extern void FloatTo2PartInteger (float IntegerToConvert,Int32_t* IntegerPart,Int32_t* FractionPart);

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    非常有魅力! 非常感谢!