Author Topic: compatibility of PICs micros (PIC18s)  (Read 801 times)

0 Members and 1 Guest are viewing this topic.

Offline glatochaTopic starter

  • Regular Contributor
  • *
  • Posts: 114
compatibility of PICs micros (PIC18s)
« on: April 17, 2020, 03:20:13 am »
So I have long going project (kind of 5 year on and off) when I started I started on PIC18F26K20. Now when I tried to order few, although they are still "active" they are not in the Microchip selector tool:
https://www.microchip.com/paramchartsearch/Chart.aspx?branchID=1026
So I put my eyes on K40, seems pin to pin compatible and yes it is nowhere written that it is a successor, that K20 is being replaced by K40 etc. but K40 is twice cheaper and seems to be a choice of the future.

But still...,
I got my code, change the CPU in MPLAB X, compile and fiasco. The UART registers are called different, oscillator config is different etc. etc. Why can't they somehow stick to the same names. I don't mind calling my UART an UART0 even if I have just one.

Long time I was thinking to have a small project like a base for all other projects, my basic timers setup, keys control etc, but it seems to be not possible.

Do you have similar problems?

Now I am trying again and plan to use the Code Configurator to set the CPU, but the number of .h and .c files and functions calls here and there is a little overwhelming for my needs. Hope it will allow easilly changing CPU in the future.

Mostly just a rand, but share your thoughts.
 

Offline TomS_

  • Frequent Contributor
  • **
  • Posts: 834
  • Country: gb
Re: compatibility of PICs micros (PIC18s)
« Reply #1 on: April 17, 2020, 05:36:35 am »
It is possible, you simply use preprocessor directives to add/remove the compatible/incompatible bits of code for the target device you are currently working on.

If you look at the source code for something like FreeBSD or Linux you will also find similar things going on in places - its impossible to write one piece of code to target 100% of available processors and architectures. Sometimes, things just have to be done a little differently.

As a more direct example, heres some code I wrote some years ago which does this to implement a simple I2C library across some PIC18 and PIC24 devices: https://github.com/tomstorey/Microchip-PIC/blob/master/i2c_routines.c

Between families you arent going to ever get copy/paste compatible code - peripherals have different complexity and use different size registers for their configuration. Well, maybe you could keep similar bit, field and maybe register names, but there will still be other incompatibilites that you will have to cater for on a per device or family basis.

If you look at the manual for each of the different compilers (XC8, XC16 and XC32) you will find that each one defines a bunch of preprocessor variables that allow you to detect a whole host of environmental things, such as the device family, whether youre doing a debug or production build, even specific device models.

Yeah it can make the code look a little messy with lots of #if's sprinkled everywhere, but MPLAB X can at least grey out sections of code that dont match, and its really the only way you'll get close to implementing a "one size fits all" solution.

From there, the best thing you can do is not directly interface with peripherals throughout the rest of your code whenever you can avoid it, and simply call into your library. That way the application stays as generic as possible and easy to follow so you dont lose track of what it is trying to do, and the libraries are the things that can be all messed up. Theres always some kind of tradeoff.  ^-^
« Last Edit: April 17, 2020, 05:38:41 am by TomS_ »
 

Online JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: compatibility of PICs micros (PIC18s)
« Reply #2 on: April 17, 2020, 05:48:15 am »
hmm.. the K22 was the direct successor. As you saw the K40 used new generation peripherals already but i skipped it and now my goto PIC18 is the PIC18F26K42.
It is a successor in the sense that it has the same pinout but the architecture is much different, between a new core, PPS and new peripherals. Beware as it also has a different number of timers.
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4228
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: compatibility of PICs micros (PIC18s)
« Reply #3 on: April 17, 2020, 06:08:57 am »
So I have long going project (kind of 5 year on and off) when I started I started on PIC18F26K20. Now when I tried to order few, although they are still "active" they are not in the Microchip selector tool:
https://www.microchip.com/paramchartsearch/Chart.aspx?branchID=1026

That table is titled "new / popular" products, which is a pretty arbitrary limitation IMHO. I'm not surprised that parts are omitted.

Microchip is pretty good at keeping older parts in production, and the 18F26K20 is listed on MAPS:

https://www.microchip.com/maps/Microcontroller.aspx

I tend to structure my projects such that all functions which access hardware directly are in a single source file. If I have to migrate a project to a new CPU, that file gets rewritten, but the rest of the project is largely unchanged.

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: compatibility of PICs micros (PIC18s)
« Reply #4 on: April 17, 2020, 09:42:28 am »
Click the big red button that says "Show ALL Products"
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf