TIM14 PWM output, duty cycle regulated using a potentiometer

If you like it, share it

This example is an evolution of the: STM32 Basic Timer in Interrupt & PWM mode
In this example the duty cycle is regulated using a potentiometer.
See the diagram below.

All is developed using CUBE-MX and are generated for ATOLLIC.
Here there is the project for CUBE-MX, the project is generated for NUCLEO-F030R8.

The result is shown below.


OK, we start this project.

The TIMER configuration is show below.

After you had generate project for Atollic (using the CUBE-MX), is necessary insert, in:
main.c
the blue lines shown at points: 1, 2 and 3
in the correct section (see the green lines below to identify the right sections).


Point n.1

/* USER CODE BEGIN PV */
/* Private variables ———————————————————*/

uint16_t adc_value; // variable for reading the ADC value
TIM_OC_InitTypeDef sConfigOC; // declaration of TIMER structure

/* USER CODE END PV */


Point n.2

/* USER CODE BEGIN 0 */

// Call Back for ADC Interrupt
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
 adc_value = HAL_ADC_GetValue(hadc); // read ADC value

__HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, adc_value); // Configure the new value of the pulse (duty cycle)

HAL_ADC_Start_IT(hadc); .// This is for restart ADC conversion because we used:
 ............. ..........// CONTINUOUS CONVERSION MODE -> DISABLE
................  .......// in CUBEMX configuration tab.
}

/* USER CODE END 0 */

NOTE:

For search the Call Back of a peripheral, open the file of the peripheral that you need to use for example: stm32f0xx_hal_adc.c
and search: __weak
See below.


Point n.3

/* USER CODE BEGIN 2 */

HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1); // Start TIMER n.14

HAL_ADC_Start_IT(&hadc); // Start ADC in Interrupt

/* USER CODE END 2 */


That is all, now compile and debug your program and you must see something like below, by turning the potentiometer.
(The frequency is 8KHz)


How to get the SW for this PWM project

Please send us an email and ask us the password for: TIM14F0NU-PWM-Duty
Please specify also your country and your city, this are only for our personal statistics.

Get the SW clicking here, but remember to ask us the password for open it.