gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:18:30 38 4
gpt4 key购买 nike

顺便说一句,如果我无法正确解释我在做什么,我对我的英语感到非常抱歉。我正在制作一个使用 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 值。在无限 while 循环中,当计数器小于 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

}

最佳答案

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

关于使用stm32f407板控制无刷直流电机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55887674/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com