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 support@ccsinfo.com

Using external crystal oscillators as clock source
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21644

View user's profile Send private message

PostPosted: Sat May 07, 2022 11:22 am     Reply with quote

Why not put out a signal on the CLKOUT pin and read it with a
frequency meter ? It will be 1/4 of Fosc. Then cut and try to get
the best correction value in the OSCTUNE register.
Ttelmah



Joined: 11 Mar 2010
Posts: 17981

View user's profile Send private message

PostPosted: Sun May 08, 2022 1:05 am     Reply with quote

Perfectly good way of setting it _at one temperature_, but of course
requires an interactive program to do this, and the setting will be wrong
when the temperature drifts from the calibration value. This what
happens with the factory stored value.
I was talking about ways of having the chip correct 'itself' as things drift.
kgng97ccs



Joined: 02 Apr 2022
Posts: 46

View user's profile Send private message

PostPosted: Sun May 08, 2022 4:50 am     Reply with quote

Thank you, Ttelmah and PCM programmer, for sharing your thoughts. I now have a better understanding of the fine-tuning feature of the MCU.

There is one thing I am puzzled about:
Code:
setup_oscillator(OSC_16MHZ | OSC_NORMAL);

When two parameters inside a CCS function are OR'ed, does the CCS compiler actually perform a bitwise OR of the two parameters?

I am asking this because I have noticed that for the above setup_oscillator() function, OSC_NORMAL is assigned a value of 0, and a bitwise OR would give the same result as the original OSC_16MHZ.

Maybe I am not understanding something correctly.
Ttelmah



Joined: 11 Mar 2010
Posts: 17981

View user's profile Send private message

PostPosted: Sun May 08, 2022 10:16 am     Reply with quote

On your chip, the 0 value is the selection for the default oscillator.
So on your chip you could omit OSC_NORMAl.
BUT, this is not the case on all chips. This is why the compiler has
'blank' defines for these. So if you put the value in, and then shift to
another chip, the code will still work.
temtronic



Joined: 01 Jul 2010
Posts: 8511
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun May 08, 2022 6:55 pm     Reply with quote

While the 46k22 is my 'goto' PIC and I use the internal oscillator, I've never had to adjust it, though all my products stay inside with humans..One device uses both serial ports at 115k200 which I'd think WOULD show problems...
If you do go to the effort to 'actively' adjust the clock (somehow), you'll need an accurate (calibrated) temperature reading device.
PCM programmer



Joined: 06 Sep 2003
Posts: 21644

View user's profile Send private message

PostPosted: Sun May 08, 2022 7:24 pm     Reply with quote

He didn't really want to adjust the oscillator. He just wondered what
all the parameters meant.
Ttelmah



Joined: 11 Mar 2010
Posts: 17981

View user's profile Send private message

PostPosted: Mon May 09, 2022 1:08 am     Reply with quote

Yes,
and this also covers the point about 'OSC_NORMAL'. It is nice to see
somebody actually looking at (and trying to understand) what the values
actually 'do'.
kgng97ccs



Joined: 02 Apr 2022
Posts: 46

View user's profile Send private message

PostPosted: Mon May 09, 2022 4:43 am     Reply with quote

Yes, indeed, I was trying to understand what the CCS functions actually do, and your answers have been really helpful.

I have another question.

I understand that the INT_OSCF interrupt will be triggered if the fail-safe clock monitor has switched to the internal oscillator (OSFIF = 1). However, I have not been able to find a similar interrupt for the Timer1 secondary oscillator, should it fail, in the datasheet.

Is it correct that the only way to check whether the secondary oscillator is working, after an initial setup, is to check the T1SOSCEN bit setting of the T1CON register?

Would the following code work?
Code:
setup_timer_1(T1_EXTERNAL | T1_ENABLE_SOSC | T1_DIV_BY_32);  /* example of initial setup */
… /* oscillator start-up delay of 1024 cycles */
if ((setup_timer_1() & T1_ENABLE_SOSC) == 0)
{
    /* secondary oscillator not working */
}

I will appreciate any comments or suggestions.
Ttelmah



Joined: 11 Mar 2010
Posts: 17981

View user's profile Send private message

PostPosted: Mon May 09, 2022 5:57 am     Reply with quote

No.
To test if it is working, you would simply have to test if the timer is
incrementing. Checking that it is enabled, does not tell you that it is
working.
This is how the oscillator fail works. It uses the internal oscillator and
checks that there is a transition from the external oscillator over a time
period. If not it has failed. You would have to do something similar yourself.
Read the timer counter value, wait long enough for it to increment if
the oscillator is working and read the new value. If it has changed then
the timer is running.
temtronic



Joined: 01 Jul 2010
Posts: 8511
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon May 09, 2022 6:00 am     Reply with quote

hmm.. just because the enable bit of the timer is '1' or 'enabled' doesn't mean the timer IS working.
You would have to somehow test it, say against another timer to confirm it is running.

'enable' is like a light switch. Just because the light switch is 'on' doesn't MEAN the light bulb is shining. The bulb could be burned out, broken wire, no mains, tripped breaker.

A poor/bad solder joint could mean the xtal is not connected,so even though the timer IS 'enabled', it's not functional.

I do agree, it's GREAT seeing someone explore the 'inner working's of the PIC !!!
Ttelmah



Joined: 11 Mar 2010
Posts: 17981

View user's profile Send private message

PostPosted: Mon May 09, 2022 6:10 am     Reply with quote

The key point is that for the master oscillator, if it stopped the chip would
be stuck. So Microchip added hardware to make sure the oscillator is
working. This drops you back to the internal oscillator, and flags to say
this has happened. For every other oscillator, it is up to you to write the
code to test it.
kgng97ccs



Joined: 02 Apr 2022
Posts: 46

View user's profile Send private message

PostPosted: Wed May 11, 2022 1:01 am     Reply with quote

Thank you very much for your clear explanations.

I wanted to find out what the following CCS functions return:
Code:
oscillator_value = setup_oscillator(osc_16MHz | osc_NORMAL);
timer0_value = setup_timer_0(T0_INTERNAL | T0_DIV_256);
timer1_value = setup_timer_1(T1_EXTERNAL | T1_ENABLE_SOSC | T1_DIV_BY_2);

... /* printf the above 3 values */

I compared the printf results with the SFRs I viewed from MPLAB IDE v8.92:
oscillator_value = 0x70 (same as OSCCON)
timer0_value = 0x87 (same as T0CON)
timer1_value = 0x00 (different, T1CON = 9F, the same as the resultant parameter value in the setup_timer_1() function)

The first two values suggest that setup_oscillator() and setup_timer_0() represent the OSCCON and T0CON registers respectively.

However, setup_timer_1() did not give the same value as T1CON. Why is this so?
PCM programmer



Joined: 06 Sep 2003
Posts: 21644

View user's profile Send private message

PostPosted: Wed May 11, 2022 1:19 am     Reply with quote

According to the CCS manual the setup_timer_x() functions don't
return anything. See pages 560 and 561:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Ttelmah



Joined: 11 Mar 2010
Posts: 17981

View user's profile Send private message

PostPosted: Wed May 11, 2022 6:31 am     Reply with quote

Yes, what he is seeing is an odd 'leakage' behaviour.

First thing is that for the internal functions the compiler does not complain
if you read a return from the internal functions, though they are void.
Somewhat odd. It does complain if you do the same for your own functions.

If you look at the listings, for the timer_0 function it does return T0CON1.
However for timer_1, it returns the T1GATE register.
They seem to always return the value in the last register written by each
function. Not something to rely on, and not something you are meant to
do...
kgng97ccs



Joined: 02 Apr 2022
Posts: 46

View user's profile Send private message

PostPosted: Thu May 12, 2022 9:02 pm     Reply with quote

Thank you all very much for your help. Your answers have helped me a lot in my efforts to learn how to program PIC MCUs using the CCS C compiler.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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