Introduction: STM32F4 Discovery Part2 - ADC Graph

Hello !

In this part I want to present using ADC with stm32 Discovery board with LCD.

How to start with stm32f4 Discovery you can see in part 1: https://www.instructables.com/id/STM32F4-Discovery-part1-Touch-Buttons/

Step 1: ADC

The ADC implementation is very simple with HAL Cube (mentioned in part 1).

I'm using 12 - bit ADC module

  • ADC prescaler = 8
  • Number of conversion = 1
  • ADC resolution = 128
  • Sample time is 112 cycles.

We also can use ADC with DMA. IRQ Handler for it is HAL_DMA_IRQHandler(AdcHandle.DMA_Handle) where AdcHandle is the ADC_HandleTypeDef type.

To display the ADC value we have to use extern value of uint32t uhADCxConvertedValue. It shpws value from 0 to 4096 (2^12) so if we want to show voltage from 0 - 3V we have to do simple calculation uhADCxConvertedValue * 30 / 4095;

Step 2: Edit Widget

On 500 page of segger's documentation there is explain how to use EDIT widget.

To create it we have to it do similary as we did in part 1 to create buttons (1). To set EDIT widget parameters do it in widget callback function in WM_INIT_DIALOG case(2). We also have to create function to attach ADC value witch edit. I'll show it (3) below.

/**** 1 **************************************************************************************************************/

#define ID_W0_EDIT0 (GUI_ID_USER + 3)

static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] =
{

{ EDIT_CreateIndirect , 0 , ID_W0_EDIT0 , 178, 15, 40, 20 },

};

/**** 2 **************************************************************************************************************/

hItem = WM_GetDialogItem(hDlg, ID_W0_EDIT0);
EDIT_SetHexMode(hItem, 0x1234, 0, 0xffffff); // set your own parameters

/**** 3 **************************************************************************************************************/

static void _ShowADCval(void)
{ WM_HWIN hItem;

int adc_val = uhADCxConvertedValue * 30 / 4095; hItem = WM_GetDialogItem(oscWindow, ID_W0_EDIT0); EDIT_SetDecMode(hItem, adc_val, 0, 300, 1, GUI_EDIT_NORMAL); }

Step 3: ADC Graph

This graph shows ADC value level from PA0 pin. In the first picture I connected PA0 with 3V and GND to PA0 in the second picture. On the graph we can see those values in real time. Also I placed Edit widget in top right corner. As you can see the value there is changing with the graph.

Function to create the graph is similar to EDIT widget creating.

Creating all framewin, edit and graph:

#define ID_W0 (GUI_ID_USER + 1)
#define ID_W0_GRAPH0 (GUI_ID_USER + 2) #define ID_W0_EDIT0 (GUI_ID_USER + 3) // // Dialog resource // static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = { { FRAMEWIN_CreateIndirect, "Osciloscope" , ID_W0 , 0, 0, 240, 320, 0 }, { GRAPH_CreateIndirect , 0 , ID_W0_GRAPH0 , 5, 5, 225, 230 }, { EDIT_CreateIndirect , 0 , ID_W0_EDIT0 , 178, 15, 40, 20 }, };