Uso del DAC para generar una onda senosoidal.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #if !DEVICE_ANALOGOUT
00004 #error You cannot use this example as the AnalogOut is not supported on this device.
00005 #else
00006 AnalogOut my_output(PA_4);
00007 #endif
00008 
00009 #define PI        (3.141592653589793238462)
00010 #define AMPLITUDE (1.0)    // x * 3.3V
00011 #define PHASE     (PI * 1) // 2*pi is one period
00012 #define RANGE     (0x7FFF)
00013 #define OFFSET    (0x7FFF)
00014 
00015 // Configuration for sinewave output
00016 #define BUFFER_SIZE (360)
00017 uint16_t buffer[BUFFER_SIZE];
00018 
00019 void calculate_sinewave(void);
00020 
00021 int main() {
00022     printf("Sinewave example\n");
00023     calculate_sinewave();
00024     while(1) {      
00025         // sinewave output
00026         for (int i = 0; i < BUFFER_SIZE; i++) {
00027             my_output.write_u16(buffer[i]);
00028             wait_us(10);
00029         }
00030     }
00031 }
00032 
00033 // Create the sinewave buffer
00034 void calculate_sinewave(void){
00035   for (int i = 0; i < BUFFER_SIZE; i++) {
00036      double rads = (PI * i)/180.0; // Convert degree in radian
00037      buffer[i] = (uint16_t)(AMPLITUDE * (RANGE * (cos(rads + PHASE))) + OFFSET);
00038   }
00039 }