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

Files at this revision

API Documentation at this revision

Comitter:
fangoman91
Date:
Fri Jul 24 13:38:10 2015 +0000
Child:
1:cb84b066ab29
Commit message:
wave generating library with escape button

Changed in this revision

WaveGen.cpp Show annotated file Show diff for this revision Revisions of this file
WaveGen.h Show annotated file Show diff for this revision Revisions of this file
--- /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;
+        }    
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WaveGen.h	Fri Jul 24 13:38:10 2015 +0000
@@ -0,0 +1,25 @@
+#ifndef WAVEGEN_H
+#define WAVEGEN_H
+
+#include "mbed.h"
+
+namespace mbed
+{
+    class WaveGen
+    {
+        public:
+            WaveGen(PinName ext, PinName WaveForm);
+            bool SineWave(float Frequency, float Amplitude);
+            bool SquareWave(float Frequency, float Amplitude, float DutyCycle);
+            bool TriangularWave(float Frequency, float Amplitude);
+            bool SawToothWave(float Frequency, float Amplitude);
+            int TCalc(int div, float Freq);
+            float VCalc(float Volt);
+            float RadCalc(float Ang);
+        protected:
+            DigitalIn _ext;
+            AnalogOut _WaveForm;    
+    };    
+}
+
+#endif
\ No newline at end of file