- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Dear All,
Can you tell me the way to declare global variables (only once) in a Psoc 5 project that contain many sub routines files ?
I tried to create a single .h file containing all the global variables and include him into each sub files, but the compiler seems to not be happy with this solution.
Is there a proper way to do that ?
Thank you,
JP
- Labels:
-
PSoC 5LP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You write
extern int value;
in the header file, which you then include in all place where you need access to the value. Then you write
int value=0;
in just one C-file. That way, the variable gets defined only once, and not multiple times. For a complete explanation, see here
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you infinitely for the answer !
I have already used the "extern" keyword but your link gave me the good way to use only one header file with him.
Thank you again
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Actually you don't need to use a single header file. The 'extern' statement is to declare the variable, so the compiler knows that it exists. Since include files are just that - text-based includes - it doesn't matter how many of them you have containing this declaration.
Whats important is that you have exactly one definition of the variable (this is, the line without 'extern'), because that is the place which defines how the variable really looks like, and reserver the space in memory for storing it. YOu will get a linker error when you have multiple definitions.
Having dais that, it still is advisable and good practice to use only a single header file. That way you ensure the declaration is always the same. Also, one can see the dependencies between different parts of the code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I feel it's the time to say something about header-files.
Header-files ( .h ) usually contain "declarations" only, no definitions. Thus only the compiler is told about some new objects to come. >very often used to declare prototypes of functions, typedefs and structures that must be known to the calling ( main() ) program.
So accidently there can pop-up an errormessage when you #include the same file in several places within your project stating that something is defined more than once.
There is a standard way out of this and commonly used (though not as commonly known) by C programmers:
A .h-file usually starts with the lines
#ifndef mysymbol // __Filename_h would be a very good value for mysymbol since it has to be unique within the project
#define mysymbol // now the preprocessor "knows" this file has been included
// here goes the .h-file body
#endif
Happy coding
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello guys,
Thank you for all of your valuable answers.
Following the link of Hli, I have used a .h file for definition and declaration of my global variables :
#ifdef DEFINE_VARIABLES
#define EXTERN
#else
#define EXTERN extern
#endif
EXTERN type myvariable1
EXTERN type myvariable2
The "DEFINE_VARIABLES" keyword, help to switch between definition and declaration of the variables.
Naturally, one can extend (and bulletproof) the method with more pragma directives (see above link from Hli) but I am already happy with these few lines because, now, I have just to change / add variable(s) in one single file.
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
and for a header file named global.h, an improvement will be maybe :
#undef
EXTERN
#ifndef
__GLOBAL_H#define
__GLOBAL_H#define
EXTERN#else
#define
EXTERN extern#endif
EXTERN type myvariable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
That's a good idea, but it will fail when the variable is initialized as
EXTERN uint8 MyVar = 27;
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Always following the link from Hli :
EXTERN#undef
INITIALIZER
#undef
__GLOBAL_H
#ifndef
__GLOBAL_HEXTERN INITIALIZER(...) = __VA_ARGS__
#else
EXTERN extern INITIALIZER(...)#endif
EXTERN uint8 myvariable INITIALIZER(7);
#define
#define
#define
#define
#define
But maybe a little tricky...
(Sorry for the text format, my version of IE seems to bug with the editor).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Argh, the previous post is completely wrong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi everybody
I have juste starded whith Psoc designer two months ago so I'm really a beginner.
Please, can you see sample code:
#include <m8c.h>
#include "PsocAPI.h"
// Déclaration du nombre de bits sur lequel s'effectue la communication I2C
char rxBuf[8];
// Prarametrage de la date et du temps pour le début du comptage de l'horloge
const char txCBuf[] = {0x00, //Adresse interne du RTC
0x06,0x07,0x14, // l'heure en BCD 14h:07:06
0x01, // Premier jour de la semaine, Lundi
0x18,0x04,0x17, // 18 Avril 2017
0x93}; // horloge activée
void main(void )
{
BYTE status, i;
I2Cm_1_Start(); // Initialisation du maitre pour la communication I2C
status = I2Cm_1_bWriteCBytes(0x1F,txCBuf,9,I2Cm_1_CompleteXfer);
do{
I2Cm_1_bWriteCBytes(0x1F,txCBuf,1,I2Cm_1_NoStop );
status = I2Cm_1_fReadBytes(0x1F,rxBuf,7,I2Cm_1_RepStart );
if(status == 0){
}
I2Cm_1_fSendStart(0x1F,I2Cm_1_WRITE);
I2Cm_1_fWrite(0x00);
I2Cm_1_fSendRepeatStart(0x1F,I2Cm_1_READ);
for(i = 0; i < 6; i++){
rxBuf = I2Cm_1_bRead(I2Cm_1_ACKslave);
}
rxBuf[7] = I2Cm_1_bRead(I2Cm_1_NAKslave);
I2Cm_1_SendStop();
}while (1);
}
It's a sample code provided by Cypress in Psoc designer5. My problem is, when I build the code, Psoc designer says to me:
Starting MAKE...
creating project.mk -- no changes
Linking..
!E obj/monprogrammetesti2c.o(45): {linker} multiple define: '_main'
make: *** [output/Config_I2C.rom] Error -1
Config_I2C - 2 error(s) 0 warning(s) 16:01:27
I dont understand what wrong because it's the own code of cypress, it's should match???
Excuse me for my ''small'' english, I'm learning!
So someone can help me please???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Welcome in the forum.
Can you please post your complete project, so that we all can have a look at all of your settings. To do so, use
Designer->File->Archive Project
and attach the resulting file.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi
It sims that, I have a probleme of version.
But I have annother problem, a warning: "Level 1 Warning - config_i2c: Currently POR voltage is set to 4.55 Volts. To change the POR Voltage the VLT_CR should be modified in main code".
So I do not know how to change it in my main.c (not in assembly code, please, its make me fool)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Well, let me say two things at first:
You are here in a PSoC5 forum, not PSoC1. So the PSoC1 gurus may not read your questions.
It is never a good idea to hijack a 4 year old thread, better start a new one in the right forum.
Forget about the warning, it has to do with the voltage when reset is applied due to power loss.
Bob