【Vitis】printf无法打印浮点数的解决办法

问题描述

  在使用vitis开发裸机程序时,将数据在串口输出时会遇到使用xil_printf和printf均只能打印字符串和整数,无法打印浮点数的问题。

出现原因

  这是由于vitis中的xil_printf函数不存在浮点数打印功能导致的,在xil_printf.c文件中,关于xil_printf函数有这样的注释。

/*---------------------------------------------------*/
/*                                                   */
/* This routine operates just like a printf/sprintf  */
/* routine. It outputs a set of data under the       */
/* control of a formatting string. Not all of the    */
/* standard C format control are supported. The ones */
/* provided are primarily those needed for embedded  */
/* systems work. Primarily the floating point        */
/* routines are omitted. Other formats could be      */
/* added easily by following the examples shown for  */
/* the supported formats.                            */
/*                                                   */

/* void esp_printf( const func_ptr f_ptr,
   const charptr ctrl1, ...) */
#if  defined (__aarch64__) && HYP_GUEST && EL1_NONSECURE && XEN_USE_PV_CONSOLE
void xil_printf( const char8 *ctrl1, ...){
	XPVXenConsole_Printf(ctrl1);
}
#else
void xil_printf( const char8 *ctrl1, ...)
{
	va_list argp;

	va_start(argp, ctrl1);

	xil_vprintf(ctrl1, argp);

	va_end(argp);
}
#endif

  注释中指出xil_printf不支持输出浮点数,这里提供一种可以代替浮点数打印的方法,在102开发板上运行可将浮点数进行输出。

一种解决办法

  利用整数打印间接打印浮点数

float tmp = 12.293;
printf("%d.%.08d", (int)(tmp), (int)(fabs(tmp)*100000000)%100000000);

你可能感兴趣的:(#,Xilinx入门,vitis,xil_printf,浮点数,串口输出)