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の出力を反転させます。

以上

main.cpp

Committer:
suupen
Date:
2013-12-22
Revision:
1:c4d2d6df69ce
Parent:
0:7ce3de2c24c3

File content as of revision 1:c4d2d6df69ce:

// ********************************************************************
// SwAnalogInput Library example program
// 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  |----|
//    |   ---------   |   ---------   |   ---------   |   ---------    |
//    |     ----      |     -----     |     -----     |                |
//    |-----o  o------.-----o  o------.-----o  o------|              -----
//           SW2            SW1              SW0                      mbed GND(0[V])
// 
//  
//  Accuracy of the resistance value that is within ± 1%
//
// <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.
//
// 
// <history>
// 120212 : first edtion
// 131221 : In this edition, I have changed the behavior of the program content
// 
// *********************************************************************

#include "mbed.h"
#include "SwAnalog.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

SwAnalog sw(p20);  // p20(adinput) :sw0,sw1,sw2

enum{
        sw0 = 0,
        sw1,
        sw2
    };
                   
int main() {
    while(1) {
        //===========================================
        // sw edge data refresh
        //===========================================
        sw.refreshEdgeData();

        //===========================================
        // SW level action
        //===========================================        
        // sw0 : OFF:LED1=ON   ON:LED1=OFF
        led1 = sw.checkLevel(sw0);
        
        // sw1 : OFF:LED2=OFF  ON:LED2=ON
        led2 = !sw.checkLevel(sw1);
        
        //===========================================
        // 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;
        }
    }
        
 
}