# 18f43K20 timing woes

Status
Not open for further replies.

#### MrDEB

##### Well-Known Member
been trying to get this chip to blink an Led but it isn't right. The blink is tooo slow.
code=basic
DEVICE = 18F43k20
CLOCK = 8
CONFIG MCLRE = OFF
CONFIG fOSC = INTIO67
CONFIG boren = off
INCLUDE "SetDigitalIO.bas"
INCLUDE "Utils.bas"
DIM MST_LED AS portE.1
setalldigital
OUTPUT (MST_LED)
WHILE true
MST_LED = 1 //portE.1
DELAYMS(500)
TOGGLE (MST_LED)
DELAYMS(500)
WEND
[/code QUOTE]

The K series of chips are a pain.

#### be80be

##### Well-Known Member
Tell me one thing where did you set the osccon setting. Without setting it your going to be slow

#### MrDEB

##### Well-Known Member
I need to check but think it is set within the include files. Using SETALLDIGITAL VERSION 2.3
I started reading about the OSCCON setting in the data sheet page 26
going to try OSCCON = 0110011X but ?

#### MrDEB

##### Well-Known Member
I tried this and when checking for complining it says osccon re declared
basic code
DEVICE = 18F43k20
CLOCK = 8
CONFIG MCLRE = OFF
config OSCCON = %01110110//fOSC = INTIO67
config boren = off
INCLUDE "SetDigitalIO.bas"
INCLUDE "Utils.bas"
DIM MST_LED AS portE.1
setalldigital
[/code/QUOTE]

#### large_ghostman

##### Well-Known Member
But why you setting the stable bit? normally you wait or 'while' that bit isnt stable then the program starts, your setting it, not that i think it matters its just not how i do it.

Read the entire oscillator section, then have a think about this CONFIG1H, its mentioned in the OSC part of the datasheet, me thinks maybe that might be worth taking a peek at.

I am not giving you the answer, but i dont mind throwing you a few clues of what to check. Learn to read and understand one datasheet and the world of pics becomes yours.

Also tell me exactly what you have told the OSC register to do in words one bit at a time or several bits where needed.

If all else fails remember the secret to unlimited led flash rates, turn a pin on. then take the GND side of the led off, touch it to GND as fast or as slow as you like! the led flashes exactly as fast as you want!
EDIT
Thats odd! only after posting did your code pop up!!

In SF do you set the clock speed by simply clock =8??

Is the WD off?
read the config section of the datasheet first, then the OSCON etc, read the DS in the order you need to execute the and set the code, miss nothing out unless you know from reading the DS its start up state is what you want

TIP OF THE DAY

Download Foxit reader (free) use the search function for bits (pun intended) you dont understand. and read all the section on each part like the clock where it explains what some the bits your setting actually do, for example the bit that tells you if the clock is stable, you read this bit not write to it.

I am not saying this is your problem, i am telling you how to tackle the problem, what to watch out for, remember the chip is as thick as a brick and your better off treating it as such and tellng it exactly what you want, its not always done or needed but its best practice unless you really know the chip well.

JONSEA get ready to go slap yourself

Last edited:

#### be80be

##### Well-Known Member
I dont think swordfish likes a osccon = xxxxxxx statement i always used the osc module.
I looked at setalldigtal it not in mine

#### large_ghostman

##### Well-Known Member
I dont think swordfish likes a osccon = xxxxxxx statement i always used the osc module.
I looked at setalldigtal it not in mine
I havnt got around to SF yet, several things have got in the way. But my little sister is actually managing on the june bug with my really old laptop and a old version of C18 and MPLAB, no idea how old it is lol. but she has done really well using the old C18 help files and the millions of docs and tutorials i collected on it. Really proud of her and a bit jealous she seems to be a natural.

#### Pommie

##### Well-Known Member
The instruction OSCCON = %01110110 should go at the start of the main code. NOT in a config statement.

Mike.

#### Pommie

##### Well-Known Member
I havnt got around to SF yet, several things have got in the way. But my little sister is actually managing on the june bug with my really old laptop and a old version of C18 and MPLAB, no idea how old it is lol. but she has done really well using the old C18 help files and the millions of docs and tutorials i collected on it. Really proud of her and a bit jealous she seems to be a natural.
She sounds like a natural. Well done for enabling and encouraging her.

Mike.

#### be80be

##### Well-Known Member
Mike swordfish dont even like it in the main bas It for some reason dont set it frist so the oscillator dosent run at full speed like it should.
Ive always make a module for it.
Mrdeb been using one for it too dont no why he is not now.
There is a osccon module that takes the clock line in code and sets it hes not using it as i see it.

#### JonSea

##### Well-Known Member
MrDEB,

Code:
INCLUDE "IntOSC8.bas"
SetAllDigital sets all ADC inputs to digital. It has absolutely nothing to do with setting the oscillator. Your troubleshooting skills are simply astonishing.

Crystals always work.

End of my participation.

#### be80be

##### Well-Known Member
Jon maybe he is writing his own code now and didnt copy paste

#### JonSea

##### Well-Known Member
*I* always copy the setup stuff from my programs wben I start something new. It just makes sense!

Hmmm...the clock is running slow. Maybe I should bave used a Word instead of an Int. Or I should have included Uart.bas.

#### tumbleweed

##### Active Member
SetAllDigital sets all ADC inputs to digital. It has absolutely nothing to do with setting the oscillator
Thank you. Took the words right out of my mouth.

There is a module (that you have used before) that will setup the internal osc for you:
http://www.sfcompiler.co.uk/wiki/pmwiki.php?n=SwordfishUser.IntOSC

Inside the zip you'll find "intosc.bas". Put it in your user library folder.
To use it all you have to do is add
Code:
include "intosc.bas"
to the start of your code and it'll automatically add the config settings and code to setup the osc to match your 'clock=' statement.

Couldn't be much easier.

#### be80be

##### Well-Known Member
I think I said all that too LOL
Lets see
Mrdeb been using one for it too dont no why he is not now.
There is a osccon module that takes the clock line in code and sets it hes not using it as i see it.
Said That
Lets see
I looked at setalldigtal it not in mine
Nope that doesn't have it in it.

And Swordfish doesn't like OSCCON = %xxxxxxxx n the main .bas
It may take hours for it to set the OSCCON if you add it in the Main.bas file.
As a matter of fact thats why there is a intosc.bas which is nice cause it uses the
Code:
CLOCK = 8
Line to set the osccon setting for using the dang intosc.bas that he has been using before.

But I always made my own module after 8 years ago I waited 2 hours for the 18f1220 to show text On a LCD. It took two hour to change from 32khz to 8 mhz for some reason.

I think it was you Jerry that said why But I can't remember May of been Gram
But one time was enough to not forget to use a module for setting it.

Last edited:

#### tumbleweed

##### Active Member
Setting the int osc frequency requires executing code to set the OSCCON register setting.

One thing that's not obvious at first is that SF will run all of the module initialization code in your include files BEFORE the main module starts up, so if you have the 'OSCCON = xx' in your main program module it's one of the last things to get setup. Many chips default to a slow internal osc freq (like 1MHz), so until that 'OSCCON = xx' statement gets executed the chip will likely be running a lot slower than you think.

The easiest way around this (as you've pointed out) is to put the 'OSCCON = xx' statement into an external module, and include that one first in your main program module. That way it'll be one of the first things executed and things will startup faster.

That's why if you use the 'intosc.bas' module you should list it first.

#### large_ghostman

##### Well-Known Member
Setting the int osc frequency requires executing code to set the OSCCON register setting.

One thing that's not obvious at first is that SF will run all of the module initialization code in your include files BEFORE the main module starts up, so if you have the 'OSCCON = xx' in your main program module it's one of the last things to get setup. Many chips default to a slow internal osc freq (like 1MHz), so until that 'OSCCON = xx' statement gets executed the chip will likely be running a lot slower than you think.

The easiest way around this (as you've pointed out) is to put the 'OSCCON = xx' statement into an external module, and include that one first in your main program module. That way it'll be one of the first things executed and things will startup faster.

That's why if you use the 'intosc.bas' module you should list it first.
Ah now i understand why it didnt make sense!
I think the chip he is using defaults to 32kHZ from vague memory, but Mr Deb what i said about those read bits stands true, they are there to declare when the OSC is stable you dont set them. AND nice to see you had a datasheet open at least.

I will go look at some SF code, see if i can get a handle on it. As with all code and compilers its always the bloody quirks that bite lol. Like Python and its white spaces

#### Pommie

##### Well-Known Member
I remember this problem many years ago. Some chips defaulted to 32kHz and so if you used the LCD module the initialisation took a couple on minutes to execute. This shouldn't be the case with this chip which defaults to a 1MHz clock after reset. So you can pretty much put your OSCCON setting wherever you want - but NOT just randomly place it in a config directive.

Mike.

#### large_ghostman

##### Well-Known Member
I remember this problem many years ago. Some chips defaulted to 32kHz and so if you used the LCD module the initialisation took a couple on minutes to execute. This shouldn't be the case with this chip which defaults to a 1MHz clock after reset. So you can pretty much put your OSCCON setting wherever you want - but NOT just randomly place it in a config directive.

Mike.
There you go first page in and i misread the datasheet already!

#### tumbleweed

##### Active Member
I will go look at some SF code, see if i can get a handle on it
SF startup works like most C runtime startup code does.

In C,most implementations insert initializers into the runtime library startup code and they run before main().

Same thing happens here, although the mechanism is slightly different. Even a chip running with a 1MHz osc (250KHz instruction cycle) is pretty slow to startup.

Status
Not open for further replies.