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.

How do I use PWM with PIC18F45K20???

Status
Not open for further replies.

hussong1555

Member level 1
Joined
Jul 3, 2008
Messages
36
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,286
Location
USA
Activity points
1,503
Does anyone have any sample "C" code for setting up and using PWM with PIC18F45k20?
 

Hi,
C meaning?
mikroC? CCS? C18? There are many out there. Which compiler do you use?
Tahmid.
 

I will be using CCS.

Thanks for any help you can offer
 

Hi,
I don't know CCS, but I can tell you the procedure or give you a sample in mikroC to set up and use PWM at some given frequency.
Tahmid.

Added after 14 minutes:

Here is a sample in mikroC:
Code:
//PWM output of 32kHz 50% duty cycle at PORTC2
//Developed by: Tahmid
//1st February, 2010
//System clock: 4MHz
//Clock source: XT Crystal

void main(void){
     TRISC = 0; //PORTC OUTPUT
     PORTC = 0;
     PR2 = 30; //~32kHz
     CCPR1L = 15; //Duty cycle 50%
     CCP1CON = 0x0C; //CCP1CON = 00001100, Single output, Active High
     T2CON = 4; //TMR2 on, prescale = 0
     while (1);
}
You should be able to adapt this to CCS.
Take a look at the 18F45K20 and study the code. Hopefully you will understand. If not, you can always ask. I'll be glad to help.
Hope this helped.
Tahmid.
 
Thanks for the input, by any chance are you familiar enough with C18 to explain how to write code for PWM. basically I just need the PWM to be 90% for a period of say 3 seconds then back to zero
 

Hi,
Change the value of CCPR1L. For 90%, it should be (0.9 * PR2) = 0.9 * 30 = 27
Write:
Code:
CCPR1L = 27; //90% duty cycle
Then place a 3second delay. For C18, I don't know, but for mikroC, it'd be:
Code:
delay_ms(3000); //3second delay
After that, turn PWM off:
Code:
CCP1CON = 0; //PWM off
Code:
//PWM output of 32kHz 50% duty cycle at PORTC2
//Developed by: Tahmid
//1st February, 2010
//System clock: 4MHz
//Clock source: XT Crystal

void main(void){
     TRISC = 0; //PORTC OUTPUT
     PORTC = 0;
     PR2 = 30; //~32kHz
     CCPR1L = 27; //Duty cycle 90%
     CCP1CON = 0x0C; //CCP1CON = 00001100, Single output, Active High
     T2CON = 4; //TMR2 on, prescale = 0
     delay_ms(3000); //3second delay
     while (PIR1.TMR2IF == 0); //Wait for timer overflow
     CCP1CON = 0; //PWM off
}
For C18, I'd make a delay routine like:
Code:
void MSDelay(unsigned int itime){
	unsigned int i, j;
	for(i=0;i<itime;i++)
		for(j=0;j<135;j++);
}
And then call the function like:
Code:
MSDelay(3000);//3second delay
I like the mikroC delay function better as it is built-in and ofcourse hassle-free.
Hope this helps.
Tahmid.
 
Thanks Tamid, you've been very helpful. And C18 does have the same delay function (it's great)

I see that 30 is equal to 32kHz, what is the scale on that, and what if I need a period of like 20ms (I think that would work out to like 50Hz)?
 

Hi,
The formula is like this:
Code:
PR2 = [Fosc / (Fpwm * 4 * N)] - 1
Fpwm is desired frequency.
Fosc is system frequency(crystal/resonator/RC).
N is timer prescaler.
You won't be able to effectively produce 50Hz with the CCP module. You should be using software to generate this, or an IC.
Code:
CCPR1L = (DC/100)*PR2
DC = duty cycle in percentage

Hope this helped.
Tahmid.
 
I wonder if this device can be used for switching power supply to run with SEPIC topology? I need a 90% efficiency or more? Any suggestion would be appreciated.
 

Tahmid,

Everything you have told me has been extremely helpful and I am learning a lot from you, after some research (what i am trying to do is drive a servo) I need a PWM period of 20msec (I think that is 50Hz as mentioned before) and a PWM width of 1msec and 2msec. so a duty cycle of 5% and 10%. Does this sound correct to you?

You mentioned that "You won't be able to effectively produce 50Hz with the CCP module. You should be using software to generate this, or an IC. " so how do people interface servos with PICs? It seems pretty common in robotics.

If you can help me get the period, I can get the rest from there.

Again you have been most helpful and I thank you for getting me this far.
 

Hi,
You can easily do that with software PWM like with Timer 1 or any of the timers. Not very difficult.
You can do it with CCP, but that means reducing your clock speed to VERY low which will cause it to be very slow. Why not just use software PWM?
Take a look at this: http://www.ermicro.com/blog/?p=771
Hope this helped.
Tahmid.
 
Thanks Tahmid,

All of your help has made my project at work a success :)
 

Hi

**broken link removed** is another link which explains how to control a servo interrupt-driven for a 18F45xx family PIC. I think this code will also work on your 18F45K20 PIC.
 

Hi Tadmid,
What you explained helped me too much. Thank you very much.

And if you have time, could you tell me about capture module a bit. I want to counter the number of pulses incoming to PIC 16F877.
Thanks
 

Thank u, I' m waiting your help.
 

Tahmid said:
For C18, I'd make a delay routine like:
Code:
void MSDelay(unsigned int itime){
	unsigned int i, j;
	for(i=0;i<itime;i++)
		for(j=0;j<135;j++);
}
And then call the function like:
Code:
MSDelay(3000);//3second delay
Hi all,
how did you calculate that the for loops with j<135 and itime<3000 will give a delay of 3 seconds?
 

Thanx

yar i am very thankful. My PWM function Was not working well. then i coppy ur code .when i run it, got exact Pwm.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top