0

I'm working on a project that calls for displaying ADC values on an LCD screen. I don't know much about coding because I'm new to STM32IDE. In essence, I'm utilising a (0–25 v) voltage sensor and an STM32F103C8T6 (blue pill). I have to use the LCD to display values. 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);
    
  }
4
  • Well, does the display show 'Vol = '? Commented Aug 20, 2022 at 9:54
  • Yes, but I want to display real time voltage values (voltage =(float)readValue/4095*16.5), I don't know what command will do it. It would be of great help if you could help me with the command or code. Thanks Commented Aug 20, 2022 at 11:30
  • Your voltage calculation will generate a value from 0 to 16.5 yet you say it is a 25V sensor.
    – Clifford
    Commented Aug 20, 2022 at 12:16
  • Style tip - give your variables the smallest possible scope. If readValue and voltage are only used in the while-loop, declare them there. e.g. uint32_t readValue = HAL_ADC_GetValue(&hadc1); and float voltage = readValue / 4095.0f * 16.5f ;. Please tell me they are not global?
    – Clifford
    Commented Aug 20, 2022 at 12:56

1 Answer 1

0

Given that HD44780_PrintStr() prints a string, you have to generate a string representation of the float value voltage.

Then you need to set the cursor to the end of the prefix string (column 6), and print the string representation. Trailing spaces at the end of the output will erase digits from any previous longer output.

Assuming the parameter order of HD44780_SetCursor() are 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; 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". The code may also end up smaller if floating point support can be omitted from the sprintf formatter.

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. 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.

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 ) 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.