• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PWM frequency problem PIC MicroC

Status
Not open for further replies.
Hello I want to generate a PWM for my application. I am using PIC16F1939, MicroC and PICkit 2 to burn the program.

Here's my code-

void interrupt()

{

if (TMR2IF_bit == 1)

{

CCPR1L = 100;

TMR2IF_bit = 0;

}

}


void main()

{

C1ON_bit = 0;

C2ON_bit = 0; // Disable Comparators

TRISC = 0XFF;

PR2 = 249;

CCP1CON = 12;

CCPR1L = 0;

TMR2IF_bit = 0;

T2CON = 0X04;

while (TMR2IF_bit == 0);

TMR2IF_bit = 0;

TRISC = 0X00;

TMR2IE_bit = 1;

GIE_bit = 1;

PEIE_bit = 1;
while(1)
{
//Do other stuff
}
}

I replaced old crystal with a new one still no change. Crystal is 20MHZ. Frequency I get on the CCP pin is 500HZ only where according to the calculations it should be 20 KHZ. Also I replaced PIC16F1939 with PIC16F877A.
Where could be the problem? Please help!
 

Pommie

Well-Known Member
Most Helpful Member
You might have the internal 32kHz oscillator selected. Try blinking an LED to work out the actual oscillator frequency.

Mike.
 
I have selected HS oscillator and inserted 20MHz in the sidebar in MicroC. Is PICkit 2 overriding the settings? How to select HS oscillator in PICkit 2 and in MicroC.
 

Pommie

Well-Known Member
Most Helpful Member
With delay. If you try for 1 Hz and you get a flash every 150 seconds then you have the 32kHz oscillator.

Or, try switching to the internal oscillator. Use the 16MHz as it's nearest.

Mike.
 
Thanks for your quick replies. I programmed the PIC with simple program-

PORTD= 0XFF;
delay_ms (10);
PORTD= 0X00;
delay_ms (10);

I am getting 1HZ blink while it should be with delay_ms (1000)? So I think PIC is being programmed with internal oscillator. But I have selected HS in MICROC config settings. Is PICkit 2 overriding config bits? Where can set them manually? I couldn't find any option?
 
Its in the project settings... Nothing to do with pickit2..

View attachment 105227
Thanks. I have tried it already. I think the crystal oscillator is not working. I got my program working by using internal oscillator. For those who want to know the settings, use - OSCCON = 0X7A for selecting 16MHZ internal oscillator ( May be for PIC16F1939 only. Check datasheet for other PICs). Thank you all. I will let you know when I'll replace the crystal.
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top