spwm using pic 16f877A

Thread Starter

dhini1411

Joined Feb 11, 2018
2
hello every one iam currently trying to produce spwm pulses of 10Khz, using pic 16f877a and i get spikes of low voltage (0.5V) ,so can anybody find the problem in the coding .thanks in advance.i have also attached the simulation pic from proteus,please see it.
C:
#include<htc.h>
#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
Moderators note : Used code tags
 

Attachments

Last edited by a moderator:

AlbertHall

Joined Jun 4, 2014
12,345
The PIC PWM will produce pulses. For it to look like a sine wave you will need some sort of filter on the output. A simple RC filter will do the job if the PWM frequency is a lot higher than the sine wave frequency.
 
Top