gpt4 book ai didi

c - 使用 Arduino Uno 生成正弦波的代码是否有效/合理?

转载 作者:太空宇宙 更新时间:2023-11-04 07:44:57 29 4
gpt4 key购买 nike

注意:我省略了其他两个阶段(V 和 W)的代码,示例中不需要。

我很确定,我这样处理“开”和“关”时间的方式并不是一种有效的方式。

我想使用查找表实现“开”和“关”脉动。计时器应与表的当前选定值进行比较,如果该值通过则切换输出。

问题:我认为我对计时器的处理“错误”。我没有得到正确数量的脉冲(我的理解是,我应该得到与数组内部的值一样多的脉冲,在本例中为 300+)。我注意到在示波器上只有几个脉冲。

我使用“输出比较寄存器”会更好吗?

您还有哪些其他提示/想法?

bool posTOneg = true;               // Variable for the change from positive to negative. 'true' in the beginning.

///// CONFIGURATION OF THE PINS /////

int igbt_high_u = 11;
int igbt_high_v = 10;
int igbt_high_w = 9;

int igbt_low_u = 6;
int igbt_low_v = 5;
int igbt_low_w = 3;

///// LOOKUP TABLE /////
int UsinPWM[]={1,2,5,7,10,12,15,17,19,22,24,27,30,32,34,37,39,42,
44,47,49,52,54,57,59,61,64,66,69,71,73,76,78,80,83,85,88,90,92,94,97,99, //42 Werte bis hier hin
101,103,106,108,110,113,115,117,119,121,124,126,128,130,132,134,136,138,140,142,144,146,
148,150,152,154,156,158,160,162,164,166,168,169,171,173,175,177,178,180,182,184,185,187,188,190,192,193, // 90
195,196,198,199,201,202,204,205,207,208,209,211,212,213,215,216,217,219,220,221,222,223,224,225,226,227,
228,229,230,231,232,233,234,235,236,237,237,238,239,240,240,241,242,242,243,243,244,244,245,245,246,246, // 142
247,247,247,248,248,248,248,249,249,249,249,249,250,250,250,250,249,249,249,249,249,248,
248,248,248,247,247,247,246,246,245,245,244,244,243,243,242,242,241,240,240,239,238,237,237,236,235,234, // 190
233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,217,216,215,213,212,211,209,208,207,205,204,
202,201,199,198,196,195,193,192,190,188,187,185,184,182,180,178,177,175,173,171,169,168,166,164,162,160, // 242
158,156,154,152,150,148,146,144,142,140,138,136,134,132,130,128,126,124,121,119,117,115,113,110,108,106,
103,101,99,97,94,92,90,88,85,83,80,78,76,73,71,69,66,64,61,59,57,54,52,49,47,44,42,39,37,34,32,30, // 300
27,24,22,19,17,15,12,10,7,5,2,1}; // 312

///// SIZE OF THE ARRAY /////
int sizeU = sizeof(UsinPWM)/sizeof(int); // Variable for PWM array (size)
///// OFFSET 120 GRAD /////
int indexU = 0; // zaehlervariable

///// FUNCTION FOR THE PHASE U (POS AND NEG HALF-CYCLE) /////
void posHalbU (int i)
{
if(TCNT0 < UsinPWM[i]) // Compare timer with the current value of the look up table
{
digitalWrite(igbt_low_v, HIGH); // Triggering hardware
digitalWrite(igbt_low_w, HIGH);
digitalWrite(igbt_high_u, HIGH);
}
else
{
digitalWrite(igbt_high_u, LOW);
digitalWrite(igbt_low_v, LOW);
digitalWrite(igbt_low_w, LOW);
}
}

void negHalbU(int i)
{
if(TCNT0 < UsinPWM[i])
{
digitalWrite(igbt_high_v, HIGH);
digitalWrite(igbt_high_w, HIGH);
digitalWrite(igbt_low_u, HIGH);
}
else
{
digitalWrite(igbt_high_v, LOW);
digitalWrite(igbt_high_w, LOW);
digitalWrite(igbt_low_u, LOW);
}
}


void setup()
{
Serial.begin(9600);

///// CONFIGURE PWM /////
pinMode(igbt_high_u, OUTPUT);
pinMode(igbt_high_v, OUTPUT);
pinMode(igbt_high_w, OUTPUT);
pinMode(igbt_low_u, OUTPUT);
pinMode(igbt_low_v, OUTPUT);
pinMode(igbt_low_w, OUTPUT);
}

void loop()
{
///// CALL PHASE U /////
if(posTOneg)
{
posHalbU(indexU);
}
else
{
negHalbU(indexU);
}


indexU = indexU + 1;

if(indexU == sizeU)
{
indexU = 0; // Reset of the counter variable
posTOneg = !posTOneg; // Negate the boolean value. Swapping between pos and neg all the time
}
}

下面是我生成的结果。大约创建了 20 个脉冲,并且这些脉冲是周期性创建的。

Enter image description here

最佳答案

使用for 循环 自动对正弦数组进行索引。使用 analogWrite() 以专用时间间隔将数据写入 PWM 引脚。时间间隔 x 数组大小 = 正弦周期。

输出看起来很糟糕......只是一个快速变化的 PWM 信号,拉伸(stretch) 0-5 V。适当过滤后,正弦波会神奇地出现。

关于c - 使用 Arduino Uno 生成正弦波的代码是否有效/合理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57875203/

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