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

CCP (capture) issue with 16f1827

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



Joined: 08 Apr 2015
Posts: 77

View user's profile Send private message

CCP (capture) issue with 16f1827
PostPosted: Sat Aug 19, 2017 8:31 am     Reply with quote

Hey,

I want to capture a square wave and measure its frequency and do certain things if that frequency falls into a small spectrum. I use a signal generator for the square wave that i feed into the PIC and this code from here https://www.ccsinfo.com/forum/viewtopic.php?t=29963

Code:

#include <16F1827.h>

#fuses   NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(internal = 4000000)
 

 int16 isr_ccp_delta;

#int_ccp4
void ccp1_isr(void)
{
int16 current_ccp;
static int16 old_ccp = 0;

// Read the 16-bit hardware CCP1 register
current_ccp = CCP_4; 
// Calculate the time interval between the
// previous rising edge of the input waveform
// and the current rising edge.  Put the result
// in a global variable, which can be read by
// code in main().
isr_ccp_delta = current_ccp - old_ccp;

// Save the current ccp value for the next pass.
old_ccp = current_ccp;
}

void main()
{

int16 current_ccp_delta;
int16 frequency;


set_timer1(0);           
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
setup_ccp4(CCP_CAPTURE_RE);
clear_interrupt(INT_CCP4);
enable_interrupts(INT_CCP4);
enable_interrupts(GLOBAL);

while(1)
  {

disable_interrupts(GLOBAL);
   current_ccp_delta = isr_ccp_delta;
   enable_interrupts(GLOBAL);
    frequency = (int16)(1000000L / current_ccp_delta);
if (frequency>20000 && frequency<22000)
output_high(PIN_A0);
else output_low(PIN_A0);
delay_ms(100);
}
}


The problem is that even though my generator outputs a smooth signal the LED that is connected to PIN_A0 goes from high to low at random intervals when the right frequency is detected (20kHz-22kHz).
ccs v.5.008
Any ideas why it's not staying ON continuously?
thanks


Last edited by irmanao on Mon Aug 21, 2017 9:46 am; edited 3 times in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Aug 19, 2017 10:47 am     Reply with quote

Before even looking at your code too much, can you answer these
questions:

1. What is the Vdd voltage of your PIC ?

2. What are the high and low voltages of your incoming square wave
signal ? If you have an oscilloscope, look at the voltages.

Also, one more thing:
Your while(1) loop doesn't have any delay in it. I would add a delay of
100 ms to the end of the loop. Then I think you would get a better
impression of the on/off timing of the LED. Add this line:
Code:
delay_ms(100);
irmanao



Joined: 08 Apr 2015
Posts: 77

View user's profile Send private message

PostPosted: Sat Aug 19, 2017 11:11 am     Reply with quote

Yeap, added the delay and it now works great. Thanks for the valuable tip and time. Very Happy
irmanao



Joined: 08 Apr 2015
Posts: 77

View user's profile Send private message

PostPosted: Sat Aug 19, 2017 1:42 pm     Reply with quote

I now came across a different problem... If i feed the CCP with a short pulse of the correct frequency, the LED does go ON but it never goes off. Any ideas?

thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Aug 19, 2017 2:46 pm     Reply with quote

A short pulse ?

Do you mean one individual, single, lone, pulse that is of some
unspecified short time duration ? If so, post the duration of the
single pulse.


Or do you mean a short burst of 20-22 KHz squarewave ?
If so, post the length of the burst in time, or in the number of full cycles
of the squarewave.


Also, post the amount of time before and after the single pulse, or burst.
irmanao



Joined: 08 Apr 2015
Posts: 77

View user's profile Send private message

PostPosted: Sat Aug 19, 2017 4:41 pm     Reply with quote

I mean a short burst (two full cycles) of the squarewave. I use a push button connected to a different PIC to send it whenever i press it. If i disconnect the wire that feeds that burst, the LED does go low again... but it doesn't after the burst is over.
thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Aug 19, 2017 5:42 pm     Reply with quote

The basic problem is, you've had a CCP interrupt, then there's no more
signal, so you don't get anymore interrupts. Therefore, 'isr_ccp_delta'
retains the last value that it was set to. The calculated frequency
doesn't change, and the LED stays on.

There are a couple ways to handle this.

1st method:
You could, after copying isr_ccp_delta into current_ccp_delta, just set
isr_ccp_delta to 1000000L or something. That number, when used in
the frequency calculation, will give you a freq of 1. Your LED will go off.
I haven't tested this, but it sounds like it would work.

2nd method:
This post explains how to do it:
http://www.ccsinfo.com/forum/viewtopic.php?t=29963&start=9
The post below gives a code example that implements that method.
You can see that a global 'gc_capture_flag' variable was added to the
program. Then it's set inside the isr to indicate a CCP interrupt occurred.
http://www.ccsinfo.com/forum/viewtopic.php?t=33153&&start=67

If the first method works, then go with it.
irmanao



Joined: 08 Apr 2015
Posts: 77

View user's profile Send private message

PostPosted: Sun Aug 20, 2017 4:47 am     Reply with quote

Yep, the first method worked. Thanks a lot for the original code also.
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