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 CCS Technical Support

Capture and Generate Pulse

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



Joined: 21 Apr 2011
Posts: 9

View user's profile Send private message

Capture and Generate Pulse
PostPosted: Tue May 10, 2011 5:43 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue May 10, 2011 6:23 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 11, 2011 2:03 am     Reply with quote

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...
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