|
|
View previous topic :: View next topic |
Author |
Message |
soondin
Joined: 21 Apr 2011 Posts: 9
|
Capture and Generate Pulse |
Posted: Tue May 10, 2011 5:43 am |
|
|
Hello All.
I try to capture pulse width from around 14ms to 300us with CCP pins
and no input(I don't know it's 0 or unlimited)
and it's use to generate pulse at PIN_A0.
Following is my code.
some interrupt code used from example.
Code: |
#include <18F2550.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7)
#zero_ram
unsigned int lim,set,kph;
unsigned long rise,fall,p_wid,us,pul,offset,min;
#int_ccp2
void isr()
{
rise = CCP_1;
fall = CCP_2;
p_wid = fall - rise;
}
void main()
{
output_high (PIN_A0);
lim = 190;
set = lim * 0.95;
pul = 140 * 500 / lim * 1.05;
offset = 280;
min = 5;
setup_ccp1 (CCP_CAPTURE_RE);
setup_ccp2 (CCP_CAPTURE_FE);
setup_timer_1 (T1_INTERNAL);
enable_interrupts (INT_CCP2);
enable_interrupts (GLOBAL);
while(TRUE)
{
us = p_wid / 5;
kph = 140 * 500 / us ;
//printf("\r%lu", kph);
if( (us == 0) || (kph <= min) )
{
output_high (PIN_A0);
}
else if( (us != 0) && ((kph > min) && (kph <= set)) )
{
output_low (PIN_A0);
delay_us (us);
output_high (PIN_A0);
delay_us (us - offset);
}
else if( (us != 0) || (kph > set) )
{
output_low (PIN_A0);
delay_us (pul);
output_high(PIN_A0);
delay_us (pul - offset);
}
}
}
|
I use this code for speed converter. but my code works okay with.
My target but have one problems.
Some time when no input to CCP pin or when start input CCP pin,
PIN_A0 makes some strange pulse.
Any one have good idea to solve this problems? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue May 10, 2011 6:23 am |
|
|
Check the datasheet to see if any internal peripherals are associated with that pin, if so, they must be disabled.
Typically pins used for ADC are enabled by default... |
|
|
soondin
Joined: 21 Apr 2011 Posts: 9
|
|
Posted: Wed May 11, 2011 2:03 am |
|
|
Thank you for good informations.
I've add followings codes, Looks like be more better.
Still my code have problems, I can't back to PIN_A0 to high.
If input make some changed width pulse to CCP.
I can make what I want at PIN_A0 but when CPP pin be just high or low.
PIN_A0 makes output around 8ms width pulse.
I don't know why...
Code: |
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_OFF);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_vref(FALSE);
|
temtronic wrote: | Check the datasheet to see if any internal peripherals are associated with that pin, if so, they must be disabled.
Typically pins used for ADC are enabled by default... |
|
|
|
|
|
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
|