+ Post New Thread
Results 1 to 10 of 10
  1. #1
    Newbie level 6
    Points: 70, Level: 1

    Join Date
    Dec 2016
    Posts
    11
    Helped
    0 / 0
    Points
    70
    Level
    1

    Generate a sine wave using a PIC microcontroller

    I am using a PIC18f6722 microcontroller and I want to generate a sine wave at 60Hz. The complier I am using is CCS C. I have the PIC hooked up to a function generator so I can see the sine wave on the oscilloscope. I want to write a code that re-creates the same sine wave on CCS C so I can output it on labVIEW.

    I am new to programming microcontrollers so I am not sure where to start. Thanks.

  2. #2
    Advanced Member level 5
    Points: 14,280, Level: 28

    Join Date
    Nov 2012
    Posts
    2,650
    Helped
    613 / 613
    Points
    14,280
    Level
    28

    Re: Generate a sine wave using a PIC microcontroller

    You need to understand that you will be able to output only a 1+sin(t) because you may not be able to show the negative side.

    The most common way is to use the PWM output and convert this to a sine wave by using a simple filter.



    •   AltAdvertisment

        
       

  3. #3
    Newbie level 6
    Points: 70, Level: 1

    Join Date
    Dec 2016
    Posts
    11
    Helped
    0 / 0
    Points
    70
    Level
    1

    Re: Generate a sine wave using a PIC microcontroller

    Are there sample codes I can reference to for this?

    - - - Updated - - -

    Quote Originally Posted by c_mitra View Post
    You need to understand that you will be able to output only a 1+sin(t) because you may not be able to show the negative side.

    The most common way is to use the PWM output and convert this to a sine wave by using a simple filter.
    Are there sample codes I can reference to for this?



    •   AltAdvertisment

        
       

  4. #4
    Advanced Member level 5
    Points: 13,005, Level: 27

    Join Date
    May 2015
    Location
    Melbourne, Australia
    Posts
    2,213
    Helped
    751 / 751
    Points
    13,005
    Level
    27

    Re: Generate a sine wave using a PIC microcontroller

    The simplest solution might be a lookup table, it will certainly be faster.

    Here is a handy calculator that will give you the values for a sinewave of any length or amplitude.
    https://www.daycounter.com/Calculato...lculator.phtml
    Cheers, Tony.


    1 members found this post helpful.

    •   AltAdvertisment

        
       

  5. #5
    Super Moderator
    Points: 63,245, Level: 61
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    12,932
    Helped
    2986 / 2986
    Points
    63,245
    Level
    61

    Re: Generate a sine wave using a PIC microcontroller

    Hi,

    Are there sample codes I can reference to for this?
    Countless.
    Here in this forum and many more in the internet.

    Do a search for "spwm".

    Klaus



  6. #6
    Advanced Member level 5
    Points: 14,280, Level: 28

    Join Date
    Nov 2012
    Posts
    2,650
    Helped
    613 / 613
    Points
    14,280
    Level
    28

    Re: Generate a sine wave using a PIC microcontroller

    Quote Originally Posted by Warpspeed View Post
    The simplest solution might be a lookup table, it will certainly be faster...
    A simple lookup table has several important advantages (apart from being faster):

    1. you need to store only half of the points, the remaining points can be generated by simply negating.

    2. you can get a decent sine curve with 36 or even 18 points- perhaps a simple filter at the output may be needed but most often the load itself will act as the filter.

    3. You can generate phase shifts (90, 120 or 180 phase shifts are most common) simply by shifting the count base.

    4. Perhaps you will get more time in the interrupt service routine (keep that simple!)



  7. #7
    Advanced Member level 5
    Points: 13,005, Level: 27

    Join Date
    May 2015
    Location
    Melbourne, Australia
    Posts
    2,213
    Helped
    751 / 751
    Points
    13,005
    Level
    27

    Re: Generate a sine wave using a PIC microcontroller

    Depending on your requirements, if memory is very limited a very basic sine lookup table only really needs to cove 0 to 90 degrees.

    And if you need much finer resolution, you can always interpolate between values stored in lookup.
    Cheers, Tony.



  8. #8
    Newbie level 3
    Points: 1,404, Level: 8
    Achievements:
    7 years registered

    Join Date
    Sep 2009
    Posts
    4
    Helped
    0 / 0
    Points
    1,404
    Level
    8

    Re: Generate a sine wave using a PIC microcontroller

    You can have look at the basic working theory @ http://www.ocfreaks.com/sine-wave-ge...ller-tutorial/ Its implemented for LPC2148 but the underlying working is the same. You need to generate or use a hardcoded sine lookup table which feeds values to your PWM block and at the PWM output use a filter which acts a DAC converting your varying PWM output to sinewave output.



    •   AltAdvertisment

        
       

  9. #9
    Advanced Member level 5
    Points: 14,280, Level: 28

    Join Date
    Nov 2012
    Posts
    2,650
    Helped
    613 / 613
    Points
    14,280
    Level
    28

    Re: Generate a sine wave using a PIC microcontroller

    Depending on your hardware, you may need to store the values in the integer format in the range of 0-255 (8 bit; unsigned) and as others have pointed out earlier, you need to store only for the range of 0-90 degrees. If you are using PIC with the built in PWM facility, you need not use more than 16 points (for 0-90 range) in my opinion and perhaps use a low pass filter in the hardware.



  10. #10
    Super Moderator
    Points: 63,245, Level: 61
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    12,932
    Helped
    2986 / 2986
    Points
    63,245
    Level
    61

    Re: Generate a sine wave using a PIC microcontroller

    Hi,

    The most simple is to output a square wave and use high order low pass filters to attenuate the overtones.

    Klaus



--[[ ]]--