In this tutorial, we are going to make Pure sine wave generation using PIC microcontroller. We can use the PIC16F73 or PIC16F76 microcontroller for this purpose. We will learn how SPWM is generated and how we can get a pure sine wave from SPWM. After learning the basic concept you can implement this into your project. So be with me and read to the end of this article.
Disclaimer: Electricity is always dangerous. Proper skill is required to work with electricity. Do work at your own risk. The author will not be responsible for any misuse or harmful act or any mistake you make. The contents of this website are unique and copyright protected. Kindly don’t do any nonsensical act of copying and claiming it as yours. Most of the articles published here are kept open-source to help you. Take the knowledge for free and use it, but if you are interested you can buy the ready resources offered here. If you need any help or guidance feel free to comment below, the author will try to help you. Also, there can be affiliation links in the article. Which will not affect you anyway, but allows the author with some commission. So please don’t take it otherwise. Thanks.
Pure sine wave generation techniques:
Generating pure sine waves is one of the most interesting works in power electronics study. A pure sine wave can be generated in different ways. You can use either
- Analog Circuits designed with Op-Amp
- Using ICL8038 function generating IC
- SPWM generation with Op-Amp circuits.
- Using SPWM generated with a micro-controller.
You may find other ways to generate a sine wave. Whatever the way you select it depends on your choice. In this article, we will discuss the last technique from the list mentioned above.
What is SPWM:
SPWM means Sinusoidal Pulse Width Modulation. A PWM signal with variable duty cycle according to a sinusoidal wave. It is something like this:
Here there is a triangular wave and a sine wave of low frequency than the triangular one. If we compare these two signals with the help of an Op-Amp, we will get a PWM signal. This PWM signal has a variable duty cycle, which is increasing slowly in the midpoint of our reference sine wave and decreasing in corners.
SPWM feature of micro-controller:
No, the PIC16F73/76 micro-controller does not have any special feature to generate SPWM. But it can generate PWM which is known as the CCP module of the micro-controller.
Using this CCP module we can generate a PWM signal. Now, all we need to do is changing the duty cycle of this PWM signal in a sinusoidal way so that we can get a pure sine wave after filtering this signal. Now we will do coding for SPWM. But to make a sine wave, here we are using a lookup table. This table can be self-written or you can use software to generate your sine table. I’ll use Smart Sine to generate my sine table here.
Sine table generation using Smart Sine:
This tool is very easy to use. Just put your No. of table entries, peak point, and angle. You’ll get a sine table clicking the calculate button.
So here
sin_table[32]=
{0, 25, 49, 73, 96, 118, 139, 159, 177, 193, 208, 220, 231, 239, 245, 249, 250,
249, 245, 239, 231, 220, 208, 193, 177, 159, 139, 118, 96, 73, 49, 25}; is our sine table of 32 entries.
MicroC Code:
/******************************************************************************* * Pure sine wave generation technique using PIC microcontroller * * Program Written by_ Engr. Mithun K. Das * * MCU:PIC16F73; X-Tal: 20MHz; mikroC pro for PIC v7.6.0 * * Date: 13-04-2020 * *******************************************************************************/ unsigned char sin_table[32]= {0, 25, 49, 73, 96, 118, 139, 159, 177, 193, 208, 220, 231, 239, 245, 249, 250, 249, 245, 239, 231, 220, 208, 193, 177, 159, 139, 118, 96, 73, 49, 25}; int duty=0; void Interrupt() iv 0x0004 ics ICS_AUTO { if (TMR2IF_bit == 1) { duty++; if(duty>=32) { duty=0; CCPR1L=0; asm nop;// no operation for one cycle } CCPR1L = sin_table[duty]; TMR2IF_bit = 0; } } void main() { TRISC = 0x00;//all output PR2 = 249; CCP1CON = 0x4C; TMR2IF_bit = 0; T2CON = 0x2C; TMR2IF_bit = 0; TRISC = 0; TMR2IE_bit = 1; GIE_bit = 1; PEIE_bit = 1; while(1) { //infinity loop } }//end
Code Explanation:
You already know that we are using a sine table here named,
sin_table[32]=
{0, 25, 49, 73, 96, 118, 139, 159, 177, 193, 208, 220, 231, 239, 245, 249, 250,
249, 245, 239, 231, 220, 208, 193, 177, 159, 139, 118, 96, 73, 49, 25};
Now, there is no limit of the number of entry for you sine table. More the entry, more the pure sine wave you’ll get. Only one thing to know that more entry will consume more memory. Anyway, here we will use 32 entries.
That means, if we want to get a sine of 50Hz, we have 10ms in each half-cycle. So we have to use a timer interrupt which is called 32 times within this 10ms.
void Interrupt() iv 0x0004 ics ICS_AUTO { if (TMR2IF_bit == 1) { duty++; if(duty>=32) { duty=0; CCPR1L=0; asm nop;// no operation for one cycle } CCPR1L = sin_table[duty]; TMR2IF_bit = 0; } }
Here is that interrupt where we are using Timer2. Timer2 is configured by this code here to generate interrupt 32 times within 10ms.
PR2 = 249; //Prescaler CCP1CON = 0x4C; //PWM control TMR2IF_bit = 0; T2CON = 0x2C;//Timer2 control bit settings TMR2IF_bit = 0; TRISC = 0; TMR2IE_bit = 1; GIE_bit = 1; PEIE_bit = 1;
PR2 is Prescaler and CCP1CON is the PWM control bit. And T2CON is a timer2 control bit. You can read the datasheet to know more about this.
Test Result:
We can test the simulated result using proteus. Note that you’ll find a small spike in the sine wave because of our sine table value. If you can edit the value if required to get a 100% pure result.
Now you can try your own to generate pure sine waves using PIC16F73. To get the result with PIC16F76, just use the same hex. It will work fine.
For Professional Designs or Help:
Also let me know what you want to get as the next article, comment below!
Read more:
Connect to Raspberry Pi from your Laptop/Desktop using VNC Viewer
How to reduce noise from DC motor
Interfacing External EEPROM with PIC microcontroller
Not Enough ROM/RAM error with micro-controllers
Read ThingSpeak Channel using ESP8266 and Arduino
Caller ID detection using Arduino
Simple voltage protector for your appliances
DC/DC converter using Transistors
Simple Component Detector using PIC16F877A
Voltage Stabilizer Circuit with 4 Relays
6V Lead-Acid battery charger circuit
5 coolest multimeters you can buy
Top 5 bench power supplies you can buy
Top 5 handheld oscilloscopes you can try
Top 5 Digital Multimeters for beginners
Digital Clock with DS3231 & PIC microcontroller
Digital Clock with PIC16F676 & Seven Segment Display
Digital Clock using a PIC microcontroller & RTC DS1307
How to remove noise/garbage from the HD44780 LCD display
How to design a voltage stabilizer using a micro-controller from A to Z
Capacitor Power Supply parts calculator
Solar Charge Controller circuit & working principle of ON/OFF charge controller