Smooth switching on of the LED on the STM32F103

Hello everyone. I need your help. You need to develop a program to smoothly turn on and off the LED using two buttons and the PWM timer TIM1 on the STM32F103. How to do this is not clear.

Author: kroow, 2019-11-14

2 answers

Timer Settings:

void TIM_initial (void) //Аппаратный таймер PWM
    {
    RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
    RCC-> APB2ENR |= RCC_APB2ENR_IOPAEN;

    //Выход PWM **********************
    GPIO_INIT_PIN(GPIOA, 1,     GPIO_MODE_OUTPUT10_ALT_PUSH_PULL); // Tim2_ch2 Яркость LED

    TIM2->PSC=72-1;  //18-1;  //Предделитель
    TIM2->ARR = 100-1; //Делитель, определяет период выходного сигнала (с учетом предделителя будет 10/40kHz)

    TIM2->CR1 |= TIM_CR1_ARPE;//Включен режим предварительной записи регистра автоперезагрузки
    TIM2->CCMR1 |= TIM_CCMR1_OC2PE;//Включен режим предварительной загрузки регистра сравнения  

    TIM2->CCMR1 |= (TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1);//OC2M = 110 - PWM mode 1

    TIM2->CCR2 = 50; //Длительность импульса (по отношению к arr). В данном случае Duty cycle = 50%

    TIM2->CCER |= TIM_CCER_CC2E;    //Выход канала захвата/сравнения включен
    TIM2->CCER |= TIM_CCER_CC1P;        //Полярность выходного сигнала
    TIM2->CR1 |= TIM_CR1_CEN;               //Старт счета таймера
    }

The brightness is adjusted by changing the value of the TIM2->CCR2 register.

 1
Author: Storozhev DJ, 2019-11-27 06:00:45

The brightness of the LED in this case depends on the PWM fill factor. That is, when the LED is turned on and off frequently, the LED does not turn off completely, but fades out a little. That is, your program should gradually increase the fill factor for a smooth turn - on of the LED, and gradually decrease it for a smooth turn-off.

 0
Author: maestro, 2019-11-19 10:50:33