我需要把3个霍尔水量传感器接到ARDUINO 板子上
我需要把3个霍尔水量传感器接到ARDUINO 板子上,分别接到了pin 2 4 7引脚上,可是为什么只有一个传感器能在串口上显示数据,其他没有反应,然后显示三个一样的数?求大仙帮忙// Code adapted by Charles Gantt from PC Fan RPM code written by Crenn @thebestcasescenario.com
// http:/themakersworkbench.com http://thebestcasescenario.com http://hotmcu.com
#define NbTopsFan1_PIN 2
#define NbTopsFan2_PIN 4
#define NbTopsFan3_PIN 7
volatile int NbTopsFan1; // measuring the rising edges of the signal
volatile int NbTopsFan2; // measuring the rising edges of the signal
volatile int NbTopsFan3; // measuring the rising edges of the signal
int Calc1;
int Calc2;
int Calc3;
void rpm () // This is the function that the interupt calls
{
NbTopsFan1++;
NbTopsFan2++;
NbTopsFan3++;// This function measures the rising and falling edge of the hall effect sensors signal
}
void setup()
{
Serial.begin(9600); // This is the setup function where the serial port is initialised,
attachInterrupt(0, rpm, RISING); // and the interrupt is attached
}
void loop ()
{
NbTopsFan1 = 0;
NbTopsFan2 = 0; // Set NbTops to 0 ready for calculations
NbTopsFan3 = 0;
sei(); // Enables interrupts
delay (1000); // Wait 1 second
cli(); // 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
Calc3 = (NbTopsFan3 * 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.print (Calc3, DEC); // Prints the number calculated above
Serial.print(" L/hour\r\n"); // Prints "L/hour" and returns anew line
} attachInterrupt(0, rpm, RISING);
这里只用了int.0啊,当然只有接在pin2上的有反应了。普通Arduino板子,中断0本来就就在pin2上,中断1在pin3 wangbin526 发表于 2017-9-1 09:32
attachInterrupt(0, rpm, RISING);
这里只用了int.0啊,当然只有接在pin2上的有反应了。普通Arduino板 ...
我是改程序还是加扩展?谢谢您改程序怎么改?加扩展用什么板子?谢谢 本帖最后由 wangbin526 于 2017-9-9 12:38 编辑
lona21 发表于 2017-9-6 19:22
我是改程序还是加扩展?谢谢您改程序怎么改?加扩展用什么板子?谢谢
不是说了啊,一般普通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
volatileunsigned long NbTopsFan1; // measuring the rising edges of the signal
volatileunsigned 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 anew line
} 谢谢您,
我买了一个 IO Sensor Shield V1,是不是可以解决3个霍尔水量传感器的问题,请您赐教 本帖最后由 wangbin526 于 2017-9-12 18:37 编辑
lona21 发表于 2017-9-11 17:44
谢谢您,
我买了一个 IO Sensor Shield V1,是不是可以解决3个霍尔水量传感器的问题,请您赐教
无语,老大,你木有基础的吗?之前说的很清楚了啊,UNO只有2个中断,这是主控的问题,外设扩展板只是接线省力点,又木有换主控,还是只有2个中断可以用啊。
再说一遍,你要接3个霍尔水量,下面三条路随你选,还看不懂的话建议重看遍基础教程吧
1、买块MEGA 2560,这货主控有6个中断,可以接6个
2、如果你这板子就只用来测流量,那别用中断数脉冲了,直接loop里轮询引脚状态数脉冲,这样不用中断,UNO有几个引脚就能接几个水量传感器,最多流速快的时候不太准而已,反正霍尔流量传感器本身就不太准,不在使用环境中标定的话或者水压不稳定的话,偏差个50%都一点不奇怪。
3、用PinChangeInt库(https://github.com/GreyGnome/PinChangeInt),给UNO扩展几个中断出来,当然代价是部分PWM和Timer不能用,非官方的冷门库不一定太稳定。
页:
[1]