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

measure period

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



Joined: 10 Oct 2008
Posts: 46
Location: GREECE

View user's profile Send private message

measure period
PostPosted: Mon Oct 26, 2009 10:39 am     Reply with quote

hello,

I have a little problem about measure of period from an input signal (square).
The method that I use is CCP1. Below is the code that I use.
I have to notice that this code is copied and modified from this topic:
http://www.ccsinfo.com/forum/viewtopic.php?t=29963
CCP1 pin RC2 (13) on PIC16f876a
Code:

#include <16F876a.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)

#byte PORTA = 0x05
#byte PORTB = 0x06
#byte PORTC = 0x07
   
int16 ccpvalue;

#int_ccp1
void ccp1_isr(void)
{
int16 temp_ccp;
int16 prev_ccp = 0;
temp_ccp = CCP_1; 
ccpvalue = temp_ccp - prev_ccp;
prev_ccp = temp_ccp;
}


void main() {

int16 main_ccp;

set_timer1(0);           
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1); 
setup_ccp1(CCP_CAPTURE_RE);   
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
PORTB=0x00;

while(TRUE){
  disable_interrupts(GLOBAL);
  main_ccp = ccpvalue;
  PORTB=0xff;
  delay_us(main_ccp);
  PORTB=0x00;
  enable_interrupts(GLOBAL);
  }             

}
Ttelmah
Guest







PostPosted: Mon Oct 26, 2009 11:12 am     Reply with quote

Critical change.
Note the keyword _static_ on the declaration of the old CCP time value in the interrupt.
Without this, 'prev_ccp' will be re-initialised to 0 every time the interrupt is called, and is not guaranteed to hold it's old value, so the result will be silly....

Best Wishes
pvol



Joined: 10 Oct 2008
Posts: 46
Location: GREECE

View user's profile Send private message

PostPosted: Mon Oct 26, 2009 11:38 am     Reply with quote

I change it but without result...
Code:

void ccp1_isr(void)
{
int16 temp_ccp;
static int16 prev_ccp = 0;
temp_ccp = CCP_1; 
ccpvalue = temp_ccp - prev_ccp; 
prev_ccp = temp_ccp;
}

Is anything else that I have to check?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 26, 2009 12:48 pm     Reply with quote

Quote:
while(TRUE){
disable_interrupts(GLOBAL);
main_ccp = ccpvalue;

PORTB=0xff;
delay_us(main_ccp);
PORTB=0x00;

enable_interrupts(GLOBAL);
}

What do you think this loop code will do ? PORTB will be = 0xFF
most of the time. It will be = 0x00 for about 50 usec if an interrupt
occurs, then it will go to 0xFF again.

If you look at it on your oscilloscope, you will see a solid +5v line
for all Port B pins, with occasionally a short glitch going to 0 volts.
pvol



Joined: 10 Oct 2008
Posts: 46
Location: GREECE

View user's profile Send private message

PostPosted: Mon Oct 26, 2009 12:56 pm     Reply with quote

I change it to ms but i don't see any changes.
The problem is that I think somewhere the program has big problem but
I don't know where. I check hardware connections and is ok.
PORTB never lights on!!!
Code:
while(TRUE){
   main_ccp = ccpvalue;
      PORTB=0xff;
          delay_ms(main_ccp);
          PORTB=0x00;
   enable_interrupts(GLOBAL);


 }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 26, 2009 1:00 pm     Reply with quote

Look at your code.

You have no time delay for the "off" time. It will only be off for a few
microseconds. It will appear to always be on. Look at it.
pvol



Joined: 10 Oct 2008
Posts: 46
Location: GREECE

View user's profile Send private message

PostPosted: Mon Oct 26, 2009 1:27 pm     Reply with quote

I receive square signal from my sensor.
Looks like the program never goes to WHILE{ }
I miss something???
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 26, 2009 1:52 pm     Reply with quote

It never lights up because the TRIS is still in the power-on reset state,
which is "all inputs".

Don't write directly to the port. It's much easier to let CCS do it.
If you use the CCS i/o functions, they automatically take care of
the TRIS. It's designed for newbies.

Use
Code:
output_b(0xFF);

and
Code:
output_b(0x00);


The compiler will setup the TRIS and it will do the output.
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