Jan 3, 2017

How to generate sine wave using 8051 microcontroller

Yes we can generate sine wave using 8051 microcontroller even though 8051 microcontroller doesn't have any analog output pin.
We will following following steps in this article to generate sine wave on 8051 microcontroller.


  1. We will generate digital values of sine wave on a port that is 8 bit binary value.
  2. We will convert that digital value into analog value to take that 8 bit output on 1 pin.
  3. Generated sine wave is in steps hence to obtain a pure sine wave, we will pass it through low pass filter. Thus by remove high frequency part, we will obtain smoother sine wave.
Firstly we have to generate digital values for sine wave. For this example we will take 16 points in 1 cycle. Thus 1 value will hold for 1/16th of 360 degree. Hence we will use sine(360 * (i/16)) where i runs from 0 to 15. This will cover 16 equally spaced points in one cycle. We will place this cycle in while(1) loop so that we will get continuous sine wave.
In a cycle of sine wave, half cycle is positive and remaining half cycle is negative. Since microcontroller cannot have negative voltage, we will shift sine wave to half of maximum value. As maximum value is 255 for 8 bits, half of it is 127.5
Thus digital value to be assigned to port is 127.5 + 127.5 * sine(360*(i/16)) where i runs from 0 to 15. Here minimum value is 127.5 - 127.5 = 0 and maximum value is 127.5 + 127.5 = 255 Hence Our sine wave will be between 0 and 255 and which can be assigned to port. Since most of the values will come in fraction, we have to round figure them to assign integer value.
This obtained value is given to digital to analog converter. Here we are using weighted register DAC. Refer this article to learn working of weighted register digital to analog converter.
Output of DAC is given to RC low pass filter to remove high frequency noise.



Program:
 #include<reg51.h>  
   
 int main(void)  
 {  
    //Digital values of sine wave  
    unsigned char x[16]={127,176,218,245,255,245,218,176,128,79,37,10,0,10,37,79};  
      
    unsigned char i;  
   
      
    while(1)  
    {  
       for(i=0;i<16;i++)  
       {  
          P1=x[i];  
       }  
    }  
 }  

Keil logic analyser output:
Sine wave in logic analyser
Sine wave in Keil
Proteus simulation:
Proteus simulation circuit of sine wave using 8051
Proteus simulation



Note:
  • You should use more samples i.e. higher value of i so that you will get smoother wave.
  • Add delay to change frequency.
  • Use buffer before using this sine wave as generator to avoid effect of load on generation of sine wave.
Download proteus file and .c file from this link.
Comment if you need any related to generation sine wave of different frquencies using buttons/keys.

5 comments:

  1. How to change a frequency
    Please help me

    ReplyDelete
  2. How to generate cosine wave?

    ReplyDelete
    Replies
    1. Inside code, put digital values for cosine wave. Cosine is 90 degree shifted some wave. So put first 4 digits(equal to 90 degree) at last and run the code.

      Delete
  3. Could you please upload the proteus file and .c file again? The download link does not work anymore.
    Thank you!

    ReplyDelete