Dependents:   InjectorValveFourthMicro InjectorValveFourthMicro1 InjectorValveFourthMicro2 ReadingInLines_copy

Fork of PWM-Coil-driver by David Wahl

Committer:
dmwahl
Date:
Thu Jul 20 20:33:16 2017 +0000
Revision:
6:e8395448fc8b
Parent:
4:ca7107dff643
Child:
7:91a84a44c81e
Updated so on() checks to see if the coil is already energized before spiking

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dmwahl 4:ca7107dff643 1 // Begin sample code
dmwahl 4:ca7107dff643 2 /*
dmwahl 4:ca7107dff643 3 #include "mbed.h"
dmwahl 4:ca7107dff643 4 #include "rtos.h"
dmwahl 4:ca7107dff643 5 #include "coil-driver.h"
dmwahl 4:ca7107dff643 6 // Coil parameters [control pin, spike time (us), hold time period (us), hold time pulse width (us)]
dmwahl 4:ca7107dff643 7 Coil orange(A0, 500, 40, 3); // Valve driven by pin A0, 500us spike time, 25kHz PWM at 7.5% duty cycle
dmwahl 4:ca7107dff643 8
dmwahl 4:ca7107dff643 9 Coil yellow(A1, 5000, 40, 6); // Valve driven by pin A1, 5ms spike time, 25kHz PWM at 15% duty cycle
dmwahl 4:ca7107dff643 10
dmwahl 4:ca7107dff643 11 int main()
dmwahl 4:ca7107dff643 12 {
dmwahl 4:ca7107dff643 13 while (true) {
dmwahl 4:ca7107dff643 14 orange.on();
dmwahl 4:ca7107dff643 15 yellow.on();
dmwahl 4:ca7107dff643 16 Thread::wait(500);
dmwahl 4:ca7107dff643 17 orange.off();
dmwahl 4:ca7107dff643 18 yellow.off();
dmwahl 4:ca7107dff643 19 Thread::wait(500);
dmwahl 4:ca7107dff643 20 }
dmwahl 4:ca7107dff643 21 }
dmwahl 4:ca7107dff643 22 */
dmwahl 4:ca7107dff643 23 // End sample code
dmwahl 4:ca7107dff643 24
dmwahl 2:6df05ae10a1a 25 #include "coil-driver.h"
dmwahl 0:eb16d6847d9e 26
dmwahl 0:eb16d6847d9e 27 // Default constructor
dmwahl 3:0b1f41462b97 28 Coil::Coil(PinName _controlPin, uint16_t _spikeus, uint16_t _holdperiod_us, uint16_t _holdpulse_us)
dmwahl 0:eb16d6847d9e 29 : controlOut(_controlPin),
dmwahl 0:eb16d6847d9e 30 spikeus(_spikeus),
dmwahl 0:eb16d6847d9e 31 holdperiod_us(_holdperiod_us),
dmwahl 0:eb16d6847d9e 32 holdpulse_us(_holdpulse_us)
dmwahl 0:eb16d6847d9e 33 {
dmwahl 3:0b1f41462b97 34 controlOut.write(0.0); // Ensure coil output is off by default
dmwahl 0:eb16d6847d9e 35 controlOut.period_us(holdperiod_us); // PWM control period (uint16_t microseconds)
dmwahl 0:eb16d6847d9e 36
dmwahl 0:eb16d6847d9e 37 };
dmwahl 0:eb16d6847d9e 38
dmwahl 6:e8395448fc8b 39 char Coil::status()
dmwahl 6:e8395448fc8b 40 {
dmwahl 6:e8395448fc8b 41 char status;
dmwahl 6:e8395448fc8b 42 if(controlOut.read() == 0) {
dmwahl 6:e8395448fc8b 43 status = 0;
dmwahl 6:e8395448fc8b 44 } else {
dmwahl 6:e8395448fc8b 45 status = 1;
dmwahl 6:e8395448fc8b 46 }
dmwahl 6:e8395448fc8b 47 }
dmwahl 6:e8395448fc8b 48
dmwahl 3:0b1f41462b97 49 void Coil::on()
dmwahl 0:eb16d6847d9e 50 {
dmwahl 6:e8395448fc8b 51 if(controlOut.read() == 0.0) {
dmwahl 6:e8395448fc8b 52 controlOut.write(1.0);
dmwahl 6:e8395448fc8b 53 wait_us(spikeus);
dmwahl 6:e8395448fc8b 54 controlOut.pulsewidth_us(holdpulse_us);
dmwahl 6:e8395448fc8b 55 }
dmwahl 0:eb16d6847d9e 56 }
dmwahl 0:eb16d6847d9e 57
dmwahl 3:0b1f41462b97 58 void Coil::off()
dmwahl 0:eb16d6847d9e 59 {
dmwahl 0:eb16d6847d9e 60 controlOut.write(0.0);
dmwahl 0:eb16d6847d9e 61 }