Generate sine wave, using analog output

Dependencies:   mbed WaveGen

Dependents:   WaveGen

Committer:
aaronice
Date:
Sat Feb 15 08:53:25 2014 +0000
Revision:
4:ec289b4b7758
Parent:
3:be30774cb93f
Modiifed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aaronice 0:fc7729e19c10 1 #include "mbed.h"
aaronice 0:fc7729e19c10 2 AnalogOut DA(PTE30);
aaronice 4:ec289b4b7758 3
aaronice 1:6b531bf059e4 4 //AnalogIn AD(PTB1);
aaronice 0:fc7729e19c10 5
aaronice 0:fc7729e19c10 6 Serial pc(USBTX, USBRX); // tx, rx
aaronice 0:fc7729e19c10 7
aaronice 0:fc7729e19c10 8 float da;
aaronice 1:6b531bf059e4 9 float Amp = 1.0;
aaronice 0:fc7729e19c10 10 float w = 1.0;
aaronice 0:fc7729e19c10 11 float pi = 3.14159;
aaronice 3:be30774cb93f 12 float deltaT = 0.01;
aaronice 4:ec289b4b7758 13
aaronice 0:fc7729e19c10 14 void callback() {
aaronice 0:fc7729e19c10 15 // Note: you need to actually read from the serial to clear the RX interrupt
aaronice 0:fc7729e19c10 16 printf("%c\n", pc.getc());
aaronice 0:fc7729e19c10 17 wait(0.5);
aaronice 4:ec289b4b7758 18 pc.printf("Input parameter A(<=1.0) and press Enter:\n");
aaronice 0:fc7729e19c10 19 pc.scanf("%f", &Amp);
aaronice 0:fc7729e19c10 20 pc.printf("%f\n", Amp);
aaronice 1:6b531bf059e4 21 pc.printf("Input parameter w and press Enter:\n");
aaronice 0:fc7729e19c10 22 pc.scanf("%f", &w);
aaronice 0:fc7729e19c10 23 pc.printf("%f\n", w);
aaronice 2:e723d10f745d 24 pc.printf("Continuing...\n");
aaronice 4:ec289b4b7758 25
aaronice 0:fc7729e19c10 26 }
aaronice 0:fc7729e19c10 27
aaronice 0:fc7729e19c10 28
aaronice 0:fc7729e19c10 29 int main(void) {
aaronice 0:fc7729e19c10 30
aaronice 0:fc7729e19c10 31
aaronice 0:fc7729e19c10 32 pc.attach(&callback);
aaronice 0:fc7729e19c10 33
aaronice 0:fc7729e19c10 34 while(1){
aaronice 1:6b531bf059e4 35 for (float i=0; i<(360); i++) {
aaronice 1:6b531bf059e4 36 da = Amp*(sin(w*i/(180)*pi)*0.5+0.5);
aaronice 0:fc7729e19c10 37 DA.write(da);
aaronice 4:ec289b4b7758 38 wait(deltaT);
aaronice 2:e723d10f745d 39 pc.printf("%f ",da);
aaronice 0:fc7729e19c10 40
aaronice 1:6b531bf059e4 41 //Monitor Received Analog In
aaronice 1:6b531bf059e4 42 //pc.printf("%f ", AD.read());
aaronice 0:fc7729e19c10 43
aaronice 0:fc7729e19c10 44 }
aaronice 0:fc7729e19c10 45 }
aaronice 0:fc7729e19c10 46 }