Easy Sine Wave Generation with PIC micro-controller

Status
Not open for further replies.

Mithun_K_Das

Advanced Member level 3
Joined
Apr 24, 2010
Messages
899
Helped
24
Reputation
48
Reaction score
26
Trophy points
1,318
Location
Dhaka, Bangladesh, Bangladesh
Visit site
Activity points
8,254
Generating Sine wave is very demanded job in power electronics field. So do to me too. I read some topics over the internet and found many ways to generate a sine wave. As most of you are really interested in generating sine wave so I’m writing today about it here.

There is some different ways out there to make a sine wave. Here I’m presenting one of my easiest one. To get this one all you need a micro-controller which have at-least 2 PWM/CCP module. But if you get the concept I’ll describe here then you can make it with your own code. So let’s see the concept generating a sine wave with two PWM/CCP module.

First of all, to get a sine wave we will need two SPWM signal. These two signals will generate sine wave after filtered with inductors and capacitors. But in between this signal and filters we need some kind of Transformer and to switch the transformer need some MOSFETs. Usually a H-Bridge will do the work for us. It will transform the DC power source being switched as per the SPWM signal that will flow through the Transformer. And from the output of the transformer we will get AC signal. And after filtering, we’ll get a pure sine wave signal.

Here is a basic H-Bridge circuit. And the image is describing itself what will happen and how we can get the AC signal from a DC signal.



OK, fine. Now lets see what is in a sine wave, a sine wave there is tow half signals. Each of these half signals consumes 10mS of time for 50Hz system. So if we wish to generate a sine wave of 50Hz, we need to do a full SPWM cycle in between this 10mS. My plan is simple. In this 10mS of time, all I need to do is taking a PWM signal and the duty cycle will be around zero at the starting point, almost full at the middle of this 10mS and then zero again at the end of the 10mS. This is simple. And if this can be done, after a filter with LC, we’ll get the result.

In the mean time, we need to generate a square wave of 10mS duration. This is only for a half cycle. So to complete another half cycle, we need to do the same as we did for the first half but just reversed of the 10mS duration and same as PWM.

This will overload the MCU. So we have to make a table for PWM duty cycle. Here I’ll present a simplest one. Keep in mind that, “The more the number in the table, more the accuracy”.

sign_table[18] = {50,75,100,125,150,175,200,225,250,250,225,200,175,150,125,100,75,50};

We will use just 18 samples here as we are just doing the basic. And what the micro-controller have to do is, MCU will generate an interrupt of 555.55uS to execute this 18 samples twitch within that 10mS. So that the PWM duty cycle starts from low in the start of the half cycle, then maximum at the middle of the half cycle and then again low at the end of the half cycle. Thus a complete SPWM can be found. You can use Timer0 or Timer1 for this purpose. Then 18 sample will take around 10mS and twitch this sampling will make a complete cycle. Meanwhile, we will use a variable to count, when the count will be equal to 18, we’ll just reverse the clock order. Thus we will get two SPWM and two 50Hz push-pull signal. If you don’t understand why this 18 samples are taken then just look at the table. And you’ll find that, the duty cycle is maximum at the middle and lowest by the start and end. Then imagine what will happen if this digital signal is converter into analog signal, a sine wave….. Yes! It will be a sine wave.

So lets see the code here,


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*******************************************************************************
*         Project For "Sine Wave generation using two PWM with PIC"            *
*                Program Written By_ Engr. Mithun K. Das                       *
*                     MCU: PIC16F73, X-Tal: 20MHz                              *
*                           16, January, 2015                                  *
*******************************************************************************/
 
// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
 
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End LCD module connections
 
 
 
 
unsigned int duty1=50,duty2=50;
unsigned int i=0;
unsigned char sign_table[18] = {50,75,100,125,150,175,200,225,250,250,225,200,175,150,125,100,75,50};
short toggle=0;
 
void InitTimer1()
{
    T1CON         = 0x01;
    TMR1IF_bit         = 0;
    TMR1H         = 0xF5;
    TMR1L         = 0x28;
    TMR1IE_bit         = 1;
    INTCON         = 0xC0;
}
 
void Interrupt()
{
    if (TMR1IF_bit)
    {
        TMR1IF_bit = 0;
        TMR1H         = 0xF5;
        TMR1L         = 0x28;
        //Enter your code here
        i++;
        if(i==18)
        {
           toggle = ~ toggle;
           i = 0;
           if(toggle)RC2_bit = 0;
           else RC1_bit = 0;
        }
 
        if(toggle)
        {
            RC0_bit = 0;
            RC3_bit = 1;
            PWM1_Set_Duty(0);
            PWM2_Set_Duty(sign_table[i]);
        }
        else
        {
            RC0_bit = 1;
            RC3_bit = 0;
            PWM2_Set_Duty(0);
            PWM1_Set_Duty(sign_table[i]);
        }
    }
}
 
 
void main() 
{
  TRISC = 0x00;//all output
  PORTC = 0x00;//all clear
  
 
 
 
  PWM1_Init(20000);//initialize at 20KHz
  PWM1_Start();
  
  PWM2_Init(20000);// initialize at 20Khz
  PWM2_Start();
  
  Lcd_Init();//initialize LCD
  Lcd_Cmd(_LCD_CLEAR);//clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);//Cursor off
  Delay_ms(100);
  Lcd_Out(1,4," SINE WAVE ");
  Lcd_Out(2,1,"Generation  Test");
  Delay_ms(2000);
  InitTimer1();//initialize Timer1 for 555us interrupt
  while(1)
  {
        Lcd_Out(1,1,"   SINE WAVE    ");
        Lcd_Out(2,1,"GENERATION  TEST");
        Delay_ms(1000);
        
        Lcd_Out(1,1," USING TWO PWM  ");
        Lcd_Out(2,1," WITH INTERRUPT ");
        Delay_ms(1000);
        
        Lcd_Out(1,1," LCD TEST WITH  ");
        Lcd_Out(2,1," TIMERS ARE ON  ");
        Delay_ms(1000);
  }
}


This code will generate the required signal. Also a LCD will be used to show some messages. After simulating the code with the circuit in proteus, we’ll get a result like this:


Now Our required signal is ready. All we need to do is to use these signals to drive the H-Bridge. Then the H-Bridge will switch a transformer and we will get an AC signal over the output of the Transformer. Then after filtering the signal, we will get the pure sine wave. Here is the rest of the circuit…



Bingo!!! We got the sine wave…!!!

This is the very basic way to generate sine wave. Now its your duty to use the concept. You can use it to design a pure sine wave inverter for yourself or as your wish. Its all yours.

Thank you all. If you need any further help, please contact with me. For more please visit my blog: **broken link removed** or visit my facebook page: www.facebook.com/mlabsbd.
 

Very useful thread for many electronics hobbyists like me. The topology presented using 2 CCP module of PIC. I think it will be pertinent here to request for a topology of generating sine wave using software PWM i.e. without CCP module of PIC.
 

Yes. Everything can't be disclosed. So I posted the concept. Rest of the job is yours....

Detail function as well as sample code with explanation of SPWM has been offered by one member in his blog long ago. So many information are disclosed there. Interested guys may get access it in the link https://tahmidmc.blogspot.in
 

There will many ways to travel from point A to point B. So everybody may not walk in a same way. Its a natural thing. Also its your choice which way you wish to walk. Its up to you. But you may follow the easiest way for you. Thank you.
 

It is not to compare which one is easier. While going through your blog it is noticed that many guys asked you many query relating to your posted thread. But almost in every case you have favoured them with "everything cannot be disclosed". That's why I thought it proper to refer the link to the interested guys so that they could grasp the idea and judge which one is easier to them.

So far I have observed, there is virtually no difference between your technique and that of the referred one.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…