由于FPGA中存在的都是二进制数。 而外部(软核)与FPGA通信数据为小数时,
首先需将小数转换为定点数(二进制)。再写入FPGA中,即可在FPGA内当作小数运算,并且其运算结果仍为定点数。
例如首先将小数转换为二进制数,然后使用FPGA自带浮点数转换定点数IP核,转换为定点数二进制数。写入FPGA内。
0.0033203125(float)---------3FD9999A(hex)-------------6D(定点数 0.000_0000_0110_1101)
当一个定点数为正数时 ,其二进制 数 以源码形式存在 ;当定点数为负数时 ,其二进制数以补码形式存在。
补码= 原码符号位不变,数值位按位取反,末位再加1。当定点数进行加减运算时 ,小数点位置不变,还为定点数,
结果与转换为小数再进行运算相同 。
下面程序为通过读写寄存器方式读取FPGA内部数据phase_data.word,读取该数据之后在Microblaze当中解析该数据。
1 union DEF_long 2 { 3 struct 4 { 5 unsigned char UH; 6 unsigned char H; 7 unsigned char L; 8 unsigned char UL; 9 }; 10 char byte[4]; 11 Xint32 word; 12 }def_long; 13 14 union DEF_long phase_data; 15 16 float VI_phase_int; //定点数整数部分 17 Xuint16 VI_phase_float_data; //定点数小数部分十进制 18 float VI_phase_float; //定点数小数部分 19 float VI_phase; //定点数 20 21 if(phase_data.UH ==0xFF) //负数 22 { 23 VI_phase_int = 0xFF - phase_data.H; 24 VI_phase_float_data = phase_data.L <<8; 25 VI_phase_float_data = VI_phase_float_data |phase_data.UL; 26 VI_phase_float_data = 0xFFFF -VI_phase_float_data; 27 VI_phase_float = VI_phase_float_data /65536.0; 28 VI_phase =(-1)*( VI_phase_int + VI_phase_float ); 29 } 30 else if(phase_data.UH ==0x00) //正数 31 { 32 VI_phase_int = phase_data.H; 33 VI_phase_float_data = phase_data.L <<8; 34 VI_phase_float_data = VI_phase_float_data |phase_data.UL; 35 VI_phase_float = VI_phase_float_data /65536.0; 36 VI_phase = VI_phase_int + VI_phase_float ; 37 }