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

Two clock source switching - RESOLUTION

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
SuperDave



Joined: 22 May 2008
Posts: 63
Location: Madison, TN

View user's profile Send private message Visit poster's website

Two clock source switching - RESOLUTION
PostPosted: Mon Apr 22, 2013 5:05 pm     Reply with quote

Thanks to all for their help, here's what I found including some things that are not obvious but very important.

1. Control the PLL either in #fuses and/or setup oscillator especially if the external clock is not between 4-8 Mhz per 16LF1828 spec. If the PLL is running and the external clock is outside the spec range it's crazy clock time.

2. I ended up using external clock all the time with the external clock switching from 135kHz to 26MHz. #flags was set and left at ECH. This increases current draw during the brief time that 135kHz is active but works.

3. Think through #use delay(). If you set it then call an include file, the include file will be compiled with that delay. If the include file changes it, then the include file will from the change point be compiled with the change. BUT, when you come back after the include, the OLD delay will be active, you need to change it again. At least that's what appears to happen. Extra #use dalay() lines are cheap, easy and clarify debugging.

4. Side note. Using the pullup on MCLR as an input apparently does not work unless you manually clear WPUEN. Activating pullups on other Portx pins do not require manually clearing WPUEN. Reset defaults the pullups to On but WPUEN to OFF(1)!

Thanks again.
------------------------------------------------------

At startup I want a pic18lf1828 to use its internal RC clock. It then uses SPI to change the startup on a "wifi" chip so that the "wifi" chip outputs its 26MHz clock to the ClkIn pin on the Pic. Then the pic switches over to the "wifi" clock. (I need its accuracy.)

Here are the simplified relevant code pieces.
Code:

#fuses ECH,NOMCLR     //fast external clock from cc2500
#use delay (clock=26MHZ)    //the eventual clock
setup_oscillator(OSC_INTRC | OSC_16MHZ); //startup at 16mhz internalRC
(do all the spi stuff to output 26MHz)
(it does, I can see the 26Mhz out on the scope)
Code:
setup_oscillator(OSC_NORMAL); //switch over to 26MHz
while (1) {
  Output_Toggle (testled);
  delay_ms(200);              //scope shows this is 325ms ie. 200*26/16
}

I see spec sheet discussion of two speed startup including delays when switching but don't see how to implement the delays. And, I would think "setup_oscillator" would take that into account.

Anyone done this? Thanks.


Last edited by SuperDave on Mon May 27, 2013 5:48 am; edited 1 time in total
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Apr 22, 2013 6:45 pm     Reply with quote

check the 16F1829 sheet:

OSCON register
Quote:
System Clock Select (SCS) bits of the OSCCON
register. The following clock sources can be selected
using the SCS bits:
• Default system oscillator determined by FOSC
bits in Configuration Word 1
• Timer1 32 kHz crystal oscillator
• Internal Oscillator Block (INTOSC)
5.3.1 SYSTEM CLOCK SELECT


BUT to do what you want -you may be forced to startup with the 32khz src ONLY

I assume you know 2 speed ops are tricky as several issus come in to play
USE_DELAY is top of the list.

But if you can do a very simple startup seqence and go right to the SPI setup init you need
there seems to be a chance
Ttelmah



Joined: 11 Mar 2010
Posts: 19451

View user's profile Send private message

PostPosted: Tue Apr 23, 2013 2:24 am     Reply with quote

You have to rely on using the FCMEM fall back to do this.
The clocks you have are:
1) External clock on the main oscillator input.
2) Internal RC.
Now, the alternative of the timer1 input for the external clock, can't handle this frequency (16MHz max).

So, with ECH selected as the 'master clock', if the 26MHz is not present when the chip starts, FCMEM will allow the chip to boot. Without this the chip won't boot without the clock (comment below).

Now assuming you are not going to do anything 'timed' when running off the internal clock, do as you currently show, and set the clock statement to reflect the high speed clock.

Delays are set at _compile time_, so if you switch clocks, it becomes your responsibility to handle timing things differently...

Then at the start of your program, set the internal clock to the rate you want, as you currently show. So:

setup_oscillator(OSC_16MHZ | OSC_INTRC);

Normally this is done for you, if you specify the frequency in the clock setting, but since you are not using a setting the internal RC can generate, you need to do this. With the clock rate selected to give the one you want.
It'll get to this point using the 'fallback', if the external clock is not present.

With the oscillator set like this, and the clock setup given, time delays in the code will run at 0.615 of the speed specified. So delay_msec(10) will give 1.6mSec etc...

Now you can code round this using your own delays. So:
Code:

#define LOW 0
#define HIGH 1
int1 speed=LOW
void mdelay_ms(int16 n) {
   switch(speed) {
   case LOW:
       delay_ms(n/8+n/2);
       break;
   case HIGH:
       delay_ms(n);
       break;
  }
}


Then when you change speed, you chance the 'speed' variable to HIGH or LOW as needed, and the using mdelay_ms, the delays will switch.

Now, I'd suggest the chip is probably defaulting to having FCMEM enabled, hence it is running (check the fuses at the end of the LST file). However it looks as if it is not switching to the other clock (hence the delay being wrong). I'd be explicit on selecting FCMEM, just in case things change later...

On the clock, some chips return an oscillator status from the setup_oscillator function, but I don't think this one has the ability. Can you verify they 26MHz, is coming _out_ on the OSC2 pin?. If it is not, the clock is not being properly seen.

Best Wishes
SuperDave



Joined: 22 May 2008
Posts: 63
Location: Madison, TN

View user's profile Send private message Visit poster's website

PostPosted: Tue Apr 23, 2013 9:10 am     Reply with quote

I'm going to do some other tests today but to be clear on what I think the order of events is at startup now (see the code in my first post)

1. The pic is told to default to external clock high (ECH) but use internal RC now.
2. It's not in the code but the pic is getting (but not using) 192kHz on ClkIn from the external chip. I believe this would keep FCMEN (used to be FCMEM) from tripping on a failed clock.
3. The program starts with 16MHz from the internal RC as requested.
4. It writes thru SPI to the external chip to change the 192kHz to 26MHz.
5. The external source and ClkIn change to 26MHz, no question.
6. The pic is told to go back to the default ECH clock (ie. 26MHz).
7. It doesn't.

My first test thought is to move the #use delay to after the SPI/ECH change, though I would think if that's the problem, the observed pulses would be shorter not longer.

My second test thought is to try the external clock alone just to prove the pin and then try switching from internal to external and back without trying to change the external frequency. I have a push button that would let me do this repeatedly.

Somewhere there's a reason (I think).
Ttelmah



Joined: 11 Mar 2010
Posts: 19451

View user's profile Send private message

PostPosted: Tue Apr 23, 2013 12:06 pm     Reply with quote

SuperDave wrote:
I'm going to do some other tests today but to be clear on what I think the order of events is at startup now (see the code in my first post)

1. The pic is told to default to external clock high (ECH) but use internal RC now.
2. It's not in the code but the pic is getting (but not using) 192kHz on ClkIn from the external chip. I believe this would keep FCMEN (used to be FCMEM) from tripping on a failed clock.
3. The program starts with 16MHz from the internal RC as requested.
4. It writes thru SPI to the external chip to change the 192kHz to 26MHz.
5. The external source and ClkIn change to 26MHz, no question.
6. The pic is told to go back to the default ECH clock (ie. 26MHz).
7. It doesn't.

My first test thought is to move the #use delay to after the SPI/ECH change, though I would think if that's the problem, the observed pulses would be shorter not longer.

My second test thought is to try the external clock alone just to prove the pin and then try switching from internal to external and back without trying to change the external frequency. I have a push button that would let me do this repeatedly.

Somewhere there's a reason (I think).

Comments:
1) - No. The chip starts up using the external clock. ECH in fuses. If FCMEM can't operate, it'll be using the external 192KHz. After a few dozen instruction, it gets to the setup oscillator line, and then switches to the internal RC.
3) - No it doesn't. It switches to this once the chip has booted. Pedantic, but you need to understand that it is not booting the RC.
5) - OK the external clock switches, but is it an acceptable clock?. You need to verify that the clock output is reflecting this clock. I have a nasty suspicion that the signal may not be being accepted by the PIC.
7) It won't change to the clock if it is not accepting it.

What voltage are you running the chip at?. It needs to be greater than 2.5v, to go over 16MHz.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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