在过去的两天里,我编写了一个程序,该程序基本上可以生成一个相当准确的用户可调脉冲信号(频率和占空比都可调)。它基本上使用 micros() 函数来跟踪时间,以便将 4 个数字输出通道拉低或拉高。
这 4 个通道需要始终具有 90 度的相位差(想想 4cyl 引擎)。为了让用户更改设置,实现了一个 ISR,它向主循环返回一个标志以重新初始化程序。此标志定义为布尔值“set4”。当它为假时,主循环中的“while”语句将运行输出。当它为真时,“if”语句将执行必要的重新计算并重置标志,以便“while”语句将恢复。
该程序与初始值完美配合。相位完美。但是,当调用 ISR 并返回主循环时,根据我的理解,它会从最初中断的位置恢复“while”语句中的程序,直到它完成并重新检查标志“set4”以查看它是现在是真的,它应该停止。然后,即使之后的“if”语句重置并重新计算所有必要的变量,这 4 个输出通道之间的相位也会丢失。手动测试我看到取决于调用 ISR 的时间,它会给出不同的结果,通常所有 4 个输出通道同步在一起!即使我可能不会更改任何值,也会发生这种情况(因此,当您第一次打开 arduino 电源时,'if' 例程会将变量重置为完全相同的变量!)。然而,
我很确定这是由于 micros() 计时器造成的,因为循环将从调用 ISR 的位置恢复。我试图通过使用 cli() 和 sei() 检查和禁用中断来做不同的事情,但我无法让它工作,因为当 cli() 的参数为真时它只会冻结。我能想到的唯一解决方案(我已经尝试了一切,花了一整天的时间搜索和尝试东西)是强制 ISR 从循环的开头恢复,以便程序可以正确初始化。想到的另一个解决方案是可能以某种方式重置 micros() 计时器..但这会弄乱我相信的 ISR。
为了帮助您可视化这里发生的事情,这是我的代码的一个片段(请不要介意 micros 变量中的“Millis”名称和任何缺少的花括号,因为它不是纯粹的复制粘贴:p):
void loop()
{
while(!set4)
{
currentMillis = micros();
currentMillis2 = micros();
currentMillis3 = micros();
currentMillis4 = micros();
if(currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
{
interval = ONTIME;
ledState = HIGH;
}
else
{
interval = OFFTIME;
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
.
.
//similar code for the other 3 output channels
.
.
}
if (set4){
//recalculation routine - exactly the same as when declaring the variables initially
currentMillis = 0;
currentMillis2 = 0;
currentMillis3 = 0;
currentMillis4 = 0;
//Output states of the output channels, forced low seperately when the ISR is called (without messing with the 'ledState' variables)
ledState = LOW;
ledState2 = LOW;
ledState3 = LOW;
ledState4 = LOW;
previousMillis = 0;
previousMillis2 = 0;
previousMillis3 = 0;
previousMillis4 = 0;
//ONTIME is the HIGH time interval of the pulse wave (i.e. dwell time), OFFTIME is the LOW time interval
//Note the calculated phase/timing offset at each channel
interval = ONTIME+OFFTIME;
interval2 = interval+interval/4;
interval3 = interval+interval/2;
interval4 = interval+interval*3/4;
set4=false;
}
}
知道出了什么问题吗?亲切的问候,肯