Function generator

Dependencies:   Serial_HL mbed

Committer:
Lenschinki
Date:
Tue May 14 12:16:55 2019 +0000
Revision:
3:c8c3b755ef54
Parent:
2:100244cef3d6
vomit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hollegha2 2:100244cef3d6 1
hollegha2 2:100244cef3d6 2 #include "FuncGen.h"
Lenschinki 3:c8c3b755ef54 3 #include "mbed.h"
hollegha2 2:100244cef3d6 4
hollegha2 2:100244cef3d6 5 // Konstruktor
hollegha2 2:100244cef3d6 6 SignedRampGen::SignedRampGen()
hollegha2 2:100244cef3d6 7 {
hollegha2 2:100244cef3d6 8 val=0;
hollegha2 2:100244cef3d6 9 SetPointsPerPeriod(10);
hollegha2 2:100244cef3d6 10 }
hollegha2 2:100244cef3d6 11
hollegha2 2:100244cef3d6 12 void SignedRampGen::SetPointsPerPeriod(int aPoints)
hollegha2 2:100244cef3d6 13 {
hollegha2 2:100244cef3d6 14 _inc = 2.0/aPoints;
hollegha2 2:100244cef3d6 15 }
hollegha2 2:100244cef3d6 16
hollegha2 2:100244cef3d6 17 void SignedRampGen::SetFrequ(float aFrequ)
hollegha2 2:100244cef3d6 18 {
hollegha2 2:100244cef3d6 19 SetPointsPerPeriod(1.0/aFrequ);
hollegha2 2:100244cef3d6 20 }
hollegha2 2:100244cef3d6 21
hollegha2 2:100244cef3d6 22 void SignedRampGen::CalcOneStep()
hollegha2 2:100244cef3d6 23 {
hollegha2 2:100244cef3d6 24 val = val + _inc;
hollegha2 2:100244cef3d6 25 if( val>=1.0 )
hollegha2 2:100244cef3d6 26 val = -1 + (val - 1.0);
hollegha2 2:100244cef3d6 27 }
hollegha2 2:100244cef3d6 28
hollegha2 2:100244cef3d6 29
hollegha2 2:100244cef3d6 30
hollegha2 2:100244cef3d6 31 TriangleGen::TriangleGen()
hollegha2 2:100244cef3d6 32 {
hollegha2 2:100244cef3d6 33 val=0;
hollegha2 2:100244cef3d6 34 _state=1;
hollegha2 2:100244cef3d6 35 SetPointsPerPeriod(10);
hollegha2 2:100244cef3d6 36 }
hollegha2 2:100244cef3d6 37
hollegha2 2:100244cef3d6 38 void TriangleGen::SetPointsPerPeriod(int aPoints)
hollegha2 2:100244cef3d6 39 {
hollegha2 2:100244cef3d6 40 _inc = 4.0/aPoints;
hollegha2 2:100244cef3d6 41 }
hollegha2 2:100244cef3d6 42
hollegha2 2:100244cef3d6 43 void TriangleGen::SetFrequ(float aFrequ)
hollegha2 2:100244cef3d6 44 {
hollegha2 2:100244cef3d6 45 SetPointsPerPeriod(1.0/aFrequ);
hollegha2 2:100244cef3d6 46 }
hollegha2 2:100244cef3d6 47
hollegha2 2:100244cef3d6 48 void TriangleGen::CalcOneStep()
hollegha2 2:100244cef3d6 49 {
hollegha2 2:100244cef3d6 50 if( _state==1 ) {
hollegha2 2:100244cef3d6 51 val = val + _inc;
hollegha2 2:100244cef3d6 52 if( val>1.0 ) {
hollegha2 2:100244cef3d6 53 val = val - _inc;
hollegha2 2:100244cef3d6 54 _state = 2;
hollegha2 2:100244cef3d6 55 }
hollegha2 2:100244cef3d6 56 } else if( _state==2 ) {
hollegha2 2:100244cef3d6 57 val = val - _inc;
hollegha2 2:100244cef3d6 58 if( val<-1.0 ) {
hollegha2 2:100244cef3d6 59 val = val + _inc;
hollegha2 2:100244cef3d6 60 _state = 1;
hollegha2 2:100244cef3d6 61 }
hollegha2 2:100244cef3d6 62 }
hollegha2 2:100244cef3d6 63 }
hollegha2 2:100244cef3d6 64
hollegha2 2:100244cef3d6 65
hollegha2 2:100244cef3d6 66
hollegha2 2:100244cef3d6 67 RectGen::RectGen()
hollegha2 2:100244cef3d6 68 {
hollegha2 2:100244cef3d6 69 val=0;
hollegha2 2:100244cef3d6 70 _inc=_phase=0;
hollegha2 2:100244cef3d6 71 SetPointsPerPeriod(10);
Lenschinki 3:c8c3b755ef54 72 _thres = 0.5;
hollegha2 2:100244cef3d6 73 }
hollegha2 2:100244cef3d6 74
hollegha2 2:100244cef3d6 75 void RectGen::SetPointsPerPeriod(int aPoints)
hollegha2 2:100244cef3d6 76 {
Lenschinki 3:c8c3b755ef54 77 _inc = 2.0/aPoints;
hollegha2 2:100244cef3d6 78 }
hollegha2 2:100244cef3d6 79
hollegha2 2:100244cef3d6 80 void RectGen::SetFrequ(float aFrequ)
hollegha2 2:100244cef3d6 81 {
hollegha2 2:100244cef3d6 82 SetPointsPerPeriod(1.0/aFrequ);
hollegha2 2:100244cef3d6 83 }
Lenschinki 3:c8c3b755ef54 84 void RectGen::SetPulsWidth(float aPercent)
Lenschinki 3:c8c3b755ef54 85 {
Lenschinki 3:c8c3b755ef54 86 _thres = aPercent;
Lenschinki 3:c8c3b755ef54 87 }
hollegha2 2:100244cef3d6 88
hollegha2 2:100244cef3d6 89 void RectGen::CalcOneStep()
hollegha2 2:100244cef3d6 90 {
hollegha2 2:100244cef3d6 91 _phase += _inc;
hollegha2 2:100244cef3d6 92 if( _phase>=1.0 )
hollegha2 2:100244cef3d6 93 _phase = _phase - 1.0;
Lenschinki 3:c8c3b755ef54 94 if( _phase>_thres )
hollegha2 2:100244cef3d6 95 val = 1.0;
hollegha2 2:100244cef3d6 96 else
hollegha2 2:100244cef3d6 97 val = -1.0;
hollegha2 2:100244cef3d6 98 }
hollegha2 2:100244cef3d6 99
Lenschinki 3:c8c3b755ef54 100 SinusGen::SinusGen()
Lenschinki 3:c8c3b755ef54 101 {
Lenschinki 3:c8c3b755ef54 102 val = 0;
Lenschinki 3:c8c3b755ef54 103 _inc = 1;
Lenschinki 3:c8c3b755ef54 104 entry = 0;
Lenschinki 3:c8c3b755ef54 105 for(int i = 0; i < 1000; i++)
Lenschinki 3:c8c3b755ef54 106 {
Lenschinki 3:c8c3b755ef54 107 sinWave[i] = sin(((3.1415*2)/1000)*i);
Lenschinki 3:c8c3b755ef54 108 }
Lenschinki 3:c8c3b755ef54 109 }
hollegha2 2:100244cef3d6 110
Lenschinki 3:c8c3b755ef54 111 void SinusGen::SetFrequ(float aFrequ)
Lenschinki 3:c8c3b755ef54 112 {
Lenschinki 3:c8c3b755ef54 113 int freq = 22000 * aFrequ;
Lenschinki 3:c8c3b755ef54 114 _inc = 1000 * freq/22000;
Lenschinki 3:c8c3b755ef54 115 }
Lenschinki 3:c8c3b755ef54 116
Lenschinki 3:c8c3b755ef54 117 void SinusGen::CalcOneStep()
Lenschinki 3:c8c3b755ef54 118 {
Lenschinki 3:c8c3b755ef54 119 val = sinWave[(int)entry];
Lenschinki 3:c8c3b755ef54 120 entry = entry + _inc;
Lenschinki 3:c8c3b755ef54 121 if(entry > 1000)
Lenschinki 3:c8c3b755ef54 122 entry = entry - 1000;
Lenschinki 3:c8c3b755ef54 123 }
Lenschinki 3:c8c3b755ef54 124
Lenschinki 3:c8c3b755ef54 125