Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:076821894a5d, 2017-10-26 (annotated)
- Committer:
- CSTritt
- Date:
- Thu Oct 26 19:25:24 2017 +0000
- Revision:
- 0:076821894a5d
Initial version.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| CSTritt | 0:076821894a5d | 1 | /* |
| CSTritt | 0:076821894a5d | 2 | Project: AnalogSine (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 | 0:076821894a5d | 7 | Generates sine and cosine wave forms to demonstrate D to A operation. Use a |
| CSTritt | 0:076821894a5d | 8 | data buffer for maximum speed (not necessary for 4 mS samples & 1.0 Hz |
| CSTritt | 0:076821894a5d | 9 | signal). Output clips at a bit 0 and 3.3 V, so use AMPLITUDE <= 0.4 and |
| CSTritt | 0:076821894a5d | 10 | OFFSET = 1.1. Zoom Horizontal scale out on MSOX2012A scopes and wait for |
| CSTritt | 0:076821894a5d | 11 | refresh. Without wait, max. freq. (with both channels) is about 2 kHz. |
| CSTritt | 0:076821894a5d | 12 | */ |
| CSTritt | 0:076821894a5d | 13 | #include "mbed.h" |
| CSTritt | 0:076821894a5d | 14 | |
| CSTritt | 0:076821894a5d | 15 | #if !DEVICE_ANALOGOUT // Flag set by mbed. |
| CSTritt | 0:076821894a5d | 16 | #error AnalogOut is not supported on this device. // Abort compilation. |
| CSTritt | 0:076821894a5d | 17 | #else |
| CSTritt | 0:076821894a5d | 18 | AnalogOut my_Sin(PA_4); // Sine |
| CSTritt | 0:076821894a5d | 19 | AnalogOut my_Cos(PA_5); // Cosine |
| CSTritt | 0:076821894a5d | 20 | |
| CSTritt | 0:076821894a5d | 21 | const float PI = 3.14159265f; // Value of Pi for radians calculation. |
| CSTritt | 0:076821894a5d | 22 | const float AMPLITUDE = 0.4f; // x * 3.3V. D to A is limited to < 3.3 V. |
| CSTritt | 0:076821894a5d | 23 | const float OFFSET = 1.1f; // Make signal all possitive. |
| CSTritt | 0:076821894a5d | 24 | |
| CSTritt | 0:076821894a5d | 25 | // Configuration for sinewave output |
| CSTritt | 0:076821894a5d | 26 | const int BUFFER_SIZE = 250; // Elements (i.e., points). |
| CSTritt | 0:076821894a5d | 27 | float buffer[BUFFER_SIZE]; |
| CSTritt | 0:076821894a5d | 28 | |
| CSTritt | 0:076821894a5d | 29 | void calculate_sinewave(void); // Fills buffer. |
| CSTritt | 0:076821894a5d | 30 | |
| CSTritt | 0:076821894a5d | 31 | int main() |
| CSTritt | 0:076821894a5d | 32 | { |
| CSTritt | 0:076821894a5d | 33 | printf("Sinewave example\n"); |
| CSTritt | 0:076821894a5d | 34 | calculate_sinewave(); |
| CSTritt | 0:076821894a5d | 35 | while(true) { |
| CSTritt | 0:076821894a5d | 36 | // sinewave output |
| CSTritt | 0:076821894a5d | 37 | for (int i = 0; i < BUFFER_SIZE; i++) { |
| CSTritt | 0:076821894a5d | 38 | my_Sin.write(buffer[i]); // Output a point. |
| CSTritt | 0:076821894a5d | 39 | // Generate Cosine by shifting sine by 1/4 cycle. |
| CSTritt | 0:076821894a5d | 40 | my_Cos.write(buffer[(i + BUFFER_SIZE/4) % BUFFER_SIZE]); |
| CSTritt | 0:076821894a5d | 41 | wait_ms(4); // Was wait_us(10); Remove for max. freq. |
| CSTritt | 0:076821894a5d | 42 | } |
| CSTritt | 0:076821894a5d | 43 | } |
| CSTritt | 0:076821894a5d | 44 | } |
| CSTritt | 0:076821894a5d | 45 | |
| CSTritt | 0:076821894a5d | 46 | void calculate_sinewave(void) // This function fills the buffer. |
| CSTritt | 0:076821894a5d | 47 | { |
| CSTritt | 0:076821894a5d | 48 | for (int i = 0; i < BUFFER_SIZE; i++) { |
| CSTritt | 0:076821894a5d | 49 | // Calculate radians from 0 to 2*Pi. |
| CSTritt | 0:076821894a5d | 50 | float rads = (2.0f * PI * (float) i / (float)BUFFER_SIZE); |
| CSTritt | 0:076821894a5d | 51 | // Fill the buffer. |
| CSTritt | 0:076821894a5d | 52 | buffer[i] = AMPLITUDE * (sin(rads) + OFFSET); // Sine of radians. |
| CSTritt | 0:076821894a5d | 53 | } |
| CSTritt | 0:076821894a5d | 54 | } |
| CSTritt | 0:076821894a5d | 55 | #endif |