使用STM32F407板控制无刷直流电机

时间:2019-04-28 06:36:51

标签: c stm32f4discovery

顺便说一句,如果我不能正确解释我在做什么,我会为我的英语感到抱歉。我正在制作一个使用stm32f407开发板控制无刷直流电机的项目。

首先,我控制了无刷直流电动机 与arduino并在示波器屏幕上观察到pwm输出引脚。我认识到无刷直流电动机不起作用 通过提供100%的功率。我知道我需要通过扩大占空比来实现软启动。 Arduino发送的pwm信号的频率为50 Hz(周期为20ms),占空比在最小%10至最大%12.5之间。我在stm上编写了一个代码,该代码通过在GPIOD12引脚上使用pwm来控制无刷直流电动机。我将timer7配置设置为每1us生成一个中断,并在TIM7_IRQHandler()函数中增加一个计数器。当计数器达到6410号时,将被重置。我定义了Duty变量,该变量具有100个值,并每1 us递增1,直到达到240个值。在无限循环中,当计数器较小的3 * Duty变量时,设置GPIOD12引脚。在3 *占空比变量和6410值之间,GPIO12引脚处于复位状态。当占空比变量增加时,脉冲宽度增加。我正在尝试使用这种方式进行软启动。但是我不能用这种方式控制直流电动机。你能告诉我我在做什么错吗?

#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); //当计数器在3 * Duty和6410值之间时,将重置计数器。     }

}

}

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.
counter=0;
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

}

1 个答案:

答案 0 :(得分:0)

Stm32f4具有良好的库函数,我建议您使用库函数进行编程,而不要直接读写寄存器,这不是重点。 没有办法阅读这个问题,因为没有人知道您正在编写什么寄存器,我必须阅读手册。 如果您是中国人,则可以在国内的“原子” BBS中问这个问题,如果您认为代码没有问题,可以检查硬件,例如该电机的电源是什么,由电源接通板无法驱动。