sin Wave generation by AnalogOut

Dependencies:   mbed

Committer:
yao6116601
Date:
Fri Jun 22 23:12:17 2018 +0000
Revision:
0:455b26cfa769
Sin wave generation By analogOut

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yao6116601 0:455b26cfa769 1 #include "mbed.h"
yao6116601 0:455b26cfa769 2 #include "iostream"
yao6116601 0:455b26cfa769 3 DigitalOut myled(PF_14);
yao6116601 0:455b26cfa769 4 AnalogOut aout(PA_4);
yao6116601 0:455b26cfa769 5
yao6116601 0:455b26cfa769 6 const double pi = 3.141592653589793238462;
yao6116601 0:455b26cfa769 7 const double amplitude = 0.5f;
yao6116601 0:455b26cfa769 8 const double offset = 65535/2;
yao6116601 0:455b26cfa769 9 using namespace std;
yao6116601 0:455b26cfa769 10 int main() {
yao6116601 0:455b26cfa769 11 double rads = 0.0;
yao6116601 0:455b26cfa769 12 uint16_t sample = 0;
yao6116601 0:455b26cfa769 13 cout<<"hello\n";
yao6116601 0:455b26cfa769 14 printf("Analog Out demo\n");
yao6116601 0:455b26cfa769 15 while(1) {
yao6116601 0:455b26cfa769 16 // sinewave output
yao6116601 0:455b26cfa769 17 for (int i = 0; i < 360; i++) {
yao6116601 0:455b26cfa769 18 rads = (pi * i) / 180.0f;
yao6116601 0:455b26cfa769 19 sample = (uint16_t)(amplitude * (offset * (cos(rads + pi))) + offset);
yao6116601 0:455b26cfa769 20 aout.write_u16(sample);
yao6116601 0:455b26cfa769 21 }
yao6116601 0:455b26cfa769 22 }
yao6116601 0:455b26cfa769 23
yao6116601 0:455b26cfa769 24 }