# From PIC16F690 to PIC18F26K20 + The big function!

#### Eric007

Joined Aug 5, 2011
1,158
Hi Guyz,

Most of you guyz know I been using PIC16F690 since i started playing with Pics uC but now I'm shifting to PIC18F26K20 (16MIPS, 3.8Kb RAM,...)

Attached is my usual template that i been using...
Now i want to know about the major changes I'll have to make about this template!?

Have a look especially at the 'variable definition section', the 'interrupt processing section', the 'config fuse section', and 'header file section'...the rest is easy to change!

In the meantime I'm quickly scrolling through the datasheet to get familiar with this PiC18F...(you can also tell me where i need to put emphasis on...)

Also where (provide links) can I order a 64Mhx Crystal for this PIC18F26K20

Will talk about the big function is next post!!

regards, Eric

#### Attachments

• 7 KB Views: 89

#### Markd77

Joined Sep 7, 2009
2,806
Have a look in the "c:/Program Files/Microchip/MPASM Suite/Template/Code" folder on your computer, you should find a template there.

#### spinnaker

Joined Oct 29, 2009
7,830
Hi Guyz,

Most of you guyz know I been using PIC16F690 since i started playing with Pics uC but now I'm shifting to PIC18F26K20 (16MIPS, 3.8Kb RAM,...)

Attached is my usual template that i been using...
Now i want to know about the major changes I'll have to make about this template!?

Have a look especially at the 'variable definition section', the 'interrupt processing section', the 'config fuse section', and 'header file section'...the rest is easy to change!

In the meantime I'm quickly scrolling through the datasheet to get familiar with this PiC18F...(you can also tell me where i need to put emphasis on...)

Also where (provide links) can I order a 64Mhx Crystal for this PIC18F26K20

Will talk about the big function is next post!!

regards, Eric

It might help if you mentioned what you are trying to do.

Why punish yourself with assembler? The 18F is optimized for C. You should take advantage of that fact.

#### Eric007

Joined Aug 5, 2011
1,158
It might help if you mentioned what you are trying to do.

Why punish yourself with assembler? The 18F is optimized for C. You should take advantage of that fact.
you might be right by saying that i'm punishing myself with assembler BUT i'm feeling much confortable with assembler than C!

I don't really like C but I'll make an effort tho...

SEEMS LIKE NONE OF YOU HAVE VIEWED MY TEMPLATE

thx!

#### spinnaker

Joined Oct 29, 2009
7,830
SEEMS LIKE NONE OF YOU HAVE VIEWED MY TEMPLATE

thx!
Why do you assume that?

Maybe the count on views is not working correctly.

#### Markd77

Joined Sep 7, 2009
2,806
I had a look at it before my last post, I guess the counter is broke.

Also, you want a 16MHz crystal and use the 4xPLL to make the internal clock 64MHz, or just use 16MHz internal oscillator and 4xPLL if accuracy isn't too important.

#### Eric007

Joined Aug 5, 2011
1,158
Why do you assume that?

Maybe the count on views is not working correctly.
Guess you are right!!! it still displaying 0 views!

Will talk about my big fuction in a few hours...Late here!!

the datasheet is a bit scary...lol...lots of banks...

#### spinnaker

Joined Oct 29, 2009
7,830
Guess you are right!!! it still displaying 0 views!

the datasheet is a bit scary...lol...lots of banks...
That is where the 18F and C shine. No need to worry about banks unless you need to larger chunks of memory and even then all you need to do is define your custom bank in the linker file.

#### Eric007

Joined Aug 5, 2011
1,158
I had a look at it before my last post, I guess the counter is broke.

Also, you want a 16MHz crystal and use the 4xPLL to make the internal clock 64MHz, or just use 16MHz internal oscillator and 4xPLL if accuracy isn't too important.

OhK!!! Actually i want to run my function very fast (16Mhz), or more if possible, so i thought i had to buy a 64Mhz crystal and as Pic divides its frequency by 4, i would get 16Mhz...

But there's another way as you suggested...will have a look at datasheet...

So you meant, if i got you right, i should just get a 16Mhz Crystal and use the 4xPLL, right?

Thx!

#### stahta01

Joined Jun 9, 2011
133
I do not care for the formatting of the template.
The mixing of tab and spaces is one thing I dislike about the PIC Asm. examples given by Microchip. Your template only looks formatted correctly when I set tabs at 8 spaces.

I try to use only lower case keywords; you used mixed case for these "Cblock" and "Org". Most or maybe all times it works both ways. But, I am used to C where it fails all the time if lower case is not used.

Tim S.

Joined Apr 16, 2011
568
OhK!!! Actually i want to run my function very fast (16Mhz), or more if possible, so i thought i had to buy a 64Mhz crystal
Maximum Crystal frequency is 25MHz for this PIC, and don't forget the you need two capacitors to go with it.

External 64MHz Crystal Oscillator module might be an option, assuming such a thing exists.

Also be aware that only the "industrial" version of your PIC runs at 64MHz. If you happen to have the "extended" version of the PIC, then you are limited to 48MHz if you want to stay within spec.

I agree with the other posters. If it were me, I would choose 16MHz crystal (+ capacitors) and use PLL to multiply this by 4

#### t06afre

Joined May 11, 2009
5,934

#### thatoneguy

Joined Feb 19, 2009
6,359
What do you need the maximum speed for anyway?

I've found that many running at max clock cycle tend to also use a lot of delays, which knocks the speed back down.

Dual Level interrupts are the best feature in the PIC18 series (well, that or USB), but speed wise, Interrupts + timers save a ton of code and execution time. However, if they aren't used optimally, they can be their greatest weakness, and people will swear dual level interrupts suck because their interrupt was interrupted.

I'd suggest trying C out, BoostC by sourceboost is free with all optimizations (2kB code limit, only $50 for unlimited code size, not the insane$1,500 some vendors want), try making a medium complexity program in assembly, and the same in C, then compare code size and execution cycles. Until you are very good at assembly, a compiler usually makes better code than raw assembly. Once you know assembly fairly well, build the framework in C, and optimize the produced assembly you don't like or can improve via inline assembly, or a separate .asm function to finish the build.

#### MMcLaren

Joined Feb 14, 2010
859
If it will help, I attached an 18F assembly language example...

#### Attachments

• 11.9 KB Views: 71
Last edited:

#### thatoneguy

Joined Feb 19, 2009
6,359
If it will help, I attached an 18F assembly language example...
Beautiful. I love the extensive comments, and minimum delays in getting stuff done.

#### thatoneguy

Joined Feb 19, 2009
6,359
SEEMS LIKE NONE OF YOU HAVE VIEWED MY TEMPLATE

thx!
On to yours..

I do like the reminder where you note which pins will be called by which name. The mixing of upper and lower case, as stated earlier, is a bit unconventional for assembly.

You do great with the comments, describing what each line does.

However:

Your lines vary between 69 and 90 columns, which will wrap on the "standard" 80 column display.

You should have at least 16F690 for the target if this is a 16F690 template, and the filename should be something like 16F690-template.asm which you can save as another name once it's a new project.

Work on your alignment to make the code a lot easier to read. You have mixed tabs and spaces on almost every line. What you can do to fix this is convert all tabs to spaces, then convert all spaces back to tabs, and re-align the parts that aren't aligned. I prefer to use all spaces as tabs myself, since different people have their tab settings set between 2 and 8, you have no idea how it will look on their screen.

Comment functional lines more.

One thing that jumped out at me is this:
Rich (BB code):
;****************************************************************************************
;    Interrupt Service Routine
;****************************************************************************************

;Timer 0 Interrupt -- Occurs once every X ms

; --- other isr here ---

goto    isr_end
You don't clear the interrupt and timer flags before exiting the interrupt, only the context. I know it's a small thing, but if it's your template, the timer should be ready to add code to and you are done, or you shouldn't set up a code block for the timer at all. You shouldn't have to debug your template.

Overall, it's workable, but the few things I mentioned above will make it look a lot more readable, and if looking for a job, the code will look far more professional.

I'm not trying to bring you down, this is all constructive criticism from having to work on code from other people, and the mixed tab/space thing drives me nuts, to the point I have a macro that converts all tabs to spaces from the get go.

#### Eric007

Joined Aug 5, 2011
1,158
What do you need the maximum speed for anyway?
you will find out when i talk about my 'big function'

Dual Level interrupts are the best feature in the PIC18 series (well, that or USB), but speed wise, Interrupts + timers save a ton of code and execution time.
Nice!

However, if they aren't used optimally, they can be their greatest weakness, and people will swear dual level interrupts suck because their interrupt was interrupted.
Hope there's a way of preventing other interrups to be interrupted...

I'd suggest trying C out, BoostC by sourceboost is free with all optimizations (2kB code limit, only $50 for unlimited code size, not the insane$1,500 some vendors want), try making a medium complexity program in assembly, and the same in C, then compare code size and execution cycles. Until you are very good at assembly, a compiler usually makes better code than raw assembly. Once you know assembly fairly well, build the framework in C, and optimize the produced assembly you don't like or can improve via inline assembly, or a separate .asm function to finish the build.
That is why i don't want to shift to C now...I first want to be VERY competent in assembly before having a look at what C offers...

#### Eric007

Joined Aug 5, 2011
1,158
If it will help, I attached an 18F assembly language example...
Like thatoneguy said, it just beautiful!! what else should i add?

And i REALLY like the way you created own intruction in the 'define' section!!

This has really helped...

Thanks a lot Mike!

#### Eric007

Joined Aug 5, 2011
1,158
On to yours..

I do like the reminder where you note which pins will be called by which name.

You do great with the comments, describing what each line does.
Thanks!

The mixing of upper and lower case, as stated earlier, is a bit unconventional for assembly.
Ohk! I will fix that...

However:

Your lines vary between 69 and 90 columns, which will wrap on the "standard" 80 column display.

You should have at least 16F690 for the target if this is a 16F690 template, and the filename should be something like 16F690-template.asm which you can save as another name once it's a new project.

Work on your alignment to make the code a lot easier to read. You have mixed tabs and spaces on almost every line. What you can do to fix this is convert all tabs to spaces, then convert all spaces back to tabs, and re-align the parts that aren't aligned. I prefer to use all spaces as tabs myself, since different people have their tab settings set between 2 and 8, you have no idea how it will look on their screen.

Comment functional lines more.
I always comment each line of code but as this is a template maybe that is why you have that impression...

please repost my template edited with all comments!

One thing that jumped out at me is this:

Rich (BB code):
;****************************************************************************************
;    Interrupt Service Routine
;****************************************************************************************

;Timer 0 Interrupt -- Occurs once every X ms

; --- other isr here ---

goto    isr_end
You don't clear the interrupt and timer flags before exiting the interrupt, only the context.
I'm a bit surprised with this...coz i always clear the flag first then go to the isr. So if got you well, you saying I should clear the flag right before exiting the isr?

I know it's a small thing, but if it's your template, the timer should be ready to add code to and you are done, or you shouldn't set up a code block for the timer at all. You shouldn't have to debug your template.
yes it's my template! you are right! I debugged my template before posting...generally I only edit an existing code block for the new application I NEVER write from scratch...

Overall, it's workable, but the few things I mentioned above will make it look a lot more readable, and if looking for a job, the code will look far more professional.

I'm not trying to bring you down, this is all constructive criticism from having to work on code from other people, and the mixed tab/space thing drives me nuts, to the point I have a macro that converts all tabs to spaces from the get go.
I'm Not down at all but on the contrary I smile while reading your comments and they all make sense plus I'm trying to improve myself...

Now, can you repost my template with correct spacings and all the above so i can see...it will help more

Thanks so much!!!

#### Eric007

Joined Aug 5, 2011
1,158
I will talk about my big function as soon as i can...as i'm doing a quick reading on the datasheet to get familiar with this Pic!

One more thing, the package type (SPDIP) of the PIC chip is the one that sits well on breadboard, right!?

regards, Eric!