compatibility of PICs micros (PIC18s) - Page 1
Forum section: electronics / general

author: glatocha
author_rank: Regular Contributor
timestamp: April 17, 2020, 03:20:13 am
content: 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=1026So 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.

author: TomS_
author_rank: Frequent Contributor
timestamp: April 17, 2020, 05:36:35 am
content: 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.cBetween 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.

author: JPortici
author_rank: Super Contributor
timestamp: April 17, 2020, 05:48:15 am
content: 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.

author: AndyC_772
author_rank: Super Contributor
timestamp: April 17, 2020, 06:08:57 am
content: Quote from: glatocha on April 17, 2020, 03:20:13 amThat 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.aspxI 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.

author: Wilksey
author_rank: Super Contributor
timestamp: April 17, 2020, 09:42:28 am
content: Click the big red button that says "Show ALL Products"