4
Answers

counting incoming pulses (parallel port)

Ask a question
ray

ray

16y
11.1k
1

I'm still beginner and trying to count incoming pulses (square wave) via pin 10 (S6) of parallel port. Frequency input is 1Hz-100Hz. No pulses are when the logic is HI, and I start counting as soon as its logic is LOW.

I did something like this:


int y = 126;  // HI logic

do
          {
                y = PortAccess.Input(889);
                textBox1.Text = Convert.ToString(y);
             
                textBox1.Refresh();

                
           } while ((y == 126) || (y == 62) );   //126 is HI level, 62 is LOW


and i detect and count the changes of HI and LOW in a textbox:

 
private void textBox1_TextChanged(object sender, EventArgs e)
        {

            count = count + 1;

            textBox3.Text = Convert.ToString(count);

            textBox3.Refresh();

        }


with this code i count the pulses twice the input frequency
however the wait loop (do while) will make the application frozen if I switch to another application.

Any suggestion here? I'm trying to do a multithreading but cant figure out how and whether it's the correct solution.

I also tried to sample the incoming signal (square wave) with a timer (1ms -30ms interval), but it cant count correctly (count too slow most of the time). Am I coding it correctly?

     private void timer1_Tick(object sender, EventArgs e)
        {
            y = PortAccess.Input(889);
            if (y == 126)
            {
                textBox1.Text = Convert.ToString(y);


            }

            else if (y == 62)
            {
                textBox1.Text = Convert.ToString(y);


Can someone help me with better algorithm?

thx a lot

Answers (4)