CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Help to understand SET_PWM1_DUTY function

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Nora



Joined: 23 Mar 2008
Posts: 50

View user's profile Send private message

Help to understand SET_PWM1_DUTY function
PostPosted: Sat Jul 30, 2011 8:52 am     Reply with quote

Hello-
It is not at all clear (to me) how to set the PWM function. I have it working, but can't get a handle on what the value is doing.
I am using it to flash some LEDs. I turn off the LEDs with
Code:
SET_PWM1_DUTY(200);                                 

When I put in any other number, they just stay on.
I use
Code:
SET_PWM1_DUTY(500);                                 

to keep them on without generating too much heat.

From the manual:
Writes the 10-bit value to the PWM to set the duty. An 8-bit value may be used if the least significant bits are not required. If value is an 8 bit item, it is shifted up with two zero bits in the lsb positions to get 10 bits. The 10 bit value is then used to determine the amount of time the PWM signal is high during each cycle as follows:
• value*(1/clock)*t2div
Where clock is oscillator frequency and t2div is the timer 2 prescaler (set in the call to setup_timer2).

I have one Timer 2 function:
Code:
setup_timer_2(T2_DIV_BY_16,0xc0,16);

Is my prescale 16? My osc frequency is 20MHz.

Enlightenment appreciated!
N_N
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 30, 2011 1:27 pm     Reply with quote

Here are some threads that discuss 10-bit PWM vs. 8-bit:
http://www.ccsinfo.com/forum/viewtopic.php?t=26751
http://www.ccsinfo.com/forum/viewtopic.php?t=24055
http://www.ccsinfo.com/forum/viewtopic.php?t=22119

Ttelmah explains 8 and 10 bit PWM modes.
http://www.ccsinfo.com/forum/viewtopic.php?t=31674
http://www.ccsinfo.com/forum/viewtopic.php?t=28761
http://www.ccsinfo.com/forum/viewtopic.php?t=24403

Other PWM threads:

PWM frequency formula:
http://www.ccsinfo.com/forum/viewtopic.php?t=17993

Duty cycle formula:
http://www.ccsinfo.com/forum/viewtopic.php?t=42417&start=1

PWM postscaler explanation:
http://www.ccsinfo.com/forum/viewtopic.php?t=29786&start=1
Sample code for PWM postscaler:
http://www.ccsinfo.com/forum/viewtopic.php?t=41473&start=3

Microchip AN539: Frequency and Resolution Options for PWM Outputs
http://ww1.microchip.com/downloads/en/AppNotes/00539c.pdf
BLL



Joined: 11 Nov 2006
Posts: 181
Location: Birmingham, UK

View user's profile Send private message

PostPosted: Sat Mar 23, 2019 1:38 pm     Reply with quote

Hi
Thanks for the link.
I have the following code and it doesn't work. I have a 12V LED lamp, switched by an optically isolated FET device (PVN012). With the following code, I expected to see the lamp change in brightness, but it just remains fully on! I am obviously doing something wrong! The PIC is now running at 16MHz:

Code:
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 1, 1);
for(;;)
 {
  set_pwm1_duty((int16)0);    //0%
  delay_ms(2000);
  set_pwm1_duty((int16)250); //25%
  delay_ms(2000);
  set_pwm1_duty((int16)500); //50%
  delay_ms(2000);
  set_pwm1_duty((int16)750); //75%
  delay_ms(2000);
  set_pwm1_duty((int16)1000);//100%
  delay_ms(2000);
}


Brian
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sat Mar 23, 2019 2:28 pm     Reply with quote

You are setting the period value to 2 (1+1).
With this a value of 8 or greater into the duty will give full on.
To work with a count of 1000 giving you full scale, you need to be using
a period value of 249. Obviously the PWM will run 125* slower.

The PWM, counts from 0 to PR2+1. PR2 is the second entry in the timer2
setup (the middle value). It counts in 1/4 PR2 counts.
So set PR2 to 1, and you only get 8 duty values allowed. Set it to 249,
and you get 1000.
BLL



Joined: 11 Nov 2006
Posts: 181
Location: Birmingham, UK

View user's profile Send private message

PostPosted: Sat Mar 23, 2019 3:34 pm     Reply with quote

Hi Ttelmah again
Thanks for the help. I have done as suggested and changed the middle value to 249.

I am getting nowhere near 1000 useful steps! I have set up a pot on one of the (10 bit) ADC channels and used this value in set_pwm1_duty. The lamp is off below 200 and at full brightness by 300. This will do, but I am intrigued as to why such a narrow range. The gap at the bottom is obviously that there needs to be a certain current (and thereforePWM) through the lamp before the LEDs start glowing, but 300 should be nowhere near 100% duty cycle and yet the lamp gets no brighter after 300.

Thanks for the help.

Brian
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 23, 2019 5:50 pm     Reply with quote

BLL, in the future when I give you a link, can you reply in your original
thread ? Because when you reply to the linked older thread, you then
have two threads on the same topic. Please reply to the original one.
BLL



Joined: 11 Nov 2006
Posts: 181
Location: Birmingham, UK

View user's profile Send private message

PostPosted: Sat Mar 23, 2019 6:12 pm     Reply with quote

Hi PCMProgrammer

I'm afraid I don't understand your post. I have made all my replies in my original thread, as far as I can see! I looked at the links you provided in your post, but that was all.

Mystified.

Brian
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 23, 2019 7:24 pm     Reply with quote

No. Your original post is here:
http://www.ccsinfo.com/forum/viewtopic.php?t=57860
Notice the thread number is 57860.

Then you went to the link I gave you and replied there:
http://www.ccsinfo.com/forum/viewtopic.php?t=45968&start=1
Notice it's an older thread, number 45968.
BLL



Joined: 11 Nov 2006
Posts: 181
Location: Birmingham, UK

View user's profile Send private message

PostPosted: Mon Mar 25, 2019 4:40 am     Reply with quote

I don't know how this happened. It was not intentional.

End of story.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group