lab3 n stuff

Dependencies:   mbed

main.cpp

Committer:
m0t0
Date:
2018-02-02
Revision:
1:38856e3bbb85
Parent:
0:8511b16665e6

File content as of revision 1:38856e3bbb85:

#include "mbed.h"

#if !DEVICE_ANALOGOUT
#error You cannot use this example as the AnalogOut is not supported on this device.
#else
AnalogOut my_output(PA_4);
#endif

#define PI        (3.141592653589793238462)
//#define AMPLITUDE (1.0)//* 3.3V
#define PHASE     (PI * 1) // 2*pi is one period
#define RANGE     (0x7FFF)
#define OFFSET    (0x7FFF)

// Configuration for sinewave output
#define BUFFER_SIZE (360)


uint16_t buffer[BUFFER_SIZE];


// Create the sinewave buffer
void calculate_sinewave(double amp){
  for (int i = 0; i < BUFFER_SIZE; i++) {
     double rads = (PI * i)/180.0; // Convert degree in radian
     buffer[i] = (uint16_t)((amp * (RANGE * (cos(rads + PHASE))) + OFFSET));
  }
}



void calculate_sinewave1(double amp, double freq){
    freq = ((1.0/freq) * 1000.0 * 27.0); 
    amp=(amp-0.2);
    amp *= 0.3333333;
    calculate_sinewave(amp);
    while(1) {      
        // sinewave output
        for (int i = 0; i < BUFFER_SIZE; i+=10) {
            my_output.write_u16(buffer[i]);
            wait_us(freq);
        }
    }

}



int main() {
    printf("Sinewave example\n");
    calculate_sinewave1(3,500);
}