首页 > 解决方案 > 使用stm32f407板控制无刷直流电机

问题描述

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

首先,我用 arduino 控制无刷直流电机,并在示波器屏幕上观察 pwm 输出引脚。我认识到无刷直流电机通过提供 %100 的功率无法工作。我知道我需要通过扩大占空比来进行软启动。Arduino 发送一个频率为 50 Hz(20ms 周期)和占空比介于最小值 %10 和最大值 %12.5 之间的 pwm 信号。我在 stm 上编写了一个代码,通过在 GPIOD12 引脚上使用 pwm 来控制无刷直流电机。我将 timer7 配置设置为每 1us 生成一个中断并在 TIM7_IRQHandler() 函数中增加一个计数器。当计数器达到 6410 数字时,它被重置。我定义了具有 100 值的 Duty 变量,每 1us 递增 1,直到达到 240 值。在无限 while 循环中,当计数器小于 3*Duty 变量时,设置 GPIOD12 引脚。在 3*duty 变量和 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

}

标签: cstm32f4discovery

解决方案


stm32f4有很好的库函数,我建议你用库函数编程,而不是直接读写寄存器,这不是重点。这个问题没有办法看,因为没人知道你写的是什么寄存器,我得查手册。如果你是中国人,你可以在国内的“原子”论坛问这个问题,如果你认为代码没有问题,你可以检查硬件,比如这个电机的电源是什么,通过电源打开董事会无法驱动。


推荐阅读