Search code examples
verilogfpgahdl

Verilog - take in input from multiple "sensors", increment "count"


I'm doing an introductory Verilog project for a class. In my code I'm using several sensors (Sensor_1, Sensor_2...). All sensors begin in an idle state; Sensor_x == 0. If the sensor senses a moving object it changes to Sensor_x == 1. Is there a way to code a "count" that will keep track of multiple sensors changing to 1 so that I can branch to different code depending on what the count is? I realize I could use nested 'if' statements in conjunction with OR (||) and AND (&&) but, if I have four or five sensors, this gets a bit redundant. Maybe I'm thinking about this the wrong way but keeping track of multiple asserted sensors in one variable seems more efficient than many 'if, 'else if' statements. Please correct me if I'm wrong. This may be very simple to many but coding in Verilog is new to me so I'm a bit stumped.


Solution

  • ... so that I can branch to different code

    No! Verilog is not a programming language where you can somehow re-direct a program counter to do something different.

    All code is present at the same time and is executed in parallel.
    All you can do is "enable" or "disable" parts of the code by using structures like if, case etc.

    Is there a way to code a "count" that will keep track of multiple sensors changing to 1...

    You can keep a count for all sensors but what do you do if two sensors change at the same time?
    It is not like a C-program where you can do:

       for (s=0; s<NUM_SENSORS; s++)
         if (sensor[s])
            count++;
    

    For a beginners course I would use a counter per sensor and add them up.

    Last but not least: if this is to really work in hardware, you may need to synchronize the sensors to the local clock before you can use them. Ask your class instructor if the sensor input is arriving synchronous or asynchronous. Beware: you risk that he may suspect that you have been asking for help if you ask that question!