使用arduino和H桥的PWM正弦波

问题描述

我正在尝试使用 PWM 调制提供正弦波。 我的电路由以下部分组成:

  1. Arduino Uno
  2. H 桥 - 控制方向(引脚 4,5)和 PWM 电压(PWM 引脚 6)
  3. 电磁马达 - 应将正弦输出馈送到马达

我在网上看到了一些指南,但其中大部分使用了我不太理解的正弦波表和低级代码,因此我决定尝试使用一些更琐碎的代码,如下所示:

const int DIR1_PIN = 4;
const int DIR2_PIN = 5;
const int PWM_PIN = 6;
const int LED_PIN = 9;

const int numSamples = 20;
const int sampleFreq = 2000;
const float pi = 3.1415;
float f = 1; // signal frequency
float T = 1/f; 
float dt = T/numSamples;
int sinesignal[numSamples]; // sine values would be added here
int dir1Array[numSamples]; // 11....100....0 indicates direction
int dir2Array[numSamples]; // 00....011....1 indicates other direction

void setup()
{

  pinMode(LED_PIN,OUTPUT); // the led is just an indicator to what the output is
  pinMode(DIR1_PIN,OUTPUT);
  pinMode(DIR2_PIN,OUTPUT);
  pinMode(PWM_PIN,OUTPUT);

  for (int n = 0; n < numSamples; n++)
  {
    sinesignal[n] = abs((int) (255 * (sin(2 * pi * f * n * dt))));
    if (n < numSamples / 2)
    {
      dir1Array[n] = HIGH;
      dir2Array[n] = LOW;
    }
    else
    {
      dir1Array[n] = LOW;
      dir2Array[n] = HIGH;
    }
  }
}

void loop()
{
  for (int n = 0; n < numSamples; n++)
  {
    analogWrite(LED_PIN,sinesignal[n]);
    analogWrite(PWM_PIN,sinesignal[n]);
    digitalWrite(DIR1_PIN,dir1Array[n]);
    digitalWrite(DIR2_PIN,dir2Array[n]);
    delay(sampleFreq); // not quite sure how the delay affects the frequency f
  }
}

长话短说,这段代码不允许我控制频率 f(对于不同的 f 值获得相同的输出

关于如何为不同的 f 生成此类输出的任何想法?应该如何设置 delay 呼叫,即使是?

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)