Acidmods

AcidMods Resources ----- ( These are helpful tools for modding ) => Pic Programmers and Programs => Topic started by: kronosslayer on August 08, 2011, 01:29:27 PM

Title: PWM duty cycle question
Post by: kronosslayer on August 08, 2011, 01:29:27 PM
I have a question regarding the duty cycle. I understand that fading an led on and off is not linear, but i would like to increase the duty cycle in a linear pattern, for now.
I seen to have a problem though. It doesn't seem as simple as to just increase a register by one, because the values associated with the duty cycle are concatenated from two registers.
(CCPR1L:CCP1CON<4:5>), and is 10 bits.
I'm wondering if there's a simple trick i can use to increase my duty cycle.

Thank you, Kronoss

If you need my code just pm me
Title: Re: PWM duty cycle question
Post by: Hazer on August 09, 2011, 02:44:52 AM
Those 2 bits in the CCP1CON are the LSB bits, you use them if you really want 10 bits of resolution. You dont really need it though. If you just simply leave them alone, you now have 8 bits of resolution.

If you want 10 steps, then 8 bits = 256 steps. So just add 25 (or 0x19) to the CCPR1L register to to step up 1/10 of the range at a time. I would have 25 as the lowet (dont leave it 0) and 250 as the highest.
Title: Re: PWM duty cycle question
Post by: kronosslayer on August 09, 2011, 12:27:37 PM
Ok, so i have the PWM working. I have everything declared up front, then, my program goes to an infinite loop while TMR2 counts up. When Tmr2 overflows the ISR is called, then i clear my TRISIO bits and increment my DC. It seems that im only able to see the LED at 10% duty cycle and no visual increment is happening.  :eyebrow: . According to the datasheet i clear my TRISIO bits before a new PWM starts, does that mean i must re-declare all registers related to the PWM to be able to see the new signal? :confused:
Title: Re: PWM duty cycle question
Post by: 802Chives on November 20, 2011, 10:02:40 AM
fading an LED on or OFF is not linear, if you want smoother fade, use a TABLE to get your duty cycle values and Use a GAMMA CORRECTION curve for the values in the table.

As for how you accomplish your PWM, that can be done any number of ways, using a Timer is how I would do it rather then CCP because the PWM function of most pics can only handle 1 or 2 hardware produced PWM signal.  Bit banging a timer is much more expandable.
SimplePortal 2.3.5 © 2008-2012, SimplePortal