c# - C#中指令之间的精确计时
问题描述
我正在尝试用 C# 重写一些我已经为 Arduino 编写的代码,以便在 Raspberry Pi 3(Windows 10 IOT)上使用。我遇到问题的 Arduino 代码如下:
void loop()
{
while(true)
{
Step();
delay(100);
}
}
void Step()
{
digitalWrite(StepPin, HIGH); //Sets the output to HIGH
delayMicroseconds(2000); //Waits 2ms
digitalWrite(StepPin, LOW); //Sets the output to LOW
}
这是使用 A4988 步进驱动器控制步进电机的代码。代码目标是每 100 毫秒给驱动板一个 2 毫秒的脉冲。
到目前为止,我在 C# 中所做的是:
public void Runner()
{
while(true)
{
Step();
//100ms delay here
}
}
public void Step()
{
StepPin.Write(GpioPinValue.High);
//2ms delay here
StepPin.Write(GpioPinValue.Low);
}
我试过 Thread.Sleep() 但它似乎不合适,而且这似乎也不是更好:
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
while(stopwatch.ElapsedMilliseconds < 2) ;
实现这种延迟的最佳方式是什么?
解决方案
推荐阅读
- git - 如何在 maven-release-plugin 发布期间将标签合并到主分支?
- c# - 从 C# 开发人员的角度理解 MatLab 数组裁剪和运算符:例如 data(1:4:end,:,:);
- ansible - 将变量传递给 Jinja2 模板文件的正确 Ansible 语法
- c++ - C ++ boost,等待来自不同线程的var init
- cmd - 如何在控制台应用程序中将输入作为输入发送
- c++ - 调用迭代函数的次数
- dns - 使用 lvh.me 而不是 localhost 进行测试是否安全?
- docker - Docker 运行镜像
- sqlite - 使用合并减去列值
- linux - dockerfile 最简单的自动化