類比輸入 + 感應溫度計

  • 前言 - 許多的延伸應用將由此開始

互動裝置就是要對環境週遭的事物有感覺,並做出一些反應。所以加入感應裝置算是製作互動裝置的輸入感應第一步。在學會基板的簡單操作之後,玩家會慢慢添加一些不同類型的感測器應用來介紹互動裝置,首先先介紹內建的溫度感應器。

那到底什麼是感應器呢? 說穿了感應器會對所在的週遭環境做判斷,如果是溫度計,就會將環境的溫度轉換成類比電壓。別想得太難,找找看周遭上生活的例子有沒有類似的互動體驗 ?就像到7-11便利商店買東西,再進去之前自動門就會感應到你的接近(類比輸入),而自己開啟門,然後播出一小段開門音樂叮咚(也算是聲音訊號類比輸出),其實這已經算是一個互動裝置。

在這裡介紹連接環境感測器,必須連接到基板的類比輸入埠PIN腳,而這裡先說明一點,並不是所有核心晶片型號都有支援類比輸入,必須有的晶片型號才有支援類比輸入Analog to Digital Converter (ADC)。例如MSP430G2553, MSP430G2231, G2131, G2132....等 有支援類比輸入埠ADC Ch/Res,但套件附的另一顆晶片MSP430就沒有ADC功能,在使用前先注意一下規格書上說明。而出貨所附的MSP430G2553晶片上有溫度感測器,可用來做些小實驗。

  • 類比取樣

解析度以位元數為單位,代表可用於輸入訊號編碼的離散為準數。

resolution bit
  • 類比輸入的原理

  • Can read values via analogRead

  • Connecting sensors

  • Result is in range 0 to 1023 (10 bits)

  • 0V = 0 and 5V =1023

取樣公式

N=1023* (Vin - VRminus)/(VRplus - VRminus) .

必須要小心上面符號代表的意義,其中 launchpad 的default VRminus數值為接地訊號 (the negative reference voltage) GND, or 0 volts, 而且VRplus (the positive reference voltage) 正電壓代表的是 VDD, or 3.3 volts。

N=1023*(Vin - GND) / (VDD- GND)

所以,最後的取樣公式會是

N=1023*Vin/3.3.

The reference voltages are selected with the ADC10CTL0 register.

  • 上傳程式碼到基板

請參照<初次使用整合開發環境>步驟,再貼上程式碼,即可運作。

  • 原始程式碼

http://www.msp430launchpad.com/2010/09/simple-adc-example-on-launchpad.html

補充說明: LaunchPad 與 Arduino 的PIN腳定義有所不同。MSPG2231核心晶片可以支援八組的數位輸出P1.0~P1.7都可以設定成PWM數位輸出或是類比輸出輸入,或許對於玩家會感到很困惑。其實,與Arduino比較不同的它指定了每一個PIN腳的用途,因為這樣可以簡化對每一個依PIN的設定,但缺點是只能限定玩家用哪一個腳位為數位與類比輸出,相較來說,LaunchPad對於I/O Port較為自由寬鬆,利用Grace圖形介面設定或在程式中指定每個PIN的用途,使用上較具彈性。

http://e2e.ti.com/support/microcontrollers/msp43016-bit_ultra-low_power_mcus/f/166/p/140591/506933.aspx#506933

The msp430g2553 data sheet has the usual footnote describing how to convert ADC measurements from channel 10 into a temperature:

The following formula can be used to calculate the temperature sensor output voltage:

VSensor,typ = TCSensor (273 + T [°C] ) + VOffset,sensor [mV]

where TCSensor is 3.55 mV/Cel. VOffset,sensor is presumably a part-specific calibration value.

The msp430g2x33_adc10_temp.c example has the following calculation to convert an ADC10 measurement of INCH10 to a temperature:

// oC = ((A10/1024)*1500mV)-986mV)*1/3.55mV = A10*423/1024 - 278

temp = ADC10MEM;

IntDegC = ((temp - 673) * 423) / 1024;

Where did the 986mV come from? It corresponds to an offset of 278 degrees, and seems to suggest the zero volt measurement would be at about 5 degrees below absolute zero. The equation from the data sheet suggests it should be 969mV (273 Cel * 3.55 mV/Cel).

  • 具有類比輸入功能的晶片

http://focus.ti.com/paramsearch/docs/parametricsearch.tsp?sectionId=95&tabId=2662&familyId=1937&family=mcu#p1020=8&o7=Value Line&p494=14PDIP;20PDIP