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.
Dependencies: mbed
piezo_bz.cpp
00001 /* 00002 * Mbed library program 00003 * Control Piezo Transducer 00004 * 00005 * Copyright (c) 2018 Kenji Arai / JH1PJL 00006 * http://www.page.sannet.ne.jp/kenjia/index.html 00007 * http://mbed.org/users/kenjiArai/ 00008 * Created: Feburary 28th, 2018 00009 * Revised: March 3rd, 2018 00010 */ 00011 00012 #include "mbed.h" 00013 #include "piezo_bz.h" 00014 00015 PIEZO_BZ::PIEZO_BZ(PinName pin, uint32_t f, uint32_t on_off_time) 00016 : _pin(pin) 00017 { 00018 out = false; 00019 _pin.write(0); 00020 freq = f; 00021 if (freq < 50) { 00022 freq = 50; 00023 } else if (freq > 10000) { 00024 freq = 10000; 00025 } 00026 flag_run = false; 00027 _t0.attach_us(callback(this, &PIEZO_BZ::create_freq_irq), 500000 / freq); 00028 t_onoff = on_off_time; 00029 if (t_onoff == 0) { 00030 flag_continuous = true; 00031 _t1.attach_us( 00032 callback(this, &PIEZO_BZ::create_onoff_irq), 00033 1000000000 // 1000sec 00034 ); 00035 } else { 00036 flag_continuous = false; 00037 _t1.attach_us( 00038 callback(this, &PIEZO_BZ::create_onoff_irq), 00039 t_onoff * 1000 00040 ); 00041 } 00042 } 00043 00044 PIEZO_BZ::~PIEZO_BZ() 00045 { 00046 _t0.detach(); 00047 _t1.detach(); 00048 } 00049 00050 void PIEZO_BZ::start() 00051 { 00052 flag_run = true; 00053 } 00054 00055 void PIEZO_BZ::stop() 00056 { 00057 flag_run = false; 00058 } 00059 00060 void PIEZO_BZ::change_frequency(uint32_t f) 00061 { 00062 _t0.detach(); 00063 freq = f; 00064 if (freq < 300) { 00065 freq = 300; 00066 } else if (freq > 10000) { 00067 freq = 10000; 00068 } 00069 _t0.attach_us(callback(this, &PIEZO_BZ::create_freq_irq), 500000 / freq); 00070 } 00071 00072 void PIEZO_BZ::change_on_off(uint32_t on_off_time) 00073 { 00074 _t1.detach(); 00075 t_onoff = on_off_time; 00076 if (t_onoff == 0) { 00077 flag_continuous = true; 00078 _t1.attach_us( 00079 callback(this, &PIEZO_BZ::create_onoff_irq), 00080 1000000000U // 1000sec 00081 ); 00082 } else { 00083 flag_continuous = false; 00084 _t1.attach_us( 00085 callback(this, &PIEZO_BZ::create_onoff_irq), 00086 t_onoff * 1000U 00087 ); 00088 } 00089 } 00090 00091 void PIEZO_BZ::create_freq_irq(void) 00092 { 00093 // <point A> 00094 if (!flag_run) { 00095 _pin.write(0); 00096 return; 00097 } 00098 if (!flag_continuous) { 00099 if (!flag_onoff) { 00100 _pin.write(0); 00101 return; 00102 } 00103 } 00104 _pin.write(out); 00105 out = !out; 00106 // <point B> 00107 // Processing time from <point A> to <point B> is 200 to 230ns 00108 // measured on Nucleo-F446RE board (System clock 180MHz) 00109 } 00110 00111 void PIEZO_BZ::create_onoff_irq(void) 00112 { 00113 // <point C> 00114 if (flag_continuous) { 00115 flag_onoff = true; 00116 } else { 00117 flag_onoff = !flag_onoff; 00118 } 00119 // <point D> 00120 // Processing time from <point C> to <point D> is 120 to 140ns 00121 // measured on Nucleo-F446RE board (System clock 180MHz) 00122 }
Generated on Thu Aug 25 2022 13:50:54 by
1.7.2