I'm using a PICDEM USB mounted with PIC18F4550 and I'm using the C18 compiler. I
want to measure a certain period of time at one of the PIC's inputs, lets say between a falling edge and a rising edge. so what i need to do is to store the value of a "timer" at the falling edge moment and store it again at the rising edge moment and do the comparison, but how to write it in C18 with all timer configuration and stuff !??
with capture/compare module? sure,
something like this:
input = CCP1 pin on my 2550 it was RC2
Code:
#define input PORTCbits.RC2
unsigned int getsize(){
//configuracion previa
T1CON=0b10110000; //div 1:8 16 bits
TMR1H=0;
TMR1L=0;
while(!input);//wait the 0...
T1CONbits.TMR1ON=1;
CCP1CON=0b0101; //search rising
PIR1bits.CCP1IF=0; //lost a us
while(!PIR1bits.CCP1IF);// here!
CCP1CON=0b0000;
//read result
return (CCPR1H*0x100)+ CCPR1L;
}
you could search the falling edge by setting CCP1CON to 0b0100 (as said on datasheet) but this is how i made it... change or set the Timer prescaler to a more suitable value (for your app)