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

问题描述 投票:0回答:1

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

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

}

c stm32f4discovery
1个回答
0
投票

Stm32f4具有良好的库函数,我建议您使用库函数进行编程,而不是直接读写寄存器,这不是重点。没有办法阅读这个问题,因为没有人知道你在写什么寄存器,我必须查看手册。如果你是中国人,你可以在国内的“原子”BBS上提出这个问题,如果你认为代码没有问题,可以通过电源检查硬件,比如这个电机的电源是什么董事会无法开车。

© www.soinside.com 2019 - 2024. All rights reserved.