mnsp90
Newbie level 4
- Joined
- May 22, 2014
- Messages
- 6
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 76
Hi all,
I am trying to implement SPWM using PIC16f877a.
I have got the initializaion of the pwm module correct and the pwm is working normally.
When i give a particular duty ratio the pulse is obtained at that particular duty and the specified frequency.
But when I try to give a sine table of around 61 values for 90°, the pulse pattern is not coming properly.
the switching frequency I am aiming is 12kHz and the crystal is 20MHz.
I understand that in order to get a SPWM pattern, the duty ratio has to be varied in proportion to a sine wave(a full wave rectified sine wave). But when I do the same I am unable to get the pwm pattern.
I also tried using a rectified sine wave at the ADC and sensing it at a sampling frequency equal to the pwm switching frequency 12kHz. Even here i was unable to get the pwm pattern.
I could not find the mistake in my code. I understand that there is some error in sending the duty ratio only.
Can anyone help me with this??
Please suggest me some docs or blogs to understand the generation of spwm better.
I used ADC in timer1 interrupt. Both the timers(tmer2 and timer1) prescalers are same (4).
Here as I said if the duty is fixed at some value say 200 or 255, I was able to get a pulse @12kHz with a specific duty ratio but I am unable to get a spwm pattern.
the code I used is
please let me know any mistakes in the code.
Thanks in advance.
I am trying to implement SPWM using PIC16f877a.
I have got the initializaion of the pwm module correct and the pwm is working normally.
When i give a particular duty ratio the pulse is obtained at that particular duty and the specified frequency.
But when I try to give a sine table of around 61 values for 90°, the pulse pattern is not coming properly.
the switching frequency I am aiming is 12kHz and the crystal is 20MHz.
I understand that in order to get a SPWM pattern, the duty ratio has to be varied in proportion to a sine wave(a full wave rectified sine wave). But when I do the same I am unable to get the pwm pattern.
I also tried using a rectified sine wave at the ADC and sensing it at a sampling frequency equal to the pwm switching frequency 12kHz. Even here i was unable to get the pwm pattern.
I could not find the mistake in my code. I understand that there is some error in sending the duty ratio only.
Can anyone help me with this??
Please suggest me some docs or blogs to understand the generation of spwm better.
I used ADC in timer1 interrupt. Both the timers(tmer2 and timer1) prescalers are same (4).
Here as I said if the duty is fixed at some value say 200 or 255, I was able to get a pulse @12kHz with a specific duty ratio but I am unable to get a spwm pattern.
the code I used is
Code:
void PWM_Init(void)
{
PR2=0x67; //period for 12kHz
TRISCbits.TRISC2=0;
TRISCbits.TRISC1=0;
T2CON = 0x01;
CCP1CON =0x0F; // PWM mode enabled and duty ratio bits are set to 0
CCP2CON =0x0F;
T2CONbits.TMR2ON =1;
}
void getdutyratio(unsigned int x)
{
unsigned int lsb=0,msb=0;
long int a;
MAXDUTY = (_XTAL_FREQ/(PWMFREQ*TMR2PRESCALE));
a = ((float)x*MAXDUTY)/1023;
//a=x;
lsb = a>>2;//a&0x03FC;
//lsb=lsb>>2;
msb = a&0x0003;
CCPR1L=lsb;
msb = msb<<4;
CCP1CON&= 0xCF;
CCP1CON|=msb;
}
int sinetable[61]={0,10,20,31,41,52,62,72,83,93,103,113,123,133,143,153,162,172,181,190,200,208,
217,226,235,243,251,259,267,275,282,290,297,304,310,317,323,329,335,341,346,
351,356,361,365,369,373,377,380,383,386,388,391,393,395,396,397,398,399,399,400};
void interrupt T1_ISR(void)
{
PIR1bits.TMR1IF=0;
TMR1L=0x97;
TMR1H=0xFF;
if(dir==1)
{
duty=sinetable[i];
i++;
if(i>60){
dir=0;
i=60;
}
}
else if(dir==0)
{
duty=sinetable[i];
i--;
if(i==0)
{ dir=1;
i=0;
}
}
getdutyratio(duty);
}
please let me know any mistakes in the code.
Thanks in advance.