This library generate four classic waves, have an escape button to stop to generate wave. And is possible to use function for calculate period,... separately
Diff: WaveGen.cpp
- Revision:
- 0:c1150498cbe9
- Child:
- 1:cb84b066ab29
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/WaveGen.cpp Fri Jul 24 13:38:10 2015 +0000
@@ -0,0 +1,146 @@
+#include "mbed.h"
+#include "WaveGen.h"
+
+using namespace mbed;
+
+WaveGen::WaveGen(PinName ext,PinName WaveForm):_ext(ext),_WaveForm(WaveForm)
+{
+ _ext.mode(PullUp);
+ _WaveForm.write(0);
+}
+
+int WaveGen::TCalc(int div,float Freq)
+{
+ float temp = 0;
+ if(div == 0)
+ {
+ temp = 1 / Freq;
+ temp *= 1000000;
+ }
+ else
+ {
+ temp = 1 / Freq;
+ temp *= 1000000;
+ temp /= div;
+ }
+ return (int)temp;
+}
+
+float WaveGen::VCalc(float Volt)
+{
+ float temp = Volt / 3.30;
+ temp /= 2;
+ return temp;
+}
+
+float WaveGen::RadCalc(float Ang)
+{
+ float temp = 0;
+ temp = Ang * 0.0174532925;
+ return temp;
+}
+
+bool WaveGen::SineWave(float Frequency,float Amplitude)
+{
+ float temp = 0;
+ int T = TCalc(20,Frequency);
+ float V = VCalc(Amplitude) * 2;
+ while(1)
+ {
+ for(int i = 0; i < 360; i += 18)
+ {
+ temp = 0.5 + V * sin(RadCalc((float)i));
+ _WaveForm = temp;
+ wait_us(T);
+ if(!_ext.read())
+ return true;
+ else
+ return false;
+ }
+ }
+}
+
+bool WaveGen::SquareWave(float Frequency,float Amplitude,float DutyCycle)
+{
+ int Ton = TCalc(0,Frequency);
+ Ton *= DutyCycle;
+ int Toff = Ton - TCalc(0,Frequency);
+ float V = VCalc(Amplitude);
+ while(1)
+ {
+ _WaveForm = V;
+ wait_us(Ton);
+ _WaveForm = 0;
+ wait_us(Toff);
+ if(!_ext.read())
+ return true;
+ }
+}
+
+bool WaveGen::TriangularWave(float Frequency,float Amplitude)
+{
+ float temp = 0;
+ int T = TCalc(80,Frequency);
+ float V = VCalc(Amplitude);
+ while(1)
+ {
+ for(int a = 50; a < V; a += 5)
+ {
+ temp = (float)a / 100;
+ _WaveForm = temp;
+ wait_us(T);
+ if(!_ext.read())
+ return true;
+ }
+ for(int b = V; b > 50; b -= 5)
+ {
+ temp = (float)b / 100;
+ _WaveForm = temp;
+ wait_us(T);
+ if(!_ext.read())
+ return true;
+ }
+ for(int c = 50; c < V - 50; c -= 5)
+ {
+ temp = (float)c / 100;
+ _WaveForm = temp;
+ wait_us(T);
+ if(!_ext.read())
+ return true;
+ }
+ for(int d = V - 50; d < 50; d += 5)
+ {
+ temp = (float)d / 100;
+ _WaveForm = temp;
+ wait_us(T);
+ if(!_ext.read())
+ return true;
+ }
+ }
+}
+
+bool WaveGen::SawToothWave(float Frequency,float Amplitude)
+{
+ float temp = 0;
+ int T = TCalc(40,Frequency);
+ float V = VCalc(Amplitude);
+ while(1)
+ {
+ for(int a = 50; a < V; a += 5)
+ {
+ temp = (float)a / 100;
+ _WaveForm = temp;
+ wait_us(T);
+ if(!_ext.read())
+ return true;
+ }
+ for(int b = 50; b > V - 50; b -= 5)
+ {
+ temp = (float)b / 100;
+ _WaveForm = temp;
+ wait_us(T);
+ if(!_ext.read())
+ return true;
+ }
+ }
+}