我必須在13號引腳上計算並行端口的輸入頻率,來自555定時器IC,實際頻率應該在3-4赫茲(ON脈衝)左右。我嘗試了幾次代碼,但是每次都給出不同的值。我曾嘗試下面的代碼:計數並行端口輸入頻率 - C#
[DllImport("inpout32.dll", EntryPoint = "Inp32")]
public static extern int Input(int adress);
private void button1_Click(object sender, EventArgs e)
{
int currentState = Input(889);
int LastState;
while (true)
{
int State = Input(889);
if (State != currentState)
{
if (Input(889) == 120)
{
LastState = 0;
}
else
{
LastState = 1;
}
break;
}
}
GetFreq(LastState);
}
void GetFreq(int LastPulse)
{
int highPulseFreq = 0;
int lowPulseFreq = 0;
if (LastPulse == 1)
{
highPulseFreq++;
}
if (LastPulse == 0)
{
lowPulseFreq++;
}
int startTime = DateTime.Now.Second;
while (true)
{
if (startTime == DateTime.Now.Second)
{
if (Input(889) != 120)// ON
{
if (LastPulse == 0)
{
highPulseFreq++;
LastPulse = 1;
}
}
else
{
if (LastPulse == 1)
{
lowPulseFreq++;
LastPulse = 0;
}
}
}
else
{
MessageBox.Show("ON Pulses: " + highPulseFreq.ToString() + Environment.NewLine + "OFF Pulses: " + lowPulseFreq.ToString());
break;
}
}
}
OUTPUT:
我應該怎麼做,才能得到準確的頻率是多少?我的代碼中有任何錯誤嗎? 我正在使用inpout32.dll
來控制並行端口。
關閉脈衝值是否始終等於120,或者可以略大於或小於120? – 2012-07-17 22:50:21