init

Dependencies:   mbed

Committer:
tulanthoar
Date:
Mon Feb 19 19:19:53 2018 +0000
Revision:
7:40c351cc34d5
Parent:
6:1b7486249c9a
Child:
8:6c2a12d6b1bf
1khz sine wave

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:c7a485a2cf28 1 #include "mbed.h"
bcostm 0:c7a485a2cf28 2
arostm 6:1b7486249c9a 3 #if !DEVICE_ANALOGOUT
arostm 6:1b7486249c9a 4 #error You cannot use this example as the AnalogOut is not supported on this device.
arostm 6:1b7486249c9a 5 #else
tulanthoar 7:40c351cc34d5 6 AnalogOut sinOut(PA_4);
tulanthoar 7:40c351cc34d5 7 AnalogOut stepOut(PA_5);
arostm 6:1b7486249c9a 8 #endif
bcostm 0:c7a485a2cf28 9
tulanthoar 7:40c351cc34d5 10 #define PI (3.141592653589793238462f)
tulanthoar 7:40c351cc34d5 11 #define AMPLITUDE (0.49998f) // x * 3.3V
tulanthoar 7:40c351cc34d5 12 #define PHASE (PI * 1.0f) // 2*pi is one period
tulanthoar 7:40c351cc34d5 13 #define RANGE (1.0f)
tulanthoar 7:40c351cc34d5 14 #define OFFSET (0.5f)
bcostm 0:c7a485a2cf28 15
bcostm 0:c7a485a2cf28 16 // Configuration for sinewave output
tulanthoar 7:40c351cc34d5 17 #define BUFFER_SIZE (540U)
tulanthoar 7:40c351cc34d5 18 float buffer[BUFFER_SIZE];
bcostm 0:c7a485a2cf28 19
bcostm 0:c7a485a2cf28 20 void calculate_sinewave(void);
bcostm 0:c7a485a2cf28 21
tulanthoar 7:40c351cc34d5 22 int main()
tulanthoar 7:40c351cc34d5 23 {
bcostm 4:6a66cf97f2cf 24 printf("Sinewave example\n");
bcostm 0:c7a485a2cf28 25 calculate_sinewave();
tulanthoar 7:40c351cc34d5 26 float stepBuff[]= {0.9090909f,0.9090909f,0.9090909f,0.9090909f, 0.606061f,0.606061f,0.606061f,0.606061f, 0.3030303f, 0.3030303f, 0.3030303f, 0.3030303f};
tulanthoar 7:40c351cc34d5 27 while(1) {
bcostm 0:c7a485a2cf28 28 // sinewave output
tulanthoar 7:40c351cc34d5 29 for( int j = 0; j < 12; ++j) {
tulanthoar 7:40c351cc34d5 30 stepOut=stepBuff[j];
tulanthoar 7:40c351cc34d5 31 for (int i = 0; i < BUFFER_SIZE; ++i) {
tulanthoar 7:40c351cc34d5 32 sinOut=buffer[i];
tulanthoar 7:40c351cc34d5 33 // wait_us(22);
tulanthoar 7:40c351cc34d5 34 }
bcostm 0:c7a485a2cf28 35 }
bcostm 0:c7a485a2cf28 36 }
bcostm 0:c7a485a2cf28 37 }
bcostm 0:c7a485a2cf28 38
bcostm 0:c7a485a2cf28 39 // Create the sinewave buffer
tulanthoar 7:40c351cc34d5 40 void calculate_sinewave(void)
tulanthoar 7:40c351cc34d5 41 {
tulanthoar 7:40c351cc34d5 42 for (int i = 0; i < BUFFER_SIZE; i++) {
tulanthoar 7:40c351cc34d5 43 float rads = 2.0f*(PI * i)/((float)BUFFER_SIZE); // Convert degree in radian
tulanthoar 7:40c351cc34d5 44 buffer[i] = (float)(AMPLITUDE * (RANGE * (cos(rads + PHASE))) + OFFSET);
tulanthoar 7:40c351cc34d5 45 }
bcostm 0:c7a485a2cf28 46 }