|
发表于 2017-9-8 22:41:34
|
显示全部楼层
本帖最后由 wangbin526 于 2017-9-9 12:38 编辑
不是说了啊,一般普通Arduino板也就2个中断,也就是说如果想用中断精确数脉冲的话,最多只能接两个水量计。要接两个以上,你得选中断多点的板子,比如 Mega2560有6个,Due所有IO都行,当然十几块的nodemcu也是全端口都行,同样用Arduino IDE。当然非得用普通328的arduino接两个以上中断,也有(https://github.com/GreyGnome/PinChangeInt)这个库可以用。
按你的要求改了下代码,2、3口都能接一个流量计。但建议自己去看看中断教程,非得像你给的例子,在主循环里开关中断和delay的话,用这代码的板子,将来除了测流量之外就啥也别干了。
- #define NbTopsFan1_PIN 2
- #define NbTopsFan2_PIN 3
- volatile unsigned long NbTopsFan1; // measuring the rising edges of the signal
- volatile unsigned long NbTopsFan2; // measuring the rising edges of the signal
- int Calc1;
- int Calc2;
- void rpm1 () // This is the function that the interupt calls
- {
- NbTopsFan1++;
- }
- void rpm2 () // This is the function that the interupt calls
- {
- NbTopsFan2++;
- }
- void setup()
- {
- Serial.begin(9600);
- pinMode(2, INPUT_PULLUP);
- pinMode(3, INPUT_PULLUP);
- attachInterrupt(0, rpm1, RISING); // and the interrupt is attached
- attachInterrupt(1, rpm2, RISING); // and the interrupt is attached
- }
- void loop ()
- {
- NbTopsFan1 = 0;
- NbTopsFan2 = 0; // Set NbTops to 0 ready for calculations
- interrupts(); // Enables interrupts
- delay (1000); // Wait 1 second
- noInterrupts(); // Disable interrupts
- Calc1 = (NbTopsFan1 * 60 / 4.5); // (Pulse frequency x 60) / 4.5Q, = flow rate in L/hour
- Calc2 = (NbTopsFan2 * 60 / 4.5); // (Pulse frequency x 60) / 4.5Q, = flow rate in L/hour
- Serial.print (Calc1, DEC); // Prints the number calculated above
- Serial.print (Calc2, DEC); // Prints the number calculated above
- Serial.println(" L/hour"); // Prints "L/hour" and returns a new line
- }
复制代码 |
|