Mechatronics geek blog

Monday, April 1, 2019

Generate Sine Wave using ATmega32

Sine wave Equation:

y=sin(x) ; where x is  the angle in radian. in a whole circle there are 2*PI radians . This means x varies from 0 to 2*3.14(PI) [0,6.28] => Half circle [0,+3.14] Other half [0,-3.14]

As sin may be negative we need to handle this situation by shifting the 0 to 127 ( 127 is our new ZERO line) anything above 127 would be +ve and anything below 127 would be -ve.

One more thing, The output of sine is from -1 to +1 so we need to scale it up by multiplying it with 127 So the Final Equation is:

                                      y=127+(127*sin(x))

Now, we need to divide the sine wave to points that will be plotted :

x=0;
for(i=0;i<100;i++)
{
   myArr[i]= 127+(127*sin(x));
   x=x+0.0628; /* as in whole circle there are 6.28 radians so 0.0628 is 1/100 of 6.28 as we need 100 points */
}


0 comments:

Post a Comment