Programming pic to generate sine wave

Status
Not open for further replies.

himarmy

New Member
Now I m trying to understand how pwm work. This is my code so far. I m trying to generate PWM and then use rc low pass filter on the the PIC18f46k20 starter kit board to generate sine wave. But the outcome is not close to sine wave. Any help would be appreciate. Thx



#pragma idata // declare statically allocated initialized variables

const long int s_table[] = {128,218,256,218,128,37,0,37};

/** D E C L A R A T I O N S *******************************************/
#pragma code // declare executable instructions

void main (void)
{
unsigned char s_table_idx = 0;
TRISCbits.TRISC2 = 0;

// Set up 8-bit Timer2 to generate the PWM period (frequency)
T2CON = 0b00000100;// Prescale = 1:1, timer on
PR2 = 249; // Timer 2 Period Register = 250 counts
CCP1CON = 0b10001101; pwm


while(1)
{

CCPR1L = s_table[s_table_idx++ % 16]; //
PIR1bits.TMR2IF = 0; // clear interrupt flag; set on every TMR2 = PR2 match
while (PIR1bits.TMR2IF == 0); // watch for match, which is end of period.
 

Attachments

  • rclow..jpg
    163.4 KB · Views: 232
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…