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 CCS Technical Support

18f26k22 PLL xtal issue

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



Joined: 02 Oct 2012
Posts: 10

View user's profile Send private message

18f26k22 PLL xtal issue
PostPosted: Tue Oct 02, 2012 2:12 am     Reply with quote

Dear friends ;

I am new to pic programming and CCS language, I have some experience with 8051 assembler.

My problem is about working speed of pic 18f26/46k22 mcus. I use 20 Mhz external crystal and could not get right results for delay function and timer overflow.

When i use internal clock with NOPLLEN fuse i get the expected results.

The code below turns on and off my LED after 20 times longer time than expected(should be 500 ms but it toogles every 10 second) . What am I missing ?


Code:
#include "18f26k22.h"

#fuses NOWDT,HSH,PUT,NOLVP,NOPLLEN   

#use delay (clock=20000000)   

int i=0;


#int_timer1
void timer1_isr(){

i++;

set_timer1(3036);

if( i>=5 ){
output_high(pin_a0);}

if(i==10){
output_low(pin_a0);
i=0;}


}


void main(){


set_tris_a(0x00);

setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
set_timer1(3036);
enable_interrupts(int_timer1);
enable_interrupts(global);

while(1){
}


}
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Oct 02, 2012 2:51 am     Reply with quote

Do you have access to a 'scope?

Is your external crystal circuit running correctly?

Mike
SBTECH



Joined: 02 Oct 2012
Posts: 10

View user's profile Send private message

PostPosted: Tue Oct 02, 2012 3:13 am     Reply with quote

I tested it with several crystals and it still does not work as it should be.

I also tried it with 4Mhz crystal and and with XT fuse. And on the scope screen i see 250 Khz pulse . So this explains 16 times slow execution.

By the way , all my software works well with internal clock 16 Mhz , why i need use external clock ? I am gonna start working on a big project with lots of timers and communications thats why i am searching the reason of this kind of operation .
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Tue Oct 02, 2012 3:28 am     Reply with quote

The chip defaults to having FSCM enabled.
This means that if your primary oscillator _is not working_, it falls back to the internal RC oscillator. The default frequency when this happens is 1MHz.

So your chip is running at 1MHz, because your external oscillator is not working properly.

Now you need to work out 'why'.

The downside of the internal oscillator, is _accuracy_. It only guarantees +/-2%. Many applications need better, and if you do, then you have to use an external oscillator.

Obvious thing that might be wrong. Have you perhaps got 220pF loading capacitors on the oscillator, rather than 22pF?. That would stop the higher speed crystal working at all, while a 4MHz crystal might well run on an undertone.

You have a hardware fault with your external oscillator.

Best Wishes
SBTECH



Joined: 02 Oct 2012
Posts: 10

View user's profile Send private message

PostPosted: Tue Oct 02, 2012 3:37 am     Reply with quote

By the way sorry for my unexperienced state but, what should be the amplitude of clock signal when measured between gnd and osc input pins of the mcu ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Tue Oct 02, 2012 4:29 am     Reply with quote

You shouldn't measure it there.
The scope will introduce so much loading, that it won't run as it should. Remember a typical scope probe has 30pF capacitance. Only place to measure is the oscillator _output_ pin.

Best Wishes
SBTECH



Joined: 02 Oct 2012
Posts: 10

View user's profile Send private message

PostPosted: Tue Oct 02, 2012 8:08 am     Reply with quote

It was a shame that i just connected crystal to wrong pins . It is now working according PLL situation.

By the what do you mean by osc out pin ? I could not find such a pin in the datasheet.

Thanks very much for your helps .
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Tue Oct 02, 2012 8:22 am     Reply with quote

The oscillator has two pins. out, and in. If you look at the circuit, it's a simple inverting buffer amplifier between these pins. The output drives the crystal, which then couples the signal back to the input pin with the right phase shift so when this drives the output again, you get oscillation. The output pin is a TTL drive, so isn't terribly affected by the load. OSC2 is the oscillator output.

Right pins. Smile

Best Wishes
SBTECH



Joined: 02 Oct 2012
Posts: 10

View user's profile Send private message

PostPosted: Thu Oct 04, 2012 1:08 am     Reply with quote

Another question came to my mind while i am working on rs 232 comms today .

When PLL enabled in the fuses with 20Mhz crystal , and
#use delay(clock=20M)

When i set baud rate to 9600 bps , i get 38400 bps because of 4x PLL . And in order to reach 9600 bps with PLLEN , i needed to use

#use delay(clock=80M) . I think this is because of delay functions used in the backgrounf of #use rs232 function.

Everything works fine with these settings but the question is how 18f26k22 reaches 80 MHz operating speed. In the datasheet maximum clock freqency with 4xPLL is said to be 64 Mhz. Our crystal is 20 Mhz , so does it cause any problems or unreliable situations ?

I test timer values with a scope and everything is ok about timing if i just consider 1 machine cycle = 1 osc period , so timers are just 4 time faster.

Do you have any recommendations or experiences about using PLL with 20 Mhz cyrstal ?

Thanks in advance..
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Thu Oct 04, 2012 1:26 am     Reply with quote

On the speed. 'of course'. The 'clock' in the clock statement, _must_ always match the speed the CPU is being clocked at. With the PLL in use, the crystal frequency is not the clock frequency. It gets worse on some chips (particularly USB ones), where you have programmable prescalers and postscalers to the PLL, so can have a crystal at (say) 20MHz, and the CPU running at 48MHz (say).
The compiler _does_ have options to possible slightly cope with this (#USE DELAY CLOCK=64MHz, CRYSTAL=16MHz), which should automatically set the PLL up for you.

Now, stepping on to your configuration, that it is running at all, is 'fluke'. Yes, it will cause unreliable operation. Historically, PIC's will often overclock significantly, but the other parts of their specification then tighten up. If you look, the chip is specified to operate to 64MHz, over a range from 2.7 to 3.6v, and temperatures from -40C to +85C. Like all such specs, the end points are not 'on off' switches, and if you are operating only at a much reduced temperature range and voltage range, the chip if well away from the endpoints will often go well outside the specifications. However there will always be a cost to this, with other parts of the spec degrading (so you may find for instance, that the chip runs hotter than specified, does not meet the specified input/output ranges, etc. etc..).

Don't use the 4*PLL with a 20MHz crystal with anything you want to be even remotely reliable.

Best Wishes
SBTECH



Joined: 02 Oct 2012
Posts: 10

View user's profile Send private message

PostPosted: Thu Oct 04, 2012 3:20 am     Reply with quote

Thank you very much Ttelmah , so as its said in the datasheet , i should not exceed 64 Mhz limit Smile

again another simple question , is there any chance adjust working frequency to 60 Mhz by using 20 .mhz crystal?

#USE DELAY CLOCK=60MHz, CRYSTAL=20MHz

I did not tried it , but just want to if we can use pll to speed up the mcu 3 times or 2 times ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Thu Oct 04, 2012 4:24 am     Reply with quote

No.
Again data sheet.
This chip just has a *4 PLL.
Some chips (especially when you move up to the DSPIC's), have programmable prescalers and postscalers for the PLL, but not this one.

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