Generates a pulses train on the order of micro-seconds.

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?

UserRevisionLine numberNew contents of line
aktk 0:986e03d93263 1 /** Pulse Fenerator cpp file
aktk 0:986e03d93263 2 *
aktk 0:986e03d93263 3 * \file PulseFenerator.cpp
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 0:986e03d93263 7 */
aktk 0:986e03d93263 8 #include "PulseGenerator.h"
aktk 0:986e03d93263 9 PulseGenerator::PulseGenerator():
aktk 0:986e03d93263 10 m_width(200),
aktk 0:986e03d93263 11 m_period(5000)
aktk 0:986e03d93263 12 {
aktk 0:986e03d93263 13 m_clockperiod = GCD(m_width, m_period);
aktk 0:986e03d93263 14 m_rt = (m_period - m_width) / m_clockperiod;
aktk 0:986e03d93263 15 m_ft = m_width / m_clockperiod;
aktk 1:1b5cd3c20187 16 m_wavestatus = false;
aktk 0:986e03d93263 17 }
aktk 0:986e03d93263 18
aktk 0:986e03d93263 19 int8_t PulseGenerator::setWaveform(
aktk 0:986e03d93263 20 const uint16_t arg_pulsewidth,///< required in [us]
aktk 0:986e03d93263 21 const uint16_t arg_pulsefreq ///< required in [Hz] > 15 [Hz]
aktk 0:986e03d93263 22 )
aktk 0:986e03d93263 23 {
aktk 0:986e03d93263 24 uint16_t tmp_width = m_width;
aktk 0:986e03d93263 25 uint16_t tmp_period = m_period;
aktk 0:986e03d93263 26
aktk 0:986e03d93263 27 m_width = arg_pulsewidth;
aktk 0:986e03d93263 28 // freq is required over 15 due to the range of the type of the period
aktk 0:986e03d93263 29 m_period= Period_us(arg_pulsefreq > 15 ? arg_pulsefreq : 16);
aktk 0:986e03d93263 30
aktk 1:1b5cd3c20187 31 if(m_width > m_period / 2) {
aktk 0:986e03d93263 32 m_width = tmp_width;
aktk 0:986e03d93263 33 m_period = tmp_period;
aktk 0:986e03d93263 34 return -1;
aktk 0:986e03d93263 35 }
aktk 0:986e03d93263 36 return 0;
aktk 0:986e03d93263 37 }
aktk 0:986e03d93263 38
aktk 0:986e03d93263 39
aktk 0:986e03d93263 40 void PulseGenerator::generatePulseWave()
aktk 0:986e03d93263 41 {
aktk 0:986e03d93263 42 static uint16_t l_counter = 0;
aktk 0:986e03d93263 43
aktk 0:986e03d93263 44 l_counter++;
aktk 1:1b5cd3c20187 45 if(m_wavestatus == false && l_counter >= m_rt ) {
aktk 0:986e03d93263 46 m_wavestatus = true;
aktk 1:1b5cd3c20187 47 l_counter = 0;
aktk 1:1b5cd3c20187 48 } else
aktk 1:1b5cd3c20187 49 if (m_wavestatus == true && l_counter >= m_ft) {
aktk 0:986e03d93263 50 m_wavestatus = false;
aktk 0:986e03d93263 51 l_counter = 0;
aktk 0:986e03d93263 52 }
aktk 0:986e03d93263 53 }
aktk 0:986e03d93263 54
aktk 0:986e03d93263 55 void PulseGenerator::startPulseWave()
aktk 0:986e03d93263 56 {
aktk 0:986e03d93263 57 m_clockperiod = GCD(m_width, m_period);
aktk 0:986e03d93263 58 m_rt = (m_period - m_width) / m_clockperiod;
aktk 0:986e03d93263 59 m_ft = m_width / m_clockperiod;
aktk 0:986e03d93263 60 m_ticker.attach_us(callback(this, &PulseGenerator::generatePulseWave), m_clockperiod);
aktk 0:986e03d93263 61 }
aktk 0:986e03d93263 62
aktk 0:986e03d93263 63 void PulseGenerator::stopPulseWave()
aktk 0:986e03d93263 64 {
aktk 1:1b5cd3c20187 65 m_wavestatus = false;
aktk 0:986e03d93263 66 m_ticker.detach();
aktk 0:986e03d93263 67 }
aktk 0:986e03d93263 68
aktk 0:986e03d93263 69 uint16_t Period_us(const uint16_t arg_freq)
aktk 0:986e03d93263 70 {
aktk 0:986e03d93263 71 uint16_t l_period, l_carried;
aktk 0:986e03d93263 72
aktk 0:986e03d93263 73 // culc integer part of 1/freq * 10^6
aktk 0:986e03d93263 74 l_period = (uint16_t)(1000000 / arg_freq);
aktk 0:986e03d93263 75
aktk 0:986e03d93263 76 // round factorial part of 1/freq * 10^6
aktk 1:1b5cd3c20187 77 l_carried = (uint16_t)(((1000000 % arg_freq) > (arg_freq / 2)) ? 1 : 0);
aktk 0:986e03d93263 78
aktk 0:986e03d93263 79 return l_period + l_carried;
aktk 0:986e03d93263 80 }
aktk 0:986e03d93263 81
aktk 0:986e03d93263 82 uint16_t GCD(uint16_t arg1, uint16_t arg2)
aktk 0:986e03d93263 83 {
aktk 0:986e03d93263 84 uint16_t a,b,r;
aktk 0:986e03d93263 85
aktk 0:986e03d93263 86 if (arg1 == 0 || arg2 == 0) return 0;
aktk 0:986e03d93263 87 if(arg1 > arg2) {
aktk 0:986e03d93263 88 a = arg1;
aktk 0:986e03d93263 89 b = arg2;
aktk 0:986e03d93263 90 } else {
aktk 0:986e03d93263 91 a = arg2;
aktk 0:986e03d93263 92 b = arg1;
aktk 0:986e03d93263 93 }
aktk 0:986e03d93263 94
aktk 0:986e03d93263 95 //Eukleides Algorithm
aktk 0:986e03d93263 96 r = a % b;
aktk 0:986e03d93263 97 while(r != 0) {
aktk 0:986e03d93263 98 a = b;
aktk 0:986e03d93263 99 b = r;
aktk 0:986e03d93263 100 r = a % b;
aktk 0:986e03d93263 101 }
aktk 0:986e03d93263 102
aktk 0:986e03d93263 103 return b;
aktk 0:986e03d93263 104 }