This program is used to verify the operations of the library SW recognize an analog port.

Dependencies:   SwAnalog_LPC1768 mbed

Fork of SwAnalogInputLibraryExampleProgram by suu pen

SW認識Library(SWAnalog)のサンプルプログラムの説明

<概要>

SW認識させるLibrary(SwAnalog)と、Libraryを使用したサンプルプログラムについて説明します。

Libraryとサンプルプログラムは次のアドレスからダウンロードできます。
Library:
http://mbed.org/users/suupen/code/SwAnalog_LPC1768/

サンプルプログラム:
http://mbed.org/users/suupen/code/SwAnalogInputLibraryExampleProgram/

サンプルプログラムの動作

<Libraryについて>

SWの状態を、抵抗による電圧の分圧比の変化で認識します。
使用する抵抗は、精度±1[%]のもの(金属皮膜抵抗)を使います。
(共立エレショップ:http://eleshop.jp/shop/c/c110312/)

/media/uploads/suupen/analogsw--.jpg
SW認識回路

アナログポートから読み込んだアナログ認識値から、SWのON,OFF状態を判定します。
この判定値を10[ms]毎に更新して、判定値が3回一致したら、その判定値を確定値として認識します。
このため、SW操作をして認識値が変化するまでに30[ms]必要になります。
複数回のレベル認識をして確定値を決めるので、SW操作によるチャタリングノイズの除去が可能です。

このLibraryで使用するmbed機能

  • チッカータイマー(Ticker) 1[us]単位、10[ms]周期
  • アナログ入力(AnalogIn)

SW入力に設定可能な端子(LPC1768)

  • p15~p20 の内の6本
    (各端子に3本のSW認識が可能で、合計18本のSW認識が可能)

SW認識処理の変更方法

SwDitital.h内の定義を変更することで、以下の条件を変更できます。

  • SW認識周期:Z_matchcycle 設定範囲:10[ms](10000)~100[ms](100000) (1[us/count])
  • SW一致回数:Z_itchPattern 設定範囲:3~8[回]

/media/uploads/suupen/sw---------.jpg
SwAnalog.hの設定箇所

Libraryの使い方

/media/uploads/suupen/---------.jpg
サンプルプログラムより

  • SW認識する端子の指定: swAnalog
  • SWのレベル認識関数  :checkLevel(swNo)
  • SWのONエッジ認識関数 :checkEdgeOn(swNo)
  • SWのOFFエッジ認識関数:checkEdgeOff(swNo)
    引数のswNoは、端子定義関数のswAnalogの第一引数には,0,1,2、第二引数には3,4,5と割り振られていきます。

<サンプルプログラムについて>

mbed(LPC1768)を使用しています。SWは3つ使用しており、
アナログポートのP20
に接続します。

/media/uploads/suupen/samplesw--.jpg
回路図

/media/uploads/suupen/-------p1170483.jpg
ブレットボードでの配線

プログラムの動作内容

  • SW0、SW1では、SWのレベル認識(OFFなのかONなのかを判定)する例です。
    SW0は、ONしている間、LED1を点灯させます。
    SW1は、OFFしている間、LED2を点灯させます。
  • SW2では、SWのエッジ認識(OFF→ON、ON→OFF)する例です。
    SW2のONエッジ(OFF→ON)の時に、LED3の出力を反転させます。
    SW2のOFFエッジ(ON→OFF)の時に、LED4の出力を反転させます。

以上

Revision:
1:c4d2d6df69ce
Parent:
0:7ce3de2c24c3
--- a/main.cpp	Sun Feb 12 00:52:09 2012 +0000
+++ b/main.cpp	Sun Dec 22 09:27:26 2013 +0000
@@ -1,11 +1,11 @@
-//********************************************************************
+// ********************************************************************
 // SwAnalogInput Library example program
-//  mbed no analog port de 3hon no sw wo ninsiki suru.
-//  6hon no analog port (p15 - p20) de 6 * 3 = 18 ko no sw ninsiki ga dekiru.
+// Per pin analog port, SW recognition There are three possible.
 //
 // <schematic>
 //   -.- mbed VOUT(+3.3[V])
 //    |                                               |--------------------> mbed p20(ADinput)
+//    |    8.2[kohm]       3.9[kohm]       2.0[kohm]  |    1.0[kohm]
 //    |   ---------       ---------       ---------   |   ---------
 //    .---| Rsw2  |---.---| Rsw1  |---.---| Rsw0  |---.---| Rout  |----|
 //    |   ---------   |   ---------   |   ---------   |   ---------    |
@@ -13,17 +13,20 @@
 //    |-----o  o------.-----o  o------.-----o  o------|              -----
 //           SW2            SW1              SW0                      mbed GND(0[V])
 // 
-//  Rsw2 : 8.2[kohm], Rsw1 = 3.9[kohm], Rsw0 = 2.0[kohm], Rout = 1.0[kohm] (R no seido ha +-1[%])
+//  
+//  Accuracy of the resistance value that is within ± 1%
 //
-// <program no naiyo>
-//  mbed LED1 : sw0 no level de tento  sw Off = led Off, sw On = led On
-//  mbed LED2 : sw1 no level de tento  sw Off = led Off, sw On = led On
-//  mbed LED3 : sw2 no level de tento  sw Off = led Off, sw On = led On
-//  mbed LED4 : sw0,sw1,sw2 no Off kara On henka de led tento jotai wo hanten.
+// <Operation details of this program>
+//  mbed LED1 : When it detects the ON level of the SW0, and turns the LED1.
+//  mbed LED2 : When it detects the OFF level of the SW1, and turns the LED2.
+//  mbed LED3 : When it detects the ON edge of SW2, inverting the output to LED3.
+//  mbed LED4 : When it detects the OFF edge of SW2, inverting the output to LED4.
 //
 // 
-// <rireki>
-// 2012/02/12 : syohan
+// <history>
+// 120212 : first edtion
+// 131221 : In this edition, I have changed the behavior of the program content
+// 
 // *********************************************************************
 
 #include "mbed.h"
@@ -35,27 +38,42 @@
 DigitalOut led4(LED4);
 
 SwAnalog sw(p20);  // p20(adinput) :sw0,sw1,sw2
+
+enum{
+        sw0 = 0,
+        sw1,
+        sw2
+    };
                    
 int main() {
     while(1) {
-        // sw level and edge data refresh
+        //===========================================
+        // sw edge data refresh
+        //===========================================
         sw.refreshEdgeData();
-        
-        // tact action (sw0 level = on : led1 = on)
-        led1 = sw.checkLevel(0);
-        
-        // tact action (sw1 level = off : led2 = on)
-        led2 = sw.checkLevel(1);
+
+        //===========================================
+        // SW level action
+        //===========================================        
+        // sw0 : OFF:LED1=ON   ON:LED1=OFF
+        led1 = sw.checkLevel(sw0);
         
-        // tact action (sw2 level = off : led3 = on)
-        led3 = sw.checkLevel(2);
+        // sw1 : OFF:LED2=OFF  ON:LED2=ON
+        led2 = !sw.checkLevel(sw1);
         
-        // toggle action (sw0 to sw3 level Off to On : led4 invert )
-        if((sw.checkEdgeOn(0) == 1)     // sw0
-        || (sw.checkEdgeOn(1) == 1)     // sw1
-        || (sw.checkEdgeOn(2) == 1)     // sw2
-        ){
-             led4 = !led4;
+        //===========================================
+        // SW edge action
+        //===========================================
+        // sw2 on edge : LED3 invert
+        if(sw.checkEdgeOn(sw2) == 1){
+            led3 = !led3;
+        }
+        
+        // sw2 off edge : LED4 invert
+        if(sw.checkEdgeOff(sw2) == 1){
+            led4 = !led4;
         }
     }
+        
+ 
 }