This library has been tested with LPC1768. I can use the analog port of six of p20 from p15 of the LPC1768.

Dependents:   SwAnalogInputLibraryExampleProgram

Fork of SwAnalog by suu pen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SwAnalog.h Source File

SwAnalog.h

00001 /* SwAnalog Library
00002  * Copyright (c) 2012 suupen
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 /***********************************************************************/
00024 /*                                                                     */
00025 /*    SwAnalog.h                                                       */
00026 /*                                                                     */
00027 /*                                                                     */
00028 /*    2012/02/12 : V1.0                                                */
00029 /***********************************************************************/
00030 #ifndef _SWANALOG_H
00031 #define _SWANALOG_H
00032 
00033 #include "mbed.h"
00034 
00035 /** SWANALOG control class, based on a "mbed function"
00036 *
00037 * Example:
00038 * @code
00039 * // ********************************************************************
00040 * // SwAnalogInput Library example program
00041 * // Per pin analog port, SW recognition There are three possible.
00042 * //
00043 * // <schematic>
00044 * //   -.- mbed VOUT(+3.3[V])
00045 * //    |                                               |--------------------> mbed p20(ADinput)
00046 * //    |    8.2[kohm]       3.9[kohm]       2.0[kohm]  |    1.0[kohm]
00047 * //    |   ---------       ---------       ---------   |   ---------
00048 * //    .---| Rsw2  |---.---| Rsw1  |---.---| Rsw0  |---.---| Rout  |----|
00049 * //    |   ---------   |   ---------   |   ---------   |   ---------    |
00050 * //    |     ----      |     -----     |     -----     |                |
00051 * //    |-----o  o------.-----o  o------.-----o  o------|              -----
00052 * //           SW2            SW1              SW0                      mbed GND(0[V])
00053 * // 
00054 * //  
00055 * //  Accuracy of the resistance value that is within ± 1%
00056 * //
00057 * // <Operation details of this program>
00058 * //  mbed LED1 : When it detects the ON level of the SW0, and turns the LED1.
00059 * //  mbed LED2 : When it detects the OFF level of the SW1, and turns the LED2.
00060 * //  mbed LED3 : When it detects the ON edge of SW2, inverting the output to LED3.
00061 * //  mbed LED4 : When it detects the OFF edge of SW2, inverting the output to LED4.
00062 * //
00063 * // 
00064 * // <history>
00065 * // 120212 : first edtion
00066 * // 131221 : In this edition, I have changed the behavior of the program content
00067 * // 
00068 * // *********************************************************************
00069 *
00070 * #include "mbed.h"
00071 * #include "SwAnalog.h"
00072 *
00073 * DigitalOut led1(LED1);
00074 * DigitalOut led2(LED2);
00075 * DigitalOut led3(LED3);
00076 * DigitalOut led4(LED4);
00077 *
00078 * SwAnalog sw(p20);  // p20(adinput) :sw0,sw1,sw2
00079 *
00080 * enum{
00081 *        sw0 = 0,
00082 *        sw1,
00083 *        sw2
00084 *    };
00085 *                   
00086 * int main() {
00087 *    while(1) {
00088 *        //===========================================
00089 *        // sw edge data refresh
00090 *        //===========================================
00091 *        sw.refreshEdgeData();
00092 *
00093 *        //===========================================
00094 *        // SW level action
00095 *        //===========================================        
00096 *        // sw0 : OFF:LED1=ON   ON:LED1=OFF
00097 *        led1 = sw.checkLevel(sw0);
00098 *        
00099 *        // sw1 : OFF:LED2=OFF  ON:LED2=ON
00100 *        led2 = !sw.checkLevel(sw1);
00101 *        
00102 *        //===========================================
00103 *        // SW edge action
00104 *        //===========================================
00105 *        // sw2 on edge : LED3 invert
00106 *        if(sw.checkEdgeOn(sw2) == 1){
00107 *            led3 = !led3;
00108 *        }
00109 *        
00110 *        // sw2 off edge : LED4 invert
00111 *        if(sw.checkEdgeOff(sw2) == 1){
00112 *            led4 = !led4;
00113 *        }
00114 *    }
00115 * }
00116 * @endcode
00117 */
00118 
00119 class SwAnalog {
00120 public:
00121 
00122     /** Create a sw analog input object connected to the specified analog Input pin
00123     *
00124     * @param PinName adinput0 : analog input pin(pin15 to pin20) : sw0 kara sw2   no ninsiki 
00125     * @param PinName adinput1 : analog input pin(pin15 to pin20) : sw3 kara sw5   no ninsiki 
00126     * @param PinName adinput2 : analog input pin(pin15 to pin20) : sw6 kara sw8   no ninsiki
00127     * @param PinName adinput3 : analog input pin(pin15 to pin20) : sw9 kara sw11  no ninsiki     
00128     * @param PinName adinput4 : analog input pin(pin15 to pin20) : sw12 kara sw14 no ninsiki     
00129     * @param PinName adinput5 : analog input pin(pin15 to pin20) : sw15 kara sw17 no ninsiki     
00130     *
00131     * Recognition of the SW period is 10ms
00132     */
00133     SwAnalog(PinName adinput0  = NC, PinName adinput1  = NC, PinName adinput2 = NC, PinName adinput3  = NC, PinName adinput4  = NC,
00134               PinName adinput5  = NC
00135               );
00136 
00137     /** refresh edge data
00138     *
00139     * @param none 
00140     * @param return none
00141     *
00142     *  main de edge data wo tukau maeni jiko suru
00143     */
00144     void refreshEdgeData(void);
00145     
00146     /** Check Off to On edge
00147     *
00148     * @param uint8_t swNo     : 0:sw0, 1:sw1, ... ,17:sw17     
00149     * @param return uint8_t  On edge check  0: edge Nasi  1: edge Ari
00150     *
00151     */
00152     uint8_t checkEdgeOn(uint8_t swNo);
00153 
00154     /** Check On to Off edge
00155     *
00156     * @param uint8_t swNo     : 0:sw0, 1:sw1, ... ,17:sw17   
00157     * @param return uint8_t Off edge check   0 : Nasi   1 : Ari
00158     *
00159     */
00160     uint8_t checkEdgeOff(uint8_t swNo);
00161     
00162     /** Check sw Level
00163     *
00164     * @param uint8_t swNo     : 0:sw0, 1:sw1, ... ,17:sw17   
00165     * @param return uint8_t sw level check   0 : Off   1 : On
00166     *
00167     */    
00168     uint8_t checkLevel(uint8_t swNo);
00169     
00170     /** SW Number call name
00171     *
00172     * @param adinput0 : Z_sw1 to Z_sw3
00173     * @param adinput1 : Z_sw4 to Z_sw6
00174     * @param  ....
00175     * @param adinput5 : Z_sw16 tp Z_sw18
00176     */
00177     enum{
00178         Z_sw0,  // adinput1 no sw
00179         Z_sw1,
00180         Z_sw2,
00181         
00182         Z_sw3,  // adinput2 no sw
00183         Z_sw4,
00184         Z_sw5,
00185         
00186         Z_sw6,  // adinput3 no sw
00187         Z_sw7,
00188         Z_sw8,
00189         
00190         Z_sw9, // adinput4 no sw
00191         Z_sw10,
00192         Z_sw11,
00193         
00194         Z_sw12, // adinput5 no sw
00195         Z_sw13,
00196         Z_sw14,
00197         
00198         Z_sw15, // adinput6 no sw
00199         Z_sw16,
00200         Z_sw17
00201         };
00202     
00203 //protected:    
00204 private:
00205    AnalogIn _adinput0;
00206    AnalogIn _adinput1;
00207    AnalogIn _adinput2;
00208    AnalogIn _adinput3;
00209    AnalogIn _adinput4;
00210 
00211    AnalogIn _adinput5;
00212    
00213    
00214    Ticker swCheckTimer;
00215    
00216    void input(void);
00217 
00218    #define Z_matchcycle (10000) // 10000[us](10[ms]) to 100000[us](100[ms])  1[us]/count
00219    
00220    uint8_t D_swPinSuu;         // touroku sareta Sw Pin Suu  1 to Z_swPinSuuMax
00221    #define Z_swPinSuuMax  (6)  // SW warituke pin suu max
00222    #define Z_swInNoMax    (3)  // 1pin atari no sw setuzoku suu (1pin ni 3ko no sw setuzoku)
00223    
00224    uint8_t B_kariLevel[Z_swPinSuuMax * Z_swInNoMax]; // kakutei mae no ninsiki Level 0bit:saisin(t) 1bit:t-1, ... ,7bit:t-7  0:Off  1:On
00225     // match number define
00226     //#define Z_itchiPattern     (0x03)   // 2kai itch
00227     #define Z_itchiPattern    (0x07)  // 3kai itchi
00228     //#define Z_itchiPattern    (0x0f)  // 4kai itchi
00229     //#define Z_itchiPattern    (0x1f)  // 5kai itchi
00230     //#define Z_itchiPattern    (0x3f)  // 6kai itchi
00231     //#define Z_itchiPattern    (0x7f)  // 7kai itchi
00232     //#define Z_itchiPattern    (0xff)  // 8kai itchi
00233    
00234    // sw level data
00235    uint8_t D_nowLevel[Z_swPinSuuMax * Z_swInNoMax];  // saisin no kakutei Level 0:Off  1:On
00236    uint8_t D_oldLevel[Z_swPinSuuMax * Z_swInNoMax];  // zenkai no kakutei Level 0:Off  1:On
00237     #define Z_levelOff (0)
00238     #define Z_levelOn  (1)
00239    
00240    // sw edge data
00241    // swDigital.c naibu hensu 
00242    uint8_t B_edgeOn[Z_swPinSuuMax * Z_swInNoMax];    // off kara on  no ninsiki(on edge)  0:Nasi  1:Ari
00243    uint8_t B_edgeOff[Z_swPinSuuMax * Z_swInNoMax];   // on  kara off no ninsiki(off edge) 0:Nasi  1:Ari
00244    // user use hensu
00245    uint8_t D_edgeOn[Z_swPinSuuMax * Z_swInNoMax];    // off kara on  no ninsiki(on edge)  0:Nasi  1:Ari
00246    uint8_t D_edgeOff[Z_swPinSuuMax * Z_swInNoMax];   // on  kara off no ninsiki(off edge) 0:Nasi  1:Ari
00247     #define Z_edgeNasi (0)
00248     #define Z_edgeAri  (1)
00249 
00250     //------------------
00251     // Resistor network
00252     //------------------
00253     
00254     //   -.- mbed VOUT(+3.3[V])
00255     //    |                                               |--------------------> mbed p15 - p20(analog port)
00256     //    |   ---------       ---------       ---------   |   ---------
00257     //    .---| Rsw2  |---.---| Rsw1  |---.---| Rsw0  |---.---| Rout  |----|
00258     //    |   ---------   |   ---------   |   ---------   |   ---------    |
00259     //    |     ----      |     -----     |     -----     |                |
00260     //    |-----o  o------.-----o  o------.-----o  o------|              -----
00261     //           SW2            SW1              SW0                      mbed GND(0[V])
00262     // 
00263     //    |                                                                |
00264     //    |<----------------------- Rall --------------------------------->|    
00265     //    |                          |                                     |
00266     //                               ----> Z_R0 to Z_R7
00267     
00268     
00269     #define Z_Rsw2 (8200.0F)   // SW2 no R (1/1 [ohm]/count)
00270     #define Z_Rsw1 (3900.0F)   // SW1 no R (1/1 [ohm]/count)
00271     #define Z_Rsw0 (2000.0F)   // adinput1 no R (1/1 [ohm]/count)
00272     #define Z_Rout (1000.0F)   // Vout no R (1/1 [ohm]/count)
00273     //Z_Rsw2,Z_Rsw1,Z_Rsw0,Z_Rout niwa +-1[%]no seido no teiko wo tukau koto
00274     
00275     #define Z_gosaMax (1.020F) // Z_Rout(max) / Z_Rx(min) = (Z_Rout * 1.01) / (Z_Rx * 0.99) = (Z_Rout / Z_Rx) * 1.020
00276     #define Z_gosaMin (0.990F) // Z_Rout(min) / Z_Rx(max) = (Z_Rout * 0.99) / (Z_Rx * 1.01) = (Z_Rout / Z_Rx) * 0.980
00277     
00278     // Rall keisanchi
00279                                                            // SW2  SW1  SW0
00280     #define Z_R0 ((Z_Rsw2 + Z_Rsw1 + Z_Rsw0 + Z_Rout))     // OFF  OFF  OFF
00281     #define Z_R1 ((Z_Rsw2 + Z_Rsw1 + 0      + Z_Rout))     // OFF  OFF   ON
00282     #define Z_R2 ((Z_Rsw2 + 0      + Z_Rsw0 + Z_Rout))     // OFF   ON  OFF 
00283     #define Z_R3 ((Z_Rsw2 + 0      + 0      + Z_Rout))     // OFF   ON   ON
00284     #define Z_R4 ((0      + Z_Rsw1 + Z_Rsw0 + Z_Rout))     //  ON  OFF  OFF
00285     #define Z_R5 ((0      + Z_Rsw1 + 0      + Z_Rout))     //  ON  OFF   ON
00286     #define Z_R6 ((0      + 0      + Z_Rsw0 + Z_Rout))     //  ON   ON  OFF
00287     #define Z_R7 ((0      + 0      + 0      + Z_Rout))     //  ON   ON   ON    
00288     
00289     // Rout : Rall (max , min)
00290     #define Z_R0max  (((Z_Rout * Z_gosaMax) / Z_R0))
00291     #define Z_R0min  (((Z_Rout * Z_gosaMin) / Z_R0))
00292     #define Z_R1max  (((Z_Rout * Z_gosaMax) / Z_R1))
00293     #define Z_R1min  (((Z_Rout * Z_gosaMin) / Z_R1))
00294     #define Z_R2max  (((Z_Rout * Z_gosaMax) / Z_R2))
00295     #define Z_R2min  (((Z_Rout * Z_gosaMin) / Z_R2))    
00296     #define Z_R3max  (((Z_Rout * Z_gosaMax) / Z_R3))
00297     #define Z_R3min  (((Z_Rout * Z_gosaMin) / Z_R3))    
00298     #define Z_R4max  (((Z_Rout * Z_gosaMax) / Z_R4))
00299     #define Z_R4min  (((Z_Rout * Z_gosaMin) / Z_R4))
00300     #define Z_R5max  (((Z_Rout * Z_gosaMax) / Z_R5))
00301     #define Z_R5min  (((Z_Rout * Z_gosaMin) / Z_R5))    
00302     #define Z_R6max  (((Z_Rout * Z_gosaMax) / Z_R6))
00303     #define Z_R6min  (((Z_Rout * Z_gosaMin) / Z_R6))   
00304     #define Z_R7max  (((Z_Rout * Z_gosaMax) / Z_R7))
00305     #define Z_R7min  (((Z_Rout * Z_gosaMin) / Z_R7))    
00306     
00307     // threshold lvevel
00308     //  GND(0[V]) -> 0_1 -> 1_2 -> 2_3 -> 3_4 -> 4_5 -> 5_6 -> 6_7 -> Vcc(3.3[V]) 
00309     //  --------------+------+------+------+------+------+------+---------------
00310     //   SW0 |    OFF     ON    OFF     ON    OFF    ON     OFF     ON     
00311     //   SW1 |    OFF    OFF     ON     ON    OFF   OFF      ON     ON
00312     //   SW2 |    OFF    OFF    OFF    OFF     ON    ON      ON     ON 
00313     
00314     #define Z_threshold0_1  (((Z_R0max + Z_R1min) / 2))
00315     #define Z_threshold1_2  (((Z_R1max + Z_R2min) / 2))    
00316     #define Z_threshold2_3  (((Z_R2max + Z_R3min) / 2))    
00317     #define Z_threshold3_4  (((Z_R3max + Z_R4min) / 2))        
00318     #define Z_threshold4_5  (((Z_R4max + Z_R5min) / 2))    
00319     #define Z_threshold5_6  (((Z_R5max + Z_R6min) / 2))    
00320     #define Z_threshold6_7  (((Z_R6max + Z_R7min) / 2))            
00321     
00322     void adInput(float ad, uint8_t swInNo);
00323 
00324 };    
00325 
00326 #endif    // _SWANALOG_H