Continue to Site

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.

  • 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.

PIC PWM help

Status
Not open for further replies.

ptewright

New Member
Hi,

I'm trying to setup a PIC for PWM. I want a period of 1 second. I am using a 4MHz clock. What values should PR2 and, TMR2 prescale should be used? If I want a 50% duty cycle, what values should CCPR1L and CCP1X and CCP1Y be? I'm having trouble calculating that.

Thanks
 
ptewright said:
I'm trying to setup a PIC for PWM. I want a period of 1 second. I am using a 4MHz clock. What values should PR2 and, TMR2 prescale should be used? If I want a 50% duty cycle, what values should CCPR1L and CCP1X and CCP1Y be? I'm having trouble calculating that.

One second is far too low with a 4MHz clock, if you consult either the data sheet, or the midrange reference manual, it shows the formulas required, and gives example values.

As 'williB' mentioned, why do you want such a low PWM frequency?.

Is it just to flash an LED?.
 
ptewright said:
Hi,

I'm trying to setup a PIC for PWM. I want a period of 1 second. I am using a 4MHz clock. What values should PR2 and, TMR2 prescale should be used? If I want a 50% duty cycle, what values should CCPR1L and CCP1X and CCP1Y be? I'm having trouble calculating that.

Thanks

Too slow, I didn't do the math but that sounds bigger than the counter can do. This is the kind of thing you'd solve by setting a timer to create an interrupt like every 1/100th of a second (still 10k instruction cycles per interrupt). The ISR just has a counter to decide whether to reassign an output. PWM is not necessary.

And it doesn't have to be an even portion of a sec, the counter inside the ISR will just count something other than 1.
 
pwm to set different frequencies

Basically, what i'm trying to do is setting the pwm for a certain frequency for about 0.5s, turning that off for 0.5s, so the period would be 1s. Then for the next period, I want to set the pwm to another frequency for 0.5s and turn that off for 0.5s.....etc.....
Is that possible with the PIC? I can't seem to get that working right.
 
Re: pwm to set different frequencies

ptewright said:
Basically, what i'm trying to do is setting the pwm for a certain frequency for about 0.5s, turning that off for 0.5s, so the period would be 1s. Then for the next period, I want to set the pwm to another frequency for 0.5s and turn that off for 0.5s.....etc.....
Is that possible with the PIC? I can't seem to get that working right.

We've already told you that the PWM can't go that low with a 4MHz clock, just do it in software!.

You also didn't answer why you are trying to do this, it seems a pretty strange thing to want to do?. If you give exact details we may be able to make more informed suggestions.
 
set up a timer interrupt for, say, 5ms.
declare a variable and increment whenever interrupt occurs.
when the variable reaches 100, toggle the pin and reset the variable.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top