Generates a pulses train on the order of micro-seconds.
PulseGenerator.h@2:583504323980, 2019-07-02 (annotated)
- Committer:
- aktk
- Date:
- Tue Jul 02 09:41:01 2019 +0000
- Revision:
- 2:583504323980
- Parent:
- 1:1b5cd3c20187
version 1.1.2019.july
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aktk | 0:986e03d93263 | 1 | /** Pulse Fenerator header file |
aktk | 0:986e03d93263 | 2 | * |
aktk | 0:986e03d93263 | 3 | * \file PulseFenerator.h |
aktk | 0:986e03d93263 | 4 | * \author Akifumi Takahashi |
aktk | 0:986e03d93263 | 5 | * \date 2018.nov.08- |
aktk | 0:986e03d93263 | 6 | * \version 1.0.2018.nov |
aktk | 2:583504323980 | 7 | * \version 1.1.2019.july |
aktk | 0:986e03d93263 | 8 | */ |
aktk | 0:986e03d93263 | 9 | #ifndef PULSEGENERATOR_H |
aktk | 0:986e03d93263 | 10 | #define PULSEGENERATOR_H |
aktk | 0:986e03d93263 | 11 | #include "mbed.h" |
aktk | 1:1b5cd3c20187 | 12 | |
aktk | 0:986e03d93263 | 13 | /** \class PulseGenerator |
aktk | 0:986e03d93263 | 14 | * A class which generate us-order Pulse wave |
aktk | 0:986e03d93263 | 15 | * |
aktk | 0:986e03d93263 | 16 | * This generates continual pulses which can be used for such as clock |
aktk | 0:986e03d93263 | 17 | * for another continual pulses having same pulse width and frequency and |
aktk | 0:986e03d93263 | 18 | * another pulse hight. |
aktk | 0:986e03d93263 | 19 | * |
aktk | 0:986e03d93263 | 20 | * start________|--|________|--|________|--|________|--|_____...stop |
aktk | 0:986e03d93263 | 21 | */ |
aktk | 1:1b5cd3c20187 | 22 | |
aktk | 0:986e03d93263 | 23 | class PulseGenerator |
aktk | 0:986e03d93263 | 24 | { |
aktk | 1:1b5cd3c20187 | 25 | public://----------------------------------------------------------------------- |
aktk | 0:986e03d93263 | 26 | /** Default constructor |
aktk | 0:986e03d93263 | 27 | * |
aktk | 1:1b5cd3c20187 | 28 | * As default, the parameters of pulse form are set as followng. |
aktk | 0:986e03d93263 | 29 | * - pulse width = 200 [us] |
aktk | 0:986e03d93263 | 30 | * - pulse period = 5000 [us] (refering to freq = 200 [Hz]) |
aktk | 0:986e03d93263 | 31 | */ |
aktk | 0:986e03d93263 | 32 | PulseGenerator(); |
aktk | 0:986e03d93263 | 33 | |
aktk | 0:986e03d93263 | 34 | /** Function to set pulse width and culc period from given frequency |
aktk | 0:986e03d93263 | 35 | * |
aktk | 0:986e03d93263 | 36 | * This function set a value m_clockduration, m_rt, and m_ft. |
aktk | 0:986e03d93263 | 37 | * The clock is culcurated from pulse width and period (= 1/frequency) |
aktk | 0:986e03d93263 | 38 | * using Eucledean Algorithm. |
aktk | 0:986e03d93263 | 39 | */ |
aktk | 0:986e03d93263 | 40 | int8_t setWaveform( |
aktk | 1:1b5cd3c20187 | 41 | const uint16_t arg_pulseWidth,///< required in [us] |
aktk | 1:1b5cd3c20187 | 42 | const uint16_t arg_pulseFreq ///< required in [Hz] bigger than 15 Hz |
aktk | 0:986e03d93263 | 43 | ); |
aktk | 0:986e03d93263 | 44 | |
aktk | 0:986e03d93263 | 45 | /** Function to start/resume generating Pulse wave |
aktk | 0:986e03d93263 | 46 | * |
aktk | 1:1b5cd3c20187 | 47 | * This function culculates a base clock freq (e.g. pulse periodand) |
aktk | 1:1b5cd3c20187 | 48 | * and a pulse rising/falling timing in the clock with which to generate |
aktk | 1:1b5cd3c20187 | 49 | * pulse wave. |
aktk | 1:1b5cd3c20187 | 50 | * And then exe TICKER.attach(). |
aktk | 0:986e03d93263 | 51 | */ |
aktk | 0:986e03d93263 | 52 | void startPulseWave(); |
aktk | 0:986e03d93263 | 53 | |
aktk | 1:1b5cd3c20187 | 54 | /** Function to stop/pose generating pulse wave |
aktk | 0:986e03d93263 | 55 | * |
aktk | 0:986e03d93263 | 56 | * This function exe TICKER.detach(). |
aktk | 0:986e03d93263 | 57 | */ |
aktk | 0:986e03d93263 | 58 | void stopPulseWave(); |
aktk | 0:986e03d93263 | 59 | |
aktk | 0:986e03d93263 | 60 | /** Function to refer whther the pulse wave is H xor L |
aktk | 0:986e03d93263 | 61 | * \retval (bool) H between pulse rising and then falling |
aktk | 0:986e03d93263 | 62 | * \retval (bool) L between pulse falling and then rising |
aktk | 0:986e03d93263 | 63 | */ |
aktk | 0:986e03d93263 | 64 | bool getState(); |
aktk | 1:1b5cd3c20187 | 65 | |
aktk | 1:1b5cd3c20187 | 66 | private://---------------------------------------------------------------------- |
aktk | 1:1b5cd3c20187 | 67 | uint16_t m_width; ///< Defalt: 200 [us]; Set in setWaveform() |
aktk | 1:1b5cd3c20187 | 68 | uint16_t m_period; ///< culcurated from frequency given with setWaveform() |
aktk | 0:986e03d93263 | 69 | bool m_wavestatus; |
aktk | 1:1b5cd3c20187 | 70 | uint16_t m_clockperiod; ///< base clock's period |
aktk | 0:986e03d93263 | 71 | uint16_t m_rt; ///< Pulse rising timing in the clock (What times clock counting is required till a pulse rises) |
aktk | 0:986e03d93263 | 72 | uint16_t m_ft; ///< Pulse falling timing in the clock (What times clock counting is required till a pulse falls) |
aktk | 0:986e03d93263 | 73 | Ticker m_ticker; |
aktk | 0:986e03d93263 | 74 | |
aktk | 1:1b5cd3c20187 | 75 | /// Pulse Generate Hundler |
aktk | 0:986e03d93263 | 76 | void generatePulseWave(); |
aktk | 0:986e03d93263 | 77 | }; |
aktk | 1:1b5cd3c20187 | 78 | |
aktk | 1:1b5cd3c20187 | 79 | // |
aktk | 1:1b5cd3c20187 | 80 | // Inline Implementation |
aktk | 1:1b5cd3c20187 | 81 | // ---------------------------------------------------------------------------- |
aktk | 0:986e03d93263 | 82 | inline bool PulseGenerator::getState() |
aktk | 0:986e03d93263 | 83 | { |
aktk | 0:986e03d93263 | 84 | return m_wavestatus; |
aktk | 0:986e03d93263 | 85 | } |
aktk | 0:986e03d93263 | 86 | |
aktk | 1:1b5cd3c20187 | 87 | // |
aktk | 1:1b5cd3c20187 | 88 | // Auxiliry Function |
aktk | 1:1b5cd3c20187 | 89 | // ---------------------------------------------------------------------------- |
aktk | 0:986e03d93263 | 90 | /** Culcurate Grate Common Divisor by Eukleides' Algorithm |
aktk | 0:986e03d93263 | 91 | * |
aktk | 1:1b5cd3c20187 | 92 | * \param[in] a value included in Natural number excluding 0 |
aktk | 1:1b5cd3c20187 | 93 | * \param[in] a value included in Natural number excluding 0 |
aktk | 0:986e03d93263 | 94 | * \retval GCD |
aktk | 0:986e03d93263 | 95 | */ |
aktk | 0:986e03d93263 | 96 | uint16_t GCD(uint16_t, uint16_t); |
aktk | 1:1b5cd3c20187 | 97 | |
aktk | 1:1b5cd3c20187 | 98 | /** Culcurate Period in [us] from frequency in [Hz] |
aktk | 1:1b5cd3c20187 | 99 | * |
aktk | 1:1b5cd3c20187 | 100 | * \param[in] Frequency [Hz] |
aktk | 1:1b5cd3c20187 | 101 | * \retval Period [us] |
aktk | 1:1b5cd3c20187 | 102 | */ |
aktk | 0:986e03d93263 | 103 | uint16_t Period_us(const uint16_t arg_freq); |
aktk | 0:986e03d93263 | 104 | #endif |