How do you intuitively pick the PWM frequency for different applications?
Does it depend on the device switching characteristics?
For example, an LED could be turned ON and OFF relatively quickly compared to say, a DC motor.
Does this dictate higher PWM frequency for a motor when compared to an LED?
What other factors (if any) need to be taken into consideration?
To begin with, here is my PWM code:
It compiles and builds correctly. However when I built the circuit the LED glows brightly instead of going to full brightness smoothly and back. The voltage across the LED is 2.5V.
Does this imply that the frequency of switching is a bit too high (19.53 MHz) so that the voltage appears continuous to the LED?
I have no prior experience with PWM much less using it alongside a microcontroller.
Thanks!
Does it depend on the device switching characteristics?
For example, an LED could be turned ON and OFF relatively quickly compared to say, a DC motor.
Does this dictate higher PWM frequency for a motor when compared to an LED?
What other factors (if any) need to be taken into consideration?
To begin with, here is my PWM code:
Rich (BB code):
//main.c
//To use PWM for dimming an LED
//PWM Frequency: 19.53 MHz
//CCP1 Register Pin=RC2
#include<htc.h>
#define _XTAL_FREQ 20000000
__CONFIG(FOSC_HS & WDTE_OFF & DEBUG_ON & LVP_OFF & CPD_OFF & CP_OFF & PWRTE_OFF & BOREN_OFF);
void main()
{
unsigned char duty; //16 bit value
TRISC=0; //set PORTC to output
PORTC=0; //set PORTC low
PR2=0xFF; //set TMR2 period register
T2CON=0b00000100; //set postscaler to 1:1; prescaler to 1
CCP1CON=0b00001111; //set the 2 LSB of duty cycle to 0 and initialize CCP module as PWM
duty=0;
while(1)
{
while(duty<255) //increase duty cycle from 0% to 100%
{
CCPR1L=duty;
duty++;
#ifdef __DEBUG //if debugging right now then skip the delay routine
#else
__delay_ms(300);
#endif
}
while(duty>0) //decrease duty cycle from 0% to 100%
{
CCPR1L=duty;
duty--;
#ifdef __DEBUG
#else
__delay_ms(300);
#endif
}
}
}
Does this imply that the frequency of switching is a bit too high (19.53 MHz) so that the voltage appears continuous to the LED?
I have no prior experience with PWM much less using it alongside a microcontroller.
Thanks!