EmbeddedRelated.com
Forums

PIC18F452 & C18: capture module set-up ?

Started by Rodo May 22, 2004
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){
   newCCPval=ReadCapture1();
   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
   PIR1bits.CCP1IF=0;
}
//------------------ 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 &
T1_OSC1EN_ON & T1_SYNC_EXT_ON);
WriteTimer1(0);
//------------------ 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
OpenCapture1(CAPTURE_INT_ON & C1_EVERY_4_RISE_EDGE);
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
LCD:

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


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

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

        WriteCmdXLCD(DISPLAY_CLEAR);
        while(BusyXLCD());
        SetDDRamAddr(LCD_LINE2);
        while(BusyXLCD());

        putsXLCD(ccpDataStr);  //display value
        newCaptureDataFlag=0;
    }
}
//------------------ 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




>OpenTimer1(TIMER_INT_OFF & T1_16BIT_RW & T1_SOURCE_EXT & T1_PS_1_1 &
T1_OSC1EN_ON & T1_SYNC_EXT_ON); 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 OpenTimer1.
http://ww1.microchip.com/downloads/en/DeviceDoc/39564b.pdf
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.



On Sat, 22 May 2004 03:41:07 GMT, "Rodo" <dsp1024@yahoo.com> wrote:
>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)
>..>.excess deleted>
>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 & >T1_OSC1EN_ON & T1_SYNC_EXT_ON); >WriteTimer1(0); >//------------------ code fragment ends ----------------------- > >There is a 32KHz crystal on the osc for timer1. The mcu is running at 4MHz.
>..>.excess deleted>
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.

Thanks


<g9u5dd43_nospam@yahoo.com> wrote in message
news:40af767c.6681217@news2.news.adelphia.net...
> http://ww1.microchip.com/downloads/en/DeviceDoc/39564b.pdf > 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. > > > > On Sat, 22 May 2004 03:41:07 GMT, "Rodo" <dsp1024@yahoo.com> wrote: > >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) > > >..>.excess deleted> > > >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 & > >T1_OSC1EN_ON & T1_SYNC_EXT_ON); > >WriteTimer1(0); > >//------------------ code fragment ends ----------------------- > > > >There is a 32KHz crystal on the osc for timer1. The mcu is running at
4MHz.
> > >..>.excess deleted>
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 :-).

Thanks


"Gary Kato" <garykato@aol.com> wrote in message
news:20040522052733.08524.00002029@mb-m02.aol.com...
> >OpenTimer1(TIMER_INT_OFF & T1_16BIT_RW & T1_SOURCE_EXT & T1_PS_1_1 & > T1_OSC1EN_ON & T1_SYNC_EXT_ON); > > 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 > OpenTimer1. > > >