Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
PulseGenerator.cpp
00001 /** Pulse Fenerator cpp file 00002 * 00003 * \file PulseFenerator.cpp 00004 * \author Akifumi Takahashi 00005 * \date 2018.nov.08- 00006 * \version 1.0.2018.nov 00007 */ 00008 #include "PulseGenerator.h" 00009 PulseGenerator::PulseGenerator(): 00010 m_width(200), 00011 m_period(5000) 00012 { 00013 m_clockperiod = GCD(m_width, m_period); 00014 m_rt = (m_period - m_width) / m_clockperiod; 00015 m_ft = m_width / m_clockperiod; 00016 m_wavestatus = false; 00017 } 00018 00019 int8_t PulseGenerator::setWaveform( 00020 const uint16_t arg_pulsewidth,///< required in [us] 00021 const uint16_t arg_pulsefreq ///< required in [Hz] > 15 [Hz] 00022 ) 00023 { 00024 uint16_t tmp_width = m_width; 00025 uint16_t tmp_period = m_period; 00026 00027 m_width = arg_pulsewidth; 00028 // freq is required over 15 due to the range of the type of the period 00029 m_period= Period_us(arg_pulsefreq > 15 ? arg_pulsefreq : 16); 00030 00031 if(m_width > m_period / 2) { 00032 m_width = tmp_width; 00033 m_period = tmp_period; 00034 return -1; 00035 } 00036 return 0; 00037 } 00038 00039 00040 void PulseGenerator::generatePulseWave() 00041 { 00042 static uint16_t l_counter = 0; 00043 00044 l_counter++; 00045 if(m_wavestatus == false && l_counter >= m_rt ) { 00046 m_wavestatus = true; 00047 l_counter = 0; 00048 } else 00049 if (m_wavestatus == true && l_counter >= m_ft) { 00050 m_wavestatus = false; 00051 l_counter = 0; 00052 } 00053 } 00054 00055 void PulseGenerator::startPulseWave() 00056 { 00057 m_clockperiod = GCD(m_width, m_period); 00058 m_rt = (m_period - m_width) / m_clockperiod; 00059 m_ft = m_width / m_clockperiod; 00060 m_ticker.attach_us(callback(this, &PulseGenerator::generatePulseWave), m_clockperiod); 00061 } 00062 00063 void PulseGenerator::stopPulseWave() 00064 { 00065 m_wavestatus = false; 00066 m_ticker.detach(); 00067 } 00068 00069 uint16_t Period_us(const uint16_t arg_freq) 00070 { 00071 uint16_t l_period, l_carried; 00072 00073 // culc integer part of 1/freq * 10^6 00074 l_period = (uint16_t)(1000000 / arg_freq); 00075 00076 // round factorial part of 1/freq * 10^6 00077 l_carried = (uint16_t)(((1000000 % arg_freq) > (arg_freq / 2)) ? 1 : 0); 00078 00079 return l_period + l_carried; 00080 } 00081 00082 uint16_t GCD(uint16_t arg1, uint16_t arg2) 00083 { 00084 uint16_t a,b,r; 00085 00086 if (arg1 == 0 || arg2 == 0) return 0; 00087 if(arg1 > arg2) { 00088 a = arg1; 00089 b = arg2; 00090 } else { 00091 a = arg2; 00092 b = arg1; 00093 } 00094 00095 //Eukleides Algorithm 00096 r = a % b; 00097 while(r != 0) { 00098 a = b; 00099 b = r; 00100 r = a % b; 00101 } 00102 00103 return b; 00104 }
Generated on Tue Jul 12 2022 22:17:32 by
1.7.2