Control DC fan motors with PWM along with a minimum PWM duty before turning on.
Embed:
(wiki syntax)
Show/hide line numbers
DcFan.cpp
00001 /* Mbed DC fan PWM control. 00002 00003 Copyright 2020 Jonathan L. Martin <jon.martini@gmail.com> 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy of 00006 this software and associated documentation files (the "Software"), to deal in 00007 the Software without restriction, including without limitation the rights to 00008 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 00009 the Software, and to permit persons to whom the Software is furnished to do so, 00010 subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in all 00013 copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00021 SOFTWARE. 00022 00023 This library allows setting a minimum threshold for fans to turn on, 00024 but otherwise uses PWM like any other motor. 00025 00026 */ 00027 00028 #include "DcFan.h" 00029 00030 #include "mbed.h" 00031 00032 DcFan::DcFan(PinName pwm, float min_active_pwm): 00033 _pwm(pwm) { 00034 00035 // Set initial condition of PWM 00036 _pwm.period(0.00005); 00037 _pwm = 0.0; 00038 if(min_active_pwm > 0.0) 00039 { 00040 _min_active_pwm = min_active_pwm; 00041 } else 00042 { 00043 _min_active_pwm = 0.0; 00044 } 00045 } 00046 00047 void DcFan::speed(float speed) { 00048 // Don't allow an invalid speed 00049 if (speed > 1.0) 00050 { 00051 speed = 1.0; 00052 // TODO warning? 00053 } 00054 if (speed < _min_active_pwm) 00055 { 00056 speed = 0.0; 00057 } 00058 _pwm = speed; 00059 }
Generated on Wed Jul 20 2022 07:13:24 by
1.7.2