Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC16F886 PWM is not working properly

Status
Not open for further replies.

ep.hobbyiest

Full Member level 4
Joined
Jul 24, 2014
Messages
212
Helped
1
Reputation
2
Reaction score
1
Trophy points
18
Activity points
1,487
Hi,

I have configured the PWM in pic16f886 using timer2. But PWM is giving fix frequency, even after i change the register value.
here is my code.

Code:
#include <xc.h>
#pragma config FOSC = INTRC_NOCLKOUT        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
#pragma config BOR4V = BOR21V   // Brown-out Reset Selection bit (Brown-out Reset set to 2.1V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

void init_pwm();
void pwm_freq(U16 freq);
void pwm_duty(U16 duty);


int main(void)
{
    OSCCON = 0x61;
    init_pwm();
    pwm_freq(1000);
    while(1)
    {
        
    }
    return 0;
}

void pwm_freq(U16 freq)
{
    PR2 = 100;
}

void init_pwm()
{
    CCP2CON = 0x0C;
    T2CON = 0x04;
    TRISCbits.TRISC1 = 0;
}

void pwm_duty(U16 duty)
{
    CCPR2L = 100;
}

code is giving 4.2 Khz freq. even after i changed the PR2 value.
 

Is that all of your code?
For example, you define the 'pwm_duty' function to set the "on" clock count but it is not called anywhere.
Also, why do you pass a parameter to the 'pwm_freq' and 'pwm_duty' functions but then ignore it and use a constant.
The way you have the device set up, it would appear that you are setting the oscillator to be 4MHZ and the timer to have pre- and post-scalar settings of 1:1. With a PR2 value of 100, that means the clock will reset every 25uSec and so the PWM will have a base frequency of 40KHz. However, you have set the combined CCPR2L:CCP2CON<5,4> value to 400 (remember that the bottom 2 bits of the PWM width value are in the CCPxCON regisater but they must be taken into account when you calculate the value you need).
As the PWM will start high but drop low after the (in your case) 400 ticks from Timer2, you have a problem as the timer will actually restart the PWM cycle in 100 ticks (check Figure 11-4 for details of this).
If you are getting 4.2KHz then you need to go back to doign some more fundamental checks that the oscillator is begin correctly configured to give the frequency you expect, then that the timer is counting in thew way you expect and finally you can correctly configure the PWM peripheral.
Susan
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top