The assigning of values to the elements of the array at the time of its declaration is called initializing of the array. For example, to declare an array “data” of type double with 5 elements, with value 44.3, 88.7, 22.2, 11.9 and 66.3 in elements data0, data1, data2, data3, data4 respectively, the statement is written as. ST online courses are free of charge, your only investment is the applicable STM32 Discovery kit from your local distributor and some USB cables. ST MCUs e-learning process. We have divided our STM32 MOOCs into five steps with different levels of expertize and focus.

  • The second declaration, extern IO uint8t.data; doesn't declare an array at all, but rather a pointer. While arrays and pointers are largely interchangeable in the arguments of functions (array arguments are implicitly turned into pointers to their first elements), this does not hold elsewhere, such as in this example.
  • Fig.1 UART Testing on STM32. STM32 microcontrollers are among the most widely adopted microcontrollers in the domain of embedded systems. They are power-efficient and have a small package size.

Previously we have tried to do a single conversion of one ADC channel. We were waiting for the ADC result in a loop, which isn’t an effective way of using processor resources. It is better to trigger a conversion and wait for the conversion to complete the interrupt. This way, a processor can do other tasks rather than wait for ADC conversion to complete. This time we will go through another example to set up more than one channel and read ADC values using interrupt service routine.

How does multichannel ADC conversion works?

If we need to convert several channels continuously, we need to set up Sequence registers (ADC_SQRx). There are three sequence registers: ADC_SQR1, ADC_SQR2, and ADC_SQR3 where we can set up a maximum of 16 channels in any order. Conversion sequence starts with SQ1[4:0] settings in ADC_SQR3 register. Bits [4:0] hold the number of ADC channels.

All 16 sequence channels can be set up the same way through all SQR registers. Then in the ADC_SQR1 register, there are four bits marked L[3:0] where you can set the number how many times sequence reading will be repeated.

Stm32 Array Declaration Tool

Another thing we will have to take care of is to set up sample time for each channel. As we know, each channel in sequence can be set for different conversion times. The sampling time for each channel can be set up in two registers: ADC_SMPR1 and ADC_AMPR2. There are three bits for each channel in sequence.

If you use a standard peripheral library setting up multichannel ADC becomes an easy task.

Setting up multichannel ADC conversion with DMA write

Let’s write an example where we will read the first 8 ADC channels four times using scan mode. Then we calculate an average value of each channel and later print results on a terminal screen using UART.

Stm32 Array Declaration Codes

We will write ADC values to memory by using a DMA channel. Once all data is stored in memory, a DMA transfer complete interrupt will be generated to trigger averaging and output. In the STM32F100x datasheet, we find that ADC pins are assigned alternate functions as follows:

  • ADC1_IN0 – PA0
  • ADC1_IN1 – PA1
  • ADC1_IN2 – PA2
  • ADC1_IN3 – PA3
  • ADC1_IN4 – PA4
  • ADC1_IN5 – PA5
  • ADC1_IN6 – PA6
  • ADC1_IN7 – PA7
  • ADC1_IN8 – PB0
  • ADC1_IN9 – PB1
  • ADC1_IN10 – PC0
  • ADC1_IN11 – PC1
  • ADC1_IN12 – PC2
  • ADC1_IN13 – PC3
  • ADC1_IN14 – PC4
  • ADC1_IN15 – PC5

We will need to set up pins A0 to A7 as analog inputs for the first eight channels. Then we can set up an ADC conversion mode. Also, we need to set up Scan Conversion Mode to go through all channels selected in ADC1_SQRx registers. In the peripheral library, this looks like:

Then we must enable to enable continuous conversion mode as we want to cycle through channel list several times:

Then we indicate the number of channels to be converted in scan mode:

The next thing is to indicate which channels and what order we need to convert. For this, we set up each channel individually with commands:

Stm32 Array Declaration

I’ve chosen to go all eight channels in the row from 0 to 7. But you can mess up the numbers as you like. The rest is to set up DMA where it copies ADC values to memory on each EOC event. After DMA copies a predefined number of values, it generates an interrupt. Then we can manipulate data as we like. As in our example, we average multiple instances.

This is a result on the terminal screen.

You can hook up a potentiometer or any other analog sensor to each channel to see its ADC value.

Working C code of multichannel ADC

Here is the complete main source code if you would like to analyze or use fragments for your purposes:

Also, you can download project files [STM32DiscoveryADC_DMAmultiple.zip] that compile with Codebench GCC and Eclipse.