![](/media/cache/profiles/8cf40e328089249cf77075692aa0c09b.jpg.50x50_q85.jpg)
CO2 Sensor MH-Z19 Class (uses interrupt input as PWM input)
MH_Z19.cpp@0:3c154d34f47e, 2016-08-11 (annotated)
- Committer:
- zeus3110
- Date:
- Thu Aug 11 00:41:23 2016 +0000
- Revision:
- 0:3c154d34f47e
Initial Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zeus3110 | 0:3c154d34f47e | 1 | /* mbed MH-Z19 Library |
zeus3110 | 0:3c154d34f47e | 2 | * Copyright (c) 2016, zeus3110 |
zeus3110 | 0:3c154d34f47e | 3 | * |
zeus3110 | 0:3c154d34f47e | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
zeus3110 | 0:3c154d34f47e | 5 | * of this software and associated documentation files (the "Software"), to deal |
zeus3110 | 0:3c154d34f47e | 6 | * in the Software without restriction, including without limitation the rights |
zeus3110 | 0:3c154d34f47e | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
zeus3110 | 0:3c154d34f47e | 8 | * copies of the Software, and to permit persons to whom the Software is |
zeus3110 | 0:3c154d34f47e | 9 | * furnished to do so, subject to the following conditions: |
zeus3110 | 0:3c154d34f47e | 10 | * |
zeus3110 | 0:3c154d34f47e | 11 | * The above copyright notice and this permission notice shall be included in |
zeus3110 | 0:3c154d34f47e | 12 | * all copies or substantial portions of the Software. |
zeus3110 | 0:3c154d34f47e | 13 | * |
zeus3110 | 0:3c154d34f47e | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
zeus3110 | 0:3c154d34f47e | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
zeus3110 | 0:3c154d34f47e | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
zeus3110 | 0:3c154d34f47e | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
zeus3110 | 0:3c154d34f47e | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
zeus3110 | 0:3c154d34f47e | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
zeus3110 | 0:3c154d34f47e | 20 | * THE SOFTWARE. |
zeus3110 | 0:3c154d34f47e | 21 | */ |
zeus3110 | 0:3c154d34f47e | 22 | |
zeus3110 | 0:3c154d34f47e | 23 | #include "MH_Z19.h" |
zeus3110 | 0:3c154d34f47e | 24 | |
zeus3110 | 0:3c154d34f47e | 25 | MH_Z19::MH_Z19(PinName p) : Port(p) { |
zeus3110 | 0:3c154d34f47e | 26 | Port.rise(this, &MH_Z19::rise); |
zeus3110 | 0:3c154d34f47e | 27 | Port.fall(this, &MH_Z19::fall); |
zeus3110 | 0:3c154d34f47e | 28 | OnWidth=2; |
zeus3110 | 0:3c154d34f47e | 29 | T.start(); |
zeus3110 | 0:3c154d34f47e | 30 | } |
zeus3110 | 0:3c154d34f47e | 31 | |
zeus3110 | 0:3c154d34f47e | 32 | int MH_Z19::ReadCO2PPM() { |
zeus3110 | 0:3c154d34f47e | 33 | |
zeus3110 | 0:3c154d34f47e | 34 | return ((CO2_MAX_PPM)*(OnWidth-2)/(PWM_CYCLE_MS-4)); |
zeus3110 | 0:3c154d34f47e | 35 | |
zeus3110 | 0:3c154d34f47e | 36 | } |
zeus3110 | 0:3c154d34f47e | 37 | |
zeus3110 | 0:3c154d34f47e | 38 | |
zeus3110 | 0:3c154d34f47e | 39 | void MH_Z19::rise() { |
zeus3110 | 0:3c154d34f47e | 40 | T.reset(); |
zeus3110 | 0:3c154d34f47e | 41 | } |
zeus3110 | 0:3c154d34f47e | 42 | |
zeus3110 | 0:3c154d34f47e | 43 | void MH_Z19::fall() { |
zeus3110 | 0:3c154d34f47e | 44 | OnWidth=T.read_ms(); |
zeus3110 | 0:3c154d34f47e | 45 | } |