请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
部件号: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
}
}
提前感谢!
此致,
古斯塔沃·韦格尔