Produces 2 cycles of a sine wave. Use to demonstrate data export to Excel and/or Matlab.

Dependencies:   mbed

Fork of AnalogSine by Charles Tritt

Committer:
CSTritt
Date:
Wed Nov 08 17:36:36 2017 +0000
Revision:
1:f2da7082966e
Parent:
0:076821894a5d
Initial version (based on AnalogSine).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CSTritt 0:076821894a5d 1 /*
CSTritt 1:f2da7082966e 2 Project: AnalogSineFinite (based on Nucleo_sinwave_output example)
CSTritt 0:076821894a5d 3 File: main.cpp
CSTritt 0:076821894a5d 4 Modified by by: Dr. C. S. Tritt
CSTritt 0:076821894a5d 5 Last revision on: 9/26/17 (v. 1.0)
CSTritt 0:076821894a5d 6
CSTritt 1:f2da7082966e 7 Finite duration version. Use to generate CSV file for import into Excel or
CSTritt 1:f2da7082966e 8 Matlab.
CSTritt 1:f2da7082966e 9
CSTritt 0:076821894a5d 10 Generates sine and cosine wave forms to demonstrate D to A operation. Use a
CSTritt 0:076821894a5d 11 data buffer for maximum speed (not necessary for 4 mS samples & 1.0 Hz
CSTritt 0:076821894a5d 12 signal). Output clips at a bit 0 and 3.3 V, so use AMPLITUDE <= 0.4 and
CSTritt 0:076821894a5d 13 OFFSET = 1.1. Zoom Horizontal scale out on MSOX2012A scopes and wait for
CSTritt 0:076821894a5d 14 refresh. Without wait, max. freq. (with both channels) is about 2 kHz.
CSTritt 1:f2da7082966e 15
CSTritt 1:f2da7082966e 16 Possible improvements/modifications:
CSTritt 1:f2da7082966e 17
CSTritt 1:f2da7082966e 18 Reduce number of points/cycle to 18 or 24 (more or less).
CSTritt 1:f2da7082966e 19 Change design to use 2 buffers.
CSTritt 1:f2da7082966e 20 Change design to use a TimeOut.
CSTritt 1:f2da7082966e 21 Use pots to adjust freq. and phase to produce Lissajous patterns.
CSTritt 0:076821894a5d 22 */
CSTritt 0:076821894a5d 23 #include "mbed.h"
CSTritt 0:076821894a5d 24
CSTritt 0:076821894a5d 25 #if !DEVICE_ANALOGOUT // Flag set by mbed.
CSTritt 0:076821894a5d 26 #error AnalogOut is not supported on this device. // Abort compilation.
CSTritt 0:076821894a5d 27 #else
CSTritt 0:076821894a5d 28 AnalogOut my_Sin(PA_4); // Sine
CSTritt 0:076821894a5d 29 AnalogOut my_Cos(PA_5); // Cosine
CSTritt 0:076821894a5d 30
CSTritt 0:076821894a5d 31 const float PI = 3.14159265f; // Value of Pi for radians calculation.
CSTritt 0:076821894a5d 32 const float AMPLITUDE = 0.4f; // x * 3.3V. D to A is limited to < 3.3 V.
CSTritt 0:076821894a5d 33 const float OFFSET = 1.1f; // Make signal all possitive.
CSTritt 0:076821894a5d 34
CSTritt 0:076821894a5d 35 // Configuration for sinewave output
CSTritt 0:076821894a5d 36 const int BUFFER_SIZE = 250; // Elements (i.e., points).
CSTritt 0:076821894a5d 37 float buffer[BUFFER_SIZE];
CSTritt 0:076821894a5d 38
CSTritt 0:076821894a5d 39 void calculate_sinewave(void); // Fills buffer.
CSTritt 0:076821894a5d 40
CSTritt 0:076821894a5d 41 int main()
CSTritt 0:076821894a5d 42 {
CSTritt 1:f2da7082966e 43 printf("Finite sine wave example\n");
CSTritt 0:076821894a5d 44 calculate_sinewave();
CSTritt 1:f2da7082966e 45 for (int j = 0; j < 2; j++) { // Output 2 cycles and stop.
CSTritt 0:076821894a5d 46 // sinewave output
CSTritt 0:076821894a5d 47 for (int i = 0; i < BUFFER_SIZE; i++) {
CSTritt 0:076821894a5d 48 my_Sin.write(buffer[i]); // Output a point.
CSTritt 1:f2da7082966e 49 printf("%f, ", buffer[i]); // Send point to PC.
CSTritt 0:076821894a5d 50 // Generate Cosine by shifting sine by 1/4 cycle.
CSTritt 0:076821894a5d 51 my_Cos.write(buffer[(i + BUFFER_SIZE/4) % BUFFER_SIZE]);
CSTritt 0:076821894a5d 52 }
CSTritt 0:076821894a5d 53 }
CSTritt 0:076821894a5d 54 }
CSTritt 0:076821894a5d 55
CSTritt 0:076821894a5d 56 void calculate_sinewave(void) // This function fills the buffer.
CSTritt 0:076821894a5d 57 {
CSTritt 0:076821894a5d 58 for (int i = 0; i < BUFFER_SIZE; i++) {
CSTritt 0:076821894a5d 59 // Calculate radians from 0 to 2*Pi.
CSTritt 0:076821894a5d 60 float rads = (2.0f * PI * (float) i / (float)BUFFER_SIZE);
CSTritt 0:076821894a5d 61 // Fill the buffer.
CSTritt 0:076821894a5d 62 buffer[i] = AMPLITUDE * (sin(rads) + OFFSET); // Sine of radians.
CSTritt 0:076821894a5d 63 }
CSTritt 0:076821894a5d 64 }
CSTritt 0:076821894a5d 65 #endif