View previous topic :: View next topic |
Author |
Message |
pvol
Joined: 10 Oct 2008 Posts: 46 Location: GREECE
|
measure period |
Posted: Mon Oct 26, 2009 10:39 am |
|
|
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
|
|
Posted: Mon Oct 26, 2009 11:12 am |
|
|
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
|
|
Posted: Mon Oct 26, 2009 11:38 am |
|
|
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
|
|
Posted: Mon Oct 26, 2009 12:48 pm |
|
|
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
|
|
Posted: Mon Oct 26, 2009 12:56 pm |
|
|
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
|
|
Posted: Mon Oct 26, 2009 1:00 pm |
|
|
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
|
|
Posted: Mon Oct 26, 2009 1:27 pm |
|
|
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
|
|
Posted: Mon Oct 26, 2009 1:52 pm |
|
|
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
and
The compiler will setup the TRIS and it will do the output. |
|
|
|