![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Simple code to control 3 or 4 Wire Fans with tacho signal as feedback
main.cpp@0:d111bbbfd9e2, 2019-05-02 (annotated)
- Committer:
- kevinganhito
- Date:
- Thu May 02 10:31:56 2019 +0000
- Revision:
- 0:d111bbbfd9e2
Fan Controller;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevinganhito | 0:d111bbbfd9e2 | 1 | // STM32L432KC |
kevinganhito | 0:d111bbbfd9e2 | 2 | // https://os.mbed.com/platforms/ST-Nucleo-L432KC/ |
kevinganhito | 0:d111bbbfd9e2 | 3 | |
kevinganhito | 0:d111bbbfd9e2 | 4 | |
kevinganhito | 0:d111bbbfd9e2 | 5 | #include "mbed.h" |
kevinganhito | 0:d111bbbfd9e2 | 6 | #include "PwmIn.h" |
kevinganhito | 0:d111bbbfd9e2 | 7 | |
kevinganhito | 0:d111bbbfd9e2 | 8 | #define PWM_FAN_1 D9 |
kevinganhito | 0:d111bbbfd9e2 | 9 | #define TACHO_FAN_1 D11 |
kevinganhito | 0:d111bbbfd9e2 | 10 | |
kevinganhito | 0:d111bbbfd9e2 | 11 | #define PWM_FREQUENCY 25000 |
kevinganhito | 0:d111bbbfd9e2 | 12 | #define DUTY_CYCLE 50 |
kevinganhito | 0:d111bbbfd9e2 | 13 | |
kevinganhito | 0:d111bbbfd9e2 | 14 | PwmOut mypwm1(PWM_FAN_1); |
kevinganhito | 0:d111bbbfd9e2 | 15 | |
kevinganhito | 0:d111bbbfd9e2 | 16 | PwmIn tacho1(TACHO_FAN_1); |
kevinganhito | 0:d111bbbfd9e2 | 17 | |
kevinganhito | 0:d111bbbfd9e2 | 18 | int main() { |
kevinganhito | 0:d111bbbfd9e2 | 19 | |
kevinganhito | 0:d111bbbfd9e2 | 20 | //PWM period |
kevinganhito | 0:d111bbbfd9e2 | 21 | |
kevinganhito | 0:d111bbbfd9e2 | 22 | mypwm1.period_us((int) (PWM_FREQUENCY/25)); |
kevinganhito | 0:d111bbbfd9e2 | 23 | mypwm1.pulsewidth_us((int)(PWM_FREQUENCY*DUTY_CYCLE/100)); // Not lower than 10 us |
kevinganhito | 0:d111bbbfd9e2 | 24 | |
kevinganhito | 0:d111bbbfd9e2 | 25 | printf("PWM Duty is set to %.2f %%\n", mypwm1.read() * 100); |
kevinganhito | 0:d111bbbfd9e2 | 26 | float speed1; |
kevinganhito | 0:d111bbbfd9e2 | 27 | while(1) { |
kevinganhito | 0:d111bbbfd9e2 | 28 | speed1 = tacho1.period() * 1000; |
kevinganhito | 0:d111bbbfd9e2 | 29 | printf("Speed: %f\n",speed1); |
kevinganhito | 0:d111bbbfd9e2 | 30 | } |
kevinganhito | 0:d111bbbfd9e2 | 31 | } |