CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Compile the same program for 2 different microcontrollers

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
erpgc82



Joined: 02 May 2020
Posts: 73

View user's profile Send private message Send e-mail

Compile the same program for 2 different microcontrollers
PostPosted: Sun Mar 06, 2022 11:46 am     Reply with quote

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: 19503

View user's profile Send private message

PostPosted: Sun Mar 06, 2022 12:53 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Sun Mar 06, 2022 2:34 pm     Reply with quote

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: 19503

View user's profile Send private message

PostPosted: Mon Mar 07, 2022 12:24 am     Reply with quote

Exactly. Very Happy

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

View user's profile Send private message

Re: Compile the same program for 2 different microcontroller
PostPosted: Mon Mar 07, 2022 10:28 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Fri Mar 18, 2022 9:20 am     Reply with quote

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: 19503

View user's profile Send private message

PostPosted: Fri Mar 18, 2022 10:03 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Fri Mar 18, 2022 11:08 am     Reply with quote

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: 19503

View user's profile Send private message

PostPosted: Fri Mar 18, 2022 11:39 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group