2023-02-10 01:22 AM
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
Solved! Go to Solution.
2023-02-11 09:11 AM
Okay, after researching the problems for two days, I bought a new servo motor of the same model. And code is working with new motor. I still have no idea why the old motor does not work with stm32.
I didn't see anything about closing the topic, but if any mod sees this message, you can close the topic.
2023-02-10 06:13 AM
what about the logic level?
Is your arduino 5v and your stm 3v3?
2023-02-10 06:33 AM
Yes, arduino gives 5v and stm32 gives 3v3 output voltage.That sounds like it could be a problem, but I didn't see anyone except me using the same card and the same motor and having problems. Am I missing something?
2023-02-11 09:11 AM
Okay, after researching the problems for two days, I bought a new servo motor of the same model. And code is working with new motor. I still have no idea why the old motor does not work with stm32.
I didn't see anything about closing the topic, but if any mod sees this message, you can close the topic.