Embedded Systems and Power Electronics

Total Pageviews

About Me

My photo
I am currently a PhD student at UC Berkeley, following a 6-year journey working at Apple after my undergrad years at Cornell University. I grew up in Dhaka, Bangladesh where my interest in electronics was cultivated, resulting in the creation of this blog.

BTemplates.com

Powered by Blogger.

Oct 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.


Let’s run the 16F684 from a 16MHz crystal oscillator and use a 16kHz switching frequency.
So, the required value of PR2 is 249.

The sine table (for half a cycle) is:
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
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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};


unsigned int TBL_POINTER_NEW, TBL_POINTER_OLD, TBL_POINTER_SHIFT, SET_FREQ;
unsigned int TBL_temp;
unsigned char DUTY_CYCLE;

void interrupt(){
     if (TMR2IF_bit == 1){
        TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ;
        if (TBL_POINTER_NEW < TBL_POINTER_OLD){
           CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge
        }
        TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 11;
        DUTY_CYCLE = TBL_POINTER_SHIFT;
        CCPR1L = sin_table[DUTY_CYCLE];
        TBL_POINTER_OLD = TBL_POINTER_NEW;
        TMR2IF_bit = 0;
     }
}

void main() {
     SET_FREQ = 410;
     TBL_POINTER_SHIFT = 0;
     TBL_POINTER_NEW = 0;
     TBL_POINTER_OLD = 0;
     DUTY_CYCLE = 0;
     ANSEL = 0; //Disable ADC
     CMCON0 = 7; //Disable Comparator
     PR2 = 249;
     TRISC = 0x3F;
     CCP1CON = 0x4C;
     TMR2IF_bit = 0;
     T2CON = 4; //TMR2 on, prescaler and postscaler 1:1
     while (TMR2IF_bit == 0);
     TMR2IF_bit = 0;
     TRISC = 0;
     TMR2IE_bit = 1;
     GIE_bit = 1;
     PEIE_bit = 1;
    
     while(1);
}
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Download the hex file from:
http://www.4shared.com/file/PlETv7nt/SPWM684.html
https://rapidshare.com/files/3811707712/SPWM684.hex

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.

Generating the sine table:

The SPWM signals:

The generated sine wave:

142 comments:

  1. Hi tahmid, I have tested the circuit with PIC16F684 in Proteus with an external crystal 16Mhz (it's correct?), but the output waveform present a discrepancy, see the image below :

    http://s8.imagestime.com/out.php/i790003_Immagine.png
    http://s9.imagestime.com/out.php/i789994_Immagine.png

    now, is it a program error or I doing something wrong ?

    ReplyDelete
    Replies
    1. i have run the simulation in proteous but it does'nt seems to work..plz provide me the complete model if you run it

      Delete
  2. The discrepancy isn't supposed to be there. It could be an error in the simulation.

    Run the simulation a few more times and see if you can notice the discrepancy. Also see if you can find it at any other points. Then, it will help to figure out where the problem lies.

    Try and observe the generated sine wave, by filtering the signal. If there really is a problem, you will notice a distortion in the sine wave generated.

    ReplyDelete
    Replies
    1. 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.

      Delete
  3. 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.

    Kind Regards.

    ReplyDelete
    Replies
    1. Hello! may themselves apply for the PIC16F684 hex file it? appointed his 50hz inverter. hainguyenyork@gmail.com .cam you email them thank you

      Delete
  4. 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.

    Kind Regards.

    ReplyDelete
  5. 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.

    Kind Regards.

    ReplyDelete
  6. Sorry for duplicate comments !!!

    ReplyDelete
  7. I think the discrepency observed is due to simulation issues.

    Glad to know that the sinewave is generated as required. Hope you can complete your project successfully!

    ReplyDelete
  8. 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...

    I really hope you can really help me...

    Thank you!

    ReplyDelete
  9. 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?

    I hope you can help me with this,
    Thanks.

    ReplyDelete
  10. What do you mean by "getting disrupted"?

    Did you use correct values for the tables? How did you calculate/generate them?

    Can you show the waveforms of your SPWM signal?

    ReplyDelete
    Replies
    1. 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.

      Here is the link for the pictures of my oscilloscope:

      https://plus.google.com/photos/107693658129190941717/albums/5839788505803417521?banner=pwa

      I really hope you can help me a little bit further. I hope you understand my situation.

      Thanks.

      Delete
    2. You should give me the details. Email me at inferno-rage (at) hotmail.com

      Delete
  11. How did you get the pure sine wave from full bridge drive? I mean what is conventional method to convert SPWM to sine wave? Could you provide me schematic?

    ReplyDelete
    Replies
    1. 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.

      Just search for LC filter in Google. And you'll get loads of results.

      Regards,
      Tahmid.

      Delete
  12. TBL_POINTER_SHIFT = TBL_POINTER_NEW >>
    11;
    DUTY_CYCLE = TBL_POINTER_SHIFT;

    while (TMR2IF_bit == 0);
    Please can u explain this portions in your code.
    The interrupt section.( Does the code get activated once PR2 = TMR2)

    ReplyDelete
    Replies
    1. Hi,

      Please go through this to get the explanation.

      http://tahmidmc.blogspot.com/2013/02/demystifying-use-of-table-pointer-in.html

      Regards,
      Tahmid.

      Delete
  13. 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 ...???

    ReplyDelete
    Replies
    1. Refer to the PIC16F684 datasheet. Go to the ECCP section - PWM mode. The diagram for the basic configuration for driving the bridge is given.

      If you still can't figure it out, feel free to ask and I'll be glad to help.

      Regards,
      Tahmid.

      Delete
    2. hello sir.

      hope you are fine.
      i want your help in making of 3-phase spwm using PIC.
      to control 3-phase induction motor.

      Please help sir.
      thanks in advance

      Delete
  14. Hallo Tahmid.

    I use Pic18F4550 8Mhz quarc with Pll 48Mhz CPU Clock
    Is my calculation is correct for 50Hz output and PWM 16Khz:
    PR2=187
    t2con = 0h05 / prescaler 4
    const table as byte[32] = (0,18,36,54,71,88,103,118,132,144,155,164,172,178,182,185,186,185,182,178,172,164,155,144,132,118,103,88,71,54,36,18)

    But not work

    When I turn on mosfet IRF840 B and D hеаt and broke
    I use resistor Gate Drain 1k
    C1 capacitator 68uf 25V and 100nf
    Uf4007



    Thanks.
    Nlupco


    code MicroBPro for Pic

    program MyProject

    ' Declarations section
    const table as byte[32] = (0,18,36,54,71,88,103,118,132,144,155,164,172,178,182,185,186,185,182,178,172,164,155,144,132,118,103,88,71,54,36,18)
    dim prom1 as word
    dim prom2 as word
    dim prom3 as word
    dim prom4 as word
    dim frek as word
    dim d1 as byte
    dim lcd as string[10]

    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


    sub procedure interrupt
    if pir1.1 = 1 then
    prom1 = prom2 + frek
    if prom1 < prom2 then
    ccp1con.7 = not ccp1con.7
    end if
    prom3 = prom1 >> 11
    d1 = prom3
    ccpr1l = table[d1]
    ccpr1h = 0
    prom2 = prom1

    pir1.1 = 0
    end if

    end sub


    main:


    Lcd_Init()
    Lcd_Cmd(_LCD_CLEAR)
    Lcd_Cmd(_LCD_CURSOR_OFF)
    Lcd_Out(1,6,"START")



    frek = 410


    prom3 = 0
    prom1 = 0
    prom2 = 0
    d1 = 0
    cmcon = 7
    adcon1 = 15
    pr2 = 187
    trisc.2 = 1
    trisd.5 = 1
    trisd.6 = 1
    trisd.7 = 1

    ccp1con = 76
    pir1.1 = 0
    t2con = 5
    if pir1.1 = 0 then
    pir1.1 = 0

    trisc.2 = 0
    trisd.5 = 0
    trisd.6 = 0
    trisd.7 = 0
    intcon = %11000000

    end if
    Lcd_Cmd(_LCD_CLEAR)

    while true

    wordtostr(frek,lcd)
    Lcd_Out(1,10,lcd)


    if Button(PORTA, 2, 50, 0) then
    frek = frek + 10



    end if
    if Button(PORTA, 3, 50, 0) then
    frek = frek - 10
    end if


    wend
    end.

    ReplyDelete
  15. hi Tahmid,
    I try and simulate using proteus, but why the output can not sine ?
    see: http://s8.imagestime.com/out.php/i840087_untitled.png

    what is the problem?

    Thank you.

    BR
    Heri

    ReplyDelete
    Replies
    1. You need to use high-low side drivers for the MOSFETs in the full-bridge. You also need to use an LC filter at the output.

      For simulation, you can use a filter (simple RC will work) at the PIC P1B and P1D outputs and then measure the wave at the output of the filter. Fine tune the filter to see the output wave.

      Regards,
      Tahmid.

      Delete
    2. hello sir.

      hope you are fine.
      i want your help in making of 3-phase spwm using PIC.
      to control 3-phase induction motor.

      Please help sir.
      thanks in advance

      Delete
  16. Hallo Tahmid

    please help me to fix problem
    send me you mail to give me project


    Thanks
    Nlupco

    ReplyDelete
    Replies
    1. Hi,

      Email me at inferno-rage (at) hotmail (dot) com

      Regards,
      Tahmid.

      Delete
    2. I send you e-mail.
      wait for a response from you

      Delete
    3. I am very busy with school exams. I'll get back to you as soon as I can. I have received the email. Please be patient. I'll reply to it very soon.

      Regards,
      Tahmid.

      Delete
    4. Thanks for reply Tahmid

      Pass the exams with excellent grades.
      No problem I'll wait for the answer.

      Best regards,

      Delete
    5. Thanks. My school exams have just ended. The A Level exams will start in a few days. I'll have a look at the email today and let you know.

      Regards,
      Tahmid.

      Delete
    6. Check your email inbox. I've sent the reply.

      Regards,
      Tahmid.

      Delete
    7. hello sir.

      hope you are fine.
      i want your help in making of 3-phase spwm using PIC.
      to control 3-phase induction motor.

      Please help sir.
      thanks in advance

      Delete
  17. Tahmid can u plz tell me the pin configuration of this microcontroller

    ReplyDelete
    Replies
    1. You can easily get the pin configuration from the datasheet: http://ww1.microchip.com/downloads/en/devicedoc/41202f-print.pdf

      I used P1A, P1B, P1C and P1D as are to be used for bridge driving. Refer to the ECCP section - PWM mode.

      Regards,
      Tahmid.

      Delete
  18. Replies
    1. You should attempt to design it yourself. You'll learn a lot in the process.

      You can easily get the pin configuration from the datasheet: http://ww1.microchip.com/downloads/en/devicedoc/41202f-print.pdf

      I used P1A, P1B, P1C and P1D as are to be used for bridge driving. Refer to the ECCP section - PWM mode.

      Try designing it yourself and if you still can't, feel free to ask. I'll be glad to help.

      Regards,
      Tahmid.

      Delete
    2. Dear Tahmid

      I've run your code: sine by PIC16F684. But the last place on the oscillators.

      I want send Email to you, but I have not your email.
      I send link (files : oxilo.JPG and invert.rar) : http://www.4shared.com/account/home.jsp#dir=z5tArTzu
      Can you repair?
      My email : mr.danghoangtung@gmail.com
      Help me.
      Thanks Tahmid
      Bests Regard
      Tung

      Delete
  19. Tahmid
    why in the code
    unsigned int TBL_temp;

    ReplyDelete
    Replies
    1. 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.

      Regards,
      Tahmid.

      Delete
    2. Thank you Tahmid.
      Regards,tantra-yog.

      Delete
  20. Hi
    Am 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.

    ReplyDelete
    Replies
    1. 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:

      http://tahmidmc.blogspot.com/2013/01/using-high-low-side-driver-ir2110-with.html

      Regards,
      Tahmid.

      Delete
  21. Hi! Sir

    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.


    Best regards,


    Angelo

    ReplyDelete
    Replies
    1. I used mikroC PRO for PIC. You can get the free version from here:

      http://www.mikroe.com/mikroc/pic/

      Regards,
      Tahmid.

      Delete
  22. 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.

    Cheers
    Grandi

    ReplyDelete
    Replies
    1. I can't seem to find the email. May have gotten lost in the inbox or junk folders. Can you resend it?

      Delete
    2. Hi Tahmid,
      Thanks for your reply.
      I'll resend it lateron today.

      Cheers
      Grandi

      Delete
    3. Hi,

      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.

      Hope this helps.

      Regards,
      Tahmid.

      Delete
    4. Hi Tahmid,

      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.

      I will keep you updated about my progress.

      Cheers
      Grandi

      Delete
  23. Tahmid
    how to change the code for Half-Bridge,
    to free for other tasks P1C and P1D.
    P1A and P1B Modulated.
    If possible?

    ReplyDelete
    Replies
    1. Thank you Tahmid.
      Regards,tantra-yog.

      Delete
    2. Why do you want to use half-bridge? How do you plan to use a half-bridge converter? Are you going to use a split rail (+ve and -ve)?

      Delete
    3. Transformer with midpoint.

      Regards,tantra-yog.

      Delete
    4. I want to get P1A-10mS , P1B - 10mS

      photo http://yadi.sk/d/kBfRJxi27l_6q

      Delete
  24. please help me for code for below description ..I am using pic 16f 877a controller

    Sine pwm i required as below .

    2 separate Sine PWM Should be generated , Switching frequency may be 5kZ , and fundamental frequency 50Hz .

    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 .



    thanks
    rameshsunil123@gmail.com

    ReplyDelete
    Replies
    1. suembeddd Look here

      http://bbs.dianyuan.com/topic/111661

      Regards,tantra-yog.

      Delete
  25. Thank you very much Tahmid.i used your sine table .it look great

    ReplyDelete
  26. 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.

    Thanks.

    ReplyDelete
    Replies
    1. I highly recommend you skip the BJT based design and look to use dedicated high-low side drivers.

      For sine table, go through these:
      http://tahmidmc.blogspot.com/2011/01/generation-and-implementation-of-sine.html
      http://tahmidmc.blogspot.com/2012/10/smart-sine-software-to-generate-sine.html

      Regards,
      Tahmid.

      Delete
    2. 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 :

      unsigned char sin_table[64]={0, 10, 21, 31, 41, 50, 59, 68, 76, 83, 89,
      94, 99, 102, 105, 106, 107, 106, 105, 102, 99, 94, 89, 83, 76, 68, 59,
      50, 41, 31, 21, 10};


      unsigned int TBL_POINTER_NEW, TBL_POINTER_OLD, TBL_POINTER_SHIFT, SET_FREQ;
      unsigned int TBL_temp;
      unsigned char DUTY_CYCLE;

      void interrupt(){
      if (TMR2IF_bit == 1){
      TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ;
      if (TBL_POINTER_NEW < TBL_POINTER_OLD){
      CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge
      }
      TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 11;
      DUTY_CYCLE = TBL_POINTER_SHIFT;
      CCPR1L = sin_table[DUTY_CYCLE];
      TBL_POINTER_OLD = TBL_POINTER_NEW;
      TMR2IF_bit = 0;
      }
      }

      void main() {
      SET_FREQ = 468;
      TBL_POINTER_SHIFT = 0;
      TBL_POINTER_NEW = 0;
      TBL_POINTER_OLD = 0;
      DUTY_CYCLE = 0;
      PR2=149; ///for 20Khz at 12MHZ of quartz
      TRISB=0x00;
      // CCP1CON=0x4D;
      CCP1CON=0x4C;
      TMR2IF_bit = 0;
      T2CON = 4; //TMR2 on, prescaler and postscaler 1:1
      while (TMR2IF_bit == 0);
      TMR2IF_bit = 0;
      TRISB=0;
      TMR2IE_bit = 1;
      GIE_bit = 1;
      PEIE_bit = 1;

      while(1);
      }

      Please help me with table values,SET_FREQ

      Thanks

      Delete
  27. 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};


    unsigned int TBL_POINTER_NEW, TBL_POINTER_OLD, TBL_POINTER_SHIFT, SET_FREQ;
    unsigned int TBL_temp;
    unsigned char DUTY_CYCLE;

    void interrupt(){
    if (TMR2IF_bit == 1){
    TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ;
    if (TBL_POINTER_NEW < TBL_POINTER_OLD){
    CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge
    }
    TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 11;
    DUTY_CYCLE = TBL_POINTER_SHIFT;
    CCPR1L = sin_table[DUTY_CYCLE];
    TBL_POINTER_OLD = TBL_POINTER_NEW;
    TMR2IF_bit = 0;
    }
    }

    void main() {
    SET_FREQ = 410;
    TBL_POINTER_SHIFT = 0;
    TBL_POINTER_NEW = 0;
    TBL_POINTER_OLD = 0;
    DUTY_CYCLE = 0;
    //ANSEL = 0; //Disable ADC commenetd myself
    // CMCON0 = 7; //Disable Comparator commenetd myself
    //PR2 = 249;
    PR2=187; ///for 16Khz at 12MHZ of quartz
    // TRISC = 0x3F; commenetd myself
    TRISB=0x3F;//defined myself for PWM output pins
    CCP1CON = 0x4C;
    TMR2IF_bit = 0;
    T2CON = 4; //TMR2 on, prescaler and postscaler 1:1
    while (TMR2IF_bit == 0);
    TMR2IF_bit = 0;
    TRISB=0; //defined my self for PWM out
    // TRISC = 0; commenetd myself the PIC doesn't have portc
    TMR2IE_bit = 1;
    GIE_bit = 1;
    PEIE_bit = 1;

    while(1);
    }

    ReplyDelete
  28. Hello,I am getting agood signal with PIC16F1827 but the voltage amplitude is vvery low(2.0V),it is not controliing the MOSFET ,what can I do??

    Thanks

    ReplyDelete
    Replies
    1. use any driver ic's like tlp 250 this will increase upto 5 to 8 volts depending upon the driver Vcc that u have applied

      Delete
    2. i mean the voltage u have applied to driver ic to operate it

      Delete
  29. 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

    ReplyDelete
  30. 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

    ReplyDelete
  31. If I want 20kHz frequency then what must be oscillator frequency and PR2 values?

    ReplyDelete
  32. 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:

    unsigned int TBL_POINTER_NEW, TBL_POINTER_OLD, TBL_POINTER_SHIFT, SET_FREQ;
    unsigned int TBL_temp;
    unsigned char DUTY_CYCLE;

    void interrupt(){
    if (TMR2IF_bit == 1){
    TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ;
    if (TBL_POINTER_NEW < TBL_POINTER_OLD){
    CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge
    }
    TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 11;
    DUTY_CYCLE = TBL_POINTER_SHIFT;
    CCPR1L = sin_table[DUTY_CYCLE];
    TBL_POINTER_OLD = TBL_POINTER_NEW;
    TMR2IF_bit = 0;
    }
    }

    void main() {
    SET_FREQ = 410;
    TBL_POINTER_SHIFT = 0;
    TBL_POINTER_NEW = 0;
    TBL_POINTER_OLD = 0;
    DUTY_CYCLE = 0;
    //ANSEL = 0; //Disable ADC
    //CMCON0 = 7; //Disable Comparator
    PR2 = 249;
    TRISC.b2 = 1;
    TRISD = 0xE0;
    CCP1CON = 0x4C;
    TMR2IF_bit = 0;
    T2CON = 4; //TMR2 on, prescaler and postscaler 1:1
    while (TMR2IF_bit == 0);
    TMR2IF_bit = 0;
    TRISC.b2 = 0;
    TRISD = 0;
    TMR2IE_bit = 1;
    GIE_bit = 1;
    PEIE_bit = 1;

    while(1);
    }

    Thanks

    ReplyDelete
  33. Hello Tahmid,
    I am facing problems while porting the code to PIC18F4520. I would be grateful if you help me out with it. I've sent you an email with my project attached. Hope to get your reply soon.

    Thanks

    ReplyDelete
  34. Hi Tahmaid
    I follow your inverter circuit with H Bridge (on Protues 8). But it does not run.
    You help me.

    Many thanks

    Link file : http://rapidshare.com/share/ADDABD0C82217EEB704EE62E9E17C940

    Email : danghoang.2013@yahoo.com

    ReplyDelete
  35. 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.

    ReplyDelete
  36. Hai, Can i get outputs in 6 pins of port D which is result of or operation with CCP1 PWM and some other inputs.

    ReplyDelete
  37. 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

    ReplyDelete
  38. 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.

    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};


    unsigned int TBL_POINTER_NEW, TBL_POINTER_OLD, TBL_POINTER_SHIFT, SET_FREQ;
    unsigned char DUTY_CYCLE;

    void interrupt(){
    if (TMR2IF_bit == 1){
    TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ;
    if (TBL_POINTER_NEW < TBL_POINTER_OLD){
    CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge
    }
    TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 11;
    DUTY_CYCLE = TBL_POINTER_SHIFT;
    CCPR1L = sin_table[DUTY_CYCLE];
    TBL_POINTER_OLD = TBL_POINTER_NEW;
    TMR2IF_bit = 0;
    }
    }

    void main() {
    SET_FREQ = 410;
    TBL_POINTER_SHIFT = 0;
    TBL_POINTER_NEW = 0;
    TBL_POINTER_OLD = 0;
    DUTY_CYCLE = 0;
    PR2 = 249;
    TRISC=0;
    TRISD=0;
    CCP1CON = 0b01001100;
    TMR2IF_bit = 0;
    T2CON = 0b00000100; //TMR2 on, prescaler and postscaler 1:1
    while (TMR2IF_bit == 0);
    TMR2IF_bit = 0;
    TMR2IE_bit = 1;
    GIE_bit = 1;
    PEIE_bit = 1;

    while(1);
    }

    ReplyDelete
  39. i have run the simulation but nothings happen there in proteous... plz someone help

    ReplyDelete
    Replies
    1. What do you mean by nothing happens? What works? What doesn't? Have you set everything properly? Have you tried making the circuit in real life?

      Delete
  40. 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

    ReplyDelete
    Replies
    1. That's definitely possible.

      Take a look here as to how the frequency is determined:
      http://tahmidmc.blogspot.com/2013/02/demystifying-use-of-table-pointer-in.html

      Delete
  41. Hello Tahmid
    please help me to generate 60Hz spwm. where the change in code.

    ReplyDelete
    Replies
    1. Go here: http://tahmidmc.blogspot.com/p/blog-page.html

      And look under: SPWM and sine wave generation and inverter

      There's a bunch of stuff you'll find helpful.

      Delete
  42. hello tahmid, can you help me, how to make spwm signal with arduino in proteus?
    hope u send the circuit in proteus to fdiky70@gmail.com
    pls help me....

    ReplyDelete
    Replies
    1. The concept is similar to here: http://tahmidmc.blogspot.com/2011/01/generation-and-implementation-of-sine.html

      I haven't yet implemented SPWM with Arduino so I don't have any code with me right now. But once you get the concepts, they shouldn't be too hard to implement.

      You may also find it of interest to look at this design since it's based on an Atmel AVR (as are most Arduinos): http://tahmidmc.blogspot.com/2013/02/sine-wave-generation-with-fast-pwm-mode_2525.html

      Delete
  43. 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

    Thank you

    ReplyDelete
    Replies
    1. 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.

      Delete
  44. 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};


    unsigned int TBL_POINTER_NEW, TBL_POINTER_OLD, TBL_POINTER_SHIFT, SET_FREQ;
    unsigned char DUTY_CYCLE;

    void interrupt(){
    if (TMR2IF_bit == 1){
    TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ;
    if (TBL_POINTER_NEW < TBL_POINTER_OLD){
    CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge

    }
    TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 11;
    DUTY_CYCLE = TBL_POINTER_SHIFT;
    CCPR1L = sin_table[DUTY_CYCLE];
    TBL_POINTER_OLD = TBL_POINTER_NEW;
    TMR2IF_bit = 0;
    }
    }

    void main() {
    SET_FREQ = 410;
    TBL_POINTER_SHIFT = 0;
    TBL_POINTER_NEW = 0;
    TBL_POINTER_OLD = 0;
    DUTY_CYCLE = 0;
    PR2 = 249;
    TRISC = 0;
    TRISD = 0;
    CCP1CON = 0b01001100;
    TMR2IF_bit = 0;
    T2CON = 0b00000100; //TMR2 on, prescaler and postscaler 1:1
    while (TMR2IF_bit == 0);
    TMR2IF_bit = 0;
    TMR2IE_bit = 1;
    GIE_bit = 1;
    PEIE_bit = 1;

    while(1);
    }
    Thank you!

    ReplyDelete
  45. 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

    Please help!Thank you!!

    ReplyDelete
  46. Sorry the PR2 value for the my previous post is 299 not 249.

    Thank

    ReplyDelete
  47. Hello, this helped me alot. i just wanted to know that if i wanted to generate a 40Hz sine wave what changes do i have to make in the code?

    ReplyDelete
  48. 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.

    ReplyDelete
  49. 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.

    ReplyDelete
    Replies
    1. 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.

      Bipolar switching results in pure sine wave no matter if load connected or not, but at expense of slightly dropped amplitude voltage, added deadtime, increased MOSFET losses due to full switching instead of halfwave switching (without 50Hz square wave on high switches), and LC filter parasitic losses.

      Unipolar is good if you have load always connected to output, and it's amplitude is usually of DC bus amplitude (minus parasitic losses in filter of course).

      I am currently designing a portable HVDC-AC pure sine wave inverter and I think bipolar scheme is better for this task.

      Delete
  50. Hi Tamhid:

    Can you tell me about the filter you used for producing pure AC wave from SPWM.

    ReplyDelete
    Replies
    1. 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

      Delete
  51. Hi Tahmid.

    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?

    ReplyDelete
    Replies
    1. implement feedback in your program... that should adapt the signals to any load.....Regards....
      CHeck this link...http://tahmidmc.blogspot.com/2012/11/feedback-in-sine-wave-inverter-pic16f.html

      Delete
  52. 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.

    ReplyDelete
    Replies
    1. JUST USE P1A AND P1B. you'd be fine but double those frequencies if you do.....Regards

      Delete
    2. Good day. You seem quite knowledgeable about these things. Please my simulation on proteus gives 50Hz on two pins, and 8khz of the other pins. Is that supposed to be so? Pls reply.

      Delete
  53. 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.png
    Does anyone know what that means?

    ReplyDelete
  54. Hello ,can i ask u what is the others PIC's we can used in place of the 16F73,can u type the names please ?

    ReplyDelete
  55. 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.

    Thank you!!

    ReplyDelete
  56. I cant use Pic in full bridge mode
    I used your code too and simulated in proteus
    But Im not getting the results , tried a lot
    Why it doesnt get simulated in full bridge mode ?

    ReplyDelete
    Replies
    1. 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.

      Delete
    2. 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

      Delete
  57. sir i have trouble with sm7759 driver which is not driving the sudden load of 800w the ac is flickering

    ReplyDelete
  58. thank you
    i want the code please

    ReplyDelete
  59. what's modification for 60Hz sine wave?

    ReplyDelete
  60. Please Mr.Tahmid i want to put "SET_FREQ=200" and when i push button on Ra0 the SET_FREQ will increase by "10" until SET_FREQ=410.. Please help me ...Thanx

    ReplyDelete
  61. hello Mr.Tahmid
    would you send the hex file for me ipmbldc@gmail.com
    tnx

    ReplyDelete
  62. Dear Sir Tahmid, how can I set the output frequency to 60Hz instead of 50Hz?

    ReplyDelete
  63. how then can feedback be achieved

    ReplyDelete
  64. Hey guys, can we ask the hex file and proteus not? hainguyenyork@gmail.com

    ReplyDelete
  65. Hi van help me i trid work un pic 16f628 but i can t iwant cod spwm un this pic van any zone help me thanks

    ReplyDelete
  66. I Try above code but mplab shows following errors
    Error [285] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 15.1 no identifier in declaration
    Error [314] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 15.1 ";" expected
    Error [285] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 19.1 no identifier in declaration
    Warning [374] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 19.1 missing basic type; int assumed
    Error [314] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 19.1 ";" expected
    Error [285] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 22.1 no identifier in declaration
    Warning [374] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 22.1 missing basic type; int assumed
    Error [314] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 22.1 ";" expected
    Warning [374] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 23.19 missing basic type; int assumed
    Error [984] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 23.19 type redeclared
    Error [1098] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 23.19 conflicting declarations for variable "TBL_POINTER_SHIFT" (C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C:9)
    Error [188] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 23.42 constant expression required
    Warning [374] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 24.12 missing basic type; int assumed
    Error [984] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 24.12 type redeclared
    Error [1098] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 24.12 conflicting declarations for variable "DUTY_CYCLE" (C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C:11)
    Error [188] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 24.31 constant expression required
    Warning [374] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 25.8 missing basic type; int assumed
    Error [984] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 25.8 type redeclared
    Error [1098] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 25.8 conflicting declarations for variable "CCPR1L" (C:\Program Files\HI-TECH Software\PICC\PRO\9.60\include\pic16f684.h:31)
    Error [188] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 25.31 constant expression required
    Warning [374] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 26.17 missing basic type; int assumed
    Error [984] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 26.17 type redeclared
    Error [1098] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 26.17 conflicting declarations for variable "TBL_POINTER_OLD" (C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C:9)
    Error [188] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 26.34 constant expression required
    Warning [374] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 27.12 missing basic type; int assumed
    Error [285] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 28.1 no identifier in declaration
    Warning [374] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 28.1 missing basic type; int assumed
    Error [314] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 28.1 ";" expected
    Error [285] C:\Users\ASHOKA SYSTEM\Desktop\SPWM\SPWM.C; 29.1 no identifier in declaration

    ReplyDelete
  67. pl help mi for hex file generation

    ReplyDelete
  68. Hi Tahmid,
    Can you tell me which programmer should i use for PIC16f684?

    ReplyDelete
  69. 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=sharing
    I have used 16MHZ crystal.

    ReplyDelete
  70. what should be done for three phase inverter 120 degree conduction? how did you manage dead time ?

    ReplyDelete
    Replies
    1. 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.

      Delete
  71. This comment has been removed by the author.

    ReplyDelete
  72. Am try to download the hex file from the site but couldn't reach the sever. Can you send me through my mail. Gavivinagadogbe@gmail.com

    ReplyDelete
  73. Please send me the pic16f684 diagram and the hex file here is my mail. Gavivinagadogbe@gmail.com

    ReplyDelete
  74. Hello every one if some one wants complete hardware and code of working of 100w sine wave inverter he can contact me on my number.
    Ahsan Ali
    +923087456767

    ReplyDelete
  75. look at https://www.alibaba.com/showroom/100w-pure-sine-wave-inverter-circuit.html

    ReplyDelete
  76. and https://www.youtube.com/results?search_query=Pure+Sine+Wave+Inverter+100w

    ReplyDelete
  77. Hi tahmid sir
    I am following you for quiet a while now and I love your explaination

    I am now trying to make sine wave inverter with your guidance but I dont know why I am not getting the required output
    if u would privide me your email I can send u my proteus sinilation for checking
    thank u

    ReplyDelete
  78. Pls do send me only the hex file and the circuit's diagram
    Gavivinagadogbe@gmail.com

    ReplyDelete
  79. hello sir,
    I read your article and implement the SPWM using 16f877a. But PWM is not showing in proteus simulation. If i passed fixed value to CCP2L and disable the interrupt, its working properly. Can you help me???

    ReplyDelete
  80. I need to generate a SPWM with 8kHz frequency and get a sine wave of 50Hz using 20MHz crystal. Kindly mention the PR2 and SET_FREQ values. Thanks!

    ReplyDelete
  81. Hi
    Can some one tell me if it matters to switch one of two MOSFETS in a Hbride to generate this Sinewave. I see that hamid is switing only one. Im building a Hbride and plan to SPWM only one MOSFET per half bridge to drive a motor but dont know if that is correct. Let me know my email (wogoos at GMX.COM)

    ReplyDelete
  82. HI TAHMID , IAM GENERATING SPWM USING PIC 16F877A AND I DONE THE FOLLOWING PROGRAMMING,But the output pulses are like spikes ,i tried to have 10khz spwm and my crystal frequency is 16MHz and my prescaler is 4,can you tell me the problem ,the following is my code:please reply soon

    #include
    #define _XTAL_FREQ_16000000
    #define TMR2PRESCALE 4
    #define OUT RC2
    int sineval[36]={0,10,20,30,40,50,58,62,70,76,79,85,88,92,95,96,98,99,100,99,98,96,95,94,92,88,79,76,70,62,58,50,40,30,20,10};
    int i,j,endi;
    int dc=0,tp=0;
    void interrupt isr()
    {
    if(TMR2IF==1)
    {
    i=0;
    endi=40;
    while(i!=endi)
    {
    if(OUT==0)
    {
    OUT=1;

    CCPR1L=dc;
    }
    if(OUT==1)
    {
    OUT=0;
    CCPR1L=tp-dc;
    }
    }
    CCP1M1=~CCP1M1;
    CCP1IF=0;
    }
    }
    void Delay(unsigned int );
    void main()
    {
    PR2=99;
    tp=PR2;
    TRISC=0;
    T2CON=0X01;
    CCP1CON=0X0F;
    CCP1IF=0;
    CCP1IE=0;
    TMR2IE=0;
    TMR2IF=0;
    CCPR1L=0;
    CCP1IE=1;

    CCPR1L=0;

    T2CON=0X05;
    while (TMR2IF== 0);
    TMR2IF = 0;
    TMR2IE = 1;
    INTCON=0XC0;

    while(1)
    {
    for(int i=0;i<36;i++)
    {
    for(int j=0;j<5;j++)
    {
    dc=sineval[i];
    Delay(50);
    }
    }
    }
    }
    void Delay(unsigned int time) // SUB FUNCTIONS
    {
    unsigned int pause; //DECLARE PAUSE AS UNSIGNED CHAR
    while( time > 0) //LOOP UNTIL TIME IS GREATER THAN ZERO
    {
    pause = 255; //INITIALIZE PAUSE TO 255
    while(pause--); //DECREMENT PAUSE UNTIL IT BECOMES ZERO
    time--; //DECREMENT TIME AND LOOP BACK UNTIL IT BECOMES ZERO
    } //END OF WHILE FUNCTION
    } //END OF DELAY FUNCTION


    ReplyDelete
    Replies
    1. HI TAHMID , IAM GENERATING SPWM USING PIC 16F877A AND I DONE THE FOLLOWING PROGRAMMING,But the output pulses are like spikes ,i tried to have 10khz spwm and my crystal frequency is 16MHz and my prescaler is 4,can you tell me the problem ,the following is my code:please reply soon

      #include
      #define _XTAL_FREQ_16000000
      #define TMR2PRESCALE 4
      #define OUT RC2
      int sineval[36]={0,10,20,30,40,50,58,62,70,76,79,85,88,92,95,96,98,99,100,99,98,96,95,94,92,88,79,76,70,62,58,50,40,30,20,10};
      int i,j,endi;
      int dc=0,tp=0;
      void interrupt isr()
      {
      if(TMR2IF==1)
      {
      i=0;
      endi=40;
      while(i!=endi)
      {
      if(OUT==0)
      {
      OUT=1;

      CCPR1L=dc;
      }
      if(OUT==1)
      {
      OUT=0;
      CCPR1L=tp-dc;
      }
      }
      CCP1M1=~CCP1M1;
      CCP1IF=0;
      }
      }
      void Delay(unsigned int );
      void main()
      {
      PR2=99;
      tp=PR2;
      TRISC=0;
      T2CON=0X01;
      CCP1CON=0X0F;
      CCP1IF=0;
      CCP1IE=0;
      TMR2IE=0;
      TMR2IF=0;
      CCPR1L=0;
      CCP1IE=1;

      CCPR1L=0;

      T2CON=0X05;
      while (TMR2IF== 0);
      TMR2IF = 0;
      TMR2IE = 1;
      INTCON=0XC0;

      while(1)
      {
      for(int i=0;i<36;i++)
      {
      for(int j=0;j<5;j++)
      {
      dc=sineval[i];
      Delay(50);
      }
      }
      }
      }
      void Delay(unsigned int time) // SUB FUNCTIONS
      {
      unsigned int pause; //DECLARE PAUSE AS UNSIGNED CHAR
      while( time > 0) //LOOP UNTIL TIME IS GREATER THAN ZERO
      {
      pause = 255; //INITIALIZE PAUSE TO 255
      while(pause--); //DECREMENT PAUSE UNTIL IT BECOMES ZERO
      time--; //DECREMENT TIME AND LOOP BACK UNTIL IT BECOMES ZERO
      } //END OF WHILE FUNCTION
      } //END OF DELAY FUNCTION


      Delete
  83. Hello Tahmid,
    We are last year electrical engineering students, we are currently working on a five level inverter. The hardware is ready but us being electrical students we are struggling with the programming part of the microcontroller. We have used PIC16F877A microcontroller. We need a five step output, can you help us with the program. Thank you. Looking forward to your reply.

    ReplyDelete
  84. hello, my name is TRUONG, i am from vietnam.
    vtc does not have cc program learning, so i do not know about micro c.
    You give me sample exercises on this inverter.
    I can run and study.
    Contact: huutruongnguyen72@gmail.com
    Sincerely thank you.

    ReplyDelete
  85. Please reload the file iname a hex file format not in a written format so that I will be able to load this file easily. Since am a novice in programming.

    ReplyDelete
  86. Have you any data of this project i.e Design And Implementation Of PhotoVoltaic(PV) Sinewave Pulse Width Modulation(SPWM) Inverter System With Maximum Power Point Tracking(MPPT)

    ReplyDelete
  87. Have you any data of this project i.e Design And Implementation Of PhotoVoltaic(PV) Sinewave Pulse Width Modulation(SPWM) Inverter System With Maximum Power Point Tracking(MPPT)

    ReplyDelete