使用单片机在图形点阵LCD上绘制波形图
需求:
假如有一组浮点数据,是通过AD转换得到的,保存在数组MyArray[]中,采集点数为len,采集周期为T,现在想用单片机在LCD上绘制出这组数据对应的波形图,该如何实现呢?
在图形点阵LCD上制波形图,主要是为了提高采集数据的可视化效果,其次是为了提升产品的竞争力。我的理解就是为了好看。
一、绘图原理
1、定义波形图的显示区域
在绘图之前,我们需要先规划好显示区域。在12864的点阵屏中,X轴坐标为0~127,Y轴坐标为0~63,因此,我们定义波形图的显示参数如下:
#define Picture_Xmin 10 //波形图在LCD中的最小x坐标
#define Picture_Ymin 25 //波形图在LCD中的最小y坐标
#define Picture_Xpp 108 //波形图在LCD中的最大长度
#define Picture_Ypp 35 //波形图在LCD中的最大高度
#define Picture_Xmax (Picture_Xmin+Picture_Xpp) //波形图在LCD中的最大x坐标
#define Picture_Ymax (Picture_Ymin+Picture_Ypp) //波形图在LCD中的最大y坐标
注意:Picture_Xmax<128, Picture_Ymax<64
2、求数组MyArray[]中的最大值,最小值和峰峰值
采集数据的最大峰峰值数据,在绘图时,落在波形图的最大高度处,纵坐标反而是最小的。因此,我们采用最大值和采集点数据减法计算,得到其纵坐标。这样,就可以绘制曲线图了。
float MaxValue; //用来保存MyArray[]中的最大值
float MinValue; //用来保存MyArray[]中的最小值
float APPValue; //用来保存MyArray[]中的最大峰峰值
//函数功能:计算"最大值,最小值和峰峰值"
void Calculate_MaxValue_MinValue_ And_APPValue (float *s,u16 len)
{
u16 i;
float f;
MaxValue = s[0]; //默认最大值为第1点
MinValue = s[0]; //默认最小值为第1点
for(i=0;i<len;i++)
{
f=s[i];
if( MaxValue < f ) MaxValue= f;//保存最大值
if( MinValue > f ) MinValue= f;//保存最小值
}
APPValue = MaxValue - MinValue;//求最大峰峰值
}
3、画图
void DrawPicture (float *s,u16 len)
{
u16 i;
s16 x0;
s16 y0;
s16 x1;
s16 y1;
float f;
for(i=1;i<len;i++)//绘制曲线图
{
f=i-1; f=f*Picture_Xpp; f=f/len; x0=f;
f=MaxValue-s[i-1]; f=f*Picture_Ypp; f=f/APPValue; y0=f;
f=i; f=f*Picture_Xpp; f=f/len; x1=f;
f=MaxValue-s[i]; f=f*Picture_Ypp; f=f/APPValue; y1=f;
x0=x0+Picture_Xmin;
x1=x1+Picture_Xmin;
y0=y0+Picture_Ymin;
y1=y1+Picture_Ymin;
if(x1<Picture_Xmin) x1=Picture_Xmin;
if(x1>Picture_Xmax) x1=Picture_Xmax;
if(y1<Picture_Ymin) y1=Picture_Ymin;
if(y1>Picture_Ymax) y1=Picture_Ymax;
LCD12864_Draw_Line(x0,y0,x1,y1);//连接两点
delay_ms(10);
IWDG_ReloadCounter(); //喂狗
}
}
4、测试数据
float zgg[500]={\
0.015,0.017,0.015,0.013,0.015,0.017,0.012,0.010,0.016,0.005,\
0.010,0.008,0.011,0.007,0.010,0.010,0.012,0.009,0.014,0.010,\
0.010,0.011,0.004,0.018,0.010,0.011,0.012,0.017,0.007,0.011,\
0.011,0.011,0.019,0.013,0.015,0.010,0.006,0.015,0.012,0.008,\
0.009,0.014,0.008,0.011,0.011,0.012,0.012,0.011,0.009,0.009,\
0.008,0.011,0.009,0.016,0.013,0.009,0.017,0.011,0.009,0.016,\
0.016,0.018,0.350,1.455,1.846,1.301,0.915,3.114,2.057,2.492,\
2.492,1.810,2.786,2.759,4.029,3.502,4.158,5.301,4.977,4.885,\
5.187,5.089,7.498,6.864,5.561,5.556,5.785,5.099,5.084,6.788,\
6.813,5.818,7.310,6.542,6.588,7.890,8.497,9.550,7.780,8.992,\
7.991,6.775,7.861,6.070,7.436,7.526,7.428,7.831,8.010,9.033,\
7.712,8.285,10.741,7.642,8.600,8.875,7.750,8.004,8.427,8.585,\
7.681,8.896,9.189,9.090,10.693,10.375,9.289,12.039,10.143,9.360,\
9.090,9.897,9.763,8.382,10.486,10.310,8.447,8.390,10.005,8.476,\
7.379,7.649,6.994,5.216,7.270,6.369,5.622,6.346,5.542,6.544,\
6.661,6.028,6.296,6.355,5.758,4.138,4.123,4.447,2.778,3.776,\
4.231,2.986,3.303,2.086,3.364,3.159,2.510,2.722,2.905,3.149,\
2.334,2.342,4.827,2.402,2.498,3.293,2.702,2.400,0.975,2.333,\
2.484,2.320,1.738,1.826,2.401,1.606,1.907,4.055,2.452,2.557,\
3.185,4.060,3.783,2.288,3.517,3.073,3.835,3.548,3.397,3.621,\
3.277,4.059,6.163,4.788,5.490,5.926,4.776,5.832,5.375,5.931,\
5.641,6.116,6.494,5.810,6.589,6.170,5.996,7.143,6.010,7.020,\
6.084,7.609,7.766,6.424,8.962,7.657,7.651,8.441,8.822,8.056,\
8.056,9.544,10.316,8.953,11.235,9.466,9.439,11.076,9.861,10.979,\
11.175,11.397,11.229,11.782,12.640,11.227,11.199,15.345,12.014,13.559,\
12.358,13.194,13.410,13.420,15.032,14.768,15.588,16.330,15.671,16.423,\
16.384,15.650,17.480,19.635,17.554,17.906,18.879,20.133,18.643,19.774,\
19.774,20.218,20.108,21.673,20.306,19.601,17.923,18.702,19.537,17.518,\
17.518,17.309,15.661,17.110,16.761,16.466,18.900,18.883,19.926,19.239,\
19.339,18.480,20.742,17.802,18.132,18.887,17.559,15.768,16.246,17.168,\
16.190,15.683,15.908,16.724,15.345,15.535,17.338,16.310,15.740,19.018,\
19.018,15.470,16.562,16.356,15.073,16.106,15.629,13.992,14.893,13.934,\
13.934,13.543,13.498,15.776,13.819,13.940,13.533,14.011,14.911,12.808,\
14.547,13.968,13.734,12.015,12.165,12.483,10.434,9.705,11.537,9.156,\
10.918,10.678,9.082,9.774,9.481,10.021,8.808,8.753,9.036,9.195,\
7.963,6.926,7.067,8.801,5.732,6.574,6.389,5.748,5.210,4.475,\
3.889,4.680,3.835,3.285,3.593,3.524,1.565,1.988,4.024,0.016,\
0.015,2.159,0.017,0.016,0.123,0.020,0.014,0.016,0.013,0.015,\
0.015,0.013,0.012,0.013,0.006,0.008,0.010,0.011,0.015,0.009,\
0.012,0.009,0.010,0.009,0.012,0.014,0.010,0.013,0.010,0.011,\
0.012,0.008,0.015,0.012,0.009,0.013,0.018,0.007,0.008,0.012,\
0.009,0.005,0.016,0.014,0.015,0.011,0.009,0.016,0.013,0.011,\
0.006,0.011,0.016,0.003,0.008,0.009,0.016,0.001,0.012,0.014,\
0.007,0.018,0.010,0.013,0.005,0.017,0.016,0.014,0.012,0.906,\
1.162,1.221,2.657,1.665,1.798,1.308,1.938,1.553,2.034,2.732,\
3.292,2.987,3.215,5.045,4.450,4.327,5.036,5.694,6.738,5.664,\
5.955,5.343,5.202,5.067,5.883,5.216,5.913,5.819,6.587,6.393,\
6.486,7.045,8.264,7.951,7.846,7.259,7.427,6.607,7.599,6.556,\
6.556,7.572,7.113,5.954,7.276,9.913,7.580,8.802,11.101,8.764,\
8.764,8.794,7.993,7.160,8.247,8.147,7.197,7.849,9.611,8.391\
};
void Test_DrawPicture(void)
{
Calculate_MaxValue_MinValue_ And_APPValue(zgg,500);
DrawPicture(zgg,500);
}
5、测试结果:
若修改Picture_Ymin和Picture_Ypp,则可以放大曲线图。
对于128*64图形点阵LCD,需要注意:Picture_Xmax<128, Picture_Ymax<64