How to set clock speed?

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
bilinkl
Posts: 13
Joined: Fri Dec 14, 2012 11:32 am
Location: Perth

How to set clock speed?

Post by bilinkl » Mon Aug 05, 2013 2:37 pm

Hi All,

2nd post for me, still finding my way with SF... V2.2.1.7

I'm having problems setting Device clock speed. I'm using a 10Mhz resonator and 18F452 with the sample GLCD code, success writing to it and blinking an LED but it's obvious timing is out.

I found various snippets in the forum and now know the clock speed needs to be set for 40 to achieve the correct timing (which I've done) but my question is how to disable the x4 PLL so it runs at the crystal frequency? Note I've tried the variations below with no difference - still x4 taking effect.

Here's my config (how to embed code in this text box?):

' if device and clock omitted, then compiler defaults to 18F452 @ 20MHz.
Device = 18F452
Clock = 40 ' Note internal x4 (10Mhz crystal used).
'Config
' OSC = HS ' default fuse no PLL
' OSC = HSPLL ' Note internal x4, 10Mhz crystal used = 40Mhz.
#option OSC = HS ' default fuse no PLL
#option WDT = true ' Disable WDT.
#option GLCD_MODEL = KS0108 ' GLCD driver. If not stated KS0108 is default.

Include "GLCD.bas"
Include "graphics.bas"
Include "Bitmaps.bas"
Include "FixedFont.bas"

' All Subs after Includes
sub Blinky() ' Heartbeat indicator.
Toggle (PORTB.7)
DelayMS(100)
Low (PORTB.7)
GLCD.Cls
GLCD.Rectangle(0,0,127,63)
end sub

Dim Led As PORTB.7
dim Index as byte

ADCON1 = $07 ' RA and RE all digital (GLCD)
Low (Led) '

Here:
Blinky
GLCD.SetFont(Fixed)
GLCD.WriteAt(4,2,"System Font")

DelayMS(5000)
Blinky

GLCD.Cls ' Remove border
GLCD.SetImage(49,51,BtnOK)
GLCD.SetImage(89,51,BtnCancel)

'While true
Index = 0
while Index < 10
GLCD.SetImage(4,2,BatteryImage, cmXOR)
DelayMS(200)
GLCD.SetImage(4,2,BatteryImage, cmXOR)
DelayMS(100)
inc(Index)
Wend
GoTo here

Thanks for the help,
Bill

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Tue Aug 06, 2013 10:49 am

Clocking the 452 is pretty straight forward compared to some of the others

The first thing is to remember that the 'clock=xx' statement is only used to tell the compiler what speed you intend to be working at so that it can adjust internal timings for delays, baud rate settings, etc. It's up to you to actually get the chip to work at that speed.

The osc/pll setting is controlled by a CONFIG fuse setting on that chip, and not by any '#option' statement.

For a 10MHz xtal:

Code: Select all

clock = 10
config OSC = HS ' default fuse no PLL
or

Code: Select all

clock = 40 ' Note internal x4 (10Mhz crystal used).
config OSC = HSPLL ' Note internal x4, 10Mhz crystal used = 40Mhz.
For a good write up check out the wiki at http://www.sfcompiler.co.uk/wiki/pmwiki ... hSwordfish. When you switch to a different chip, be sure to check out the osc settings for that device since they usually change from chip to chip

Oh, and to get the code formatting you can use the "Code" button on the editing page (middle, right below the 'Subject' entry text box) or place the text "[ code ]" and "[ /code ]" in the body of your post without the quotes or spaces

bilinkl
Posts: 13
Joined: Fri Dec 14, 2012 11:32 am
Location: Perth

Still no good...

Post by bilinkl » Tue Aug 06, 2013 12:34 pm

Hi Jerry,

Thanks for the suggestions - but still no good using this:

Code: Select all

Device = 18F452
'Clock = 40  ' Note internal x4 (10Mhz crystal used).
Clock = 10  ' Note internal x4 (10Mhz crystal used).
Config OSC = HS       ' default fuse no PLL
'Config OSC = HSPLL    ' Note internal x4, 10Mhz crystal used = 40Mhz.

#option WDT = true  ' Disable WDT.
#option GLCD_MODEL = KS0108 ' GLCD driver. If not stated KS0108 is default.

Include "GLCD.bas"
etc, etc
With my scope on B.7 I confirm a 25ms pulse to the LED which should be 100ms per the same code as before, apart from the lcd going screwy also with too fast a timing in this mode.

What to try next?

Cheers,
Bill

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Tue Aug 06, 2013 4:52 pm

Hmmm, with

Code: Select all

Device = 18F452
Clock = 10  ' Note internal x4 (10Mhz crystal used).
Config OSC = HS       ' default fuse no PLL
You should be getting 100ms delay for 'delayMS(100)'. Are you sure your programmer is actually setting the CONFIG bits based in the hex file info? Some programmers can/do override this.

See if your programmer can read the settings. CONFIG1H should be 11111010

richardb
Posts: 310
Joined: Tue Oct 03, 2006 8:54 pm

Post by richardb » Wed Aug 07, 2013 8:54 am

You've probably done this anyway but always make sure you cycle the power after changing to and from pll.

Secondly, I know its good to understand why something doesn't work the way you were expecting, but why not just use the device with pll enabled.

And thirdly,

try and show the minimum code when posting it's less distracting.

Code: Select all

Device = 18F452 
Clock = 10 ' Note (10Mhz crystal used). 
Config  OSC = HS 'default fuse no PLL 

ADCON1 = $07 ' RA and RE all digital (GLCD) 
TRISB.7=0 
while true
	Toggle (PORTB.7) 
	DelayMS(100)
wend
Hmmm..

bilinkl
Posts: 13
Joined: Fri Dec 14, 2012 11:32 am
Location: Perth

Post by bilinkl » Wed Aug 07, 2013 3:24 pm

Thanks for the information Jerry and Richard.

You may be right about the programmer Jerry, as you know I'm already doing a cludge to burn the chip - so your suggestion is probably correct and I need to update... What is the recommended USB (power and com) with ZIF programmer SF users are happy with?

Richard, programming the chip is external i.e. removed from circuit board to do this includes power down, of course.

I want the option of running lower clock speeds for various reasons, primarily because I can but also to minimize power consumption - probably a farce with 18F series anyway. But SF doesn't cater for the 12F or 16F parts I've had great success with in remotely deployed instruments which run on flea power for months.

Thanks for the help guys,
Bill

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Wed Aug 07, 2013 7:40 pm

I managed to get a copy of the K128 programmer app. Even though I used its fixhex2 utility to add the extra bytes to the hex file that the app wanted, it STILL showed HSPLL when I used the Fuses button. I used hexmate on the original hex file like I talked about in your other thread, and when I load it the OSC displayed correctly as HS.

I don't know what the issue is with the K128... try using HEXMATE on it, or for the time being try manually changing the OSC to HS after you load the hex file just as a sanity check.

Maybe we can get what you have to play nice. I imagine where you're at, getting a new programmer might not be the easiest of things.

bilinkl
Posts: 13
Joined: Fri Dec 14, 2012 11:32 am
Location: Perth

Post by bilinkl » Thu Aug 08, 2013 5:56 am

Hi Jerry,

You are right I can change the fuse manually - so problem solved and I can continue to use the programmer I have. I didn't attempt this earlier because a warning box came up which I wasn't going to risk at the time, but it's all good now. :D

Thanks for all your help and I look forward to some SF development now.

Cheers,
Bill

Post Reply