CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

How to drive PIC at 2KHz frequency?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Spradecom



Joined: 01 Jun 2011
Posts: 19
Location: INDIA

View user's profile Send private message MSN Messenger

How to drive PIC at 2KHz frequency?
PostPosted: Tue Oct 18, 2011 5:41 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 18, 2011 2:08 pm     Reply with quote

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

View user's profile Send private message MSN Messenger

PostPosted: Tue Oct 18, 2011 10:46 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 18, 2011 10:55 pm     Reply with quote

Example programs in which the PWM duty cycle is set by ADC value:
http://www.ccsinfo.com/forum/viewtopic.php?t=40222&start=1
http://www.ccsinfo.com/forum/viewtopic.php?t=43169&start=1
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group