View previous topic :: View next topic |
Author |
Message |
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: 19503
|
|
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. |
|
|
|