Additional AnalogOut example

Committer:
sarahmarshy
Date:
Fri Jun 23 16:22:08 2017 -0500
Revision:
1:e758daa05e53
Parent:
0:066510b55650
"Update mbed-os"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mab5449 0:066510b55650 1 #include "mbed.h"
mab5449 0:066510b55650 2
mab5449 0:066510b55650 3 const double pi = 3.141592653589793238462;
mab5449 0:066510b55650 4 const double amplitude = 0.5f;
mab5449 0:066510b55650 5 const double offset = 65535/2;
mab5449 0:066510b55650 6
mab5449 0:066510b55650 7 // The sinewave is created on this pin
mab5449 0:066510b55650 8 AnalogOut aout(A5);
mab5449 0:066510b55650 9
mab5449 0:066510b55650 10 int main()
mab5449 0:066510b55650 11 {
mab5449 0:066510b55650 12 double rads = 0.0;
mab5449 0:066510b55650 13 uint16_t sample = 0;
mab5449 0:066510b55650 14
mab5449 0:066510b55650 15 while(1) {
mab5449 0:066510b55650 16 // sinewave output
mab5449 0:066510b55650 17 for (int i = 0; i < 360; i++) {
mab5449 0:066510b55650 18 rads = (pi * i) / 180.0f;
mab5449 0:066510b55650 19 sample = (uint16_t)(amplitude * (offset * (cos(rads + pi))) + offset);
mab5449 0:066510b55650 20 aout.write_u16(sample);
mab5449 0:066510b55650 21 }
mab5449 0:066510b55650 22 }
mab5449 0:066510b55650 23 }
mab5449 0:066510b55650 24