View previous topic :: View next topic |
Author |
Message |
Nora
Joined: 23 Mar 2008 Posts: 50
|
Help to understand SET_PWM1_DUTY function |
Posted: Sat Jul 30, 2011 8:52 am |
|
|
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
|
|
|
BLL
Joined: 11 Nov 2006 Posts: 181 Location: Birmingham, UK
|
|
Posted: Sat Mar 23, 2019 1:38 pm |
|
|
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
|
|
Posted: Sat Mar 23, 2019 2:28 pm |
|
|
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
|
|
Posted: Sat Mar 23, 2019 3:34 pm |
|
|
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
|
|
Posted: Sat Mar 23, 2019 5:50 pm |
|
|
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
|
|
Posted: Sat Mar 23, 2019 6:12 pm |
|
|
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
|
|
|
BLL
Joined: 11 Nov 2006 Posts: 181 Location: Birmingham, UK
|
|
Posted: Mon Mar 25, 2019 4:40 am |
|
|
I don't know how this happened. It was not intentional.
End of story. |
|
|
|