Pravin Gosavi
Member
Hello everyone. I am building a sinewave inverter. I am copying a program from the web. I have PORTED it for Hi-tech Compiler and PIC 628. I tested the pins on DSO. But the results are not satisfying coz I get gating signals on the Pins but not getting the SPWM signals on RB3 pin. What I get two pulses of same width after a very long DISTANCE. I cud not measure the freqncy. These are my assmptions:
PWM freq= 5Khz, Osc= 20Mhz. I want the sinewave of 50 Hz output.
So PR2= 999; SET_FREQ= 1311 (2^16/no. values in sine table ) As the Author SAID.
I have also done some Modifications like mult to make the REMAINED value ie 14 zero after every half cycle.
I don't know you are getting me or not but I hope u will certainly help me. Program is as below:-
//Programmer: Pravin Gosavi
//---------------------------------------------------------------
#include<htc.h>
#define _XTAL_FREQ 20000000UL
unsigned char sin_table[50]={ 0 , 8 , 24 , 46 , 76 , 114 , 156 , 206 , 258 ,
314 , 374 , 436 , 500 ,562 , 624 , 684 , 740 , 792 , 842 , 884 ,
922 , 952 , 974 , 990 , 998 , 998 , 990 , 974 , 952 ,922 , 884 ,
842 , 792 , 740 , 684 , 624 , 562 , 500 , 436 , 374 , 314 , 258 ,
206 , 156 , 114 ,76 , 46 , 24 , 8 , 0};
unsigned int TBL_POINTER_NEW,
TBL_POINTER_OLD, TBL_POINTER_SHIFT, SET_FREQ;
unsigned int TBL_temp;
unsigned char DUTY_CYCLE, mult;
unsigned char Direction;
void interrupt isr ()
{
if (TMR2IF == 1)
{
TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ;
if (TBL_POINTER_NEW < TBL_POINTER_OLD)
{
if (Direction == 0)
{
PORTBbits.RB1 = 0; //A
PORTBbits.RB7 = 0; //D
PORTBbits.RB5 = 1; //B
PORTBbits.RB6 = 1; //C
Direction = 1;
}
else
{
PORTBbits.RB5 = 0; //B
PORTBbits.RB6 = 0; //C
PORTBbits.RB1 = 1; //A
PORTBbits.RB7 = 1; //D
Direction = 0;
}
mult =0;
}
TBL_POINTER_SHIFT = TBL_POINTER_NEW%1311;
DUTY_CYCLE = TBL_POINTER_SHIFT;
CCPR1L = sin_table[DUTY_CYCLE];
TBL_POINTER_OLD = TBL_POINTER_NEW*mult;
mult=1;
TMR2IF = 0; //finally set intrpt flag zero
}
} //interupt end
void main()
{
CMCON=0X07;
TRISA = 0;
SET_FREQ = 1311; //65536/(no of values in sine table)
PORTB = 0;
TRISB = 0;
PR2 = 999; // 5kHz
CCPR1L = 0;
CCP1CON = 12;//PWM mode
TRISB = 0xFF;
TMR2IF = 0;
T2CON = 0x04; //---------------TMR2 on
while (TMR2IF == 0);
TMR2IF = 0; //Clear TMR2IF
PORTB = 0;
TRISB = 0;
// Enable Timer 2 interrupt, global interrupt and peripheral interrupt
TMR2IE = 1;
GIE = 1;
PEIE = 1;
while (1)
{
PORTAbits.RA1=0;
PORTAbits.RA1=1;
__delay_ms(3);
PORTAbits.RA1=0;
__delay_ms(5);
PORTAbits.RA1=1;
__delay_ms(3);
PORTAbits.RA1=0;
__delay_ms(100);
}
}
Thanks.
PWM freq= 5Khz, Osc= 20Mhz. I want the sinewave of 50 Hz output.
So PR2= 999; SET_FREQ= 1311 (2^16/no. values in sine table ) As the Author SAID.
I have also done some Modifications like mult to make the REMAINED value ie 14 zero after every half cycle.
I don't know you are getting me or not but I hope u will certainly help me. Program is as below:-
//Programmer: Pravin Gosavi
//---------------------------------------------------------------
#include<htc.h>
#define _XTAL_FREQ 20000000UL
unsigned char sin_table[50]={ 0 , 8 , 24 , 46 , 76 , 114 , 156 , 206 , 258 ,
314 , 374 , 436 , 500 ,562 , 624 , 684 , 740 , 792 , 842 , 884 ,
922 , 952 , 974 , 990 , 998 , 998 , 990 , 974 , 952 ,922 , 884 ,
842 , 792 , 740 , 684 , 624 , 562 , 500 , 436 , 374 , 314 , 258 ,
206 , 156 , 114 ,76 , 46 , 24 , 8 , 0};
unsigned int TBL_POINTER_NEW,
TBL_POINTER_OLD, TBL_POINTER_SHIFT, SET_FREQ;
unsigned int TBL_temp;
unsigned char DUTY_CYCLE, mult;
unsigned char Direction;
void interrupt isr ()
{
if (TMR2IF == 1)
{
TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ;
if (TBL_POINTER_NEW < TBL_POINTER_OLD)
{
if (Direction == 0)
{
PORTBbits.RB1 = 0; //A
PORTBbits.RB7 = 0; //D
PORTBbits.RB5 = 1; //B
PORTBbits.RB6 = 1; //C
Direction = 1;
}
else
{
PORTBbits.RB5 = 0; //B
PORTBbits.RB6 = 0; //C
PORTBbits.RB1 = 1; //A
PORTBbits.RB7 = 1; //D
Direction = 0;
}
mult =0;
}
TBL_POINTER_SHIFT = TBL_POINTER_NEW%1311;
DUTY_CYCLE = TBL_POINTER_SHIFT;
CCPR1L = sin_table[DUTY_CYCLE];
TBL_POINTER_OLD = TBL_POINTER_NEW*mult;
mult=1;
TMR2IF = 0; //finally set intrpt flag zero
}
} //interupt end
void main()
{
CMCON=0X07;
TRISA = 0;
SET_FREQ = 1311; //65536/(no of values in sine table)
PORTB = 0;
TRISB = 0;
PR2 = 999; // 5kHz
CCPR1L = 0;
CCP1CON = 12;//PWM mode
TRISB = 0xFF;
TMR2IF = 0;
T2CON = 0x04; //---------------TMR2 on
while (TMR2IF == 0);
TMR2IF = 0; //Clear TMR2IF
PORTB = 0;
TRISB = 0;
// Enable Timer 2 interrupt, global interrupt and peripheral interrupt
TMR2IE = 1;
GIE = 1;
PEIE = 1;
while (1)
{
PORTAbits.RA1=0;
PORTAbits.RA1=1;
__delay_ms(3);
PORTAbits.RA1=0;
__delay_ms(5);
PORTAbits.RA1=1;
__delay_ms(3);
PORTAbits.RA1=0;
__delay_ms(100);
}
}
Thanks.