Controlling brushless dc motor with using stm32f407 board


By the way, I am so sorry about my english if I couldn't explain properly what I am doing .I am making a project that controls brusless dc motor with stm32f407 development board.

First, I controlled brushless dc motor with arduino and observed pwm output pin on the oscilloscope screen. I recognized that brushless dc motor doesn't work by giving %100 power. I understand that I need to make soft start by widening duty cycle. Arduino sends a pwm signal that has 50 Hz frequency(20ms period) and duty cycle in between minimum %10, maximum %12.5. I wrote a code on stm that controls brushless dc motor by using pwm on GPIOD12 pin. I set timer7 configurations as generate an interrupts every 1us and increment a counter in TIM7_IRQHandler() function. When counter reaches 6410 number,it is resetted. I defined Duty variable that has 100 value and incremented by 1 every 1 us till it reaches 240 value.I n infinite while loop, when counter is smaller 3*Duty variable, GPIOD12 pin is set. In between 3*duty variable and 6410 value, GPIO12 pin is in reset. When duty variable increases, pulse width is increases .I am trying to make a soft start with using this way. But I can't control dc motor with using this way. Can you tell me what am I doing wrong?

#include "stm32f4xx.h"

void Timer7pwmGeneratorInit(void); //Timer7pwmGeneratorInit prototype
void SystemInitt(void); //SystemInitt prototype

int  Duty = 100; //Duty variable that represents duty cycle.
int i;       //Counter variable
long counter=0;  //counter variable

int main() {
Timer7pwmGeneratorInit(); //Timer7pwmGeneratorInit is called in main/                                                                                     SystemInitt();        //SystemInitt is called in main

while(1) { 

if(counter < ( 3 * Duty)){ 
GPIOD->ODR |= (0x1000); } //When counter is smaller than 3*Dutyvariable,set GPIOD12 pin.

else if( counter > ( 3 * Duty) && counter < 6410) 

{ GPIOD->ODR &= ~(0x1000); //When counter is in between 3*Duty and 6410 value,counter is resetted. }



void Timer7pwmGeneratorInit(void){

RCC->APB1ENR|=0x00000020;       // Timer7 clock is activated(84 Mhz)
TIM7->CR1=0x0080;               // Automatic Reload

/*********Timer 7  frequency --> fCK_PSC / (Loaded Value + 1) 84E6 / (42) = 2000 KHz**************/

TIM7->PSC =41;   // Prescaler value is 41, Counting frequency = fCK_PSC /    (Loaded Value + 1) 84E6 / (42) = 2000 KHz
TIM7->ARR =1;                   // When counter is equals to 1,returns.    Timer interrupt is generated every 1 uS
TIM7->DIER=0x0001;              // Update Int enable
NVIC->ISER[1] = 0X00800000;     // NVIC de Timer 7 interrupta is enabled
TIM7->CR1 |= 0x0001;            // Counter Enabled


void TIM7_IRQHandler(){
TIM7->SR=0;  //Timer 7 status register is resetted
counter++;  //counter is incremented by 1
if(counter>=6410) //When counter is equals to 6410,counter is                 resetted.
if(Duty < 240) {  duty is smaller than 240, increase duty by 1.
Duty = Duty + 1; //increase duty by 1.}


void SystemInitt(void){
unsigned int i;

for (i=0;i<0x00100000;i++);      
RCC->CFGR |= 0x00009400;        // AHB ve APB speeds are setted max value
RCC->CR |= 0x00010000;          // HSE Xtal osc start to work       
while (!(RCC->CR & 0x00020000));// Xtal osc get stabilized
RCC->PLLCFGR = 0x07402A04;      // PLL coefficients M = 4, N = 168, P = 2 and Q = 7   168 Mhz 
RCC->CR |= 0x01000000;          // PLL starts  (Rehber Sayfa 95)
while(!(RCC->CR & 0x02000000)); // Wait until PLL is ready
FLASH->ACR = 0x00000605;        // 5 Wait state was selected for Flash ROM and ART is activated. (Rehber Sayfa 55)
RCC->CFGR |= 0x00000002;        // System Clk feed through PLL
while ((RCC->CFGR & 0x0000000F) != 0x0000000A); // Wait till feeds
RCC->AHB1ENR |= (1UL << 3);          // Clock Signal Active for Port D
GPIOD->MODER |= 0x01000000; // output pin for LED D12
GPIOD->OSPEEDR |= (2UL << 0);   // GPIO Port Output Speed(High)
GPIOD->PUPDR &= ~(3UL << 0);       // No Pull-Up Pull-Down for PD0


asked on Stack Overflow Apr 28, 2019 by FurkanAltay

1 Answer


Stm32f4 has good library functions, and I recommend that you program with library functions instead of reading and writing registers directly, which is not the point. There is no way to read this question, because no one knows what registers you are writing, and I have to check the manual. If you are Chinese, you can ask this question in the domestic "原子" BBS, if you think there is no problem with the code, you can check the hardware, such as the power supply of this motor what, by the power supply on the board is unable to drive.

answered on Stack Overflow Apr 28, 2019 by Eovl.Han

User contributions licensed under CC BY-SA 3.0