View previous topic :: View next topic |
Author |
Message |
Spradecom
Joined: 01 Jun 2011 Posts: 19 Location: INDIA
|
Help to read pulse width on PIC 16F684 pin |
Posted: Wed Jul 25, 2012 6:14 am |
|
|
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
|
|
Posted: Wed Jul 25, 2012 7:48 am |
|
|
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: 1615 Location: Central Illinois, USA
|
|
|
Spradecom
Joined: 01 Jun 2011 Posts: 19 Location: INDIA
|
Re: Help to read pulse width on PIC 16F684 pin |
Posted: Thu Jul 26, 2012 1:58 am |
|
|
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
|
|
Posted: Thu Jul 26, 2012 2:44 pm |
|
|
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 |
|
|
|