Dependents:   InjectorValveFourthMicro InjectorValveFourthMicro1 InjectorValveFourthMicro2 ReadingInLines_copy

Fork of PWM-Coil-driver by David Wahl

Committer:
dmwahl
Date:
Tue Jun 20 19:48:42 2017 +0000
Revision:
4:ca7107dff643
Parent:
3:0b1f41462b97
Child:
6:e8395448fc8b
Added sample code

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 3:0b1f41462b97 39 void Coil::on()
dmwahl 0:eb16d6847d9e 40 {
dmwahl 0:eb16d6847d9e 41 controlOut.write(1.0);
dmwahl 0:eb16d6847d9e 42 wait_us(spikeus);
dmwahl 0:eb16d6847d9e 43 controlOut.pulsewidth_us(holdpulse_us);
dmwahl 0:eb16d6847d9e 44 }
dmwahl 0:eb16d6847d9e 45
dmwahl 3:0b1f41462b97 46 void Coil::off()
dmwahl 0:eb16d6847d9e 47 {
dmwahl 0:eb16d6847d9e 48 controlOut.write(0.0);
dmwahl 0:eb16d6847d9e 49 }