Page 254 - ARM Based Microcontroller Projects Using MBED
P. 254

240                          8. INTERMEDIATE LEVEL PROJECTS

                       inc = amplitude / Steps;
                       dly = Period / Steps;
                       dly = dly / 1000.0f;                             // in seconds

                       while(1)                                         // Do forever
                       {
                            for(k = 0.0f; k <= amplitude; k = k + inc)
                            {
                                aout = k;                               // Analog out
                                wait(dly);                              // Wait dly sec
                            }
                       }
                    }

                    //
                    // This function generates a Triangular waveform. The required number
                    // of staps and the period are arguments of the function
                    //
                    void GenerateTriangular(float Amp, int Steps, float Period)
                    {
                       float k, amplitude, inc, dly;

                       amplitude = Amp / 3.3f;                          // Required amplitude
                       inc = amplitude / Steps;
                       dly = Period / Steps / 2;
                       dly = dly / 1000.0f;                             // in seconds

                       while(1)                                         // Do forever
                       {
                            for(k = 0.0f; k <= amplitude; k = k + inc)  // Rising
                            {
                                aout = k;                               // Analog out
                                wait(dly);                              // Wait dly sec
                            }

                            for(k = amplitude; k > 0; k = k - inc)      // Falling
                            {
                                aout = k;                               // Analog out
                                wait(dly);                              // Wait dly sec
                            }
                       }

                    }

                    //
                    // This function generates a Sine waveform. The required number
                    // of staps and the period are arguments of the function
                    //
                    void GenerateSine(float Amp, int Steps, float Period)
                    {
                       float k, amplitude, inc, dly;
                       float Pi = 3.14159;
                 FIG. 8.113, CONT’D
                                                                                          (Continued)
   249   250   251   252   253   254   255   256   257   258   259