cancel
Showing results for 
Search instead for 
Did you mean: 

Displaying the value of a temperature on the LCD

idrissmc
Associate III

Hello everyone,

so what I’m going to do is displaying the value of the temperature on the LCD of the STM32F413H-Disco with a PT100, i wrote this code:

int value;

char getvalue[20];

...

MX_ADC_Init();

HAL_ADC_Start_IT(&hadc1); (I choose Interruption because in reality i started with a simple potentiometer so I think that if I turn it faster I have to see the right value, I don’t want that the program run all the code and then display the value, that’s why I used the interruption )

HAL_ADC_PollForConversion(&hadc1, 100);

value = HAL_ADC_GetValue(&hadc1);

itoa(value, getvalue, 10);

..

GUI_Clear();

GUI_SetFont(....);

GUI_DisplayStringAt(...);

I declared the MX_ADC_Init function

but it displays 0 on the LCD.

Clive Two.Zero can you help me to resolve this please?

thanks

1 ACCEPTED SOLUTION

Accepted Solutions

I don't use CubeIDE (or the predecessor Atollic), so I can't tell you exactly how. Check the CubeIDE quick start and debugging guides on the CubeIDE web page (search the docs for "SWV"). In your code, you need to call ITM_SendChar() to output each character to the debugger console.

View solution in original post

10 REPLIES 10

Guess you need to make a decision about polling or interrupting, and then use one consistent method. The HAL examples might provide something useful, I don't use CubeMX

Is the problem with the value you are reading, or how it displays?

If you set value=1234 do you get "1234" on the screen? Might help you focus on where the problem is.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Yes yes Clive Two.Zero, I tried the basic method so I declared an int =12 then declared a char and it displays “12�?, but when I use the ADCs function it’s shows 0 all time, and when I put the potentiometer to the critic value (0K) the LCD Goes out( here I think I’ve done a short circuit is this right?)

Bob S
Principal

As @Community member​ said - you are mixing interrupt and polling functions. I suggest changing HAL_ADC_Start_IT() to HAL_ADC_Start(). Then you can use your existing HAL_ADC_PollForConversion() call to determine when the conversion is done. BUT.... please check the return status from HAL_ADC_PollForConversion(), and for that matter, HAL_ADC_Start() as well.

Then, you should be able to see if you are getting expected data from the ADC. If not, then you need to debug either your hardware or MC_ADC_Init() code. See the examples in your Cube library directory tree (STM32Cube_FW_XXXX\Projects). There may not be any ADC examples for your exact board, but there should be some for another DISCO or Nucleo board.

Ok @Bob S​  but how can I check the status from HAL_ADC_PollForConvertion()? According to the datasheet of HAL & LL Drivers it returns a status but how can I do it?

and what’s MC_ADC_Init().. :( I know only HAL_ADC_...() or MX_ADC_Init()

Sorry, MC_ADC_Init() was a typo on my part - should have been MX_ADC_Init().

As for checking the return value - The HAL functions return a status, such as HAL_OK, HAL_ERROR, HAL_TIMEOUT, etc. Just like HAL_ADC_GetValue() returns the ADC reading. Test that return value to see if it is HAL_OK. If not, you got some kind of error.

@Bob S​ with this code I have a problem, it said undefined statement (Error_Handler())

  1. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  2. {
  3. Error_Handler();
  4. }

Do you have a function called Error_Handler() defined somewhere? The CubeMX/HAL code provides a function called _Error_Handler (note the leading underscore), but unless you have added code to it, that function is useless.

You would be better off storing the return value in a local variable so that you can print it so some debug device (UART, LCD, something) and/or look at it in the debugger. So you code would be something like this:

HAL_StatusTypeDef sts;
 
sts = HAL_ADC_Start(&hadc1);
if ( sts == HAL_OK ) {
  sts = HAL_ADC_PollForConversion(&hadc1, 100);
  if ( sts == HAL_OK ) {
    value = HAL_ADC_GetValue(&hadc1);
    itoa(value, getvalue, 10);
  }
  else { // Error from "poll for conversion"
    // This may be HAL_TIMEOUT, or some other error
    // Generate some error message here
  }
}
else {  // Error from HAL_ADC_Start()
  // Display an error message somehow
}

Of course, compiler optimization may make the "sts" variable disappear from the debugger's standpoint (i.e. "gets optimized out").

EDIT: And if you don't get any errors from the HAL functions, then you need to go back and look at the actual ADC and GPIO configuration registers to make sure they are configured as you intend.

That’s great @Bob S​ ! Is getvalue a char*?

I have one more question please! I want to print on the CubeIDE a message like we do with C/C++, python, Java... I mean on the console! How can I do it with CubeIDE

I don't use CubeIDE (or the predecessor Atollic), so I can't tell you exactly how. Check the CubeIDE quick start and debugging guides on the CubeIDE web page (search the docs for "SWV"). In your code, you need to call ITM_SendChar() to output each character to the debugger console.