STM32 Development with CubeMX and Eclipse

Complex Programs

In the next step, you see a more complex example with A/D conversion and output on the serial interface. The STM32F0-UART-Tutorial [10] provides help here in controlling the serial interface and also guidance for STM32-ADC [11]. Information on using the A/D converter can be found in the visual GDB tutorial for UART [12].

The first thing to do is build a bridge between the HAL functions for controlling the serial interface, here in simple polling mode, and the C standard library. You will also need to implement the _read and _write functions for the input and output of characters. This should be done according to the Visual GDB tutorials for A/D.

Add the source text from Listing 8 to the relevant comment area. Afterwards, save the actual program according to the steps in Listing 9. It asks for an A/D conversion value by means of simple polling. It then outputs the value, together with the throughput number, as a conversion to the voltage value.

Listing 8

HAL_UART Fragment

.
.
.
/* USER CODE BEGIN PFP */
/* Private function prototypes -------------------------------*/
int _write(int file, char *ptr, int len) {
  if(HAL_UART_Transmit(&huart1,(uint8_t *) ptr,len,1000)==HAL_OK) {
  return len;
  }
  else {
  return -1;
  }
}
int _read(int file, char *ptr, int len) {
  if(HAL_UART_Receive_IT(&huart1,(uint8_t *) ptr,len)==HAL_OK) {
  return len;
  }
  else {
  return -1;
  }
}
/* USER CODE END PFP */
.
.
.

Listing 9

HAL_ADC Fragment

.
.
.
/* USER CODE BEGIN 3 */
unsigned int counter=0;
const unsigned int Nbit=12;
unsigned int quantIndex;
unsigned int voltageInMilliVolt;
if(HAL_ADC_Start(&hadc1) != HAL_OK) {
  puts("ADC: Initialisation error!");
  while(1) {}
}
while (1) {
  if(HAL_ADC_PollForConversion(&hadc1,100000) != HAL_OK) {
  puts("ADC: conversion error!\r\n");
  }
  else {
  quantIndex=HAL_ADC_GetValue(&hadc1);
  voltageinMilliVolt=(quantIndex*3300) >> Nbit;
  printf("Throughput %u: Index %u -> %d.%03d V\r\n", ++counter, quantIndex, voltageInMilliVolt / 1000, voltageInMilliVolt % 1000);
  }
}
/* USER CODE END 3 */
.
.
.

The serial interface must be connected at least to PA9 with grounding and the RxD connection for testing purposes. Start the Terminal program and set the baud rate you configured. If you want to vary the input voltage of the A/D converter, you could for example, use the middle tap to connect a Potentiometer with PA1. Make sure that it only connects to 3.3 V and ground.

Conclusion

This article provides a quick introduction to the fascinating world of the STM32 micro controller. If your interest has been piqued, you can learn more by going through the examples and documentation we've listed.

Buy this article as PDF

Express-Checkout as PDF

Pages: 7

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content