View previous topic :: View next topic |
Author |
Message |
monsun
Joined: 17 Jan 2012 Posts: 17
|
Problem with passing array as an argument to function |
Posted: Tue Jan 17, 2012 6:45 am |
|
|
Hello everyone
I'm trying to pass an array as an argument to function using pointer,
and after all day fight I can't make it working.
My pic is PIC18F6720
I have global variable
which values I would like to change using function
key_change(key_array).
Code: |
void key_change(int1 *key[])
{
int8 i;
for(i=0;i<5;i++) *(key+i)=0;
if(!input(PIN_G0)) {*key=1;}
if(!input(PIN_G1)) {*(key+1)=1;}
if(!input(PIN_G2)) {*(key+2)=1;}
if(input(PIN_G3)) {*(key+3)=1;}
if(input(PIN_G4)) {*(key+4)=1;}
}
|
The program compiles without any errors but it doesn't work like it should.
On my lcd display I see only the first value changing.
I tried the same function using global variable and it works
perfectly but I prefer to do this universal so that why I'm trying with pointers.
any ideas? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Tue Jan 17, 2012 8:08 am |
|
|
Realistically I doubt if this can ever be made to work.
What you could do, is look at how 'pin numbers' are implemented. Here the number is the pointer to the memory address the pin is on, multiplied by 8, and then added to the bit number. The code divides the number by 8 before using it as a pointer.
Key to understand is that a 'pointer', is the address of a byte in memory. Given that an int1, is smaller than a byte, you basically can't have a 'pointer' to it directly....
Best Wishes |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1355
|
|
Posted: Tue Jan 17, 2012 9:17 am |
|
|
one option you might look at is bit fields in a struct. Depending on your compiler version, you could do something like:
Code: |
//can use unsigned int8 instead if desired.
typedef union{
unsigned int16 value;
struct{
unsigned int16 g0 : 1;
unsigned int16 g1 : 1;
unsigned int16 g2 : 1;
unsigned int16 g3 : 1;
unsigned int16 g4 : 1;
unsigned int16 : 11;
} bits;
} key_type;
static volatile key_type keys; //will be initialized to 0
// the &key uses pass by reference to get same speed as a pointer
// but also avoids using pointer overhead in a lot of cases. CCS does
// support this on some versions of its compilers. You can use pointers
// if not though and just replace . operators with ->
void key_change(key_type &key);
void key_change(key_type &key){
//I think it is ~...could be !...been a while
key.bits.g0 = ~input(PIN_G0);
key.bits.g1 = ~input(PIN_G1);
key.bits.g2 = ~input(PIN_G2);
key.bits.g3 = ~input(PIN_G3);
key.bits.g4 = ~input(PIN_G4);
}
|
Mind you this is off the top of my head without a compiler to test it, and it may not be the most efficient method. using the union gives the option of being able to use the values pin by pin or all at once as a single value. The order of the bits in the struct portion determines which order they are in the value portion. If I remember correctly, as done above, g0 will be bit0 of value, g1 will be bit1, etc. |
|
|
|