前面兩文介紹了兩種量轉速的方法,本文介紹另一種。

用中斷(interrupt)來進行風扇轉速量測很方便,

但也有佔用中斷的問題發生,而且對UNO這類的只有

兩個中斷的低階CPU來說問題會比較大。

所以有人專門開發了量測風扇轉速用的程式庫,可以

解決這個問題。

程式庫網址:

https://github.com/porrey/PcFan

程式庫下載解壓縮後名稱為 PcFan-master

copy到arduino的程式庫目錄底下即可使用。

 

範例程式:

#include <Arduino.h>
#include "FanMonitor.h"

// ***
// *** Pins
// ***
#define FAN_MONITOR_PIN 9

// ***
// *** Define the FanMonitor instance to monitor
// *** the 3-wire fan.
// ***
FanMonitor _fanMonitor = FanMonitor(FAN_MONITOR_PIN, FAN_TYPE_UNIPOLE);

void setup()
{
  // ***
  // *** Initialize the serial port.
  // ***
  Serial.begin(9600);

  // ***
  // *** Initialize the fan monitor.
  // ***
  _fanMonitor.begin();
}

void loop()
{
  // ***
  // *** Get the fan speed.
  // ***
  uint16_t rpm = _fanMonitor.getSpeed();

  // ***
  // *** Print the speed to the serial port.
  // ***
  Serial.print("Speed = "); Serial.print(rpm); Serial.println(" RPM");
  
  // ***
  // *** Delay 1 second.
  // ***
  delay(1000);
}

 

以上範例使用digital pin9當風扇轉速輸入腳,可以 照實際需求修改,不需要

特別使用interrupt輸入腳位,可使用一般I/O腳位。

com port輸出如下:

 

接線圖:

將程式改寫成我的style並輸出到1602A:

/*
使用1602A I2C LCD模組及函式庫
使用Interrupt 0 讀取風扇轉速並顯示在1602A LCD螢幕上
LCD I2C Library,從這裡可以下載:
https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
本程式使用Newliquidcrystal_1.3.5版函式庫
PcFan程式庫從這裡下載:
https://github.com/porrey/PcFan
作者:Lu yaku

日期:2018.05.02
*/

#include <Arduino.h>
#include "FanMonitor.h"
#include <Wire.h>               
#include <LiquidCrystal_I2C.h>  
// 初始化 I2C 1602A LCD,I2C預設地址為0x27
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);


// ***
// *** Pins
// ***
#define FAN_MONITOR_PIN 3

// ***
// *** Define the FanMonitor instance to monitor
// *** the 3-wire fan.
// ***
FanMonitor _fanMonitor = FanMonitor(FAN_MONITOR_PIN, FAN_TYPE_UNIPOLE);

void setup()
{

lcd.begin(16, 2);
//背光閃爍三次,間隔0.25秒
for(int i = 0; i < 3; i++) {
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight();
// 輸出初始化文字
  lcd.setCursor(0, 0); // 設定游標位置在第一行行首
  lcd.print("Hello, world!");
  delay(1000);
  lcd.setCursor(0, 1); // 設定游標位置在第二行行首
  lcd.print("LCD is Working!");
  delay(2000);
 
 lcd.clear();
 lcd.setCursor(0, 0); 
 lcd.print("DC FAN Speed");
 delay(250);
 
  // ***
  // *** Initialize the fan monitor.
  // ***
  _fanMonitor.begin();
}

void loop()
{
  // ***
  // *** Get the fan speed.
  // ***
  uint16_t rpm = _fanMonitor.getSpeed();

 //將轉速計算後輸出到1602A LCD上
 lcd.setCursor(0, 1); 
 lcd.print("RPM = ");
 lcd.print(rpm); 
 lcd.print("   ");
  
  // ***
  // *** Delay 1 second.
  // ***
  delay(1000);
}

 

為了實驗方便我將pin腳接到D3

量測的結果跟前面兩種方式很接近。

優點是可以改用任意pin腳來當轉速輸入,

缺點是偵測轉速的跳動會比較大一點,

可以依照自己的系統需求決定要用哪一種方式來偵測轉速。

arrow
arrow
    全站熱搜

    呂阿谷 發表在 痞客邦 留言(1) 人氣()