/*SINEC.C - REAL-TIME SINE GENERATION BY RECURSIVE EQUATION*/ #include "aiccomc.c" /*AIC comm routines */ #include "math.h" /*math library function */ #define sample_freq 10000 /*sample frequency */ #define sine_freq 3000 /*desired frequency */ #define pi 3.14159 /*constant pi */ int AICSEC[4] = {0x162C,0x1,0x3872,0x67}; /*AIC config data*/ void sinewave(float A, float B, float C) { float y[3] = {0.0,0.0,0.0}; /*y[n] array */ float x[3] = {0.0,0.0,1.0}; /*x[n] array */ int n = 2, result; /*declare variables */ while(1) { TWAIT; y[n] = A*y[n-1] + B*y[n-2] + C*x[n-1]; /*determine y[n]*/ result = (int)(y[n]*1000); /*out y[n] scaled by 1000 */ PBASE[0x48] = result << 2; /*output to AIC */ y[n-2] = y[n-1]; /*shift y's back in array */ y[n-1] = y[n]; x[n-2] = x[n-1]; /*shift x's back in array */ x[n-1] = x[n]; x[n] = 0.0; /*set future x's to 0 */ } } main() { float Fs, Fosc, w, T, A, B, C; /*declare variables */ AICSET(); /*initialize AIC */ Fs = sample_freq; /*get sampling frequency */ Fosc = sine_freq; /*get oscillator frequency*/ T = 1/Fs; /*determine sample period */ w = 2*pi*Fosc; /*determine angular freq */ A = 2 * cos((w * T)); /*determine coefficient A */ B = -1.0; /*coeff B is constant */ C = sin((w * T)); /*determine coefficient B */ sinewave(A, B, C); /*call sinewave function */ }