|
|
View previous topic :: View next topic |
Author |
Message |
lucromain
Joined: 04 Apr 2011 Posts: 4
|
Pointer to ram in rom struct |
Posted: Fri Mar 29, 2013 7:24 pm |
|
|
This is for a PIC16F1937. I have a problem trying to store a pointer to (int *) inside a struct that will be in rom. Here is a piece of code:
Code: |
int Gain,Polarity;
typedef struct reg_s {
char name[9];
int *reg_ptr;
int default_value;
};
struct reg_s rom data[]={
{"POLARITY",&Polarity ,15},
{"GAIN" ,&Gain ,32},
};
int *pObj;
pObj = data[0].reg_ptr; // Problem : pObj = 0 when executing this line !!*pObj = 15; // This obviously does not work since pObj was not assigned correctly on the previous line. |
I would expect that the content of pObj is assigned the address the Polarity register but it is incorrectly assigned to 0 instead.
If I store the struct content in RAM, (by taking away the "rom" declaration before data[]), then everything works fine.
This seems to be related to a ram pointer stored in rom problem.
My compiler version is 4.140.
Any ideas on how to fix this? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sat Mar 30, 2013 1:50 am |
|
|
No, it's to do with how RAM is allocated.
RAM on the PIC, is a dynamic resource. A variable in a function, can have the same memory re-used by another function that is not running at the same time. So RAM allocations of standard variables do not physically exist till you are inside the routine using them....
Solution is to use static. This 'locks' the RAM allocated to a variable so that other variables can't use the same area.
Code: |
void main()
{
static int Gain,Polarity=0x55;
typedef struct reg_s {
char name[9];
int *reg_ptr;
int default_value;
};
struct reg_s rom data[]={
{"POLARITY",&Polarity ,15},
{"GAIN" ,&Gain ,32},
};
int *pObj;
setup_timer_3(T3_DISABLED | T3_DIV_BY_1);
printf("Polarity = %u\n\r",Polarity);
pObj = data[0].reg_ptr;
*pObj=0xAA;
printf("Polarity = %u\n\r",Polarity);
while(TRUE)
{
}
}
|
You will nicely get:
Code: |
Polarity = 85
Polarity = 170
|
Best Wishes |
|
|
lucromain
Joined: 04 Apr 2011 Posts: 4
|
Fixed |
Posted: Mon Apr 01, 2013 2:21 pm |
|
|
Thank you Ttelmah for your reply. I tried your code and it works correctly.
However I noticed that this solution works only if the data[] array and the Gain/Polarity static variables are declared inside the main() function.
In my case, the data[] array and the (now static) Gain/Polarity variables are defined inside the .h file because I would like these variables to be "global". When declared in the .h file, the data[x].reg_ptr always returns 0 for all [x] indices.
I cannot get my code to work if I use global variables. It only works when data[] and static variables are declared inside the main() function.
Any ideas how I can make this work using global variables?
If not, I will need to declare the data[] array inside the main() function and pass a pointer to this array to all functions that need it.
Thanks |
|
|
|
|
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
|