概述
在FreeBSD中针对寄存器在特殊情况下可能需要写入float类型的值,具体实现步骤如下:
//变量
typedef struct{
float under_limit;
float over_limit;
}VFEPIO_SetRawLimit_t;
typedef struct{
VFEPIO_ReqHeader_t header;
union{
VFEPIO_SetRawLimit_t raw_limit;
}param;
}VFEPIO_Request_t;
VFEPIO_Request_t VFEPIO_Request;
VFEPIO_SetRawLimit_t raw_limit;
float under_limit;
float over_limit;
uint32_t *piTemp1;
uint32_t *piTemp2;
//代码的实现
raw_limit = VFEPIO_Request->param.raw_limit;
under_limit = raw_limit.under_limit ;
over_limit = raw_limit.over_limit;
piTemp1 = (uint32_t *)(&under_limit);
piTemp2 = (uint32_t *)(&over_limit);
pci_write_bar1(sc, UNDERFLOW_LIMIT_ADDR, *piTemp1);
pci_write_bar1(sc, OVERFLOW_LIMIT_ADDR, *piTemp2);
◆在C语言中也是可以证明这点的,具体实现如下
#include <stdio.h>
#include <stdlib.h>
int main()
{
float a = 1.23;
float b = 0.00;
float *d = 0;
int c = 0x00;
int *ptr ;
ptr = (int *)&a;
c = *ptr;
// printf("c = %x!rn", c);
d = (float *)&c;
b = *d;
printf("b = %0.2f!rn", b);
return 0;
}
结果如下:
-
在C#中可以编辑为如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Float2Byte
{
class Program
{
static void Main(string[] args)
{
float f = -0.6f;
byte[] b = BitConverter.GetBytes(f);
Console.WriteLine(BitConverter.ToString(b));
float f3 = (float)(0.6);
byte[] b4 = BitConverter.GetBytes(f3);
Console.WriteLine(BitConverter.ToString(b4));
float f2 = 120.5f;
byte[] b2 = BitConverter.GetBytes(f2);
Console.WriteLine(BitConverter.ToString(b2));
float ff2 = 10.0f;
byte[] b3 = BitConverter.GetBytes(ff2);
Console.WriteLine(BitConverter.ToString(b3));
byte[] bb = { 0x00, 0x00, 0x20, 0x41 };
Console.WriteLine("n" + BitConverter.ToSingle(bb, 0));
}
}
}
-
如果上下层需要通过byte数组进行float数据的交互
(1)如下是底层传到上层的处理,在底层的代码如下:
float highlimitvalue;
int32_t highlimitvalueTemp;
if (exmodthresholdlimitack.highlimit[1] == 0x80)
{
highlimitvalue = 0.0 - (exmodthresholdlimitack.highlimit[0] / 100.0);
}
else
{
highlimitvalue = (exmodthresholdlimitack.highlimit[0] / 100.0);
}
IIC_Status_Ret = 0x00;
//DvmodExmodErr | ExmodScontErr
IIC_Status_Ret = 0x00 | DVMOD_Status | exmodthresholdlimitack.err1;
highlimitvalueTemp = *((int32_t *)(&highlimitvalue));
output->Data[0] = (uint8_t)( ( highlimitvalueTemp >> 0) & 0xff );
output->Data[1] = (uint8_t)( ( highlimitvalueTemp >> 8) & 0xff );
output->Data[2] = (uint8_t)( ( highlimitvalueTemp >> 16) & 0xff );
output->Data[3] = (uint8_t)( ( highlimitvalueTemp >> 24) & 0xff );
在上层的代码如下:
highlimit = BitConverter.ToSingle(getthresholdlimitack.highthreshold, 0);
highlimit = (short)(highlimit * 100) / 100.0;
(2)如下是上层传到底层的处理,在上层的代码如下:
byte[] ByteThresholdlow = new byte[4];
byte[] ByteThresholdup = new byte[4];
ByteThresholdlow = BitConverter.GetBytes((float)Thresholdlow[exmodid, scontid]);
ByteThresholdup = BitConverter.GetBytes((float)Thresholdup[exmodid, scontid]);
在底层接收的操作处理代码如下:
uint8_t HighThresholdLimit[4];
uint8_t *pucTemp1;
float HighThresholdLimitValue;
HighThresholdLimit[0] = input->RcvData[7];
HighThresholdLimit[1] = input->RcvData[8];
HighThresholdLimit[2] = input->RcvData[9];
HighThresholdLimit[3] = input->RcvData[10];
pucTemp1 = (uint8_t*)&HighThresholdLimitValue;
pucTemp1[0] = HighThresholdLimit[0];
pucTemp1[1] = HighThresholdLimit[1];
pucTemp1[2] = HighThresholdLimit[2];
pucTemp1[3] = HighThresholdLimit[3];
if (HighThresholdLimitValue < 0)
{
exmodsetthresholdcmd.highlimit[1] = 0x80;
}
else
{
exmodsetthresholdcmd.highlimit[1] = 0x00;
}
exmodsetthresholdcmd.highlimit[0] = (uint8_t)(fabs(HighThresholdLimitValue) * 100);
最后
以上就是激昂大炮为你收集整理的将float类型数据写入寄存器中的全部内容,希望文章能够帮你解决将float类型数据写入寄存器中所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复