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

Help to read pulse width on PIC 16F684 pin

 
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

Help to read pulse width on PIC 16F684 pin
PostPosted: Wed Jul 25, 2012 6:14 am     Reply with quote

I am using PIC 16F684 to read pulse from optocoupler that outputs a square wave on INT pin. I am trying to write code with input frequency 100Hz.
16F684 uses crystal frequency 4MHz internal oscillator. And want to generate PWM on Pin 5 with 250Hz. Can you draft any example program ? THANKS
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Jul 25, 2012 7:48 am     Reply with quote

This is a help forum, NOT a write it for you one.

Have a go yourself.
Come back when you're stuck.
Show us what you have done.
Explain what your code should do.
Explain what it does / doesn't do.

There are loads of examples provided by CCS.
Read the CCS forum guide, CCS manual, and data sheet for your PIC.

When you can demonstrate that you have made an effort, it is more than likely that someone here will offer real/valuable assistance.

Mike
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Jul 25, 2012 8:20 am     Reply with quote

And make sure to read: http://www.ccsinfo.com/forum/viewtopic.php?t=47549

before you post.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
Spradecom



Joined: 01 Jun 2011
Posts: 19
Location: INDIA

View user's profile Send private message MSN Messenger

Re: Help to read pulse width on PIC 16F684 pin
PostPosted: Thu Jul 26, 2012 1:58 am     Reply with quote

Spradecom wrote:
I am using PIC 16F684 to read pulse from optocoupler that outputs a square wave on INT pin. I am trying to write code with input frequency 100Hz.
16F684 uses crystal frequency 4MHz internal oscillator. And want to generate PWM on Pin 5 with 250Hz. Can you draft any example program ? THANKS


I had try on this code plz can any help
Code:

//------------------------------------------------------
unsigned int1 state;
unsigned int1 start_count;
unsigned int16 count;
unsigned int16 old_count;
unsigned int16 duty;
unsigned int16 new_duty;
unsigned int8 ramp_delay_counter;

void read_data(void);
void initialise_ports(void);
void init_variable(void);
//------------------------------------------------------
#int_EXT
void  EXT_isr(void)
{
   state=0;
   state = input(PIN_A2);
   if(state   ==   0)
   { 
      start_count=1;
   }
   else
   {
      start_count=count=0;
   }
   
}
//------------------------------------------------------
#int_TIMER0
void  TIMER0_isr(void)
{
   if(start_count)
   {
      if(count < 80)
      {
         count++;
      }
      else
      {
         count=0;
      }
   }
   else
   {
      count=start_count=0;
   }
   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(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_16,249,1);
   setup_oscillator(OSC_4MHZ);
   setup_ccp1(CCP_PWM);
   set_pwm1_duty(0);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   ext_int_edge(H_TO_L);
   initialise_ports();
   init_variable();
   enable_interrupts(INT_EXT);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   while(TRUE)
   {
      read_data();
      state = input(PIN_A2);
      if(state   ==   1)
      { 
         old_count=count;
         start_count=0;
         count=0;
      }
   }
}
//------------------------------------------------------
//------------------------------------------------------
void initialise_ports(void)
{
   output_float(PIN_A0);
   output_float(PIN_A1);
   output_high(PIN_A2);
   output_float(PIN_A3);
   output_float(PIN_A4);
   output_float(PIN_A5);

   output_float(PIN_C0);
   output_float(PIN_C1);
   output_float(PIN_C2);
   output_float(PIN_C3);
   output_float(PIN_C4);
   output_low(PIN_C5);
}
//------------------------------------------------------
void init_variable(void)
{
   duty=0;
   new_duty=0;
   ramp_delay_counter = 0;
}
//------------------------------------------------------
void read_data(void)
{
   
   unsigned int16 calc_duty;
   clear_interrupt(INT_EXT);
   old_count=35-count;
   calc_duty=(unsigned int16)(old_count * 48);
/*   if(calc_duty <= 51)
   {
      new_duty=2;
   }
   else
   {
      if(calc_duty >= 662)
      {
         new_duty=1023;
      }
      else
      {
         calc_duty=calc_duty-51;
         new_duty=(unsigned int16)(calc_duty * 1.54);
      }
   }*/
   new_duty=calc_duty;
   
}
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Jul 26, 2012 2:44 pm     Reply with quote

You're not following temtronic's mantra, and also trying to do several things at once.

I, for one, can't follow what you're doing, I'm not going to spend hours deciphering your code.

So, simplify, simplify, simplify, when you're done, simplify some more.

Break your problem into manageable chunks, then deal with them one at a time.

Mike
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