Introducing to STM32 ADC programming. Part2

After we had a quick overview of the STM32 ADC peripheral, we can dig deeper into specifics. To understand simple things, let’s go with the simplest case – single conversion mode. In this mode, ADC does one conversion and then stops. After the ADC conversion result is stored into the 16-bit ADC_DR data register (remember that the conversion result is 12-bit), then the End of Conversion (EOC) flag is set. An interrupt is generated if the EOCIE flag is set. The same situation is if the injected channel is converted. The difference is that the result is stored in the corresponding ADC_DRJx register, the JEOC flag is set, an interrupt is generated if the JEOCIE flag is set. In our example, we will measure the internal temperature sensor value and send it using USART. A temperature sensor is internally connected to the ADC1_IN16 channel. The algorithm will start a single conversion and wait for the conversion complete flag EOC. We will then read the ADC value from the ADC_DR register, which will later be used to calculate Celsius’s temperature value…

Continue reading

Programming STM32 USART using GCC tools. Part 2

In the last part of the tutorial, we have covered simple USART routines that send data directly to USART peripheral. This is OK to use such an approach when a project isn’t time-critical and processing resources are far from limits. But most often, we stuck with these limiting factors, mainly when RTOS is used or when we perform necessary real-time data processing. And having USART routines with while the loop-based wait isn’t a good idea – it steals processing power only to send a data. As you may guess – next step is to employ interrupts. As you can see, there are many sources to trigger interrupts, and each of them is used for a different purpose. To use one or another interrupt, first, it has to be enabled in USART control register (USART_CR1, USART_CR2, or USART_CR3). Then NVIC USART_IRQn channel has to be enabled to map interrupt to its service routine. Because NVIC has only one vector for all USART interrupt triggers, service routine has to figure out which of interrupts has triggered an event. This is done by…

Continue reading