Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Nucleo_Une_Mesure Nucleo_LIDAR_PRJ_2018-19
PwmIn.cpp@2:cb7cc6f299eb, 2019-02-20 (annotated)
- Committer:
- Nilox
- Date:
- Wed Feb 20 20:44:54 2019 +0000
- Revision:
- 2:cb7cc6f299eb
- Parent:
- 1:6d68eb9b6bbb
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 1:6d68eb9b6bbb | 1 | /* mbed PwmIn Library |
simon | 1:6d68eb9b6bbb | 2 | * Copyright (c) 2008-2010, sford |
simon | 1:6d68eb9b6bbb | 3 | * |
simon | 1:6d68eb9b6bbb | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
simon | 1:6d68eb9b6bbb | 5 | * of this software and associated documentation files (the "Software"), to deal |
simon | 1:6d68eb9b6bbb | 6 | * in the Software without restriction, including without limitation the rights |
simon | 1:6d68eb9b6bbb | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
simon | 1:6d68eb9b6bbb | 8 | * copies of the Software, and to permit persons to whom the Software is |
simon | 1:6d68eb9b6bbb | 9 | * furnished to do so, subject to the following conditions: |
simon | 1:6d68eb9b6bbb | 10 | * |
simon | 1:6d68eb9b6bbb | 11 | * The above copyright notice and this permission notice shall be included in |
simon | 1:6d68eb9b6bbb | 12 | * all copies or substantial portions of the Software. |
simon | 1:6d68eb9b6bbb | 13 | * |
simon | 1:6d68eb9b6bbb | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
simon | 1:6d68eb9b6bbb | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
simon | 1:6d68eb9b6bbb | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
simon | 1:6d68eb9b6bbb | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
simon | 1:6d68eb9b6bbb | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
simon | 1:6d68eb9b6bbb | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
simon | 1:6d68eb9b6bbb | 20 | * THE SOFTWARE. |
simon | 1:6d68eb9b6bbb | 21 | */ |
simon | 1:6d68eb9b6bbb | 22 | |
simon | 1:6d68eb9b6bbb | 23 | #include "PwmIn.h" |
simon | 1:6d68eb9b6bbb | 24 | |
simon | 1:6d68eb9b6bbb | 25 | PwmIn::PwmIn(PinName p) : _p(p) { |
simon | 1:6d68eb9b6bbb | 26 | _p.rise(this, &PwmIn::rise); |
simon | 1:6d68eb9b6bbb | 27 | _p.fall(this, &PwmIn::fall); |
simon | 1:6d68eb9b6bbb | 28 | _period = 0.0; |
simon | 1:6d68eb9b6bbb | 29 | _pulsewidth = 0.0; |
simon | 1:6d68eb9b6bbb | 30 | _t.start(); |
simon | 1:6d68eb9b6bbb | 31 | } |
simon | 1:6d68eb9b6bbb | 32 | |
simon | 1:6d68eb9b6bbb | 33 | float PwmIn::period() { |
simon | 1:6d68eb9b6bbb | 34 | return _period; |
simon | 1:6d68eb9b6bbb | 35 | } |
simon | 1:6d68eb9b6bbb | 36 | |
simon | 1:6d68eb9b6bbb | 37 | float PwmIn::pulsewidth() { |
simon | 1:6d68eb9b6bbb | 38 | return _pulsewidth; |
simon | 1:6d68eb9b6bbb | 39 | } |
simon | 1:6d68eb9b6bbb | 40 | |
simon | 1:6d68eb9b6bbb | 41 | float PwmIn::dutycycle() { |
simon | 1:6d68eb9b6bbb | 42 | return _pulsewidth / _period; |
simon | 1:6d68eb9b6bbb | 43 | } |
simon | 1:6d68eb9b6bbb | 44 | |
simon | 1:6d68eb9b6bbb | 45 | void PwmIn::rise() { |
Nilox | 2:cb7cc6f299eb | 46 | _period = _t.read_us(); |
simon | 1:6d68eb9b6bbb | 47 | _t.reset(); |
simon | 1:6d68eb9b6bbb | 48 | } |
simon | 1:6d68eb9b6bbb | 49 | |
simon | 1:6d68eb9b6bbb | 50 | void PwmIn::fall() { |
Nilox | 2:cb7cc6f299eb | 51 | _pulsewidth = _t.read_us(); |
simon | 1:6d68eb9b6bbb | 52 | } |