View previous topic :: View next topic |
Author |
Message |
erpgc82
Joined: 02 May 2020 Posts: 73
|
Compile the same program for 2 different microcontrollers |
Posted: Sun Mar 06, 2022 11:46 am |
|
|
Hello friends, I studied programming in 2020 until 2021, and learned to program Microcontrollers. I love doing it, that's what I'll do until the last day of my life.
I made a small program that uses 16F876.
intcon 0x0b.2 is the "if" bit of timer0
Now I migrated it to 18f252 or 18f2525.
incton 0xff2.2 is the "if" bit of timer0
Equal pinout.
When I was studying, I saw something about #IFDEF, #ENDIF and #IFNDEF that I could compile the same program for 2 different microcontrollers. But now after almost 2 years, I don't remember where I studied (I didn't learn) and I can't find an example.
How do I compile the same program that serves on 2 different microcontrollers?
Can anybody help me? _________________ Gradually you will go far with persistence, will and determination! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19501
|
|
Posted: Sun Mar 06, 2022 12:53 pm |
|
|
First thing is the examples with the compiler show lots of parts using
the preprocessor if statements. Key though is you are probably looking
at the wrong one. Just use #if
Code: |
#if __device__==2525
//this code will be compiled when you are using the 2525
#else
//this code when you are not
#endif
|
__device__ automatically receives the 'tail' of the devices part number. |
|
|
erpgc82
Joined: 02 May 2020 Posts: 73
|
|
Posted: Sun Mar 06, 2022 2:34 pm |
|
|
Hello friend, Ttelmah!
I understand now, what I wanted is not even possible, using #if, #else or #endif.
For what I wanted, it would suffice:
situation 1)
Code: | #include <18F252.h>
//and
//#include <18F2525.h>
|
or
Code: | situation 2)
#include <18F2525.h>
//and
//#include <18F252.h>
|
The #if, #else and #endif are actually for, in the program header, I can compile certain lines if it is XXXX microcontroller or other certain lines if it is YYYY microcontroller.
Thank you very much!!! _________________ Gradually you will go far with persistence, will and determination! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19501
|
|
Posted: Mon Mar 07, 2022 12:24 am |
|
|
Exactly.
Have fun.
Understand though that the test as I show, doesn't just have to be in headers,
you can use this in your code to change the location of the register bit
for the timer. Then when you change the include file use, the settings get
automatically changed. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
Re: Compile the same program for 2 different microcontroller |
Posted: Mon Mar 07, 2022 10:28 am |
|
|
erpgc82 wrote: | Hello friends, I studied programming in 2020 until 2021, and learned to program Microcontrollers. I love doing it, that's what I'll do until the last day of my life.
I made a small program that uses 16F876.
intcon 0x0b.2 is the "if" bit of timer0
Now I migrated it to 18f252 or 18f2525.
incton 0xff2.2 is the "if" bit of timer0
Equal pinout.
|
Yall already tackled the headers, but I wanted to at least give some advice for the IF bit. CCS provides a getenv() compile time macro where you can specify and SFR register and and register bit in string form and it returns the correct address and bit position automatically.
For example, on more current compilers I use:
#bit U2_TRMT = getenv("BIT:U2STA.TRMT")
to always get the TRMT bit for the 2nd uart no matter which chip I actually have chosen.
Something to consider for portability of code for later on.
For the IF bit there are also the interrupt_active() and clear_interrupt() functions but not all bits have dedicated functions sometimes |
|
|
erpgc82
Joined: 02 May 2020 Posts: 73
|
|
Posted: Fri Mar 18, 2022 9:20 am |
|
|
Hello friend Ttelmah, it seems that it is not correct as I am using the #if #endif directives.
the Code that appears between the directives, appears light gray, as if it was commented!
Code: |
#include <18F252.h>
//#include <16f876a.h>
//#include <18F2525.h>
#case // directive that defines ANSI standardization
#if __DEVICE__ == 252 // if it's the PIC18F252 or PIC18F2525
#use delay(crystal=10000000) // frequency for calculations and delays, Physical Crystal
#use delay(clock=40000000) // frequency for calculations and delays, CPU clock
#endif
#if __DEVICE__ == 876 // if PIC18F252 or PIC18F2525 // if PIC16F876a
#use delay(clock=40000000) // frequency for calculations and delays, CPU clock
#endif
/****************************************************** ************************************/
/****************************************************** ************************************/
//// Configuration of Control Bits // Fuses ////
//////////////////////////////////////////////// /////////////////////////////
#if __DEVICE__ == 252
#fuses H4 // uses crystal >4mhz // Should be using 12pf or 18pf capacitor. TABLE 12-2, in Datasheet
#endif
#if __DEVICE__ == 876
#fuses HS // uses crystal >4mhz // Should be using 12pf or 18pf capacitor. TABLE 12-2, in Datasheet
#endif
|
where am i wrong?
Ttelmah wrote: | First thing is the examples with the compiler show lots of parts using
the preprocessor if statements. Key though is you are probably looking
at the wrong one. Just use #if
Code: |
#if __device__==2525
//this code will be compiled when you are using the 2525
#else
//this code when you are not
#endif
|
__device__ automatically receives the 'tail' of the devices part number. |
_________________ Gradually you will go far with persistence, will and determination! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19501
|
|
Posted: Fri Mar 18, 2022 10:03 am |
|
|
I suspect you may be using MPLAB?. If so it is just getting confused. The
code does correctly switch and generates the right fuses, |
|
|
erpgc82
Joined: 02 May 2020 Posts: 73
|
|
Posted: Fri Mar 18, 2022 11:08 am |
|
|
yes, within the MPLAB
you really are an expert in C programming at CCS.
so even being grayed out, as if it were commented out, it will compile and work correctly!!!
I understood
Thank you for the support!
Ttelmah wrote: | I suspect you may be using MPLAB?. If so it is just getting confused. The
code does correctly switch and generates the right fuses, |
_________________ Gradually you will go far with persistence, will and determination! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19501
|
|
Posted: Fri Mar 18, 2022 11:39 am |
|
|
Basically it doesn't 'know' these internal defines, so it thinks that it
isn't defined, but the compiler does knows the define.
On the older MPLAB, you could change such things. It might be worth
pointing this out to CCS. They might be able to tell the IDE about this. |
|
|
|