简体   繁体   English

使用STM32在LCD1602中显示ADC值

[英]Displaying ADC values in LCD1602 using STM32

I'm working on a project that calls for displaying ADC values on an LCD screen.我正在做一个需要在 LCD 屏幕上显示 ADC 值的项目。 I don't know much about coding because I'm new to STM32IDE.我对编码不太了解,因为我是 STM32IDE 的新手。 In essence, I'm utilising a (0–25 v) voltage sensor and an STM32F103C8T6 (blue pill).本质上,我使用的是 (0–25 v) 电压传感器和 STM32F103C8T6(蓝色药丸)。 I have to use the LCD to display values.我必须使用 LCD 来显示值。 Does anyone have any insight into this or know how to assist me?有没有人对此有任何见解或知道如何帮助我?

  HAL_ADC_Start(&hadc1);
  HD44780_Init(2);
    HD44780_Clear();
    HD44780_SetCursor(0,0);
    HD44780_PrintStr("Vol = ");


 while (1)
  {
      HAL_ADC_PollForConversion(&hadc1,1000);
          readValue = HAL_ADC_GetValue(&hadc1);
          voltage =(float)readValue/4095*16.5;
          HAL_Delay(100);
    
  }

Given that HD44780_PrintStr() prints a string, you have to generate a string representation of the float value voltage .鉴于HD44780_PrintStr()打印一个字符串,您必须生成一个表示floatvoltage的字符串。

Then you need to set the cursor to the end of the prefix string (column 6), and print the string representation.然后您需要将 cursor 设置为前缀字符串的末尾(第 6 列),并打印字符串表示。 Trailing spaces at the end of the output will erase digits from any previous longer output. output 末尾的尾随空格将删除以前更长的 output 中的数字。

Assuming the parameter order of HD44780_SetCursor() are row, column :假设HD44780_SetCursor()的参数顺序为row, column

for(;;)
{
    HAL_ADC_PollForConversion(&hadc1,1000);
    readValue = HAL_ADC_GetValue(&hadc1);
    voltage =(float)readValue/4095*16.5 ;

    char vstr[16] ;
    sprintf( vstr, "%.2f    ", voltage ) ;
    HD44780_SetCursor(0,6);
    HD44780_PrintStr( vstr ) ;

    HAL_Delay(100);
}

If you want leading zeros (fixed 2-digit width) in the integer part;如果您想要 integer 部分中的前导零(固定的 2 位宽度); that is a little more complex:这有点复杂:

unsigned wholev = (int)voltage ;
unsigned fractv = (int)((voltage - wholev) * 100.0f + 0.5f) ;

char vstr[16] ;
sprintf( vstr, "%02u.%02u", wholev, fractv ) ;
HD44780_SetCursor(0,6);
HD44780_PrintStr( vstr ) ;

but in that case you do not need the trailing spaces to delete previous digits, because the string is always a fixed width "xx.yy" .但在这种情况下,您不需要尾随空格来删除以前的数字,因为字符串始终是固定宽度"xx.yy" The code may also end up smaller if floating point support can be omitted from the sprintf formatter.如果可以从sprintf格式化程序中省略浮点支持,那么代码也可能会变得更小。

Apart from the floating-point formatter adding a great deal of code and probably increase in stack usage, remember that your MCU lacks hardware floating point support so that the floating point operations will both add code and be rather slow.除了浮点格式化程序添加大量代码并可能增加堆栈使用量外,请记住您的 MCU 缺乏硬件浮点支持,因此浮点操作既会添加代码又会相当慢。 In simple applications it may not matter, but generally you should avoid (and know how to avoid) floating point operations in embedded code, especially if there is no FPU - even if there is an FPU there are reasons to avoid it for trivial uses such as this in any case.在简单的应用程序中这可能无关紧要,但通常您应该避免(并且知道如何避免)嵌入式代码中的浮点操作,尤其是在没有 FPU 的情况下 - 即使有 FPU,也有理由避免它用于琐碎的用途,例如在任何情况下都是如此。

The floating-point code can be removed simply as follows:浮点代码可以简单地删除,如下所示:

unsigned millivolts = readValue * 16500u / 4095u ;
unsigned wholev = millivolts / 1000u  ;
unsigned fractv = ((millivolts + 5u) % 1000) / 10u ;

sprintf( vstr, "%02u.%02u", wholev, fractv ) ;
HD44780_SetCursor(0,6);
HD44780_PrintStr( vstr ) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM