Jésus Neto / Mbed OS Triangular and sinusoidal wave generator
Committer:
jesuspity
Date:
Sun Mar 17 17:59:47 2019 +0000
Revision:
0:214bf6a684c1
Generate a sine and triangle wave

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jesuspity 0:214bf6a684c1 1 /* mbed Example Program
jesuspity 0:214bf6a684c1 2 * Copyright (c) 2006-2014 ARM Limited
jesuspity 0:214bf6a684c1 3 *
jesuspity 0:214bf6a684c1 4 * Licensed under the Apache License, Version 2.0 (the "License");
jesuspity 0:214bf6a684c1 5 * you may not use this file except in compliance with the License.
jesuspity 0:214bf6a684c1 6 * You may obtain a copy of the License at
jesuspity 0:214bf6a684c1 7 *
jesuspity 0:214bf6a684c1 8 * http://www.apache.org/licenses/LICENSE-2.0
jesuspity 0:214bf6a684c1 9 *
jesuspity 0:214bf6a684c1 10 * Unless required by applicable law or agreed to in writing, software
jesuspity 0:214bf6a684c1 11 * distributed under the License is distributed on an "AS IS" BASIS,
jesuspity 0:214bf6a684c1 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jesuspity 0:214bf6a684c1 13 * See the License for the specific language governing permissions and
jesuspity 0:214bf6a684c1 14 * limitations under the License.
jesuspity 0:214bf6a684c1 15 */
jesuspity 0:214bf6a684c1 16 #include "mbed.h"
jesuspity 0:214bf6a684c1 17 #define PI (3.141592653589793238462)
jesuspity 0:214bf6a684c1 18 #define AMPLITUDE (1.0) // x * 3.3V
jesuspity 0:214bf6a684c1 19 #define PHASE (PI * 1) // 2*pi is one period
jesuspity 0:214bf6a684c1 20 #define RANGE (0x7FFF)
jesuspity 0:214bf6a684c1 21 #define OFFSET (0x7FFF)
jesuspity 0:214bf6a684c1 22
jesuspity 0:214bf6a684c1 23
jesuspity 0:214bf6a684c1 24 //Ticker flipper;
jesuspity 0:214bf6a684c1 25 //DigitalOut led1(LED1);
jesuspity 0:214bf6a684c1 26 //DigitalOut led2(LED2);
jesuspity 0:214bf6a684c1 27 //AnalogIn ain(A0);
jesuspity 0:214bf6a684c1 28
jesuspity 0:214bf6a684c1 29 //Timer t;
jesuspity 0:214bf6a684c1 30 //float duration = 0;
jesuspity 0:214bf6a684c1 31 // Configuration for sinewave output
jesuspity 0:214bf6a684c1 32 #define BUFFER_SIZE (360)
jesuspity 0:214bf6a684c1 33 float buffer_triang[BUFFER_SIZE]={};
jesuspity 0:214bf6a684c1 34 float buffer_sine[BUFFER_SIZE]={};
jesuspity 0:214bf6a684c1 35 float buffer_out0[BUFFER_SIZE]={};
jesuspity 0:214bf6a684c1 36 float buffer_out1[BUFFER_SIZE]={};
jesuspity 0:214bf6a684c1 37 //int K = 0;
jesuspity 0:214bf6a684c1 38
jesuspity 0:214bf6a684c1 39 void calculate_sinewave(void);
jesuspity 0:214bf6a684c1 40 void calculate_triangwave(void);
jesuspity 0:214bf6a684c1 41 void calculate_out(void);
jesuspity 0:214bf6a684c1 42 // Create the sinewave buffer
jesuspity 0:214bf6a684c1 43
jesuspity 0:214bf6a684c1 44 void calculate_sinewave(void){
jesuspity 0:214bf6a684c1 45 for (int i = 0; i < BUFFER_SIZE; i++) {
jesuspity 0:214bf6a684c1 46 double rads = (PI * i)/180.0; // Convert degree in radian
jesuspity 0:214bf6a684c1 47 buffer_sine[i] = (float)( ( (cos(rads + PHASE))));
jesuspity 0:214bf6a684c1 48 }
jesuspity 0:214bf6a684c1 49 }
jesuspity 0:214bf6a684c1 50
jesuspity 0:214bf6a684c1 51 // Create the triangular buffer
jesuspity 0:214bf6a684c1 52 void calculate_triangwave(void)
jesuspity 0:214bf6a684c1 53 {
jesuspity 0:214bf6a684c1 54 for (int k = 1; k < 100; k++) {
jesuspity 0:214bf6a684c1 55 for (int i = 0; i < BUFFER_SIZE; i++) {
jesuspity 0:214bf6a684c1 56 double rads = (PI * i)/180.0; // Convert degree in radian
jesuspity 0:214bf6a684c1 57 buffer_triang[i] = (float)( (8/(PI*PI)) *sin(PI*k/2) *(sin(k*rads*10)/(k*k)) + buffer_triang[i]);
jesuspity 0:214bf6a684c1 58 }
jesuspity 0:214bf6a684c1 59 }
jesuspity 0:214bf6a684c1 60 }
jesuspity 0:214bf6a684c1 61 // Create the comparação buffer
jesuspity 0:214bf6a684c1 62 void calculate_out(void)
jesuspity 0:214bf6a684c1 63 {
jesuspity 0:214bf6a684c1 64 for (int k = 1; k < 100; k++) {
jesuspity 0:214bf6a684c1 65 for (int i = 0; i < BUFFER_SIZE; i++) {
jesuspity 0:214bf6a684c1 66 buffer_out0[i] = buffer_triang[i]>buffer_sine[i] ;
jesuspity 0:214bf6a684c1 67 }
jesuspity 0:214bf6a684c1 68 }
jesuspity 0:214bf6a684c1 69 }
jesuspity 0:214bf6a684c1 70
jesuspity 0:214bf6a684c1 71
jesuspity 0:214bf6a684c1 72 int main() {
jesuspity 0:214bf6a684c1 73 printf("Sinewave example\n");
jesuspity 0:214bf6a684c1 74 calculate_sinewave();
jesuspity 0:214bf6a684c1 75 calculate_triangwave();
jesuspity 0:214bf6a684c1 76 calculate_out();
jesuspity 0:214bf6a684c1 77
jesuspity 0:214bf6a684c1 78 while(1) {
jesuspity 0:214bf6a684c1 79 //led2 = !led2;
jesuspity 0:214bf6a684c1 80 // sinewave output
jesuspity 0:214bf6a684c1 81 for (int i = 0; i < BUFFER_SIZE; i++) {
jesuspity 0:214bf6a684c1 82 printf("%f, %f, %f \n", buffer_triang[i]*1.0f,buffer_sine[i]*1.0f,buffer_out0[i]*1.0f);
jesuspity 0:214bf6a684c1 83 wait(0.001);
jesuspity 0:214bf6a684c1 84 }
jesuspity 0:214bf6a684c1 85 //printf("__________________________________\n");
jesuspity 0:214bf6a684c1 86 //printf("The time taken was %f s \n", duration);
jesuspity 0:214bf6a684c1 87 //wait(0.2);
jesuspity 0:214bf6a684c1 88 }
jesuspity 0:214bf6a684c1 89 }