Pages

Total Pageviews

Wednesday, October 10, 2012

Generation of sine wave using SPWM in PIC16F684

I have previously shown how to calculate the values for the
sine table. Now I will show you how to use that sine table for generating a sine
wave using a PIC16F684. Why PIC16F684? It is a nice little 14-pin PIC that
contains all that is needed for SPWM (sinusoidal pulse width modulation) – the ECCP module. Since the ADC or
comparator or other peripherals are not used and there are enough pins, I selected
the PIC16F684.

To make the process of updating sine table value simple, table
pointer was used. The frequency here is set to 50Hz and the switching frequency
is 16kHz. The 16F684 generates SPWM signals on P1A, P1B, P1C and P1D pins which
should then be connected to a full-bridge stage for feeding into transformer.

16kHz frequency is used since it is toward the end of the
audible spectrum and so the noise emitted will not be intolerable. Frequency
above 20kHz is not used as 20kHz is the maximum frequency for the ECCP module
of 16F684.

Since the ECCP module and interrupt take care of
the SPWM, it is being executed by the hardware modules. So, while these
are running, you can do other stuff as well by coding them in the
while(1) loop, which, as you can see, is blank now, since no other task
is being carried out besides SPWM.

Dear Tahmid I have faced the same problem here. There is a glitch at every half cycle(after low pass). I have tried with several sine table and discrepancy disappear only when the table is generated with the peak of 100 with your smart sine. Thanks Tahmid for your great effort.

Hi, thanks for your reply. I tried many time to simulate it but the discrepancy still occurs. However when I filter the two pwm half-waves with a low pass filter, I obtain a beautiful sinewave.Thanks so much for your attention.

Hi, thanks for your reply. I tried many time to simulate it but the discrepancy still occurs. However when I filter the two half-waves with a low pass filter, I obtain a beautiful sinewave. Thanks for your attention.

Hi, thanks for your reply. I tried many time to simulate it but the discrepancy still occurs. However when I filter the two half-waves with a low pass filter, I obtain a beautiful sinewave. Thanks for your attention.

Hello Tahmid, I am trying to figure out they way of putting this c code and assemble it with either MPLAB or MicroC.I understand how it works and everything but I actually want to put more sample values into the step and after I modified I tried to assemble but it just does not work.

Is it possible that you can help me realize what is that I'm doing wrong on this software assembler. you don't really need to go into specifics if you don't have the time, just a general setup of the project and creation of the main c file for the project.

I have already created the SPWM and already created the H bridge and the LC filter... if you need to see it I have no problem...

Hello again Tahmid, I don't even now if you are going to answer my question but I already solve it myself. Now what I'm trying to do is to use 64 steps now that I figure out that any number steps higher than that wont work. The problem with this is that my SPWM signal is getting disrupted. I changed the sin_table to 64, and leaved the rest the same. Is there any other modification that I need to do in the code?

Oh no.... The signal coming from the PIC is perfect at least from my perspective.

I did use your sine table generation tool (Very Useful By the way!)

Here is actually TWO picture of my circuit, one shows the output of the PIC ( the 4 different waveforms ) and the other one, the output before (yellow) and after (pink) it passes through the LC filter.

I have much more to show you Tahmid, but I would rather go into specifics (detailed numbers for my design by contacting you trough email. This circuit design is for my graduation and I am in a competition with a couple of other groups. (I can tell you a little bit more about it if you like).

I you think that is fine please let me know what do you think, I really have much more to ask you about.

The SPWM signals are fed to MOSFET drivers which drive MOSFETs that are set up in a full-bridge configuration. The output of the full-bridge is filtered with an LC filter to obtain the pure sine wave output.

how d SPWM signals(fig 2) r given to mosfets in a H-bridge..???yellow>> Q1 (high side 1)blue>> Q2 (low side 1)red>> Q3 (high side 2)green>> Q4 (low side 2)high side fets switching at 50hz to lower losses...filtered o\p of bridge will b sine wave... green sig inverted from blue one or phase shifted 90deg without inverting ...???

dim LCD_RS as sbit at RB2_bit LCD_EN as sbit at RB3_bit LCD_D4 as sbit at RB4_bit LCD_D5 as sbit at RB5_bit LCD_D6 as sbit at RB6_bit LCD_D7 as sbit at RB7_bit

LCD_RS_Direction as sbit at TRISB2_bit LCD_EN_Direction as sbit at TRISB3_bit LCD_D4_Direction as sbit at TRISB4_bit LCD_D5_Direction as sbit at TRISB5_bit LCD_D6_Direction as sbit at TRISB6_bit LCD_D7_Direction as sbit at TRISB7_bit

Ooops! I had declared it to use as a 16-bit variable when required in the code. I did that for debugging purposes. But I seem to have forgotten to remove the declaration of the variable when I removed the parts using that variable. You can just ignore that.

HiAm doing a project on speed control of single phase induction motor using variable frequency igbt power inverter. Am failing how to link the microcontroller and igbt circuit. please help me out with the designing of the circuit.

The microcontroller generates the varying SPWM signals for the IGBT circuit. The link will be a bridge driver. Since it's single phase, I assume you're using full-bridge. Then you can use IR2110 as the driver. I've written an article on this that you should find helpful:

I am new to programming Pic i just wanted to know if what compiler you're using of the above code, I tried to use CCS Compiler but seems many error occur, I just wanted to have basic circuit of sine wave reproduction using PIC, hope you could advise me where to start.

Hi Tahmid,Hope all is well with you.I am still working on this one, all is good except for the saw tooth i'm getting at the rc low pass filter.I've tried many many rc configuration and even use a program to calculate the best rc combo for 50 and 60hz but i'm still getting the saw tooth wave.Hope you gotten my email with all the info about it.Waiting patienly for your reply.

I've gone through the files. Sorry for the delay. I just arrived in Brooklyn a few days back and was busy with some other stuff.

Here are my comments regarding the circuits.

1) I haven't simulated the entire thing with drivers and MOSFETs in PROTEUS. PROTEUS doesn't handle it well and fails to simulate - with errors like that you mentioned. So, for the entire circuit, I just test it out on hardware.

2) With that in mind, you need to use larger bootstrap capacitors. Use 68uF bootstrap capacitors in parallel with 100nF ceramic capacitors. I'm sure you know this and removed the larger capacitor for simulation purposes, as you mention in the email.

3) Replace all the 1N4007 diodes with ultrafast diodes, such as UF4007. This will reduce reverse recovery losses.

4) Don't use VDD = VCC. Use VDD = +5V.

5) Experiment with LC filters at the output. Select LC filter such that the output impedance isn't too high and the capacitance isn't too large that it causes high rush currents.

6) Connect 100nF ceramic capacitors across all power lines.

7) Besides that, everything seems fine. You should go on and build the circuit and let me know how it goes.

8) Where do you live? I only knew where to get transformers in Dhaka. You can order the transformers online but I haven't done that myself. You can easily get the cores in the local market or online and you can wind the transformer yourself.

9) I am happy to be blogging and am happy to share my knowledge. If you do want to donate, do so by knowledge and not monetarily or in other ways. Visit my blog regularly and provide feedback and suggestions, and do let your friends know about the blog.

Many thanks for your reply and no need to apologize for the delay.Many thanks for all the extra informations and tips and i will definitely implement them in my builds.Yes on the simulation in proteus, it is helpfull but it doesnt react like the real life situations many times wich can confuse people.I have almost everything now to start building, i only need the toroid but i have found a store in Germany where i can buy FT140-77 ones wich seems pretty good for this smps style inverters.I live in Holland and i can get almost everything from ebay.

Both Sine pwm should be starts generated based on the External Interpret , 1st External Interpret to 2nd External Interpret time gap will be 10mS .

Once 1st External Interpret comes, based on the falling edge detection - 1st channel Sine pwm should be start , at the end of 1st External Interpret , or Start of 2nd External Interpret 1st channel Sine pwm should be stop and same time 2nd Channel Sine pwm should be start .

At starting of both the Sine pwm should be start with Minimum to maximum ( like soft starting )

Once the both pwm reached minimum to maximum bandwidth , then Analog input should be start monitoring . if this analog input reaching 3Volts or try to increase above 3Volts , then both the Sine pwm band width start decrease . vs-varsa .

Hello Tahmid, I want to generate a PWM with 16f1827 and control my H_Bridge but the main problem I have is the MOSFET drive I want to make ones with BJT transistors,can you please suggest help for this design;Previoiusly I used 16f876a with simple biased BJT which control the gate of MOSFET but I didn'ty succeed can you please help me for that.And also I don't understand how you calculate the table values because when I calculate using your formula the output signal is not good.

Hello tahmid, the problem I am having with the SPWM is that I am using the formula you used in your logs but when inserting the results in the program I don't get the good signals as yours,why?see the codes below :

Hello, Tahmid can you please help me to adjust your codes so I can use PIC16F1827?Please consider my modifications below: unsigned char sin_table[32]={0,25,49,73,96,118,137,159,177,193,208,220,231,239,245,249,250,249,245,239,231,220,208,193,177,159,137,118,96,73,49,25};

buddy,i need a little help for my project. i need to compare two sine waves, one is off shifted and another is normal one with the same ramp signal of frequency 15khz. i have done them in matlab but have no idea how to do the hard ware part i.e., using pic. i will send u the details and the matlab files to ur email id. so plz reply to my email aditya.reddy85@gmail.com

i saw ur article..i designed 'MULTI INPUT MULTI OUTPUT DC-DC CONVERTER' with two inputs and two outputs..i have two mosfet switches in input side as well as in output side..may i knw is it possible to use pic16f684 for my converter or not? if not,tell me any other controller to produce gate signal for mosfet. may i email my circuit to u? if yes,tell me ur email.id

Hi, Tahmid. I tried to port the code to PIC 18F4520 running with 16Mhz Crystal with no pre/postscaler. But its not working. Simulation in Proteus shows continuous PWM signal on P1A only.Can you please help me out with the code? Here is the modified code:

Hi Tahmid, I m getting the simulation output from PIC16f877A. But couldn't get the output from IR2110 driver in real time hardware. I have used the same connection suggested by you for the driver. i dont get output from the Low side output pin( pin 1) but pin 7 shows output voltage. What I am wrong with? I have used the same connections u have given. Please help me to complete the project.

Hello Tahmid,Good post. I have built a circuit for DC/AC with DC-DC stage, I used your sinePWM code for the DC-AC stage but I run into a slight problem. When I measure the high frequency output from the PIC16f684 using oscilloscope, I got the required 16khz but the low frequency output is given only 12Hz(aprrox 82ms on the time base for period). Please, can you clarify where the problem is coming from

i'm trying to use the pic18f4520 to do this code, but i'm not getting the outputs P1B,P1C and P1D here is the code for a crystal of 20MHz and carrier frequency 20Khz, I need some help to finish my project. Thank you.

that a great work Tahmid, Im wondering if I can control the frequency of the Output sine wave from PC via UART for example , im trying to build a computer based sine wave signal generator by sending the frequency each time from pc , Could u please tell me how is that done if possible :D

Hello Tahmid I'm trying to make a DC-AC inverter but with a switching frequency of 85 kHz. What microcontroller would you recommend me to use? I'm planing on using two IR2013 MOSFET drivers and a 24 characters LCD display to show simple data like the system is ON or Off... Any advice will be greatly appreciated

You may look into the dsPIC33 or the PIC32 series. It also depends on the amount of PWM resolution you want.

What microcontrollers have you used before? What microcontroller can you use? For example, if you come from a "PIC background", moving to the dsPIC or the PIC32 wouldn't be too difficult. If you've worked with ARM before, you can look into some sort of ARM microcontroller, maybe a Cortex M3 for example.

Hello Tahmid I'm trying to do this code with pic18f4550 but this pic doesn't have on ECCP module the option that reverse the bridge. Can you help me ?Here is the code:unsigned char sin_table[32]={0,25,49,73,96,118,137,159,177,193,208,220,231,239,245,249,250,249,245,239,231,220,208,193,177,159,137,118,96,73,49,25};

Hello Tahmid, I am having difficulties by calculating the sine table values because the output I m getting is not good at all;I tried to read and follow your tutorials on the blogs but I am not sorting out the issue,I want to control an H-bridge with at 10KHz,with a quartz of 12MHZ and the DC_AC converter must be of 50HZ at the output; if implemented I want to have a stable 220VAC also meanwhile I need a feedback, can you please help me to understand very well with those values how I can get the SET_FREQ value and the sine table values? I tried to calculate and see what I am getting PR2=249;TBL_POINTER_NEW >> 13 (it shifts 13 bits);each value has to be called 3 times ans so I found that the SET_FREQ=683,can you please tell me where I am wrong? I followed tour tutorials at http://tahmidmc.blogspot.com/2013/02/demystifying-use-of-table-pointer-in.html.I am using 16F1936 as a PIC

Hello Tahmid, great work u have done. Please i want to know if u can write code like low voltage dectector that can stop the pwm and automatic voltage control for output to prevent it from going above 220v and below when load is apply(feedback). Also Low voltage led indicator.

Hello, Tahmid. I'm Vladimir from Uzbekistan. Thank you for the nice article about using the pic 16f684 for spwm generation. Currently I'm working on a 5kW pure sine wave inverter. I've made the circuit based on pic16f684 and two IR2110. It works correctly, but ... But there's one little problem: I have pure sine wave only when the load on the output of my inverter is greater than 300W. If the load is less than 300W, the sine distorts, it reminds of square wave. If I don't connect the load at all, the output is clear square wave. I know, what the problem is: the capacitor of output LC filter under light load charges quickly even if we have low duty cycle. It remains charged during the whole half sine wave. That's how square wave instead of sine wave is generated. I've tried to lower duty cycle under no load, it doesn't help. 50Hz square wave remains. May be you have some suggestions about this? And by the way, there is not problem in LC filter. I'm using factory made LC-filter from broken 5kW inverter. This factory made 5kW inverter also has driving PCB for full-bridge. Only it contains pic16f716 instead pic16f684. I think my problem is in software. And... I've examined the factory made PCB. It has strange connections. It uses pic16f716 in half-bridge mode, not in full - bridge mode. Only P1A and P1B are used and additional, not P1C and P1D. And all four pin connected in a tricky way to IR2110 through 8 1n4148 diodes and pullup and pulldown resistors. I've found ISIS simulation picture of such connection. It has interesting output signals. The shape of output signals you cal see here:

http://sivaprojects.blogspot.com/2013/09/high-frequency-inverter.html

Any suggestions on this point? Thank you a lot in advance for possible help.

It is probably because unipolar switching is issue. Tahmid's circuit is simple, and does not include any control, just SPWM conversion.

Unipolar goes from 0 to +V each positive halfwave and from 0 to -V each negative halfwave and during full halfwave it "kicks" capacitor always in single direction, i.e. pulsed DC, and that charge must be removed by heavy load to form a sine wave voltage across it. Otherwise capacitor will just maintain a stored charge and resulting voltage will be closer or equal to square wave.Bipolar goes from -V to +V each high frequency switch event, thus recharging LC filter and moving it slowly from -V to +V and vice versa. Even without load connected this will form a good sine wave because there is always a high frequency SPWM AC.

For LC filter values, you know by experiment but the formula is 1/((2pi (root(LC))). IF YOUR design uses low frequency transformer then the Leakage inductance is inherently your L. hence vary only capacitance values

I uploaded the firmware to a PIC16F684, build the H bridge with 4*IRFP460 and at the output after the low pass filter i have a nearly square wave output. With 40w light bulb the wave starts to look like a sine wave and with about 400w load i have a beautiful sine wave.Can you help me to obtain a sine wave even at low power, something like 10w?

Nice work Tahmid, I need two signal to be able to feed the output to a center taped transformer, but the signals from the pic is four, please how do i combine the four signal so i can use ir2110 to drive my fets and feed a center taped transformer. need ur help ASAP.

Hello! I was able to generate the same wave as shown in the first image, but when I connect the MOSFET circuit the following error appears: http://i.imgur.com/mCkCLcc.pngDoes anyone know what that means?

Hello,Tahmid could you please help me to find how the three phase asynchronous motor can be controlled with PWM controlling the six IGBTs,I read the AN 889 on microchip.com but the codes are incomplete so need help.

stop relying on just proteus results. find project board and get put the code into an actual pic (pic16f684). Afterwards check the various pins with an oscilloscope...you can send me your email address for further help.

Noah TJ March i'm making sine wave inverter using ATMEGA16 i done the conversion of 12vdc to 220 v ac but i need to make the feedback (closed loop) to control the output when loads changes kindly help me in thsi matter

Hi Tahmid,I got this output of spwm from pic16f684. But wave is not accurate. Can you please verify it have i got the correct output?https://drive.google.com/drive/folders/0B5o1G2oMcz2YeG9lb0ZZQm9JZDg?usp=sharingI have used 16MHZ crystal.

I think you either need:- A µIC (microcontroller) which can emit three phase signals at same time, do a phase shift for you in software. Look at frequency converters for AC motors.- Or analog circuit to do (desired) phase shift. You can try CR phase shift with several op amps.

Recent Posts

Translate this blog

Search This Blog

Follow by Email

About Me

I am Syed Tahmid Mahbub, from Dhaka, Bangladesh, born on August 1, 1994.
Electronics is my passion and from class V, I have been learning electronics. I learnt and worked mostly on SMPS, power electronics, microcontrollers and integration of microcontrollers with SMPS and power electronics. I've used PIC and AVR microcontrollers - PIC 10F, 12F, 16F, 18F, 24F, dsPIC 30F, 33F, PIC32, ATmega and ATtiny, integrating them with various SMPS and power electronics circuits.
I have completed my Bachelor's degree from Cornell University (Class of 2017) in Ithaca, New York, USA, majoring in Electrical and Computer Engineering (ECE).
I am a member of the forum www.edaboard.com, where I am an "Advanced Member Level 5" (the highest level attainable) and also the forum allaboutcircuits.com, where I am a "Senior Member". I post to help solve electronics-related problems of engineers and engineering students from all over the world.
I love watching and playing cricket and football (soccer), and listening to music.
I am now a hardware engineer at Apple in Silicon Valley, California, USA.