如何在 Microchip Data Visualizer 中绘制浮点数

问题描述

我在通过 UART 发送浮点数以在 microchip 的数据可视化器上绘制图表时遇到问题。

我可以毫无问题地绘制 int 数字,但浮点数让我发疯。

我用拉普拉斯变换做了一个正弦波。之后,将其放在带有双线 z 变换的“z”平面上,然后将等式放在 dsPIC33FJ128GP802 的主程序中。它工作正常。在终端中,我可以看到这些值,如果我在 gnumeric 上复制/粘贴这些值并制作图表,它会显示我的离散正弦波。

当我尝试在 MPLABX 的数据可视化器中绘制浮点数“yn”时,问题就出现了。我在中间缺少一些东西。

我在 Debian bullseye 上使用 MPLABX v5.45、XC16 v1.61。与微控制器的通信是透明的@9600-8N1。

这是我的主要代码

int main(void)
{
    InitClock(); // This is the PLL settings
    Init_UART1();// This is the UART Init values for 9600-8-N-1
    float states[6] = {0,0};
    // states [xn-2 xn-1 xn yn yn-1 yn-2]
    xn = 1.0; //the initial value
    
    while (1)
    {
        yn = 1.9842*yn1-yn2+0.0013*xn1+0.0013*xn2; // equation for the sine wave
        yn2 = yn1;
        yn1 = yn;
        xn2 = xn1;
        xn1 = xn;
        
        putc(0x03,stdout);
        //Here I want to send the xn to plot in MDV
        putc(0xFC,stdout);
        
    }
}

方程中的变量

yn = 1.9842*yn1-yn2+0.0013*xn1+0.0013*xn2;

#define是这样的

#define xn  states[2]
#define xn1 states[1]
#define xn2 states[0]
#define yn  states[3]
#define yn1 states[4]
#define yn2 states[5]

WriteUART1(0x03);WriteUART1(0xFC); 用于 Data Visualizer 查看第一个字节和最后一个字节。就像 microchip 视频中的示例一样。

问题是:我如何管理要由 microchip 数据可视化器绘制的浮点数 yn

提前致谢。

解决方法

好的,这就是答案。

一个浮点数,它有 32 位长,但你不能像 int 那样一点一点地管理它们。所以方法是像字符一样管理。

您必须创建一个指向字符的指针,将浮点数的地址分配给指针(强制转换地址,因为字符指针与浮点指针不同)。然后只发送 4 个字节,增加字符指针。

代码如下:

while (1)
{
    yn = 1.9842 * yn1 - yn2 + 0.0013 * xn1 + 0.0013 * xn2; // sine recursive equation
    yn2 = yn1;
    yn1 = yn;
    xn2 = xn1;
    xn1 = xn;
    ptr = (char *) & yn; // This is the char pointer ptr saving the address of yn by casting it to char*,because &yn is float*
    putc(0x03,stdout); // the start frame for MPLABX Data Visualizer
    for (x = 0; x < sizeof(yn) ; x++) // with the for we go around the four bytes of the float
        putc(*ptr++,stdout); // we send every byte to the UART
    putc(0xFC,stdout); // the end frame for MPLABX Data Visualizer.
}

通过这项工作,您必须配置数据可视化工具、波特率,然后选择新的流变量。您选择一个名称,然后您选择帧模式,您选择 One 的补码,在本例中为起始帧 0x03 和结束帧 0xFC。只需命名变量,然后键入 float32,按下一步,绘制变量,完成,您就可以在 MPLABX 时间绘图仪中获得变量。

Here is the image of the plot

希望,这对某人有所帮助。

问候。-