Uso del DAC para generar una onda senosoidal.

Dependencies:   mbed

Committer:
CCastrop1012
Date:
Fri Sep 03 05:31:55 2021 +0000
Revision:
0:fe3bc59ac2dd
Generador de onda senosoidal.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CCastrop1012 0:fe3bc59ac2dd 1 #include "mbed.h"
CCastrop1012 0:fe3bc59ac2dd 2
CCastrop1012 0:fe3bc59ac2dd 3 #if !DEVICE_ANALOGOUT
CCastrop1012 0:fe3bc59ac2dd 4 #error You cannot use this example as the AnalogOut is not supported on this device.
CCastrop1012 0:fe3bc59ac2dd 5 #else
CCastrop1012 0:fe3bc59ac2dd 6 AnalogOut my_output(PA_4);
CCastrop1012 0:fe3bc59ac2dd 7 #endif
CCastrop1012 0:fe3bc59ac2dd 8
CCastrop1012 0:fe3bc59ac2dd 9 #define PI (3.141592653589793238462)
CCastrop1012 0:fe3bc59ac2dd 10 #define AMPLITUDE (1.0) // x * 3.3V
CCastrop1012 0:fe3bc59ac2dd 11 #define PHASE (PI * 1) // 2*pi is one period
CCastrop1012 0:fe3bc59ac2dd 12 #define RANGE (0x7FFF)
CCastrop1012 0:fe3bc59ac2dd 13 #define OFFSET (0x7FFF)
CCastrop1012 0:fe3bc59ac2dd 14
CCastrop1012 0:fe3bc59ac2dd 15 // Configuration for sinewave output
CCastrop1012 0:fe3bc59ac2dd 16 #define BUFFER_SIZE (360)
CCastrop1012 0:fe3bc59ac2dd 17 uint16_t buffer[BUFFER_SIZE];
CCastrop1012 0:fe3bc59ac2dd 18
CCastrop1012 0:fe3bc59ac2dd 19 void calculate_sinewave(void);
CCastrop1012 0:fe3bc59ac2dd 20
CCastrop1012 0:fe3bc59ac2dd 21 int main() {
CCastrop1012 0:fe3bc59ac2dd 22 printf("Sinewave example\n");
CCastrop1012 0:fe3bc59ac2dd 23 calculate_sinewave();
CCastrop1012 0:fe3bc59ac2dd 24 while(1) {
CCastrop1012 0:fe3bc59ac2dd 25 // sinewave output
CCastrop1012 0:fe3bc59ac2dd 26 for (int i = 0; i < BUFFER_SIZE; i++) {
CCastrop1012 0:fe3bc59ac2dd 27 my_output.write_u16(buffer[i]);
CCastrop1012 0:fe3bc59ac2dd 28 wait_us(10);
CCastrop1012 0:fe3bc59ac2dd 29 }
CCastrop1012 0:fe3bc59ac2dd 30 }
CCastrop1012 0:fe3bc59ac2dd 31 }
CCastrop1012 0:fe3bc59ac2dd 32
CCastrop1012 0:fe3bc59ac2dd 33 // Create the sinewave buffer
CCastrop1012 0:fe3bc59ac2dd 34 void calculate_sinewave(void){
CCastrop1012 0:fe3bc59ac2dd 35 for (int i = 0; i < BUFFER_SIZE; i++) {
CCastrop1012 0:fe3bc59ac2dd 36 double rads = (PI * i)/180.0; // Convert degree in radian
CCastrop1012 0:fe3bc59ac2dd 37 buffer[i] = (uint16_t)(AMPLITUDE * (RANGE * (cos(rads + PHASE))) + OFFSET);
CCastrop1012 0:fe3bc59ac2dd 38 }
CCastrop1012 0:fe3bc59ac2dd 39 }