PIC18F452 & C18: capture module set-up ?

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Hi all,

I've been trying to set up the ccp1 on this MCU for two days now. I got to
the point where I have the ccp1 irq working (here is the irq fragment)

//------------------ code fragment begins -----------------------
if (PIR1bits.CCP1IF==(unsigned int)1){
   newCaptureDataFlag=1;                         <--- I only process data in
main when this flag is set
   index++;                                                     <---- this
selects where the next ccp1 value gets stored
   LATDbits.LATD0=~LATDbits.LATD0;         <----- this pin toggles at the
correct  rate
//------------------ code fragment ends -----------------------

The problem I have is when I read the timer value. It always reads the same
value. Even If I change prescaler or timer source oscillator . Here is how
the timer is initialized:

//------------------ code fragment begins -----------------------
OpenTimer1(TIMER_INT_OFF & T1_16BIT_RW & T1_SOURCE_EXT & T1_PS_1_1 &
//------------------ code fragment ends -----------------------

There is a 32KHz crystal on the osc for timer1. The mcu is running at 4MHz.
Here is the init for the ccp1:

//------------------ code fragment begins -----------------------
T3CONbits.T3CCP1=0; // timer1 is the source for ccp1
T3CONbits.T3CCP2=0; // timer1 is the source for ccp2
PIR1bits.CCP1IF=0; //clear the flag before enabling ccp1
PIE1bits.CCP1IE=1; //enable ccp1

INTCONbits.GIE=1; //enable global interrupts
INTCONbits.PEIE=1; //enable peripheral interrupts
//------------------ code fragment ends -----------------------

Here is the code in main that process the new value and displays it on the

//------------------ code fragment begins -----------------------
        if(index%2==0)                       // alternate saving new capture
value to ccpData[0] and ccpData[1] . Use these two vars to do calculations.

            if(ccpData[0]>ccpData[1])                    // if there is
overflow, subtract the larger from the smaller
                frequency=(65535-ccpData[0])+ccpData[1];     // if there was
overflow correct for roll over

        ultoa(frequency,ccpDataStr);    //unsigned long to ascii


        putsXLCD(ccpDataStr);  //display value
//------------------ code fragment ends -----------------------

I know there is a lot of code here. I'm not looking for a "here is the
solution" deal. But if you see anything funny looking could you please let
me know so I can check further. I'm rather frustrated. This doesn't seem to
be that difficult :-).... and yet ... I can't get it to work :-(.

Thanks a bunch

Re: PIC18F452 & C18: capture module set-up ?
Quoted text here. Click to load it

I'm not familiar with C on the PIC18. I assume the use of & is correct. I seem
to remember someone mentioning a compiler that worked that way.

Do you ever turn Timer1 on? In the OpenTimer1 call, I see you enabling the
oscillator, but you might want to check if bit 0 of TCON1 is 1 (timer1
enabled). I'm not sure the TIMER_INT_OFF is the right flag to pass to

Re: PIC18F452 & C18: capture module set-up ?
I've used the timers for other stuff before and the "OpenTimer1(...)" call
does turns it on. TIMER_INT_OFF prevents the time-out (FFFF -> 0000) from
generating an interrupt that you have to service in the ISR. The capture
just reads the free running timer1 value and stores it when an event occurs.
If you see  my code, I check for an overflow flag because there could be a
rollover from the previous captured timer1 value.

This is how I understand this thing ... but then ... I'm unable to capture
... yet :-).


Quoted text here. Click to load it

Re: PIC18F452 & C18: capture module set-up ?
Page 109 states that the external clock for Timer1 should be:
"External clock from pin RC0/T1OSO/T13CKI (on the rising edge)"
You have indicated that there is a 32kHz crystal on "OSC ".

If you are sure that the crystal is on the right pin,  then change
T1_SYNC_EXT_ON to T1_SYNC_EXT_OFF and use the internal clock just to
make sure the count changes.  If it does, then you know its a problem
with the oscillator.  If it doesn't, then you know the problem is with
the timer setup.

Quoted text here. Click to load it

Re: PIC18F452 & C18: capture module set-up ?
I changed from ext to int osc. No change on the numbers. I've tried so many
variations I lost track of what I've done but I did check this right before
I left work yesterday.


Quoted text here. Click to load it

Site Timeline