Function generator

Dependencies:   Serial_HL mbed

Files at this revision

API Documentation at this revision

Comitter:
Lenschinki
Date:
Tue May 14 12:16:55 2019 +0000
Parent:
2:100244cef3d6
Commit message:
vomit

Changed in this revision

FuncGen.cpp Show annotated file Show diff for this revision Revisions of this file
FuncGen.h Show annotated file Show diff for this revision Revisions of this file
FuncGenMain.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/FuncGen.cpp	Thu Mar 17 13:45:13 2016 +0000
+++ b/FuncGen.cpp	Tue May 14 12:16:55 2019 +0000
@@ -1,5 +1,6 @@
 
 #include "FuncGen.h"
+#include "mbed.h"
 
 // Konstruktor
 SignedRampGen::SignedRampGen()
@@ -68,27 +69,57 @@
     val=0;
     _inc=_phase=0;
     SetPointsPerPeriod(10);
+    _thres = 0.5;
 }
 
 void RectGen::SetPointsPerPeriod(int aPoints)
 {
-    _inc = 1.0/aPoints;
+    _inc = 2.0/aPoints;
 }
 
 void RectGen::SetFrequ(float aFrequ)
 {
     SetPointsPerPeriod(1.0/aFrequ);
 }
+void RectGen::SetPulsWidth(float aPercent)
+{
+    _thres = aPercent;
+}
 
 void RectGen::CalcOneStep()
 {
     _phase += _inc;
     if( _phase>=1.0 )
         _phase = _phase - 1.0;
-    if( _phase>0.5 )
+    if( _phase>_thres )
         val = 1.0;
     else
         val = -1.0;
 }
 
+SinusGen::SinusGen()
+{
+    val = 0;
+    _inc = 1;
+    entry = 0;
+    for(int i = 0; i < 1000; i++)
+    {
+        sinWave[i] = sin(((3.1415*2)/1000)*i); 
+    }
+}       
 
+void SinusGen::SetFrequ(float aFrequ)
+{
+    int freq = 22000 * aFrequ;
+    _inc = 1000 * freq/22000;
+}
+
+void SinusGen::CalcOneStep()
+{
+    val = sinWave[(int)entry];
+    entry = entry + _inc;
+    if(entry > 1000)
+        entry = entry - 1000;
+}
+
+
--- a/FuncGen.h	Thu Mar 17 13:45:13 2016 +0000
+++ b/FuncGen.h	Tue May 14 12:16:55 2019 +0000
@@ -42,6 +42,22 @@
     void CalcOneStep();
 };
 
+class SinusGen
+{
+    public:
+        float val; // momentaner Ausgangswert
+    private:
+        float sinWave[1000];
+        float _inc;
+        float entry;
+    public:
+        SinusGen();
+        
+        void SetFrequ(float aFrequ);
+        
+        void CalcOneStep();        
+};
+
 
 class RectGen
 {
@@ -50,6 +66,7 @@
 private:
     float _inc;
     float _phase;
+    float _thres;
 public:
     RectGen();
 
--- a/FuncGenMain.cpp	Thu Mar 17 13:45:13 2016 +0000
+++ b/FuncGenMain.cpp	Tue May 14 12:16:55 2019 +0000
@@ -5,6 +5,7 @@
 
 SerialBLK pc(USBTX, USBRX);
 SvProtocol ua0(&pc);
+AnalogOut dacout(p18);
 
 // BusOut leds(LED1,LED2,LED3,LED4); Bertl14
 // M0-Board
@@ -12,33 +13,32 @@
 
 void CommandHandler();
 void ExecSignalChain();
+void output();
 
 RectGen fg1;
+SignedRampGen rg1;
+TriangleGen tg1;
+SinusGen sg1;
 
-float ampl1 = 1.0; //
+Ticker ticker;
+
+float ampl1 = 0.5; 
 float v1; // Wert nach dem Koeffizientenglied
+float v2;
+float v3;
+float v4;
+
+int mode = 1;
 
 int main(void)
 {
     pc.format(8,SerialBLK::None,1);
-    pc.baud(500000); // 115200
+    pc.baud(115200); // 115200
     leds = 9;
-    ua0.SvMessage("FuncGenMain"); // Meldung zum PC senden
-
-    Timer stw;
-    stw.start();
+    ua0.SvMessage("FuncGenMain"); // Meldung zum PC senden    
+    ticker.attach_us(&output,45);
     while(1) {
         CommandHandler();
-        if( (stw.read_ms()>10) ) { // 100Hz
-            // dieser Teil wird mit 10Hz aufgerufen
-            stw.reset();
-            ExecSignalChain();
-            if( ua0.acqON ) {
-                // nur wenn vom PC aus das Senden eingeschaltet wurde
-                // wird auch etwas gesendet
-                ua0.WriteSV(1, v1);
-            }
-        }
     }
     return 1;
 }
@@ -49,7 +49,28 @@
 void ExecSignalChain()
 {
     fg1.CalcOneStep();
-    v1 = ampl1 * fg1.val; // Koeffizientenglied rechnen
+    v1 = 0.5 + ampl1 * fg1.val; // Koeffizientenglied rechnen 
+    
+    rg1.CalcOneStep();
+    v2 = 0.5 + ampl1 * rg1.val;
+    
+    tg1.CalcOneStep();
+    v3 = 0.5 + ampl1 * tg1.val;
+    
+    sg1.CalcOneStep();
+    v4 = 0.5 + ampl1 * sg1.val;
+}
+void output()
+{
+    ExecSignalChain();
+    if(mode==1)
+        dacout = v1;
+    else if(mode==2)
+        dacout = v2;
+    else if(mode==3)
+        dacout = v3;
+    else
+        dacout = v4;
 }
 
 void CommandHandler()
@@ -67,13 +88,23 @@
     if( cmd==2 ) {
         float frequ = ua0.ReadF();
         fg1.SetFrequ(frequ);
+        rg1.SetFrequ(frequ);
+        tg1.SetFrequ(frequ);   
+        sg1.SetFrequ(frequ); 
         ua0.SvMessage("Set Frequ");
     }
-    // mit dem Command=3 die Amplitude verstellen
     if( cmd==3 ) {
-        ampl1 = ua0.ReadF();
-        ua0.SvMessage("Set Ampl");
+        int points = ua0.ReadI16();
+        fg1.SetPointsPerPeriod(points);
+        rg1.SetPointsPerPeriod(points);
+        tg1.SetPointsPerPeriod(points);   
+        ua0.SvMessage("Set Points");
+    }   
+    if( cmd ==4)
+    {
+        mode = ua0.ReadI16();
     }
+    
 }