如何在C中缩放数字

如何在C中缩放数字

本文介绍了如何在C中缩放数字/数字范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在我的LCD模块上以英尺为单位显示海拔高度,范围从0英尺到20000英尺.这些数字是从电位计读取的,我可以旋转它来更改值.目前,电位器的显示范围是0到1023,但我需要适当地缩放这些值,以使它们的读数从0-20000ft.除此之外,我还希望下面的线显示一个条形图"来表示海拔高度的增加. LCD每行有20个块,因此条形图的范围从1个块到20个块.

I want to be able to display the altitude in ft ranging from 0ft to 20000ft on my LCD module. The numbers are read from a potentiometer that I can rotate to change the values. At the moment the potentiometer displays from 0 to 1023 but I need to scale these appropriately so they read from 0-20000ft. As well as this I want the line below to display a 'bar graph' to represent the altitude increasing. The LCD has 20 blocks per line so the bar graph can range from 1 block to 20 blocks.

   sprintf(buf, "Altitude: %d    ", DELVAL2);    // display altitude level
   lcd_putxy(1,0,buf);
   for (delay = 0; delay < 50000; delay++);      // introduce a delay

   sprintf(buf, "*", DELVAL2 );                  // display bar graph
   lcd_putxy(2,0,buf);
   for (delay = 0; delay < 50000; delay++);      // introduce a delay

到目前为止,这是我的代码.它从电位计DELVAL2读取值并将其显示在LCD上.有人可以解释一下我如何适当缩放数据以生成高度和条形图的方法.

This is my code so far. It reads the value from the potentiometer DELVAL2 and displays it on the LCD. Can someone please explain a method of how I can scale the data appropriately to produce an altitude and bar graph.

推荐答案

如果您的DELVAL2在0-1023范围内,则可以将其缩放为0-20000,但分辨率不能超过(1/1024)* 20000.为什么?您可以读取的最小值(单个位)为1/1024.您的最大值为20000,因此对DELVAL2进行1位更改将导致20000/1024 = 19,53对比例值进行更改.

If your DELVAL2 is in range 0-1023 you can scale it to 0-20000, but you cannot get bigger resolution than (1/1024) * 20000. Why? Minimum value (single bit) you can read is 1/1024. Your maximum value is 20000, so one bit change on DELVAL2 will result in 20000/1024 = 19,53 change on scaled value.

您可以计算出如何缩放它,它已经在stackoverflow上进行了描述:如何缩小具有已知最大值和最小值的数字范围

You can calculate how to scale it, it's already described on stackoverflow:How to scale down a range of numbers with a known min and max value

您必须记住,您可能会陷入浮点运算,这是您可能要避免的事情.例如,您可以执行以下操作

You must keep in mind that you may fall into floating point operation and this is something that you may want to avoid. For example, you can do something like this

scaled = (DELVAL2 * 1953) / 1000;

代替

scaled = DELVAL2 * 19.53;

请记住,您可以在此计算中获得的最大值为1024 * 1953 = 1999872,因此您需要32位变量.可能需要额外的转换,具体取决于您的体系结构和编译器,例如

Keep in mind that maximum value that you can get inside this computation will be 1024*1953 = 1999872, so you need 32 bit variable. Additional cast may be needed, depending on your architecture and compiler, eg

scaled = (DELVAL2 * (uint32_t)1953) / 1000;

关于第二个问题-条形图-您情况良好.计算您需要多少个符号并绘制它们.它是按比例缩小而不是按比例缩小.简单的划分就足够了.当您知道需要多少个符号时,只需简单的循环即可生成它们

About second question - bar graph - you are on good way. Calculate how many symbols do you need and draw them. It's scaling down instead of up. Simple division should be enough. When you know how many symbols you need, generate them in simple loop

for(int i = 0; i < num; i++)
    buf[i] = '*';
buf[i] = 0; //last symbol is 0 to stop drawing
lcd_putxy(2,0,buf);

这篇关于如何在C中缩放数字/数字范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:21