STM32F407 - Servo Motor (SG90) is not work. (PWM output and motor seem to be working properly)
Hello, I am trying to learn bare metal programming and servo motor control with my stm32f407g-disc1 card.
I cannot move the servo motor with stm. I tried with arduino to check if the problem is in the motor and the motor worked. I thought my PWM signal was wrong and I found codes from the internet but it didn't work again.
I am attaching the code and logic analyzer graphs. Can you help me what could be the problem?
void GPIO_Init(void);
void TIM2_Init(void);
void TIM4_ms_Delay(uint32_t delay);
void GPIO_Init(){
RCC->AHB1ENR |= 1; //Enable GPIOA clock
GPIOA->AFR[0] |= 0x00100000; // Select the PA5 pin in alternate function mode
GPIOA->MODER |= 0x00000800; //Set the PA5 pin alternate function
}
void TIM2_Init(){
RCC->APB1ENR |=1;
TIM2->PSC = 16-1; //Setting the clock frequency to 1MHz.
TIM2->ARR = 20000; // Total period of the timer
TIM2->CNT = 0;
TIM2->CCMR1 = 0x0060; //PWM mode for the timer
TIM2->CCER |= 1; //Enable channel 1 as output
TIM2->CCR1 = 500; // Pulse width for PWM
}
void TIM4_ms_Delay(uint32_t delay){
RCC->APB1ENR |= 1<<2; //Start the clock for the timer peripheral
TIM4->PSC = 16000-1; //Setting the clock frequency to 1kHz.
TIM4->ARR = (delay); // Total period of the timer
TIM4->CNT = 0;
TIM4->CR1 |= 1; //Start the Timer
while(!(TIM4->SR & TIM_SR_UIF)){} //Polling the update interrupt flag
TIM4->SR &= ~(0x0001); //Reset the update interrupt flag
}
int main(){
RCC->CFGR |= 0<<10; // set APB1 = 16 MHz
GPIO_Init();
TIM2_Init();
TIM2->CR1 |= 1;
while(1){
if(TIM2->CCR1 < 2500){
TIM2->CCR1 = TIM2->CCR1 + 50;
TIM4_ms_Delay(50);
}
else{
TIM2->CCR1 = 500;
TIM4_ms_Delay(50);
}
}
}
Edit: I tried with HAL library but nothings change. Is it possible my STM is broken?
For HAL library, I follow this videos instruction: https://www.youtube.com/watch?v=owbuhblItBA
