不韋 呂
/
UIT2_SinGenerator_SinFunction
Sin generator using sinf() function for ST Nucleo F401RE.
Diff: main.cpp
- Revision:
- 0:28696920540e
- Child:
- 1:920c46269ce8
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Oct 21 10:00:13 2014 +0000 @@ -0,0 +1,67 @@ +//-------------------------------------------------------------- +// Sin generator using sinf() function +// Analog Output: MCP4922 using SPI +// 2014/10/20, Copyright (c) 2014 MIKAMI, Naoki +//-------------------------------------------------------------- + +#include "mbed.h" + +#include "ADC_Base.hpp" // for ADC not using interrupt +#include "DAC_MCP4922.hpp" // for DAC MCP4922 +#include "ScfClockTim3.hpp" // for clock supplied to SCF + +using namespace Mikami; + +const int FS_ = 12000; // Sampling frequency: 12 kHz +ADC_Base adc_(A0, FS_); // for AD +DAC_MCP4922 myDac_(DAC_MCP4922::DAC_A); // for DA + +int main() +{ + const float PI = 3.1415926536; + const float PI2 = PI*2; + ScfClockTim3(500000); // cutoff frequency: 5 kHz + + float phi = 0; + float dPhi = PI2*440.0f/(float)FS_; + + while (true) + { + float xn = adc_.Read(); // Dummy read for synchronization to Fs + //----------------------------------------------- + + float yn = (float)(0.8f*sinf(phi)); + phi = phi + dPhi; + if (phi > PI) phi = phi -PI2; + + //----------------------------------------------- + myDac_.Write(yn); // Write to DAC + } +} +/* +// Following version uses double type variable +int main() +{ + const double PI = 3.1415926536; + const double PI2 = PI*2; + ScfClockTim3(500000); // cutoff frequency: 5 kHz + + double phi = 0; + double dPhi = PI2*440.0/(double)FS_; + + while (true) + { + float xn = adc_.Read(); // Dummy read to synchronize Fs + //----------------------------------------------- + dOut_.write(1); + + float yn = (float)(0.8*sin(phi)); + phi = phi + dPhi; + if (phi > PI) phi = phi -PI2; + + dOut_.write(0); + //----------------------------------------------- + myDac_.Write(yn); // Write to DAC + } +} +*/ \ No newline at end of file