4 years, 6 months ago.

how create wave forms?

how create sine wave, square wave, triangular wave and saw tooth wave?

include the mbed library with this snippet

#include "mbed.h"

void Wave(void)
{
    int i = 0;
    //Preparazione dati
    Frequency = 1 / Frequency;
    Frequency /= 20;
    Frequency *= 1000000;  
    int T = (int)Frequency;
    float v = Amplitude / 3.3; 
    switch(Type)
    {
        case 1://sine wave
            v /= 2;
            T /= 20;
            while(i != 35)
            {
                for(float x = 0; x <= 1; x =+ 0.05)
                {
                    WaveForm = v * sin(x * 3.14159);
                    WaveForm =+ 0.5; 
                    wait_us(T);  
                }    
                i = Keys.read();   
            }
            break;
        case 2://Square wave
            int Ton;
            int Toff;
            Ton = (int)Frequency;
            Ton *= DutyCycle;  
            Toff = (int)Frequency - Ton;      
            while(i != 35)
            {
                WaveForm = v;
                wait_us(Ton);
                WaveForm = 0;
                wait_us(Toff);
                i = Keys.read();
            }
            break;
        case 3://Triangular wave
            v /= 2;
            T /= 80;
            while(i != 35)
            {
                for(float g = 0.5; g <= v; g =+ 0.05)
                {
                    WaveForm = g;
                    wait_us(T);
                }
                for(float a = v; a >= 0.5; a =- 0.05)
                {
                    WaveForm = a;
                    wait_us(T); 
                }
                for(float b = 0.5; b <= v - 0.5; b =- 0.05)
                {
                    WaveForm = b;
                    wait_us(T);
                }
                for(float c = v - 0.5; c <= 0.5; c =+ 0.05)
                {
                    WaveForm = c;
                    wait_us(T);
                }
                i = Keys.read();              
            }
            break;
        case 4://Saw tooth wave   
            v /= 2;
            T /= 40;
            while(i != 35)
            {
                 for(float y = 0.5; y <= v; y =+ 0.05)
                 {
                     WaveForm = y;
                     wait_us(T);
                 }    
                 for(float z = 0.5; z >= v - 0.5; z =- 0.05)
                 {
                     WaveForm = z;
                     wait_us(T); 
                 }       
                 i = Keys.read();     
            }
            break;
    } 
    return;
}

What is your question?

posted by Andy A 21 May 2015

this function create four basics waves but it doesn't work correctly. If I try to print on TeraTerm all used value I have problems with "Frequency = 1 / Frequency;" operation. Now the question is how can I calculate the period?

posted by Davide Luongo 21 May 2015

The period is 1/frequency.

posted by Sarah Marsh 21 May 2015

Ok! I know it but why the statement Frequency = 1 / Frequency ==> Frequency = 0?

posted by Davide Luongo 21 May 2015

Can you post the rest of your code, it's hard to find the problem when the code posted deosn't define half the variables. Frequency, Type, WaveForm and Keys are all undefined.

What sort of frequency are you aiming for?

posted by Andy A 21 May 2015

1 Answer

1 year, 7 months ago.

If you declre frequency as int, then it will allways be 0, if you divide 1 by something. It will be 1 only when frequency = 1.