Function Generator for TINF 2021

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FuncGenMain_21.cpp Source File

FuncGenMain_21.cpp

00001 #include "mbed.h"
00002 #include "Serial_HL.h"
00003 #include "FuncGenFSST.h"
00004 
00005 SerialBLK pc(USBTX, USBRX);
00006 SvProtocol ua0(&pc);
00007 
00008 SignedRampGen fg1;
00009 float ampl1 = 1.0;
00010 float v1 = 0; // ausgang nach der Amplitude
00011 
00012 void CommandHandler();
00013 
00014 void ExecSignalChain();
00015 
00016 Ticker tc;
00017 
00018 AnalogOut dac1(PA_5);
00019 
00020 int main(void)
00021 {
00022     pc.format(8,SerialBLK::None,1);
00023     pc.baud(115200); // 115200
00024     ua0.SvMessage("FuncGen"); // Meldung zum PC senden
00025     
00026     // tc.attach_us(&ExecSignalChain, 100); // Mit timer-Interrupt für 10khz audio
00027     
00028     Timer stw; stw.start();
00029     while(1) {
00030         
00031     ua0.SvMessage("Communicating"); // send test message
00032     ThisThread::sleep_for(1000);
00033     
00034         CommandHandler();
00035         if( stw.read_us()>10000 ) { // 100Hz
00036             stw.reset();
00037             ExecSignalChain(); // zur simulation mit 100Hz aufrufen
00038             if( ua0.acqON ) {
00039                 ua0.WriteSV(1, v1);
00040             }
00041         }
00042     }
00043     return 1;
00044 }
00045 
00046 void ExecSignalChain()
00047 {
00048     fg1.CalcOneStep();
00049     v1 = ampl1*fg1.val;
00050     // Achtung skalierung und Übersteuerung bedenken
00051     // dac1.write( 0.5 + 0.5*v1);
00052 }
00053 
00054 void CommandHandler()
00055 {
00056     uint8_t cmd;
00057     if( !pc.IsDataAvail() )
00058         return;
00059     
00060     cmd = ua0.GetCommand();
00061     
00062     if( cmd==2 )
00063     {
00064         fg1.SetFrequ(ua0.ReadF());
00065         ua0.SvMessage("Set Frequ");
00066     }
00067     
00068     if( cmd==3 )
00069     {
00070         ampl1=ua0.ReadF();
00071         ua0.SvMessage("Set Ampl.");
00072     }
00073 }