Global variables and global functions in Codewarrior 5.1

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Global variables and global functions in Codewarrior 5.1

Jump to solution
2,938 Views
Simone_Italy
Contributor I
Hi,
 
I'm using Codewarrior 5.1 (special edition) to develop a simple application on MC68HC908JK1CDW (I use C language). Please can you suggest where inserting global variables and global functions whose scope is main.c (the .c file containing the main function), event.c (I need to share variables between main() function and event functions) and all the other files I need?
Thank you in advance for your help.
Bye.
 
Simone
Labels (1)
Tags (1)
1 Solution
1,297 Views
CrasyCat
Specialist III
Hello
 
I assume you are using ProcessorExpert to generate code. Am I right?
 
I would recommend you to keep your source files separated from Processor Expert generated ones. You can do that the following way:
  - Create a new source file where you implement your function and define the necessary global
     variables / constants.
     For example you have a file called myfunc.c containing the following:
Code:
     unsigned char myCount;     unsigned char foo(void) {       /*Some code here*/       return myCount;     }

  - Add the file to the project (selecting Project -> "Add <...> to Project". 
  - Create a header file containing declaration for the global variables/constants you need and
     prototype for the function. For the above source file the include file will be called myfunc.h and will
     look as follow:
Code:
extern unsigned char myCount;unsigned char foo(void);
  - In the source file containing the implementation of the main function as well as in the Events.c file add an
    include from the new header file (myfunc.h in our example).
Code:
#include "Cpu.h"#include "Events.h"#include "myfunc.h"<...>

Then in main or in the Events module you can just use the  function or variable the usual way.
in the main function make sure you write your code between the marks:

  /* Write your code here */
  /* For example: for(;:smileywink: { } */
and
  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/

That should do it and keep some kind of structure in the application.
I hope this helps.

CrasyCat

View solution in original post

2 Replies
1,298 Views
CrasyCat
Specialist III
Hello
 
I assume you are using ProcessorExpert to generate code. Am I right?
 
I would recommend you to keep your source files separated from Processor Expert generated ones. You can do that the following way:
  - Create a new source file where you implement your function and define the necessary global
     variables / constants.
     For example you have a file called myfunc.c containing the following:
Code:
     unsigned char myCount;     unsigned char foo(void) {       /*Some code here*/       return myCount;     }

  - Add the file to the project (selecting Project -> "Add <...> to Project". 
  - Create a header file containing declaration for the global variables/constants you need and
     prototype for the function. For the above source file the include file will be called myfunc.h and will
     look as follow:
Code:
extern unsigned char myCount;unsigned char foo(void);
  - In the source file containing the implementation of the main function as well as in the Events.c file add an
    include from the new header file (myfunc.h in our example).
Code:
#include "Cpu.h"#include "Events.h"#include "myfunc.h"<...>

Then in main or in the Events module you can just use the  function or variable the usual way.
in the main function make sure you write your code between the marks:

  /* Write your code here */
  /* For example: for(;:smileywink: { } */
and
  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/

That should do it and keep some kind of structure in the application.
I hope this helps.

CrasyCat
1,297 Views
Simone_Italy
Contributor I
Thank you very much for your help!
Bye.
 
Simone
0 Kudos