Basic_sine_wave_generator

Dependencies:   mbed 4DGL-uLCD-SE mbed-rtos AD5206

Committer:
taoqiuyang
Date:
Wed Nov 25 15:24:52 2015 +0000
Revision:
0:52af3c50f548
Child:
1:fb8f053747d6
Basic_sine_wave_generator

Who changed what in which revision?

UserRevisionLine numberNew contents of line
taoqiuyang 0:52af3c50f548 1 #include "mbed.h"
taoqiuyang 0:52af3c50f548 2
taoqiuyang 0:52af3c50f548 3 // Initialize a pins to perform analog and digital output fucntions
taoqiuyang 0:52af3c50f548 4 AnalogOut aout(p18);
taoqiuyang 0:52af3c50f548 5 DigitalOut dout(LED1);
taoqiuyang 0:52af3c50f548 6
taoqiuyang 0:52af3c50f548 7 const double pi = 3.1416;
taoqiuyang 0:52af3c50f548 8 float fs=100;
taoqiuyang 0:52af3c50f548 9 float offset=0.5;
taoqiuyang 0:52af3c50f548 10 uint16_t sample = 0;
taoqiuyang 0:52af3c50f548 11
taoqiuyang 0:52af3c50f548 12 float freq=2; //Hz
taoqiuyang 0:52af3c50f548 13 float amp=1; //Sacle factor, 1=full range
taoqiuyang 0:52af3c50f548 14 float x;
taoqiuyang 0:52af3c50f548 15
taoqiuyang 0:52af3c50f548 16
taoqiuyang 0:52af3c50f548 17 int main(void){
taoqiuyang 0:52af3c50f548 18 while (1){
taoqiuyang 0:52af3c50f548 19 for (float t = 0.0f; t < 1/freq; t += 1/fs) {
taoqiuyang 0:52af3c50f548 20 x=amp*(cos(2*pi*freq*t)+1);
taoqiuyang 0:52af3c50f548 21 sample=(uint16_t) (x*32767);
taoqiuyang 0:52af3c50f548 22 aout.write_u16(sample);;
taoqiuyang 0:52af3c50f548 23 wait(1/fs);
taoqiuyang 0:52af3c50f548 24 }
taoqiuyang 0:52af3c50f548 25 }
taoqiuyang 0:52af3c50f548 26 }