View previous topic :: View next topic |
Author |
Message |
Spradecom
Joined: 01 Jun 2011 Posts: 19 Location: INDIA
|
How to drive PIC at 2KHz frequency? |
Posted: Tue Oct 18, 2011 5:41 am |
|
|
I am using PIC 16F684 for light dimming application. Previously I had used 250Hz frequency. My application is to read ADC and generate PWM according to input of ADC, dimming from 420 count (2.0V ADC) to 921 count (4.5V ADC) from .2% to 100% (I from 2 to 1023 count) PWM, now I want to change PWM frequency from 250 Hz to 2 KHz. I have tried but change of ADC count occur also using timer0 interrupt overflow at 512us. What step i have followed to get proper output? Can any help me.
My initialization are following. Crystal Frequency 4MHz.
Code: |
#int_TIMER0
void TIMER0_isr(void)
{
if(duty != new_duty)
{
if(ramp_delay_counter < 5)
{
ramp_delay_counter ++;
}
else
{
if(duty < new_duty)
{
duty ++;
}
else if(duty > new_duty)
{
duty --;
}
ramp_delay_counter = 0;
}
set_pwm1_duty(duty);
}
}
//------------------------------------------------------------------------------
void main()
{
setup_adc_ports(sAN2|sAN5|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_64);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_4,124,1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(0);
setup_oscillator(OSC_4MHZ);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER0);
initialise_ports();
init_variable();
delay_ms(100);
read_init_position();
enable_interrupts(GLOBAL);
while(TRUE)
{
new_duty=collect_adc_data(5);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Oct 18, 2011 2:08 pm |
|
|
This program will create a PWM signal on pin C2 with a frequency of
2.0 KHz. Try it. It works.
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
//==========================================
void main()
{
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_4, 124, 1);
set_pwm1_duty(62);
while(1);
} |
|
|
|
Spradecom
Joined: 01 Jun 2011 Posts: 19 Location: INDIA
|
|
Posted: Tue Oct 18, 2011 10:46 pm |
|
|
Thanks sir for your valuable response. If I want to use 10bit ADC and 10 Bit PWM with 2KHzs freqency how to use program?
I had worked with 250Hzs it gives proper output but frequency changes from 250 Hzs to 2 KHzs.output does not give proper.
Last edited by Spradecom on Tue Oct 18, 2011 11:44 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|