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

problem in CCP (capture mode)

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



Joined: 18 May 2010
Posts: 78

View user's profile Send private message

problem in CCP (capture mode)
PostPosted: Tue May 18, 2010 2:32 am     Reply with quote

I want to measure a pulse wave's period(T) that its duty cycle is variable. For this, I used a capture mode on CCP.
There is my source code (CCS C compiler). But when I run it in Proteus, unfortunately the CCP interrupt doesn't work (MCU doesn't detect an interrupt on its input). I've connected a digital pulse to PIN_C2 by a pullup resistor in Proteus.
What is my faults?
Question Question Question
Code:

#include<16f887.h>
#use delay(clock=4000000)
#include <lcd.c>
#int_TIMER1
#int_CCP1 //capture mode interrupt
#define use_lcd_portd true //set port D to use LCD

//declared variables
static unsigned long old_rise,new_rise;
unsigned char period;
unsigned int flag_counter,multiple;

void  TIMER1_isr(void)  //timer1 interrupt
{
flag_counter++; //this is a counter for timer1 overflows
clear_interrupt(int_timer1);// clear timer1 interrupt's flag
}

void  CCP1_isr(void)
{
  multiple=flag_counter;
  flag_counter=0;
  old_rise=new_rise;
  new_rise=ccp_1;
  period=((new_rise)-(old_rise)+(256*(multiple+1)))*8u;
  clear_interrupt(int_ccp1); //clear ccp1 flag to run for next edge
}

void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_CCP1);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);  //set timer1 in 8us resolution
setup_ccp1(CCP_CAPTURE_FE);
set_tris_c(0xff);   //CCP1 pin is declared as a input

lcd_init();

while(1){
lcd_gotoxy(1,2);
printf(lcd_putc,"%u s", period);
delay_us(50);
}

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 18, 2010 12:45 pm     Reply with quote

CCS has an example to measure the pulse width by using two CCP
modules:
Quote:

c:\program files\picc\examples\ex_ccpmp.c


If you want to use only one CCP module, then look at the sample
programs near the end of this thread. They show how to measure
the pulse width with one CCP:
http://www.ccsinfo.com/forum/viewtopic.php?t=31470

CCS also has an example which uses Timer0 (not the CCP) to measure
the pulse width:
Quote:

c:\program files\picc\examples\ex_pulse.c


If you want to measure the pulse width entirely in software (without the
CCP or Timer0), this thread has code to do that:
http://www.ccsinfo.com/forum/viewtopic.php?t=42353

-------------------

If you want to measure the frequency of a signal (Not just the pulse
width), then look at these threads. These are multi-page threads.
http://www.ccsinfo.com/forum/viewtopic.php?t=33153
http://www.ccsinfo.com/forum/viewtopic.php?t=39318
http://www.ccsinfo.com/forum/viewtopic.php?t=29963
http://www.ccsinfo.com/forum/viewtopic.php?t=36852

Software measurement of frequency (Does not use CCP or Timer):
http://www.ccsinfo.com/forum/viewtopic.php?t=41877&start=3
mojsa2000



Joined: 18 May 2010
Posts: 78

View user's profile Send private message

PostPosted: Tue May 18, 2010 2:07 pm     Reply with quote

I'm so appreciate for your attention. But I want to find my program's bugs if is possible. I think the program routine is true but there are bugs that I can't find them. Please help me.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 18, 2010 2:24 pm     Reply with quote

Quote:
#include<16f887.h>
#use delay(clock=4000000)
#include <lcd.c>
#int_TIMER1
#int_CCP1 //capture mode interrupt

One problem is that you have stuck these interrupt routine declarations
up in the front part of the program. That is not correct. They must
be placed in front of the routine that they are used for.

Look at any of the programs in the links that I gave you, and you will
see where to put the #int_xxx statements.

Look in the .LST file for your program, after you compile it.
You will see that when you put the #int_xxx lines "up in the air" like
you have done, that the compiler does not generate any ASM code for
the interrupt routines:

Code:
.................... 
.................... void  TIMER1_isr(void)  //timer1 interrupt 
.................... { 
.................... flag_counter++; //this is a counter for timer1 overflows 
.................... clear_interrupt(int_timer1);// clear timer1 interrupt's flag 
.................... } 
.................... 
.................... void  CCP1_isr(void) 
.................... { 
....................   multiple=flag_counter; 
....................   flag_counter=0; 
....................   old_rise=new_rise; 
....................   new_rise=ccp_1; 
....................   period=((new_rise)-(old_rise)+(256*(multiple+1)))*8u; 
....................   clear_interrupt(int_ccp1); //clear ccp1 flag to run for next edge 
.................... } 
.................... 
........


Put the #int_xxx lines in the correct place, as shown below, and compile
it. You will notice a change in the .LST file.
Quote:

#int_timer1
void TIMER1_isr(void) //timer1 interrupt
{
flag_counter++; //this is a counter for timer1 overflows
clear_interrupt(int_timer1);// clear timer1 interrupt's flag
}

#int_ccp1
void CCP1_isr(void)
{
multiple=flag_counter;
flag_counter=0;
old_rise=new_rise;
new_rise=ccp_1;
period=((new_rise)-(old_rise)+(256*(multiple+1)))*8u;
clear_interrupt(int_ccp1); //clear ccp1 flag to run for next edge
}
mojsa2000



Joined: 18 May 2010
Posts: 78

View user's profile Send private message

PostPosted: Wed May 19, 2010 1:48 pm     Reply with quote

Dear PCM Programmer
Thank you. I found my bugs in CCS programming because of your help.
But I had a big problem too. My problem was in my Proteus.
Its version is 7.6 and I didn't know that has a big fault in PICs.
I found files and fixed it.
The files are:
LCDPIXEL.dll
pic16.dll
READOUT.dll

Anyway thank you.
vsmguy



Joined: 13 Jan 2007
Posts: 91

View user's profile Send private message Visit poster's website

PostPosted: Tue Jul 06, 2010 4:30 pm     Reply with quote

You fixed an error in Proteus where the CCP Interrupt was not firing regardless of transitions at that pin?

How did you solve that?
allr



Joined: 28 Sep 2006
Posts: 4

View user's profile Send private message

Re: problem in CCP (capture mode)
PostPosted: Wed Jul 07, 2010 1:51 am     Reply with quote

A question,

What happens when "new_rise" value are < that "old_rise" valued because an overflow of the Timer1 has happened?

mojsa2000 wrote:
... period=(new_rise)-(old_rise)...


Thanks


Last edited by allr on Wed Jul 07, 2010 9:04 am; edited 1 time in total
vsmguy



Joined: 13 Jan 2007
Posts: 91

View user's profile Send private message Visit poster's website

Re: problem in CCP (capture mode)
PostPosted: Wed Jul 07, 2010 8:29 am     Reply with quote

allr wrote:
A question,

What happens when "new_rise" value are < that "old_rise" valued because an overflow of the Timer1 has happened?

mojsa2000 wrote:
... period=(new_rise)-(old_rise)...


Thanks


He has handled it in TIMER1_isr. Look closely.
gmua



Joined: 07 Feb 2011
Posts: 11

View user's profile Send private message

PostPosted: Mon Mar 14, 2011 2:05 pm     Reply with quote

mojsa2000 wrote:
But I had a big problem too. My problem was in my Proteus.
Its version is 7.6 and I didn't know that has a big fault in PICs.
I found files and fixed it.
The files are:
LCDPIXEL.dll
pic16.dll
READOUT.dll


Hello mojsa2000, where did you find those files? I'm having similar problems with int_ccp1 in Proteus 7.6 SP0, but it works fine in version 7.4 SP3
desert eagle



Joined: 27 Sep 2013
Posts: 7
Location: Frankfurt, Germany

View user's profile Send private message

Re: problem in CCP (capture mode)
PostPosted: Fri Oct 04, 2013 6:36 am     Reply with quote

Hi there!! I have tried to use the above explanation and example to measure the average period of a wave and I want to use this 'period' value as the period of my output pulse. For that, I am writing this period value to a timer and when the timer overflows after this 'period', the ISR for the timer generates a pulse. but its not working. I am using PIC24FV32KA301. Can anyone help in this regard.
_________________
Asghar
temtronic



Joined: 01 Jul 2010
Posts: 9074
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Oct 04, 2013 8:35 am     Reply with quote

REAL hardware or Proteus ?

If inside real hardware, easy.. if Proteus, not possible

hth
jay
desert eagle



Joined: 27 Sep 2013
Posts: 7
Location: Frankfurt, Germany

View user's profile Send private message

PostPosted: Fri Oct 04, 2013 8:47 am     Reply with quote

Real Hardware. Not proteus.
_________________
Asghar
desert eagle



Joined: 27 Sep 2013
Posts: 7
Location: Frankfurt, Germany

View user's profile Send private message

PostPosted: Mon Oct 07, 2013 12:40 am     Reply with quote

Anyone who can help in this regard? at least give some suggestion of how its possible
_________________
Asghar
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