Sinewave Generator

From DSignT Support Database
Jump to: navigation, search

Sinewave Generator Source Code

/***********************************************************************
  includes
***********************************************************************/
#include <stdint.h>
#include <math.h>

/*******************************************************************//**
  defines
***********************************************************************/
#define PI          3.141592654

#define FSAMP       50000       /**< sampling frequency in Hz         */
#define RES         16          /**< signal resolution in bits        */
#define VOUT        2.5         /**< fullscale output voltage         */

#define FSIG	    1000.0      /**< sine signal frequency in Hz      */
#define ASIG	    2.499       /**< sine amplitude in Vpk            */
#define PHSIG	    -PI/2.0     /**< sine phase in rad                */

#define MAXLEVEL    ((exp2(RES-1.0) - 1.0) / VOUT)


/*******************************************************************//**
  globals
***********************************************************************/
float   yn, yn_1, yn_2;     /**< sinewave states                      */
float   coeff;              /**< sinewave coefficient                 */

volatile uint16_t DAC;      /**< dummy 16-bit DAC                     */


/*******************************************************************//**
 @brief     DAC Interrupt: calculate and output sinewave
 
 @param     none
 @return    -
***********************************************************************/
interrupt void dac_int (void)
{
    yn_2 = yn_1;
    yn_1 = yn;
    yn = coeff * yn_1 - yn_2;
    DAC = (int16_t)(MAXLEVEL * ASIG * yn);    
}



/*******************************************************************//**
 @brief     main program
***********************************************************************/
void main (void)
{
    /*******************************************************************
      local variables
    *******************************************************************/
    double omega;
    
    /*******************************************************************
      sinewave initialization
    *******************************************************************/
    omega = 2.0 * PI * FSIG/FSAMP;
    
    yn = sin (PHSIG);
    yn_1 = sin (omega + PHSIG);
    coeff = 2.0 * cos (omega);

    /*******************************************************************
      interrupt processing
    *******************************************************************/
    for (;;);
    
}


Contact Post.png Support Tool.png