AnsweredAssumed Answered

STM32F103C8T6 PWM Generation

Question asked by Domen Visnar on Feb 2, 2018
Latest reply on Feb 14, 2018 by crazy crazy

Hello! I have a question: How to generate a PWM on STM32F103C8T6 in Keil uVision 5. I watched some tutorials but I couldn't make it work, I want to enable TIM1 on pin PA11 and PA12. PWM needs to run at 20kHz => period = 3600.

It will be used for motor control on a robot. This is what I managed to do:

 

#include "stm32f10x.h"
int main(void){

RCC-> APB2ENR |= RCC_APB2ENR_TIM1EN; 
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
GPIOA->ODR |= GPIO_ODR_ODR11;
GPIOA->CRH |= GPIO_CRH_MODE11_1;
GPIOA->CRH &= ~(GPIO_CRH_MODE11_0);

TIM1->PSC = 0;
TIM1->ARR = 3600;
TIM1->CCR4 = 1800;
TIM1->CCMR2 |= TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4PE; 
TIM1->CCER |= TIM_CCER_CC4E; 
TIM1->BDTR |= TIM_BDTR_MOE;
TIM1->CR1 |= TIM_CR1_CEN; 
TIM1->EGR |= TIM_EGR_UG;
while(1)
{

//User code
}
}

I watched this tutorials:

43. How to Use or Create a PWM (Pulse Width Modulation) Signal Part 1 - STM32 ARM Microcontroller - YouTube 

44. How to Use or Create a PWM (Pulse Width Modulation) Signal Part 2 - STM32 ARM Microcontroller - YouTube 

45. How to: Edge Aligned PWM (Pulse Width Modulation) Signal Part 3 - STM32 ARM Microcontroller - YouTube 

 

Maybe if you have some more tutorials on PWM and encoder readings with timers I would be very glad!

 

Thank you in advance!

Any help would be appreciated!

Outcomes