Junaid Ahmed
/
202A_HW1_ADCDAC
Program to generate sine wave via adc
Diff: sine-wave.cpp
- Revision:
- 0:66a0740d3ac0
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sine-wave.cpp Sat Feb 15 01:02:50 2014 +0000 @@ -0,0 +1,56 @@ +#include "mbed.h" +#include "AnalogOut.h" +#include "AnalogIn.h" +#include <math.h> + +#define I2C0_SDA PTE25 +#define I2C0_SCL PTE24 +#define MMA8451_I2C_ADDRESS (0x1d<<1) +#define MAG3110_I2C_ADDRESS (0x0E<<1) +#define LIGHT_SENSOR_PIN PTE22 +#define PI 3.141f + +Serial pc_sine(USBTX, USBRX); +Timer timer; + + +void sine_wave() +{ + int t = 0; + float s; + AnalogOut a(PTE30); + while (1) { // sin(2 * PI * f * t / Fs) + s = sin(2 * PI * 1 * t / 100.0); + s = s/2 + .5; + a.write(s); + pc_sine.printf ("%f\n", s); + if (t == 100) + t = 0; + else + t++; + wait(0.01); + } +} + +void sine_wave_read() +{ + AnalogIn a(PTB0); + while(1) { + pc_sine.printf("%f\n", a.read()); + wait(0.01); + } +} + + +int main() { + sine_wave(); + + char c = pc_sine.getc(); + + switch (c) { + case '1':sine_wave(); + break; + case '2': sine_wave_read(); + break; + } +}