View previous topic :: View next topic |
Author |
Message |
tavioman
Joined: 22 Feb 2006 Posts: 65
|
Map pins into a struct. |
Posted: Mon Sep 24, 2007 2:22 am |
|
|
Hi everyone.
I have a small question.
I need to make an array of structs. Code: |
MY_STRUCT_TYPE array_of_structs[2]; |
Code: | typedef struct
{
UNSIGNED_8 member_1;
UNSIGNED_16 member_2;
A PIN FROM A PORT the_pin;
}
MY_STRUCT_TYPE; |
I have an init function where I will set up some initial values of both structs.
But how can I set-up in one struct to have access to a pin to have something like this:
Code: | void DoSomething(UNSIGNED_8 index)
{
MY_STRUCT_TYPE* pointer;
pointer = &array_of_structs[index];
if(pointer->the_pin == TRUE)
{
printf("Pin is High\r\n");
}
} |
Is this possible?
Thank you. |
|
|
Ttelmah Guest
|
|
Posted: Mon Sep 24, 2007 4:02 am |
|
|
First question, what compiler version?.
If you are using a V4 compiler, then the 'pin numbers' for functions like input_pin, can be variables. So you can have:
Code: |
typedef struct
{
UNSIGNED_8 member_1;
UNSIGNED_16 member_2;
int16 the_pin;
}
MY_STRUCT_TYPE;
//Then with the array declared define the pins using something like:
array_of_structs[0].the_pin=PIN_B0;
array_of_structs[1].the_pin=PIN_B1;
//etc.
//Which can then be accessed, with:
if (input(pointer->the_pin))
|
The same basic construction, can be used in older compilers, by 'emulating' the variable based functions. Code for this has been published here in the past. A search for the keyword 'procedures', should fid the version published by PCM_programmer to do this.
You can 'map' a variable directly 'onto' a port, or bit, but you can't do this for a single pin inside a structure.
Best Wishes |
|
|
tavioman
Joined: 22 Feb 2006 Posts: 65
|
|
Posted: Mon Sep 24, 2007 4:42 am |
|
|
Thanks!
I will try your solution. |
|
|
|