PIC Clocks in Flowcode

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

hackinblack
Posts: 26
Joined: Mon Apr 17, 2017 9:55 pm
Has thanked: 2 times
Been thanked: 14 times
Contact:

PIC Clocks in Flowcode

Postby hackinblack » Sat Jun 09, 2018 12:29 am

PIC Clocks In FlowCode

In The PIC

By default PICs come with their internal oscillator enabled at a default value
so they can be programmed reliably,without connecting an external crystal.

The OSCCON register (responsible for configuring the PIC's operating speed) has 8-bits, Each bit has a value
which doubles as they travel leftwards.The rightmost bit is '1' next 2,4,8,16,and so on.

For example in Binary 0b00010000 means:- 'no 1s,no 2s,no 4s,no 8s,and one 16'...

These numbers are then AND'ed together by the assembler to produce a hex* word of the same value; which in this case
adds up to the decimal number '16' or '0x10' in hex; which,by sheer cunning,is the word for the 31.25Khz oscillator!
(at least for the 12F1572!)

*hexadecimal format ('hex' for short) in code is nearly always displayed as '0x..'
easier to type,and less error-prone than typing '0b100100...' but not as clear to us humans.
Conversions can be done using the Window's built-in accessory 'calculator',set to 'scientific' mode,
to convert between binary,decimal,and hex values.


In FlowCode

For the 12F1572 the default INTOSC clock speed is 500KHz.
This is the only int osc setting NOT requiring a C-block for correct operation.

The clock speed settings selected inside 'Build>Project>Options'
only tell Flowcode what speed the oscillator is set to for simulation...
They do NOT set up the oscillator in the PIC's hardware! so if you are NOT using the default setting...

To actually configure the oscillator on the chip
***IF YOU ARE NOT USING THE CHIPS DEFAULT INT.OSC SPEED***
you need to use a C code icon in your code before the main loop
to set the PIC's OSCCON register to the correct values.

Using for example,a 1Mhz clock,your C-block should be:-

// C Code:
/*
Enter C code below this comment
*/
OSCCON = 0x58;

OSCCON words for the 12F1572 are:- 0x00 for 31Khz (slowest clock available),
0x10 for 31.250Khz, 0x20 for 62.5Khz, 0x28 for 125Khz
0x30 for 250Khz, 0x38 for 500Khz (not actually required as it's the default config!)
0x58 for 1Mhz, 0x60 for 2Mhz, 0x70 for 8Mhz, 0x78 for 16Mhz,
and 0xF0 for 32Mhz PLL-generated clock (fastest clock available).

Sooo, where has all this got us?
We now understand how binary relates to hex, and how bit-values add up to words; which can then be used to set up our hardware!
Using the internal oscillator frees up 2 more pins for use as I/O, saves three extra parts, and uses up less PCB space!
Last time i priced up a crystal it was 1/3 of the price of the little PIC using it...
These users thanked the author hackinblack for the post:
medelec35 (Sat Jun 09, 2018 10:08 am)
Rating: 5.26%
 

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8440
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2412 times
Been thanked: 3450 times
Contact:

Re: PIC Clocks in Flowcode

Postby medelec35 » Sat Jun 09, 2018 10:08 am

hackinblack wrote:he clock speed settings selected inside 'Build>Project>Options'
only tell Flowcode what speed the oscillator is set to for simulation...
They do NOT set up the oscillator in the PIC's hardware! so if you are NOT using the default setting...

Whilst is true that clock speed does not set up internal oscillator in PIC's hardware as OSCCON does.
The clock speed setting is still vital for correct operation of hardware, it's not just for simulation.
Its used for any components that relying on critical timing.
E.g Baud rates, LCD's, delays etc.
So it's vital that clock speed within flowchart matches PIC hardware speed,
otherwise hardware will not work as expected.

Martin
These users thanked the author medelec35 for the post (total 2):
kersing (Sat Jun 09, 2018 11:07 am) • EtsDriver (Sat Jun 09, 2018 12:43 pm)
Rating: 10.53%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

hackinblack
Posts: 26
Joined: Mon Apr 17, 2017 9:55 pm
Has thanked: 2 times
Been thanked: 14 times
Contact:

Re: PIC Clocks in Flowcode

Postby hackinblack » Sat Jun 09, 2018 10:47 pm

That is useful to know;i couldn't make much headway in all the scattered information i've gathered.
but i completely forgot about communication sync speeds. :oops:
i've also found the newer enhanced PIC's are much more complex than the older families.
The 12F1572 has far more registers to get wrong,2 just for clock selection,more for pre-scalers etc.
makes the 12F675 seem like a Model-T ford in comparison!
looks like i'll have to relearn more than i've forgotten :roll: