Hi, I'm starting with ColdFire Family C language and I will appreciate if somebody can help me with C structure.
I have some time programming in C for 8 bits microcontrollers using CodeWarrior V10.1 and I found my first difference between 8 bits microcontrollers and ColdFire:
When I define my variables, I use to write a header file like this:
#if Global_Variable//- MY_ZEROPAGE RAM from 0x0080 to 0x00FF --------------------------------------------------------------------- #pragma DATA_SEG __SHORT_SEG MY_ZEROPAGE unsigned char SCI2_Input_Buffer [30]; unsigned char SCI2_Output_Buffer [30]; unsigned char SCI2_Input_Chr_Cnt = 0; unsigned char SCI2_Status = 0; unsigned char SCI2_Output_Chr_Cnt = 0; unsigned char SCI2_Output_Chr_Limit = 0; unsigned char *Data_Pointer; #pragma DATA_SEG DEFAULT//- MY_RAM from 0x0100 to 0x087F --------------------------------------------------------------------- #pragma DATA_SEG MY_RAM unsigned char Function_Request = 0; unsigned int Delay_Cnt = 0; #pragma DATA_SEG DEFAULT #endif//**************************************************************************************************************#if Extern_Variable//- MY_ZEROPAGE RAM from 0x0080 to 0x00FF --------------------------------------------------------------------- #pragma DATA_SEG __SHORT_SEG MY_ZEROPAGE extern unsigned char SCI2_Input_Buffer [30]; extern unsigned char SCI2_Output_Buffer [30]; extern unsigned char SCI2_Input_Chr_Cnt; extern unsigned char SCI2_Status; extern unsigned char SCI2_Output_Chr_Cnt; extern unsigned char SCI2_Output_Chr_Limit; extern unsigned char *Data_Pointer; #pragma DATA_SEG DEFAULT//- MY_RAM from 0x0100 to 0x087F --------------------------------------------------------------------- #pragma DATA_SEG MY_RAM extern unsigned char Function_Request; extern unsigned int Delay_Cnt; #pragma DATA_SEG DEFAULT #endif
In this way I can use this variables shared with all functions.
I get MY_ZEROPAGE and MY_RAM labels from the Proyect.prm file were it specified a RAM or ROM space.
/* This is a linker parameter file for the mc9s08gt32a */NAMES END /* CodeWarrior will pass all the needed files to the linker by command line. But here you may add your own files too. */SEGMENTS /* Here all RAM/ROM areas of the device are listed. Used in PLACEMENT below. */ Z_RAM = READ_WRITE 0x0080 TO 0x00FF; RAM = READ_WRITE 0x0100 TO 0x087F; ROM = READ_ONLY 0x8000 TO 0xFFAF; ROM1 = READ_ONLY 0xFFC0 TO 0xFFCB; /* INTVECTS = READ_ONLY 0xFFCC TO 0xFFFF; Reserved for Interrupt Vectors */ENDPLACEMENT /* Here all predefined and user segments are placed into the SEGMENTS defined above. */ DEFAULT_RAM, /* non-zero page variables */ INTO RAM; _PRESTART, /* startup code */ STARTUP, /* startup data structures */ ROM_VAR, /* constant variables */ STRINGS, /* string literals */ VIRTUAL_TABLE_SEGMENT, /* C++ virtual table segment */ DEFAULT_ROM, COPY /* copy down information: how to initialize variables */ INTO ROM; /* ,ROM1: To use "ROM1" as well, pass the option -OnB=b to the compiler */ _DATA_ZEROPAGE, /* zero page variables */ MY_ZEROPAGE INTO Z_RAM;ENDSTACKSIZE 0xF0/* VECTOR 0 _Startup /* Reset vector: this is the default entry point for an application. */
Now, where is the Proyect.prm file in CodeWarrior proyect for a ColdFire device like MCF51CN128?
Please, can you explain me how I can specify Global Variables that I can use in any function?
Theank you for your time.
Best regards, Pablo Suárez from Buenos Aires.
已解决! 转到解答。
It really has little to do with the MCU's any "C" compiler will do this. In "C" this will work:
// My header file, globals.h:extern int gRAMVariable; extern const int gROMVariable;
// Define the global variable in a "C" file. globalsl.cint gRAMVariable = 10; const int gROMVariable = 20;
// In another "C" file, say function.c:#include "globals.h"int function(){ gRAMVariable = 100; gROMVariable = 50; // ERROR! Can write to a const! return gRAMVariable * gROMVariable; }
There is no prm file, instead it uses an lcf file.
Unless there is some special requirement I am missing, you should not have to place globals explicitly in segments, as variables not decalred as const will just be put in RAM. Also, the ram is just a linear block of memory.
Jim, Thank you for your reply.
But, how I define a variable that I want to use in all functions?
With 8 bits microcontrollers if I define a variable in an specific function like:
unsigned int variable;
it can be used just in this function. If I define it like this:
static unsigned int variable;
the data stored will be preserved for the next time the function will called, but I can't get the data of this variable from another function.
I think there is another way to specify variables or ROM segments that I don't know.
It really has little to do with the MCU's any "C" compiler will do this. In "C" this will work:
// My header file, globals.h:extern int gRAMVariable; extern const int gROMVariable;
// Define the global variable in a "C" file. globalsl.cint gRAMVariable = 10; const int gROMVariable = 20;
// In another "C" file, say function.c:#include "globals.h"int function(){ gRAMVariable = 100; gROMVariable = 50; // ERROR! Can write to a const! return gRAMVariable * gROMVariable; }