AnsweredAssumed Answered

STM32F100RBT6B using timer TIM2

Question asked by pereguda.anton on Sep 19, 2016
Latest reply on Sep 20, 2016 by FTITI.Walid
I have code that rotates small motor.

#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_usart.h"
 
void Delay(unsigned int t) {
    unsigned int i;
    for (i = 0; i < t; i++)
        ;
}
 
void RotateToLeft() {
    const int time = 20000;
    uint16_t steps[8];
    steps[0] = GPIO_Pin_5;
    steps[1] = GPIO_Pin_5 | GPIO_Pin_3;
    steps[2] = GPIO_Pin_3;
    steps[3] = GPIO_Pin_3 | GPIO_Pin_7;
    steps[4] = GPIO_Pin_7;
    steps[5] = GPIO_Pin_7 | GPIO_Pin_1;
    steps[6] = GPIO_Pin_1;
    steps[7] = GPIO_Pin_1 | GPIO_Pin_5;
     
    for (int i = 0; i < 8; i++) {
        GPIO_Write(GPIOC, steps[i]);
        Delay(time);
    }
}
 
 
int main(void) {
 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    GPIO_InitTypeDef gpio_port;
    gpio_port.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_5 | GPIO_Pin_7;
    gpio_port.GPIO_Mode = GPIO_Mode_Out_PP;
    gpio_port.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &gpio_port);
 
    while (1) //Бесконечный цикл в нем мы проверяем ...
    {
        RotateToLeft();
    }
}
I want to rewrite this code using TIM2 times. I found example of PWM modulation that using 3 pins.

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
    GPIO_InitTypeDef PORT;
    PORT.GPIO_Pin = (GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);
    PORT.GPIO_Mode = GPIO_Mode_AF_PP;
    PORT.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOA, &PORT);
    TIM2->CCER |= (TIM_CCER_CC2E|TIM_CCER_CC3E|TIM_CCER_CC4E);
    TIM2->CCMR1|=(TIM_CCMR1_OC2M_0| TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2M_2);
    TIM2->CCMR2|=(TIM_CCMR2_OC3M_0 | TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC4M_0 | TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4M_2);
    TIM2->CR1 |= TIM_CR1_CEN;
But in my case I need to generate PWM on 4 pins. But how to do that?

Outcomes