-->

PWM sinewave generation Using for pic16f877a

By
To generate a PWM sine wave using a PIC16F877A microcontroller, you can use the following steps: 1)Configure the Timer2 module of the PIC16F877A microcontroller to generate a PWM signal. The PWM frequency can be set by configuring the PR2 register and the duty cycle can be set by configuring the CCPR1L and CCP1CON registers. 2)Generate a sine wave lookup table using a spreadsheet software or an online tool. The lookup table should contain the sine values for one cycle of the wave, ranging from 0 to 2π. 3)Store the sine wave lookup table in the program memory of the microcontroller. 4) Set up a Timer1 module of the PIC16F877A microcontroller to generate interrupts at a frequency that is equal to the desired frequency of the sine wave. In the interrupt service routine (ISR) for Timer1, read the values from the sine wave lookup table and update the duty cycle of the PWM signal generated by Timer2.
       

            #include 

// sine wave lookup table
const unsigned char sine_table[64] = {128,140,152,163,174,185,195,205,214,222,230,237,243,248,252,255,
                                      255,255,252,248,243,237,230,222,214,205,195,185,174,163,152,140,
                                      128,116,104,93,82,71,61,51,42,34,26,19,13,8,4,0,
                                      0,0,4,8,13,19,26,34,42,51,61,71,82,93,104,116};

// timer1 ISR
void __interrupt() timer1_isr(void) {
    static unsigned char table_index = 0;
    
    // update duty cycle of PWM signal generated by timer2
    CCPR1L = sine_table[table_index];
    
    // increment table index
    table_index++;
    if (table_index >= 64) {
        table_index = 0;
    }
}

void main(void) {
    // configure timer2 for PWM generation
    T2CON = 0b00000111; // prescaler = 1, postscaler = 1, timer2 on
    PR2 = 255; // set PWM frequency
    CCP1CON = 0b00001100; // PWM mode, PWM duty cycle controlled by CCPR1L
    
    // configure timer1 for interrupt generation
    T1CON = 0b00110101; // prescaler = 8, timer1 on, enable timer1 interrupt
    TMR1IF = 0; // clear timer1 interrupt flag
    TMR1H = 0xFC; // set timer1 reload value for 50 Hz interrupt frequency
    TMR1L = 0x18;
    PEIE = 1; // enable peripheral interrupts
    GIE = 1; // enable global interrupts
    
    while (1) {
        // main program loop
    }
}

       
 
In this example code, the sine wave lookup table contains 64 values, so the Timer1 module is configured to generate interrupts at a frequency of 50 Hz (the table is updated once per cycle). The Timer2 module is configured to generate a PWM signal with a frequency of 8 kHz. The duty cycle of the PWM signal is updated in the Timer1 ISR by reading the values from the sine wave lookup table.

More Posts

gElectron. Powered by Blogger.

Contributors

16x2 LCD Interfacing with STM32,STM32F103C6

 16x2 LCD  Interfacing with STM32,STM32F103C6 lcd_init(); LCD_LINE1; lcd_String(" GeElectron"); LCD_LINE2; lc...

Contact Form

Name

Email *

Message *

Contact us

Name

Email *

Message *

Follow Us

https://www.facebook.com/gElectron-393939667321867/ FBbox/https://www.facebook.com/IVYthemes

Comments

[blogger]

MKRdezign

Test
google.com, pub-8429441124104529, DIRECT, f08c47fec0942fa0
[blogger]

Latest

[recent][newsticker]

Technology

Top Ads

RECENT COMMENTS

Subscribe Via Email

Subscribe to our newsletter to get the latest updates to your inbox. ;-)


Your email address is safe with us!

RECENT COMMENTS