ep.hobbyiest
Full Member level 4
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 is giving 4.2 Khz freq. even after i changed the PR2 value.
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.