Airian_007
Member

Hi,
I am trying to Generate sine wave (50Hz) using Pic and I have attached output Wave form and frequency of sine wave seems to be ok round about 50Hz but i am not satisfied with the shape n i dont know where is the mistake in code i am pasting code as well so please help me with this thanks In advance..
Code:
#include<p18f452.h>
#pragma config OSC=HS
#pragma config WDT=OFF
#pragma config LVP=OFF
void init(void);
void timer2_ISR(void);
rom int sine[130]={0,4,8,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68,72,76,80,84,88,92,96,100,104,108
,112,116,120,124,128,132,136,140,144,148,152,156,160,164,168,172,176,180,184,188,192,196,200,204,208,212
,216,220,224,228,232,236,240,244,248,244,240,236,232,228,224,220,216,212,208,204,200,196,192,188,184,180,
176,172,168,164,160,156,152,148,144,140,136,132,128,124,120,116,112,108,104,100,96,92,88,84,80,76,72,68,
64,60,56,52,48,44,40,36,32,28,24,20,16,12,8,4}; //128 Samples
int cnt=0,cnt1=0;
void init()
{
INTCONbits.GIE = 1;//Interrupts Enable
INTCONbits.GIEL = 1;
INTCONbits.PEIE=1;
PIE1bits.TMR2IE=1;
CCP1CON=0b00001100;//PWM Mode
T2CON=0b00000100;
PR2=249;//16MHz Crystal And Switching Frequency Is 16KHz
CCPR1L=0;
PIR1bits.TMR2IF=0;
PORTCbits.RC2=0;
TRISC=0;
}
//-----------Interrupt---------//
#pragma code low_vector = 0x00018
void low_interrupt(void)
{
_asm goto timer2_ISR _endasm
}
#pragma code
#pragma interrupt timer2_ISR
//----------------------------//
void timer2_ISR()
{
T2CONbits.TMR2ON=0;
CCPR1L=sine[cnt];
if(cnt1<=16)
{
cnt++;
cnt1=0;
if(cnt==128)
{
cnt=0;
}
}
else
CCPR1L=sine[cnt];
cnt1++;
PIR1bits.TMR2IF=0;
T2CONbits.TMR2ON=1;
}
//----------------------------//
void main()
{
init();
while(1);
}
////////////////
Last edited by a moderator: